servlet如何将SESSION传递给另一SERVLET?

sssaaazzz 2001-05-31 10:15:00
因业务需要,我要用一个SERVLET(A)对客户端提交的数据处理后再提交到另一个SERVLET(B)进行处理,之后将B的返回值交A处理后返回客户端,现在问题是:A可以记录下用户的SESSION,而B记录的SESSION是A的而不是客户的,且因A是SERVLET,所以B记录的SESSION总是新的,请问如何将A记录的SESSION传递给B?
我利用URL Rewriting来做时系统总是提示“该页无法显示”
我的测试程序如下:
public class test4 extends javax.servlet.http.HttpServlet
{
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
performTask(request,response);
}

public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
performTask(request,response);
}

public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
String strURL="http://10.1.1.21/servlet/test.test5";
try
{
HttpSession session = request.getSession(true);
int count=1;
Integer i = (Integer) session.getValue("I");
if (i!= null) {count=i.intValue()+1;}
session.putValue("I",new Integer(count));
strURL=strURL+";$sessionid$"+session.getId();
// strURL=response.encodeURL(strURL);
URL url= new URL(strURL);
URLConnection connection =url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print("UserID=3&Password=123456\n");
out.close();

BufferedReader in = new BufferedReader (new InputStreamReader(connection.getInputStream()));
String line;
String str = "";
while ((line=in.readLine()) != null)
{
str += line;
}
in.close();

strURL="http://10.1.1.21/servlet/test.test4";
strURL=response.encodeURL(strURL);

PrintWriter out2 = response.getWriter();
out2.println("<html><head><title>test</title></head><body>");
out2.println(str+"<br>strURL="+strURL+" sessionid="+session.getId()+" i=");
out2.println((new Integer(count)).toString());
out2.println("<form method=get action=\""+strURL+"\">");
out2.println("<input type=submit values=\"ok\"></form></body></html>");
out2.flush();
out2.close();
}
catch (Exception e) {}
}
}



public class test5 extends javax.servlet.http.HttpServlet
{
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
performTask(request,response);
}

public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
performTask(request,response);
}

public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
try
{
String uri = request.getRequestURI();
String qstr = request.getQueryString();
String strUserID=request.getParameter("UserID");
String strPassword=request.getParameter("Password");

HttpSession session = request.getSession(true);
int count=1;
Integer i = (Integer) session.getValue("I");
if (i!= null) {count=i.intValue()+1;}
session.putValue("I",new Integer(count));

PrintWriter out2 = response.getWriter();
out2.println("uri="+uri+" qstr="+qstr+" userid="+strUserID+" Password="+strPassword+" i="+count+" SessionID="+session.getId());
out2.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
...全文
37 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sssaaazzz 2001-06-08
我要在A中处理request后才能向B提交,B返回的response要经A处理后才能返回客户端,应如何处理?
回复
howlet 2001-06-06
你可以A中用RequestDispatcher rd = Request.getRequestDispatcher("url_B");
rd.forward(request,reponse);//定向到url_B
B处理完后,再用同样方法定向A
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2001-05-31 10:15
社区公告
暂无公告