This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused on IllegalStateException

 
indeevar goduguchinta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

Can any one explain the cases when an IllegaStateException is thrown by sendError and forward methods?

Thanks
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have already sent the response to the client by calling out.flush() and after that trying to use sendError or forward, it will give IllegalStateException

This is a code for sending the jar file to client

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
response.setContentType(�application/jar�);
ServletContext ctx=getServletContext();
InputStream is=ctx.getResourceAsStream(�bookCode.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();
}
Here os.flush() cause the response to be sent to the client and that point this response is done.Now if you try view.forward(request,response) , it will give IllegalStateException
same is with sendError()
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seethis post as well
 
indeevar goduguchinta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gaurav and Ed. I got the first scenario if the response has already been flushed.
Coming to the second case i.e. assuming the response is still in buffer and not been flushed if I try to write something to the response after these method calls then also an IllegalStateException will be thrown right?? Pls confirm.
 
Salvador Cecilio
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very confusing.

I am unable to get the exception after explicitly flushing the PrintWriter object. I have been able to get the error if I set the content type to a binary type and use an OutputStream instead of PrintWriter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic