简体   繁体   English

在JSP中设置会话并在Servlet中获取会话

[英]Setting session in JSP and getting session in servlet

I am setting a session attribute in JSP code as below and submitting the HTTP form to servlet: 我在JSP代码中设置会话属性,如下所示,并将HTTP表单提交给servlet:

session.setAttribute("qnaInfo", qnaInfo);
System.out.println("session.getAttribute" + session.getAttribute("qnaInfo"));

I am trying to get session attribute in a servlet code as below: 我正在尝试在servlet代码中获取会话属性,如下所示:

HttpSession session = request.getSession(true);
AuthQnAInfo storedQnaInfo = (AuthQnAInfo) session.getAttribute("qnaInfo");

In servlet it is null only on IE. 在servlet中,仅在IE上为null。 But in Chrome I get the value. 但是在Chrome浏览器中,我得到了价值。
Please help to resolve this. 请帮助解决此问题。

在所有浏览器中,session.getAttribute(“ aaaa”)正常工作,尝试

From How can I enable session tracking for JSP pages if the browser has disabled cookies? 如果浏览器禁用了cookie,如何我如何为JSP页面启用会话跟踪? :

If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. 如果浏览器不支持cookie,或者禁用了cookie,您仍然可以使用URL重写启用会话跟踪。 URL rewriting essentially includes the session ID within the link itself as a name/value pair. URL重写实际上将会话ID包含在链接本身中,作为一个名称/值对。 However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. 但是,为了使此方法有效,您需要为属于Servlet响应的每个链接附加会话ID。 Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. 通过以下两种方法大大简化了将会话ID添加到链接的过程:response.encodeURL()将会话ID与给定的URL关联起来;如果您使用的是重定向,则可以通过以下方式使用response.encodeRedirectURL():重定向的URL作为输入。

Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; 首先,encodeURL()和encodeRedirectedURL()都确定浏览器是否支持cookie。 if so, the input URL is returned unchanged since the session ID will be persisted as a cookie. 如果是这样,则输入URL不变,因为会话ID将作为cookie保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM