Jetty http 会话始终为 null(嵌入式容器、ServletHolder)

发布于 2024-10-21 07:39:28 字数 834 浏览 2 评论 0原文

我正在尝试实现一个简单的 servlet,它使用 HTTP 会话 嵌入式码头(7.3.0 v20110203)容器。为了启动 jetty,我使用以下代码:

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();

servlet 获取一个会话

HttpSession session = request.getSession(true);

并在其中存储一些数据。在下一个请求时,它会获取会话 使用以下代码:

HttpSession session = request.getSession(false);

会话始终为空。

我没有找到任何信息 互联网上关于这个特定问题的信息。我也尝试过 设置 SessionManager 或 SessionIdManager,但这似乎没有 改变任何事情。我怀疑我在这里遗漏了有关 SessionManager 或 SessionIdManager 或 SessionHandler 的信息,但这只是一个疯狂的猜测。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杯敬自由 2024-10-28 07:39:28

您的代码与 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();
    }

因此会话可能在代码中的其他地方失效。

还可以使用 ServletContextHandlersetSessionHandler() 方法显式设置 SessionHandler

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.

携余温的黄昏 2024-10-28 07:39:28
//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");
...
//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");
...
千寻… 2024-10-28 07:39:28

HttpServletResponse#reset() 方法会销毁新创建的会话,因此下次会创建一个新会话,依此类推。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文