简体   繁体   English

在Servlet中设置后,HttpSession变量为null

[英]HttpSession variable null after being set in the Servlet

Here's my environment. 这是我的环境。 Windows 7, Tomcat 8.x and Java 7.x I'm writing an application, that has an entry point in the servlet, as opposed to a JSP. Windows 7,Tomcat 8.x和Java 7.x我正在编写一个应用程序,该应用程序在servlet中具有一个入口点,而不是JSP。 In this servlet I create a session, assign a variable to it, and then redirect the user to a JSP page. 在此servlet中,我创建一个会话,为其分配一个变量,然后将用户重定向到JSP页面。 Here's the JAVA code: 这是JAVA代码:

HttpSession session = request.getSession();
logger.debug("Session Id: "+session.getId()+
"New Session? "+session.isNew()+
"Created: "+new java.util.Date(session.getCreationTime()));
  session.setAttribute("implementation", sImplementation);
  session.setAttribute("RequestId", sRequestId);

response.reset();
response.sendRedirect(request.getContextPath()+"/jsp/ProductSelection.jsp");

In the JSP page that I just redirected to I try to retrieve the session attribute that I just set in the Servlet and the value comes back as null, but only the first time that I use the JSP page. 在我刚刚重定向到的JSP页面中,我尝试检索我刚刚在Servlet中设置的session属性,并且该值返回为null,但这只是我第一次使用JSP页面。 If I call it again by resubmitting the URL in the browser then everything works. 如果我通过在浏览器中重新提交URL再次调用它,则一切正常。 :O :( Here's the relevant JSP code, the value of sImplementation is null the first time this JSP is hit. :O :(这是相关的JSP代码,第一次单击此JSP时sImplementation的值为null。

<% System.out.println("Session Id: "+session.getId()+
            "New Session? "+session.isNew()+
            "Created: "+new java.util.Date(session.getCreationTime()));
String sImplementation = (String)session.getAttribute("implementation"); %>

Also if I make an entry point into my system to be a JSP page that does nothing but redirect the user to the Servlet everything works as expected. 同样,如果我将进入系统的入口点设置为一个JSP页面,则该页面除了将用户重定向到Servlet之外什么也不做,一切都按预期工作。 So the session created in the servlet is not valid until. 因此,直到servlet中创建的会话才有效。 Only when a JSP page is hit. 仅在点击JSP页面时。 :( :(

Lastly I tried using dispatcher.forward instead of response.sendRedirect and the session variable is there, however, the bootstrap framework that I'm using to render my pages do not render properly at all. 最后,我尝试使用dispatcher.forward而不是response.sendRedirect,并且会话变量在那里,但是,我用来呈现页面的引导框架根本无法正确呈现。 :( I tried this in both Internet Exploder 11.x and Chromium 33.x :(我在Internet Exploder 11.x和Chromium 33.x中都尝试过

So my question is whether the behavior that I'm seeing is normal and expected or if there's something wrong here and there's a solution out there somewhere? 所以我的问题是我所看到的行为是否正常并且是预期的,或者这里是否有问题并且某个地方有解决方案? :) :)

Thanks to all in advance, and let me know if anything is unclear or needs more code. 预先感谢所有人,如果有任何不清楚的地方或需要更多代码,请通知我。

If im not wrong (I might), you should redirect to your jsp using 如果im没错(我可能),则应使用重定向到jsp

request.getRequestDispatcher("ProductSelection.jsp").forward(request, response);

This way it will be the same request and you will be able to get your session. 这样,它将是相同的请求,您将能够获得会话。 By the way if you are creating sessions on your own you should disable the default session made by all jsp's 顺便说一句,如果您自己创建会话,则应禁用所有jsp的默认会话

<%@ page session=“false”%>

Make me know if it worked. 让我知道它是否有效。 :·) :·)

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

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