当前位置: 技术问答>java相关
怎样向一个类传递Session?送100
来源: 互联网 发布时间:2017-04-24
本文导语: 在一个类里,无法通过(String)request.getSession().getAttribute("id");来接收 导入import javax.servlet.*;import javax.servlet.http.*;并实例化一request也不行不通,各位大侠有什么好的点子,送100分 | 直接将Attribut...
在一个类里,无法通过(String)request.getSession().getAttribute("id");来接收
导入import javax.servlet.*;import javax.servlet.http.*;并实例化一request也不行不通,各位大侠有什么好的点子,送100分
导入import javax.servlet.*;import javax.servlet.http.*;并实例化一request也不行不通,各位大侠有什么好的点子,送100分
|
直接将Attribute的值传递进去
举例如下
Class a
{
String o=null;
public void setObject(String o)
{
this.o=o;
}
}
之后在servlet的doPost or doGet方法中:
a asdf=new a();
a.setObject(request.getSession().getAttribute("id"));
举例如下
Class a
{
String o=null;
public void setObject(String o)
{
this.o=o;
}
}
之后在servlet的doPost or doGet方法中:
a asdf=new a();
a.setObject(request.getSession().getAttribute("id"));
|
不是吧,我在jsp里
然后在servlet里
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ttt extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
out.println(request.getSession().getAttribute("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
}
打印出来的结果就是——
beyond_xiruo
然后在servlet里
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ttt extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
out.println(request.getSession().getAttribute("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
}
打印出来的结果就是——
beyond_xiruo
|
如果用的是javabean,那么在
javabean里这样写
package test;
import javax.servlet.*;
import javax.servlet.http.*;
public class test{
String s="";
public String action(javax.servlet.http.HttpSession session) {//把session作为参数传近来
s=(String)session.getAttribute("username");//这里就可以取到值了
}
}
在jsp里
javabean里这样写
package test;
import javax.servlet.*;
import javax.servlet.http.*;
public class test{
String s="";
public String action(javax.servlet.http.HttpSession session) {//把session作为参数传近来
s=(String)session.getAttribute("username");//这里就可以取到值了
}
}
在jsp里
|
要在一个class里面操作Session,我一般是这样来做的,不知道你是不是这个意思:
class a{
private HttpServletRequest request;
private HttpServletResponse response;
/**
*初始化
*/
public a(HttpServletRequest request,HttpServletResponse response){
this.request = request;
this.response = response;
}
/**
*你的处理接口
*/
public void operatorSession(){
HttpSession session = request.getSession("sessionName");
/**
接下来对Session进行操作就行了
**/
}
}
class a{
private HttpServletRequest request;
private HttpServletResponse response;
/**
*初始化
*/
public a(HttpServletRequest request,HttpServletResponse response){
this.request = request;
this.response = response;
}
/**
*你的处理接口
*/
public void operatorSession(){
HttpSession session = request.getSession("sessionName");
/**
接下来对Session进行操作就行了
**/
}
}
|
/*
* ListReadingTag.java
*
* Created at 2002-12-31 13:45
* Title: ムァヤココレマオヘウ キムモテサセミナマ「メサタタメウテ豬ト Tag
* Copyright: Copyright (c) 2002
* Company: GlNPU
*/
package com.glnpu.logistic.tag;
import java.util.Properties;
import java.util.List;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.CreateException;
import org.apache.log4j.Category;
import com.glnpu.logistic.ejb.*;
import com.glnpu.logistic.common.MsgConstants;
import com.glnpu.logistic.tag.BodyTagBase;
/**
*
* @author yingping.zhang
*/
public class ListReadingTag extends ReadingTag{
/**
* オヤモテカヤマ・
*/
private static Category cat = Category.getInstance( MessageTag.class.getName() );
// ウシサッ constant カヤマ」
MsgConstants constants = new MsgConstants();
/**
* クイクヌククタ犢スキィ,ハ莎ML
* @return String XMLラヨキ逸ョ
*/
public String outXMLString(){
cat.debug("タ・ListReadingTag start");
String strMessage = (String)request.getAttribute("message");
StringBuffer bufXML = new StringBuffer();
try{
// ウシサットレヨテEJBカヤマ・
initEJBObject();
// オテオスア」エ贇レFeeRecordBeanヨミオトイ鰉ッフ。」
FeeRecordBean objFeeRecord = (FeeRecordBean)session.getAttribute("FeeRecordBean");
// オテイ鰉ッキスキィ」ャオテオスイ鰉ッス盪・
Vector vectorFeeRecord = ManagerRemote.getReadingBySearch( objFeeRecord );
cat.debug("エモEJBヨミオテオス Vector");
if( null == vectorFeeRecord ){
bufXML.append(getSystemXML( "テサモミユメオスキ釚マフオトシヌツシ", 0));
cat.debug("テサモミユメオスキ釚マフオトシヌツシ");
}
else{
cat.debug("ケケヤ・ムモテサセミナマ「メサタタメウテ豬トXML start");
int iCount = vectorFeeRecord.size();
//bufXML.append( getXMLHead() ); cocoon イサミ靨ェ。」
bufXML.append("");
for( int j=0; j
* ListReadingTag.java
*
* Created at 2002-12-31 13:45
* Title: ムァヤココレマオヘウ キムモテサセミナマ「メサタタメウテ豬ト Tag
* Copyright: Copyright (c) 2002
* Company: GlNPU
*/
package com.glnpu.logistic.tag;
import java.util.Properties;
import java.util.List;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.CreateException;
import org.apache.log4j.Category;
import com.glnpu.logistic.ejb.*;
import com.glnpu.logistic.common.MsgConstants;
import com.glnpu.logistic.tag.BodyTagBase;
/**
*
* @author yingping.zhang
*/
public class ListReadingTag extends ReadingTag{
/**
* オヤモテカヤマ・
*/
private static Category cat = Category.getInstance( MessageTag.class.getName() );
// ウシサッ constant カヤマ」
MsgConstants constants = new MsgConstants();
/**
* クイクヌククタ犢スキィ,ハ莎ML
* @return String XMLラヨキ逸ョ
*/
public String outXMLString(){
cat.debug("タ・ListReadingTag start");
String strMessage = (String)request.getAttribute("message");
StringBuffer bufXML = new StringBuffer();
try{
// ウシサットレヨテEJBカヤマ・
initEJBObject();
// オテオスア」エ贇レFeeRecordBeanヨミオトイ鰉ッフ。」
FeeRecordBean objFeeRecord = (FeeRecordBean)session.getAttribute("FeeRecordBean");
// オテイ鰉ッキスキィ」ャオテオスイ鰉ッス盪・
Vector vectorFeeRecord = ManagerRemote.getReadingBySearch( objFeeRecord );
cat.debug("エモEJBヨミオテオス Vector");
if( null == vectorFeeRecord ){
bufXML.append(getSystemXML( "テサモミユメオスキ釚マフオトシヌツシ", 0));
cat.debug("テサモミユメオスキ釚マフオトシヌツシ");
}
else{
cat.debug("ケケヤ・ムモテサセミナマ「メサタタメウテ豬トXML start");
int iCount = vectorFeeRecord.size();
//bufXML.append( getXMLHead() ); cocoon イサミ靨ェ。」
bufXML.append("");
for( int j=0; j