如何在 servlet 之间共享会话状态?
我有一个 Web 应用程序,其中包含两个 servlet,一个用于呈现 JSP 页面,另一个用于生成 PDF。我在 JSP 页面之间使用会话状态,并希望将会话对象传递给 PDF servlet。
下面是我如何在 JSP 中设置会话值的示例:
MyObject o = (MyObject)session.getAttribute("my.object");
if (o == null)
{
o = new MyObject();
session.setAttribute("my.object", o);
}
然后,我从 JSP 中的链接发布到新的 servlet 以生成 PDF,
<a href="../pdfgen?f=d&t=c" target="_blank">Generate a draft report for review</a>
我认为可以使用 HTTPRequest 对象在 servlet 中返回会话,如下所示:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession(false);
MyObject o = (MyObject) session.getAttribute("my.object");
}
使用上面的代码,我从请求中获取了一个空会话对象。
如果我使用 request.getSession(true) ,我会得到一个会话对象,但当然它在属性 my.object 中不包含任何内容。
这应该如何运作?在 servlet 之间共享会话状态的规则是什么?
雄猫 6
TIA
I have a web app which contains two servlets, one to render my JSP pages, and another to generate PDFs. I use session state between the JSP pages and want to pass a session object to the PDF servlet.
Here's an example of how I set the session values in the JSP:
MyObject o = (MyObject)session.getAttribute("my.object");
if (o == null)
{
o = new MyObject();
session.setAttribute("my.object", o);
}
I then post off to my new servlet for the PDF generation from a link in my JSP
<a href="../pdfgen?f=d&t=c" target="_blank">Generate a draft report for review</a>
I thought I could use the HTTPRequest object to return the session in my servlet as follows:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession(false);
MyObject o = (MyObject) session.getAttribute("my.object");
}
Using the above code I get a null session object from the request.
If I use request.getSession(true)
I get a session object, but of course it doesn't contain anything in the attribute my.object
.
How is this supposed to work? What are the rules about sharing session state between servlets.
Tomcat 6
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在 JSP 中设置它。那时可能已经太晚了,因为 JSP 是 HTTP 响应的一部分并且可能已经提交了响应。每当需要创建新会话时,servlet 容器都需要将会话 cookie 添加到 HTTP 响应标头中。但当响应已经提交时,这种情况就不会发生。根据 servlet 容器,尝试执行此操作时,您应该在服务器日志中看到
IllegalStateException
。在将请求转发到 JSP(您所说的“呈现”JSP 的其中一个)之前,请在 servlet 中设置它。
与具体问题无关,如果您需要会话,请不要使用
request.getSession(false)
,只需使用request.getSession()
>。那些潜在的 NPE 和空检查很笨拙。另外,在 JSP 中编写原始 Java 代码(使用 scriptlet)也被视为 不良做法。Don't set it in the JSP. It might be already too late then since JSP is part of the HTTP response and can already have committed the response. Whenever a new session needs to be created, the servlet container needs to add the session cookie to the HTTP response header. But that ain't going to happen when the response is already committed. Depending on the servletcontainer, you should have seen an
IllegalStateException
in the server logs when attempting to do so.Set it in the servlet instead, before forwarding the request to the JSP (the one of which you said that it "renders" JSP).
Unrelated to the concrete problem, don't use
request.getSession(false)
in cases you need the session, just userequest.getSession()
. Those potential NPEs and nullchecks are clumsy. Also writing raw Java code in JSP (using scriptlets) is considered poor practice.您能否检查一下您是否有这样的声明:<%@ page session="false" %> ?
如果您使用 JSP 隐式会话对象来设置值,则该值应该可供 servlet 使用。
不过你的代码看起来有效..
Can you check if you have this by any chance declared :<%@ page session="false" %> ?
If you are using JSP implicit session object to set a value, it should be available to the servlet.
You code looks valid though..