servlets - Embedded Jetty: How do I call setSessionTrackingModes without a ServletContextListener -
i'm wiring embedded jetty server in main
, want enforce cookies session tracking mode.
so try do:
//in main servletcontexthandler contexthandler = new servletcontexthandler(servletcontexthandler.sessions); contexthandler .getservletcontext() .setsessiontrackingmodes(enumset.of(sessiontrackingmode.cookie));
but following:
exception in thread "main" java.lang.illegalstateexception @ org.eclipse.jetty.servlet.servletcontexthandler$context.setsessiontrackingmodes(servletcontexthandler.java:1394)
my servlet context not yet initialized.
the obvious solution in servletcontextlistener
, i'd rather not. want wiring , setup stay in 1 central place without using listeners.
is there way?
the reason exception servletcontext
doesn't exist yet (your server isn't started yet).
but there's 2 ways accomplish this.
technique 1) explicit session management:
server server = new server(8080); // specify session id manager hashsessionidmanager idmanager = new hashsessionidmanager(); server.setsessionidmanager(idmanager); // create sessionhandler handle sessions hashsessionmanager manager = new hashsessionmanager(); manager.setsessiontrackingmodes(enumset.of(sessiontrackingmode.cookie)); // <-- here sessionhandler sessions = new sessionhandler(manager); // create servletcontext servletcontexthandler context = new servletcontexthandler(servletcontexthandler.sessions); context.setsessionhandler(sessions); // <-- set session handling server.sethandler(context);
this more appropriate way accomplish embedded jetty, can control entire creation / storage / behavior of sessions.
technique 2) use defaults, configure hashsessionmanager:
server server = new server(8080); // create servletcontext servletcontexthandler context = new servletcontexthandler(servletcontexthandler.sessions); context.getsessionhandler() .getsessionmanager() .setsessiontrackingmodes(enumset.of(sessiontrackingmode.cookie)); server.sethandler(context);
for simple web apps work fine.
Comments
Post a Comment