• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Confused on IllegalStateException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI

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

Thanks
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seethis post as well
 
indeevar goduguchinta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic