Caused by: java.lang.IllegalStateException: Response already committed: unable to send session in cookies | Community
Skip to main content
rrakesh
New Participant
October 16, 2015
Solved

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

  • October 16, 2015
  • 7 replies
  • 21827 views

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) 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sham_HC

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

7 replies

Sham_HC
Sham_HCAccepted solution
New Participant
October 16, 2015

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
smacdonald2008
New Participant
October 16, 2015

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

rrakesh
rrakeshAuthor
New Participant
October 16, 2015

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.

rrakesh
rrakeshAuthor
New Participant
October 16, 2015

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.

rrakesh
rrakeshAuthor
New Participant
October 16, 2015

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
 

rrakesh
rrakeshAuthor
New Participant
October 16, 2015

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?

New Participant
October 16, 2015

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.