aspose file tools*
The moose likes Servlets and the fly likes IllegalStateException: Cannot forward a response that is already committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException: Cannot forward a response that is already committed" Watch "IllegalStateException: Cannot forward a response that is already committed" New topic
Author

IllegalStateException: Cannot forward a response that is already committed

Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I encountered this exception now and then in a logout servlet. Going through the codes, this is typically what happens:

Is it okay to have:

Before the sendRedirect or forward? Would these be the cause of the exception?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Kevin Mukhar
Ranch Hand

Joined: Nov 28, 2000
Posts: 83
No, those are not the problem. Neither System.out.println or e.printStackTrace write to the output stream, so they will not cause the exception you see. Something else is writing to or committing the response before the call to forward().
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

See this entry in our FAQ.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
there is no problem in calling System.out.println() or e.printStackTrace as they all perform the operation on the Web Server(which hosts the servlet) itself. The IllegalStateException will be thrown if there's any response being committed to the client's browser before forwarding or redirecting.

Saurabh
Karthick Sundaram
Greenhorn

Joined: Feb 04, 2007
Posts: 15

We will not be able to do a redirect if we write something to the response..

Writing something to the response also includes setting headers, cookies, contenttype

Creating a session creates a SessionID cookie to be set in the response.

Did this answer your question?


SCJP 1.4, SCWCD for J2EE 1.4
Karthick Sundaram
Greenhorn

Joined: Feb 04, 2007
Posts: 15

And similarly for a request to be forwarded, nothing should be written to a response..

But request attributes can be added before forwarding.

Hey I got another doubt..

But we can't have a session attribute before forwarding.. right? Because that requires a session.
Karthick Sundaram
Greenhorn

Joined: Feb 04, 2007
Posts: 15

And similarly for a request to be forwarded, nothing should be written to a response..

But request attributes can be added before forwarding.

Hey I got another doubt..

But we can't have a session attribute before forwarding.. right? Because that requires a session.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Karthick Sundaram:
But we can't have a session attribute before forwarding.. right? Because that requires a session.


So? Why would you think that that woud create an issue?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Thank you all for your replies.
Karthick Sundaram
Greenhorn

Joined: Feb 04, 2007
Posts: 15

Because using a session requires setting a JSESSIONID cookie in the response. And we are not supposed to use cookies if we are forwarding..right??
manisha Gupta Garg
Ranch Hand

Joined: Jul 03, 2009
Posts: 41
HI All,


I am facing the same issue...
i have to use two response objects in my servlet abc.class

in abc.class, i am reading a file from server using
out = response.getOutputStream();

and after file is read, i have to redirect the page to another jsp main.jsp using

RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/ebip/redirect/main.jsp");
rd.forward(request,response);

but I can't do this as it is not allowing me to have another response set. It is giving me exception "Cannot forward a response that is already committed".

Is there other method to redirect to main.jsp from abc.class only??

Thanks,
Manisha
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

What I suppose you want to do is the client to download a file as well as see a page. Usually this is implemented using Client-Pull mechanism in which the user sees a page and then the download starts. What you want to achieve can also be done using Server push mechanism (although I've never tried it so not sure). You can do this using a library class like com.oreilly.servlet.MultipartResponse


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
manisha Gupta Garg
Ranch Hand

Joined: Jul 03, 2009
Posts: 41
Hi Ankit,

Thanks for inputs !

but it seems, Microsoft Internet Explorer does not understand this sort of response.


so what can be done...??

Thanks,
Manisha
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

You can always use client pull technique. It is supported by all browsers and it is used on most of the sites...
Jaya Kolluru
Greenhorn

Joined: Oct 29, 2007
Posts: 5
Manisha,
I am facing the same issue. (2 responses...one is commited and another is in forwarded), How did you make it to work? Did you use client pull mechanism, did it work for you, can you please send me the same code!

Regards,
jaya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalStateException: Cannot forward a response that is already committed