读书人

怎么通过jsessionid找到session

发布时间: 2011-12-19 23:23:36 作者: rapoo

如何通过jsessionid找到session
我知道一个jsessionid,那么如何找到匹配的session,就是说如何调用原session而不是新生成一个session?
first.jsp
<html>

<body scroll= "no " background= " <%=request.getContextPath()%> /login/registeredUser/USA/loginBGLine.jpg ">
<% out.println(session.getId());
session.setAttribute( "test ", "sessionTest ");
%>
<a href= "testSession.jsp " > link to testSession </a>
</body>
</html>


testSession.jsp
<html>
<body scroll= "no " background= " <%=request.getContextPath()%> /login/registeredUser/USA/loginBGLine.jpg ">
<% out.println(session.getId());
//HttpSession s = new HttpSession();

out.println( "tesst= "+session.getAttribute( "test "));
out.println( "request.isRequestedSessionIdFromURL()= "+request.isRequestedSessionIdFromURL());
%>
</body>
</html>

我通过first.jsp访问testSession.jsp就能得到数据。
假设我通过这样的方式访问了一次以后,我重新开启浏览器,这时候也知道了jseesionid,那我如何取得相应数据呢?

[解决办法]
HttpSessionContext SessCon= request.getSession(false).getSessionContext();
HttpSession Sess = SessCon.getSession(SessionId);

[解决办法]
这个使用IE是不行的,必须使用特殊的工具,来提交一个自定义的HTTP数据包
[解决办法]
session是使用threadlocal保护的变量,如果你当前所在的线程不属于所找session的线程,是不能获取该session的。
[解决办法]
在这个session还没有过期或invaildate前,

你可以在新开的IE中,调用JavaScript修改cookie

document.cookie = 'JSESSIONID= ' + cookieid;


这样这个IE之后的访问就会自动使用此前的Session

读书人网 >Java Web开发

热点推荐