[undertow-dev] Sharing session attributes in NonBlocking Handlers and Traditional Servlets

Stuart Douglas sdouglas at redhat.com
Mon Feb 17 22:27:57 EST 2014


Basically you need to use io.undertow.servlet.spec.ServletContextImpl#getSession(). 

If your handler is running after the initial servlet handler you can get this from the io.undertow.servlet.handlers.ServletRequestContext, which is attached to the exchange. 

If you are running before the initial handler you need to store a reference to the servlet context or the Deployment somewhere.

Servlet does not use SessionAttachmentHandler, because each servlet context can have its own session with its own settings, and multiple contexts can be involved in the same request if forward/include is used. As a result each servlet context has its own session manager, and manages it internally.

Stuart

----- Original Message -----
> From: "Lightspoke Discussion" <lsgroup at gmail.com>
> To: undertow-dev at lists.jboss.org
> Sent: Tuesday, 18 February, 2014 1:08:58 AM
> Subject: [undertow-dev] Sharing session attributes in NonBlocking Handlers	and Traditional Servlets
> 
> Hello,
> 
> Does anyone know how you might go about sharing session attributes between a
> servlet and a non-blocking handler in the same project?
> 
> InMemorySessionManager sessman = new InMemorySessionManager(foundsessid);
> SessionAttachmentHandler sessattach = new SessionAttachmentHandler(sessman,
> sessconf);
> SessionManager regsessman = sessattach.getSessionManager();
> Session session;
> session = sessman.getSession(exchange, sessconf);
> //returns null
> 
> 
> 
> SessionCookieConfig sessconf = new SessionCookieConfig();
> String foundsessid = sessconf.findSessionId(exchange);
> session = regsessman.getSession(foundsessid);
> System.out.println("getsession by id ="+session );
> //returns null
> 
> 
> session = regsessman.createSession(exchange, sessconf);
> System.out.println("create session ="+session );
> 
> //returns a session - but ...
> 
> 1. session attributes from servlet do not return its value and
> 2. session attributes across http requests do not persist in memory
> 
> Thanks in advance!
> 
> 
> 
> --
> --
> Mike
> 
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev


More information about the undertow-dev mailing list