gpt4 book ai didi

embedded-jetty - 嵌入式码头 - IllegalStateException : No SessionManager

转载 作者:行者123 更新时间:2023-12-04 02:05:56 27 4
gpt4 key购买 nike

我在谷歌上找到了很多关于这个问题的引用,但没有答案。我正在使用最新版本的 jetty (8.1.2.v20120308),但似乎无法获得能够使用 session 的嵌入式 servlet。该示例当然是在 Scala 中的,但它应该对任何 Java 程序员都可读。

val server = new Server();
val connector = new SelectChannelConnector()
connector.setPort(Integer.getInteger("jetty.port", 8080).intValue())
server.setConnectors(Array(connector))

val webapp = new ServletContextHandler(ServletContextHandler.SESSIONS)
webapp.setContextPath("/")
webapp.setResourceBase(webDir)
webapp.setServer(server)

val brzyServ = new ServletHolder(new BrzyDynamicServlet())
webapp.addServlet(brzyServ, "*.brzy")

server.setHandler(webapp);
server.start()

在我的 servlet 代码中:
...
log.debug("session manager: {}",req.asInstanceOf[Request].getSessionManager)
val session = req.getSession
...

req.getSession 抛出这个异常,它之前的调试行总是空的。
java.lang.IllegalStateException: No SessionManager
at org.eclipse.jetty.server.Request.getSession(Request.java:1173)

在日志中,我可以看到:
DEBUG org.eclipse.jetty.server.session - sessionManager=org.eclipse.jetty.server.session.HashSessionManager@2a8ceeea
DEBUG org.eclipse.jetty.server.session - session=null

我不确定这是否相关,但看起来有一个 session 管理器,但它在请求中不可用。

我已经用 WebAppContext 尝试过这个,结果相同。更不用说以十几种不同的方式显式设置 sessionManager 了。

最佳答案

我相信问题来自于您正在实例化 ServletContextHandler 的事实。而不是 WebappContext
试试

val webapp = new WebappContext();

要么
val webapp = new ServletContextHandler(ServletContextHandler.SESSIONS)
webapp.setSessionHandler(new SessionHandler())

来自 ServletContextHandler javadoc
 [...]construction of a context with ServletHandler and optionally session and security handlers [...]

optionally可能是这里的关键。

关于embedded-jetty - 嵌入式码头 - IllegalStateException : No SessionManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9862216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com