Set Jetty session cookie name programmatically

2019-06-04 03:40发布

问题:

I'm running in this issue, how can I set session cookie name by code in Jetty 8?

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
sessionHandler = new SessionHandler();
sessionHandler.getSessionManager().setSessionCookie("JSESSIONID_"+runningPort);
context.setSessionHandler(sessionHandler);

Is wrong, in Jetty8 SessionManager setSessionCookie(String) was removed.

回答1:

Here is the answer:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
SessionManager sm = new HashSessionManager();
((HashSessionManager)sm).setSessionCookie("JSESSIONID_"+activity.WEB_SERVER_PORT);
context.setSessionHandler(new SessionHandler(sm));


回答2:

I had to solve this problem with Jetty 9.3 and the solution is slightly different:

SessionManager sessionManager = new HashSessionManager();
sessionManager.setMaxInactiveInterval(60 * 15); //session time out of 15 minutes
HashSessionIdManager idManager = new HashSessionIdManager();
sessionManager.getSessionCookieConfig().setName("JSESSIONID_" + Integer.toString(m_serverSettings.getM_webServerPort()));
sessionManager.setSessionIdManager(idManager);
SessionHandler sessionHandler = new SessionHandler(sessionManager);


回答3:

Try to use Servlet 3.0 Session Configuration , here is a doc that could help you.