简体   繁体   English

如何保持会话属性

[英]How to keep alive a session attribute

I have a "look up" html page that works together with a java HttpServlet class that handles some functionality and sends the results to a jsp page. 我有一个“查找” html页面,该页面与java HttpServlet类一起使用,该类处理某些功能并将结果发送到jsp页面。

To perform the look up I need a loginresult object that I put in the session in a different servlet. 为了执行查找,我需要一个我在会话中放入不同servlet的loginresult对象。

This is what happens in the login servlet: 这是在登录servlet中发生的情况:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        // get attributes that were filled in on the login webpage
        request.getAttribute("username");
        request.getAttribute("password");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        LoginManager loginManager = new LoginManager(username.trim(), password.trim());
        if (loginManager.doLogin() == true) {
            javax.servlet.http.HttpSession session = request.getSession();
            session.setAttribute("loginResult", loginManager.getLoginResult());
            session.setAttribute("binding", loginManager.getBinding());
            response.sendRedirect("lookup.html");

This is what happens on the look up servlet 这是在查找servlet上发生的情况

request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");
            javax.servlet.http.HttpSession session = request.getSession();
            LoginResult lr = (LoginResult) session.getAttribute("loginResult");
            if(lr == null)
            {
                throw new Exception("Your session has expired, please log in again");
            }

            // some look up code

            RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
            request.setAttribute("result", result);
            request.setAttribute("question", question);
            request.setAttribute("xml", xml);
            dispatcher.forward(request, response);

In my jsp page is a input button that does the following : ONCLICK="window.location.href='lookup.html'"/> 在我的jsp页面中是一个执行以下操作的输入按钮:ONCLICK =“ window.location.href ='lookup.html'” />

Sometimes when i leave my result page unfocused or minimize or whatever or I wait too long and I click the return to the look up page it appears that my loginresult object == null. 有时,当我让结果页面不集中注意力或使其最小化时,或者等待太久,然后单击返回查找页面时,似乎我的loginresult对象== null。 So in my opinion it seams to be expired. 因此,我认为它即将过期。 Although I thought that the Apache Tomcat server keeps sessions alive for 30 minutes standard. 尽管我认为Apache Tomcat服务器可以将会话保持30分钟的标准运行时间。

Any guess why my session attribute disappear? 任何猜测为什么我的会话属性消失了?

First you should determine if you are actually getting the same session or not. 首先,您应该确定您是否实际上正在获得相同的会话。 There are 2 easy ways I can think of to do this. 我可以想到两种简单的方法来做到这一点。

1.) Look at the contents of the JSESSIONID cookie. 1.)查看JSESSIONID cookie的内容。 Most browsers make this trivial. 大多数浏览器都做到这一点。 If the contents change, you have received a different session. 如果内容更改,您将收到另一个会话。

2.) You could try plugging in an HttpSessionListener to log when your sessions are being destroyed. 2.)您可以尝试插入HttpSessionListener来记录会话被销毁的时间。

If you are getting a new session, you have to narrow it down to a configuration issue (Tomcat, web.xml, context snippet etc.) or an app issue. 如果要获得新会话,则必须将其范围缩小到配置问题(Tomcat,web.xml,上下文片段等)或应用程序问题。 If it's a configuration issue, the problem should be repeatable on other pages than the ones you mention. 如果是配置问题,则该问题应该在您提到的页面之外的其他页面上重复出现。

Also consider using getSession(false), which won't create a new session if one isn't already present. 还可以考虑使用getSession(false),如果尚不存在,则不会创建新会话。 If you get null from this, it's another indicator that your sessions are timing out. 如果您从中得到空值,则表明会话正在超时。

If you determine you have the same session, but for some odd reason attributes are disappearing, you can implement a HttpSessionAttributeListener and either log or breakpoint when items are removed from the session. 如果确定您具有相同的会话,但是由于某些奇怪的原因,属性消失了,则可以实现HttpSessionAttributeListener以及从会话中删除项目时的日志或断点。

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

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