Jetty http 会话始终为 null(嵌入式容器、ServletHolder)
我正在尝试实现一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码与 BaseServlet 的骨架实现配合得很好:
因此会话可能在代码中的其他地方失效。
还可以使用
ServletContextHandler
的setSessionHandler()
方法显式设置SessionHandler
。Your code works fine with this skeletal implementation of BaseServlet:
so the session is probably being invalidated somewhere else in your code.
The
SessionHandler
can also be explicity set using thesetSessionHandler()
method ofServletContextHandler
.HttpServletResponse#reset()
方法会销毁新创建的会话,因此下次会创建一个新会话,依此类推。The method
HttpServletResponse#reset()
destroys a newly created session, so a new one is created the next time and so on.