aspose file tools*
The moose likes Servlets and the fly likes IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException" Watch "IllegalStateException" New topic
Author

IllegalStateException

al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Hi,
I purposely called RequestDispatcher.forward() after I already committed a response. I believe that doing so should throw an IllegalStateException.
I think that the following code should be throwing an exception, but it actually lets me download the jar file BUT doesn't bring me to result.jsp.

The RequestDispatcher method understandably doesn't work in the code below, but my question is, shouldn't I get some sort of error statement?
I even tried configuring the DD using the <error-page> tag but that doesn't seem to work either.
I'm using tomcat 6.

Thanks for taking the time to read my post.

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I agree with you, if what you're saying is true (I'm not doubting your eyes, I just like to see these things myself)

The RequestDispatcher should fail, it should throw the exception as you say. I have TC6 available, if I get a chance I might need to try this.
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
I think, if you put "os.close()" before the point where you invoke the RequestDispatcher, you might get the desired behaviour.
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Thanks for your replies! I tried moving the os.close() before the call to RequestDispatcher and I still get the same behavior, namely, I get a file download dialog which allows me to download and save the file and the RequestDispatcher doesn't work, but I don't get an error message either.

I'll keep on experimenting, to see if I am doing something wrong, if I figure something out, I'll post it.

Thanks for your help.
[ April 14, 2008: Message edited by: al langley ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

Originally posted by Anirvan Majumdar:
I think, if you put "os.close()" before the point where you invoke the RequestDispatcher, you might get the desired behaviour.

No. You should never close a stream that you did not open.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Kaufman
Greenhorn

Joined: Mar 17, 2008
Posts: 8
One thing that occurs to me is that the code is calling "flush" (or "close") on the underlying output stream itself, rather than on the response.

I don't think this is necessarily going to be recognized by the response as having "committed" it. Depending on how the implementation works internally, there's no guarantee that the response will keep track of everything you do directly to its internal elements. It might only know that the output has been flushed when it has done it itself.

I'd expect you to get the expected exception if the call is changed from "os.flush()" to "response.flushBuffer()".

You could also try using some calls to "response.isCommitted" to see whether/where the response actually becomes committed.

Mike
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Thanks again for your suggestions.
It seems that the response gets committed after a call to response.flushBuffer(). But still not getting an error thrown.
Once again, thanks again for all who took the time to read the post. I'm going to keep on experimenting, if I figure something out, I'll post it.


[ April 14, 2008: Message edited by: al langley ]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Originally posted by Bear Bibeault:

No. You should never close a stream that you did not open.


I thought "OutputStream os = response.getOutputStream();" opened the stream. I just advised him to move os.close() before getting the RequestDispatcher object.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

No. The Javadoc says nothing like that.
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Originally posted by Bear Bibeault:
No. The Javadoc says nothing like that.


Oh well, the Javadoc doesn't say anything of the sorts about instantiating the RequestDispathcer either.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

It doesn't say not to stick bamboo shoots in your left eye either. Are you going to try that?

My point is, you shouldn't close streams that you don't open unless the documentation for the returning method explicitly requires it.
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Originally posted by Bear Bibeault:
It doesn't say not to stick bamboo shoots in your left eye either. Are you going to try that?

My point is, you shouldn't close streams that you don't open unless the documentation for the returning method explicitly requires it.


Jeez. Why don't you back off a bit. For all the shenanigans of being a "Sheriff" out here, you surely need to exhibit a little more patience and courtesy. I wouldn't say this is the best manner in which one could prove his/her point on JavaRanch!

As for my suggestion to Al, he only wanted to verify a not so common behaviour. He was expecting to get an exception and he wasn't. I simply *suggested* something he could try. It seemed to me that closing the stream before instantiating the RequestDispatcher *might* work. If that was wrong you could very well just point out the same and also state a reason which can be verified! I think it's in the best spirit of this forum if people don't get feisty about their responses. In my honest opinion, knowledge sharing doesn't really floursih that way
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
I do appreciate all the responses, and just from this one post, I think I learned a lot.

I found a possible explanation (while reading HFSJ) of why I am not getting the exception message even though an exception occurs. To paraphrase, once the outputstream finishes writing and by the time the Container comes across the forward() method, its already too late for the Container to send anything else--including the IllegalStateException. The forward() won't do anything, but you won't see an error message either.
So MAYBE, because of the nature of the exception, users won't see an IllegalStateExceptions message most of the time.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you checked the server's logs to see if an IllegalStateException is occuring?


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

Joined: Mar 28, 2008
Posts: 35
I checked in Tomcat6/logs/stderr_ and there is no error message.
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
i am responding little lately

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestRequestDispatcher extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException
{
try
{ response.setContentType("application/jar");ServletContext ctx = getServletContext();InputStream is = ctx.getResourceAsStream("/test.jar");int read=0;byte[] bytes = new byte[1024];OutputStream os = response.getOutputStream();
while((read = is.read(bytes))!=-1)
{
os.write(bytes,0,read);
}os.flush();
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
os.close();
}catch(Exception e){
System.out.println(e);
}
}
}

In stdout log file you find
java.lang.IllegalStateException: Cannot forward after response has been committed

Sravanthi
[ May 04, 2008: Message edited by: Vijaya Sravanthi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalStateException
 
Similar Threads
Confused on IllegalStateException
Illegal State exception example
does the write() method of the OutputStream comit the respose before this call to forward()?
[HFSJ 2nd Edition] chapter 5 Errata
RequestDispatcher -> forward -> IllegalStateException - inconsistency