Embedded Jetty - IllegalStateException: No Session

2019-04-20 18:11发布

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.

3条回答
Luminary・发光体
2楼-- · 2019-04-20 18:37

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);           
}
查看更多
Juvenile、少年°
3楼-- · 2019-04-20 18:54

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.

查看更多
时光不老,我们不散
4楼-- · 2019-04-20 19:00

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.

查看更多
登录 后发表回答