I have an application using Jetty 8.1.8 (I can't upgrade it to the most current 9.4 at this time although I did also try the latest 8.1.22) as an embedded server started programmatically. It's been working great for a number of years and we haven't had to change any of it until now, however we now have need of basic user sessions so we're adding that.
Unfortunately, when we try to extract the session to use it in our handlers with HttpSession session = request.getSession(true);
we get the following exception:
java.lang.IllegalStateException: No SessionManager
at org.eclipse.jetty.server.Request.getSession(Request.java:1270)
I've dug through all the documentation and many SO posts such as this and this and cannot get this to work without throwing the above exception.
Here is the code we are currently using after many variations attempting to add the sessions.
Server server = new Server(Port);
server.setGracefulShutdown(5000);
server.setStopAtShutdown(true);
HandlerCollection handlers = new HandlerCollection();
// MainHandler inherits from Jetty's AbstractHandler and just contains a couple of additional simple variables that get updated when `handle` is called
MainHandler main = new MainHandler(...);
// *** Session additions begin here
HashSessionIdManager idManager = new HashSessionIdManager();
server.setSessionIdManager(idManager);
HashSessionManager sessionManager = new HashSessionManager();
SessionHandler sessions = new SessionHandler(sessionManager);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setHandler(sessions);
handlers.setHandlers(new Handler[] { main, context });
// *** Session additions end here
server.setHandler(handlers);
//server.setHandler(main); // this is how it was set previously
server.start();
Does anyone have ideas on what might be wrong with this? Hopefully it's something simple that we're overlooking.