Embedded Jetty - IllegalStateException: No SessionManager

淺唱寂寞╮ 提交于 2020-01-12 14:08:33

问题


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

in my servlet code:

...
log.debug("session manager: {}",req.asInstanceOf[Request].getSessionManager)
val session = req.getSession
...

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.

I've tried this with the WebAppContext with the same result. Not to mention explicitly setting the sessionManager in a dozen different ways.


回答1:


I believe the issue comes from the fact that you are instantiating a ServletContextHandler rather than a WebappContext

Try

val webapp = new WebappContext();

or

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

From the ServletContextHandler javadoc

 [...]construction of a context with ServletHandler and optionally session and security handlers [...]

The word optionally is likely the key here.




回答2:


In jetty 9.4, to enable a very simple session handler for a servlethandler:

private static void setSessionEnableContext( Server server,ServletHandler handlerServlet ) {
      // Specify the Session ID Manager        
    SessionIdManager idmanager = new DefaultSessionIdManager(server);
    server.setSessionIdManager(idmanager);
   // Specify the session handler
    SessionHandler sessionsHandler = new SessionHandler();       
    handlerServlet.setHandler(sessionsHandler);           
}



回答3:


ok, I feel a little foolish, this issue was in my servlet, I was accessing the request in a child thread, that accessed the session when the request was out of scope. And in googling the error, it sent me down the wrong path because the error message was a bit vague. Thanks to BGR for the reply.



来源:https://stackoverflow.com/questions/9862216/embedded-jetty-illegalstateexception-no-sessionmanager

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