Jetty http session is always null (Embedded Container, ServletHolder)

时光毁灭记忆、已成空白 提交于 2020-01-01 08:56:27

问题


I am trying to implement a simple servlet which uses a HTTP session in an embedded jetty (7.3.0 v20110203) container. To start jetty I use the following code:

Server server = new Server(12043);
ServletContextHandler handler = new
            ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
server.setHandler(handler);
ServletHolder holder = new ServletHolder(new BaseServlet());
handler.addServlet(holder, "/*");
server.start();
server.join();

The servlet acquires a session with

HttpSession session = request.getSession(true);

and stores some data in it. Upon the next request it gets the session with the following code:

HttpSession session = request.getSession(false);

and there the session is always null.

I did not find any information on the internet about this particular problem. I have also experimented with setting a SessionManager or SessionIdManager, but that did not seem to change anything. I suspect I am missing something about SessionManager or SessionIdManager or SessionHandler here, but this is just a wild guess.


回答1:


Your code works fine with this skeletal implementation of BaseServlet:

public class BaseServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        boolean create = "true".equals(req.getParameter("create"));

        HttpSession session = req.getSession(create);
        if (create) {
            session.setAttribute("created", new Date());
        }

        PrintWriter pw = new PrintWriter(resp.getOutputStream());
        pw.println("Create = " + create);
        if (session == null) {
            pw.println("no session");
        } else {
            pw.println("Session = " + session.getId());
            pw.println("Created = " + session.getAttribute("created"));
        }

        pw.flush();
    }

so the session is probably being invalidated somewhere else in your code.

The SessionHandler can also be explicity set using the setSessionHandler() method of ServletContextHandler.




回答2:


//You need to setup SessionManager first. This worked for me.
Server server = new Server(8031);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);

HashSessionIdManager hashSessionIdManager = new 
HashSessionIdManager();
SessionHandler sessionHandler = new SessionHandler();
SessionManager sessionManager = new HashSessionManager();
sessionManager.setSessionIdManager(hashSessionIdManager);
sessionHandler.setSessionManager(sessionManager);
sessionHandler.setHandler(handler);
sessionHandler.setServer(server);
server.setSessionIdManager(hashSessionIdManager);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet("webservlet.IndexServlet", "/index");
context.addServlet("webservlet.HomeServlet", "/home");
context.addServlet("webservlet.CategoryServlet", "/category");
context.addServlet("webservlet.ProductServlet", "/product");
...



回答3:


The method HttpServletResponse#reset() destroys a newly created session, so a new one is created the next time and so on.



来源:https://stackoverflow.com/questions/5284262/jetty-http-session-is-always-null-embedded-container-servletholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!