2 Replies Latest reply on Mar 10, 2014 10:57 AM by pferraro

    Sharing session attributes in NonBlocking Handlers and Traditional Servlets

    lsgroup

      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

       

      Also, I noticed that if the session id on a given servlet is : ABCD1234, then in the non-blocking handler side, the session id is : ABCD1234.REMOTECLIENTID

       

      Are the two the same session or different?  I need to be able to share the user session between the two.

       

       

      Thanks in advance!

        • 1. Re: Sharing session attributes in NonBlocking Handlers and Traditional Servlets
          lsgroup

          Do sessions not work for non-blocking servlets?  I've tried several ways to obtain an existing session, but they all return a null object.  When I call

          createSession() is creates a non-null session object that holds the same sessionid as when findSessionId(exchange) is called.  However, I can't seem to retrieving a session using that id. 

           

           

                  SessionCookieConfig sessconf = new SessionCookieConfig();

                  String foundsessid = sessconf.findSessionId(exchange);

                  //foundsessid has value...

                  SessionCookieSource sesscookiesource = sessconf.sessionCookieSource(exchange);

                  InMemorySessionManager sessman = new InMemorySessionManager(foundsessid);

           

                  session = sessman.getSession(exchange, sessconf);

                  //session returns null...

           

                  SessionAttachmentHandler sessattach = new SessionAttachmentHandler(sessman, sessconf);

                  SessionManager regsessman = sessattach.getSessionManager();

                  session = regsessman.getSession(exchange, sessconf);

                  //session returns null...

           

                  session = regsessman.getSession(foundsessid);

                 //session returns null...

           

          Has anyone out there gotten session to work for non-blocking handlers in Wildfly 8?

          • 2. Re: Re: Sharing session attributes in NonBlocking Handlers and Traditional Servlets
            pferraro

            I'm not sure I understand what you're trying to do...

             

            Why are you creating your own SessionManager instance?

             

            Instead your servlet should use the standard HttpServletRequest.getSession(...) method to obtain a reference to the session.

             

            In your non-blocking handler, you can access the servlet request from the exchange.

            e.g.

            public void handleRequest(HttpServerExchange exchange) {
                 ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                 HttpServletRequest request (HttpServletRequest) context.getServletRequest();
                 HttpSession session = request.getSession();
            }