java 你如何保持一个tomcat会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/834330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:57:47  来源:igfitidea点击:

How do you persist a tomcat session?

javatomcat

提问by SS.

i have a JSP web page that refreshes every 1 minute. on each refresh, the session object is checked for validity. When the tomcat web server restarts, the session goes away...and when the page refreshes, it says "invalid". anyone has a solution to my problem?

我有一个 JSP 网页,每 1 分钟刷新一次。每次刷新时,都会检查会话对象的有效性。当 tomcat Web 服务器重新启动时,会话就会消失……当页面刷新时,它会显示“无效”。有人能解决我的问题吗?

回答by Guillaume

Have a look at the configuration in your Tomcat config file. The documentation is at http://tomcat.apache.org/tomcat-6.0-doc/config/manager.htmlLook for the section on persistent managers ...

查看 Tomcat 配置文件中的配置。该文档位于http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html查找有关持久管理器的部分...

回答by Clinton

You have to make sure that ALL your objects your store in your Session are Serializable. If one of them isn't (or doesn't meet the Serializable requirements) you will lose your session on web app reload or tomcat restart.

您必须确保您在会话中存储的所有对象都是可序列化的。如果其中之一不是(或不满足可序列化要求),您将在 Web 应用程序重新加载或 tomcat 重新启动时丢失会话。

EG: The following works fine for a Servlet:

EG:以下适用于 Servlet:

public class MainServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        HttpSession session = request.getSession();
        Date date = (Date) session.getAttribute("date");
        if (date == null) {
                date = new Date();
                session.setAttribute("date", date);
        }
        response.setContentType("text/plain");
        PrintWriter pw = response.getWriter();
        pw.println("New Session? " + session.isNew());
        pw.println("Date : " + date);
        pw.flush();
    }

}