Search code examples
javajspstruts2interceptorhttpsession

java.lang.IllegalStateException thrown while setSessionToken in TokenHelper


I have a Java application running on Linux OS with Jetty server.

Struts action which changes the date and time of Linux System and after it renders another page. An action is performed successfully but while rendering the page it will throw java.lang.IllegalStateException.

I have used s:token tag in that JSP page for preventing double submissions of form.

Error Trace is as below:

ERROR TokenHelper Error creating HttpSession due response is committed to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: null
 java.lang.IllegalStateException
    at org.eclipse.jetty.server.session.AbstractSession.checkValid(AbstractSession.java:109)
    at org.eclipse.jetty.server.session.HashedSession.checkValid(HashedSession.java:73)
    at org.eclipse.jetty.server.session.AbstractSession.getAttribute(AbstractSession.java:132)
    at org.apache.struts2.dispatcher.SessionMap.get(SessionMap.java:161)
    at org.apache.struts2.dispatcher.SessionMap.put(SessionMap.java:179)
    at org.apache.struts2.util.TokenHelper.setSessionToken(TokenHelper.java:93)
    at org.apache.struts2.util.TokenHelper.setToken(TokenHelper.java:79)
    at org.apache.struts2.components.Token.buildToken(Token.java:107)
    at org.apache.struts2.components.Token.evaluateExtraParams(Token.java:97)
    at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:886)
    at org.apache.struts2.components.UIBean.end(UIBean.java:535)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.pages.Continue_jsp._jspx_meth_s_token_0(org.apache.jsp.pages.Continue_jsp:495)
    at org.apache.jsp.pages.Continue_jsp._jspx_meth_s_form_0(org.apache.jsp.pages.Continue_jsp:429)
    at org.apache.jsp.pages.Continue_jsp.access$6(org.apache.jsp.pages.Continue_jsp:407)
    at org.apache.jsp.pages.Continue_jsp$Continue_jspHelper.invoke2(org.apache.jsp.pages.Continue_jsp:1197)
    at org.apache.jsp.pages.Continue_jsp$Continue_jspHelper.invoke(org.apache.jsp.pages.Continue_jsp:1221)

Please let us know possible solution or suggestions to handle the error.


Solution

  • ERROR TokenHelper Error creating HttpSession due response is committed to client. You can use the CreateSessionInterceptor class or create the HttpSession from your action before the result is rendered to the client.

    You can create HttpSession with the code similar to

    ActionContext context = ActionContext.getContext();
    SessionMap<String, T> sessionMap = (SessionMap<String, T>) context.getSession();
    
    if (sessionMap == null) {
        sessionMap = new SessionMap<String, T>(ServletActionContext.getRequest());
        context.setSession((Map<String, Object>) sessionMap);
    }
    

    There's createSession interceptor.

    This interceptor creates the HttpSession if it doesn't exist, also SessionMap is recreated and put in ServletActionContext.

    This is particular useful when using the <@s.token> tag in freemarker templates. The tag do require that a HttpSession is already created since freemarker commits the response to the client immediately.