嵌入式码头-IllegalStateException:没有SessionManager [英] Embedded Jetty - IllegalStateException: No SessionManager
问题描述
我在Google上找到了很多有关此问题的参考,但没有找到答案。我正在使用最新版本的jetty(8.1.2.v20120308),但似乎无法获得嵌入式servlet来使用会话。该示例当然在scala中,但是任何Java程序员都应该可以阅读。
I've found plenty of references to this issue on google but no answers. I'm using the latest version of jetty (8.1.2.v20120308) and I can't seem to get an embedded servlet to be able to use sessions. The example is in scala of course, but it should be readable to any java programmer.
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代码中:
in my servlet code:
...
log.debug("session manager: {}",req.asInstanceOf[Request].getSessionManager)
val session = req.getSession
...
req.getSession抛出此异常,并且之前的调试行始终为null。
The req.getSession throws this exception, and the debug line before it, is always null.
java.lang.IllegalStateException: No SessionManager
at org.eclipse.jetty.server.Request.getSession(Request.java:1173)
在日志中,我可以看到:
In the log I can see this:
DEBUG org.eclipse.jetty.server.session - sessionManager=org.eclipse.jetty.server.session.HashSessionManager@2a8ceeea
DEBUG org.eclipse.jetty.server.session - session=null
我不确定这是否相关,但是似乎有一个会话管理器,但在请求中不可用。
I'm not sure if that's relevant, but it would appear that there is a session manager but it's not available on the request.
我已经使用WebAppContext尝试了相同的结果。更不用说以多种方式显式设置sessionManager了。
I've tried this with the WebAppContext with the same result. Not to mention explicitly setting the sessionManager in a dozen different ways.
推荐答案
我认为问题出在您实例化 ServletContextHandler
而不是 WebappContext
I believe the issue comes from the fact that you are instantiating a ServletContextHandler
rather than a WebappContext
尝试
val webapp = new WebappContext();
或
val webapp = new ServletContextHandler(ServletContextHandler.SESSIONS)
webapp.setSessionHandler(new SessionHandler())
来自ServletContextHandler javadoc
From the ServletContextHandler javadoc
[...]construction of a context with ServletHandler and optionally session and security handlers [...]
单词可选
可能是这里的关键。
The word optionally
is likely the key here.
这篇关于嵌入式码头-IllegalStateException:没有SessionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!