Struts 2
  1. Struts 2
  2. WW-3773

CreateSessionInterceptor and HttpSessionListener do not cooperate well

    Details

      Description

      When the current http session has expired, a new http session can be automatically created by using a CreateSessionInterceptor. However, this new session is not 'synchronized' with the action context session, i.e., the map that is injected into any action that is SessionAware. This map is always empty, however a newly created http session need not necessarily be empty, for example when we register a HttpSessionListener with the web application that automatically adds something to the session. (May be useful if you want to avoid null checks when retrieving a known object from the session.)

        Activity

        Kris Coolsaet created issue -
        Hide
        Lukasz Lenart added a comment - - edited

        Simple

        ServletActionContext.getContext().setSession(new SessionMap<String, Object>(ServletActionContext.getRequest()));
        

        should solve problem, right ?

        Show
        Lukasz Lenart added a comment - - edited Simple ServletActionContext.getContext().setSession( new SessionMap< String , Object >(ServletActionContext.getRequest())); should solve problem, right ?
        Lukasz Lenart made changes -
        Field Original Value New Value
        Assignee Lukasz Lenart [ lukaszlenart ]
        Fix Version/s 2.3.2 [ 12319199 ]
        Hide
        Kris Coolsaet added a comment -

        I am not sufficiently familiar with the inner workings of Struts to comment on Lukasz' solution (although it looks OK), but in any case I would only execute this statement if the HTTP session was indeed renewed. Something like

        HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
        if (httpSession == null) {
            ServletActionContext.getRequest().getSession(true);
            ServletActionContext.getContext().setSession(new SessionMap<String, Object>(ServletActionContext.getRequest()));
        }
        
        Show
        Kris Coolsaet added a comment - I am not sufficiently familiar with the inner workings of Struts to comment on Lukasz' solution (although it looks OK), but in any case I would only execute this statement if the HTTP session was indeed renewed. Something like HttpSession httpSession = ServletActionContext.getRequest().getSession( false ); if (httpSession == null ) { ServletActionContext.getRequest().getSession( true ); ServletActionContext.getContext().setSession( new SessionMap< String , Object >(ServletActionContext.getRequest())); }
        Hide
        Lukasz Lenart added a comment -

        Hmmm... you are right

        Show
        Lukasz Lenart added a comment - Hmmm... you are right
        Hide
        Lukasz Lenart added a comment -

        but ServletActionContext.getRequest().getSession(true); creates session if it doesn't exist. My only concern is about SessionMap, it should be created only if a new session is created.

        Show
        Lukasz Lenart added a comment - but ServletActionContext.getRequest().getSession(true); creates session if it doesn't exist. My only concern is about SessionMap, it should be created only if a new session is created.
        Hide
        Lukasz Lenart added a comment -

        Please test the snapshot, thanks for reporting!

        Show
        Lukasz Lenart added a comment - Please test the snapshot, thanks for reporting!
        Lukasz Lenart made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Hide
        Hudson added a comment -

        Integrated in Struts2 #432 (See https://builds.apache.org/job/Struts2/432/)
        WW-3773 recreates HttpSession and SessionMap if HttpSession doesn't exist (Revision 1301989)

        Result = SUCCESS
        lukaszlenart :
        Files :

        • /struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java
        • /struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CreateSessionInterceptorTest.java
        Show
        Hudson added a comment - Integrated in Struts2 #432 (See https://builds.apache.org/job/Struts2/432/ ) WW-3773 recreates HttpSession and SessionMap if HttpSession doesn't exist (Revision 1301989) Result = SUCCESS lukaszlenart : Files : /struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java /struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CreateSessionInterceptorTest.java
        Lukasz Lenart made changes -
        Fix Version/s 2.3.3 [ 12320642 ]
        Fix Version/s 2.3.2 [ 12319199 ]
        Lukasz Lenart made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Lukasz Lenart
            Reporter:
            Kris Coolsaet
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development