14

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.

Michael Fortin
  • 171
  • 1
  • 1
  • 6

3 Answers3

13

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.

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • Thanks for the reply, but I've tried both those variants, with no success. I've also tried adding the HashSessionManager explicitly to the SessionHandler, but no matter what I try, the jetty Request.getSession() always throws IllegalStateException. The log output from jetty shows that a session manager exists, it just doest seem to be available in the request. – Michael Fortin Mar 26 '12 at 17:01
  • Michael, looking at the Jetty code, the only way I could see this happening is that no SessionHandler is associated to and initialized on the request. Do you have any sort of authentication in place in your web.xml? – Bruno Grieder Mar 27 '12 at 07:11
  • I was having the same problem, and a slight variant of BGR's answer worked for me, I simply added a new session handler to my servlet context handler directly without creating a WebAppContext and it worked fine. servletContextHandler.setSessionHandler(new SessionHandler()) – Ben Hardy Jan 05 '13 at 02:40
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);           
}
Bertrand
  • 53
  • 4
0

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.

Michael Fortin
  • 171
  • 1
  • 1
  • 6