Expand my Community achievements bar.

SOLVED

Caused by: java.lang.IllegalStateException: Response already committed: unable to send session in cookies

Avatar

Level 1

Hi

I am trying to store a object in session in SlingAllMethodsServlet and retrieve it back in CQ JSP. When doing so i get the error as below. Attached the full stacktrace.

This error appears random. Couldn't find the actual scenario when it occurs to.

Caused by: java.lang.IllegalStateException: Response already committed: unable to send session in cookies

    at com.day.j2ee.servletengine.ServletHandlerImpl.getSession(ServletHandlerImpl.java:1072)
    at com.day.j2ee.servletengine.RequestImpl.getSession(RequestImpl.java:525)
    at com.day.j2ee.servletengine.RequestImpl.getSession(RequestImpl.java:532)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:235)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:235) 

1 Accepted Solution

Avatar

Correct answer by
Level 10

The message "Response already committed" this mean the http response object already got "flush" so that part of the response is already transmitted to the client and thus you cannot set anymore cookies as this is part of the header in the http protocol.

You have to check in your template/components if there are

  • any previous code that write in the output stream (sometime simple cariage returns in a jsp is enough to produce this effect).
  • if already you see any code that output in the stream and may get flushed (usually if the buffer reach a certain size it will automatically flush the response).
  • if your jsp is using session by default.  Can you try including the global.jsp, which already has the directive <%@ page session="false" %> or may be just including  <%@ page session="false" %> on the jsp page

View solution in original post

7 Replies

Avatar

Correct answer by
Level 10

The message "Response already committed" this mean the http response object already got "flush" so that part of the response is already transmitted to the client and thus you cannot set anymore cookies as this is part of the header in the http protocol.

You have to check in your template/components if there are

  • any previous code that write in the output stream (sometime simple cariage returns in a jsp is enough to produce this effect).
  • if already you see any code that output in the stream and may get flushed (usually if the buffer reach a certain size it will automatically flush the response).
  • if your jsp is using session by default.  Can you try including the global.jsp, which already has the directive <%@ page session="false" %> or may be just including  <%@ page session="false" %> on the jsp page

Avatar

Level 10

Can you please post your code so we can better understand what you are trying to do. 

Avatar

Level 1

smacdonald2008 wrote...

Can you please post your code so we can better understand what you are trying to do. 

 

Need help on this. Still the problem exists.

Avatar

Level 1

In Servlet

@SlingServlet(resourceTypes = "/apps/geometrixx/full/download", methods = {"POST" })
public class DownloadRequestServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

    //Some Logic to get the List Object.  

    String nextPage="/content/geometrixx/samplejsp.html";

    request.getSession().setAttribute("list", list);

    response.sendRedirect(nextPage);

}

}

 

JSP code

i try to the list object which was set in session.

List list = (List)request.getSession().getAttribute("list");

I get error in the above line.

Avatar

Level 1

I am able to retrieve the session object in CQ5.6 and 5.5

If i had the below code in CQ JSP:

<%  out.println(request.getSession());    %>

The output is:

 accessTime: Mon Oct 28 17:36:44 IST 2013 creationTime: Mon Oct 28 17:36:44 IST 2013 isNew: true lifeTime: 600 seconds sessionId: c8367fed-1a56-4984-9804-13cc580f8e24 

 

But in CQ5.6.1 i m getting the exception, attached the error log for reference
 

Avatar

Level 1

I m getting the below exception if i access the session object in CQ JSP

Caused by: java.lang.IllegalStateException: Response already committed: unable to send session in cookies

 

Code:

request.getSession(); results in the above error. Cant i access session object in CQ Jsps?

Avatar

Level 1

hi rrakesh,

i have the same problem in one of our components. i tried to reproduce it with a simple jsp including the following code:

<h2>Session info</h2> <p><%out.println(request.getSession());%></p> <h2>Read attribute from session</h2> <p>Attribute value: <%=request.getSession().getAttribute("foo")%></p> <h2>Write attribute to session</h2> <%request.getSession().setAttribute("foo", new Random().nextInt());%>

This seems to work on 5.5, 5.6 AND 5.6.1 for me. I'm not sure if i missed something here. On the same systems, I get the IllegalStateException for other components using similar code.