• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why am I getting IllegalStateException

 
A. Aka
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I do not understand why I am getting an exception , for me I should not because because there is no out.flush() or response.flushBuffer();
see also HFSJ, on page 850(question 45), in mock exam answers, they said this should be working


this is a code from the servlet with GET







strangely enough, this works, why ?




regards,


 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2453
97
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strangely enough, this works, why ?

A PrintWriter buffers the output, so that means that your response is not automatically committed.

Regards,
Frits
 
A. Aka
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1/ok, in HFJS they are wrong (see page 850 , question 45) ?
they said, this should work because there is no flush




2/what is the purpose of having flush() in ServletOutputStream (or OutputStream) if it is automatically fushed then ?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2453
97
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1/ok, in HFJS they are wrong (see page 850 , question 45) ?

Yes, see HFSJ errata

2/what is the purpose of having flush() in ServletOutputStream (or OutputStream) if it is automatically fushed then ?

As it is an OutputStream it inherits the flush() method, but I don't think you will ever use it in web-apps. It is good to know the mechanism behind it.

Regards,
Frits
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frits,

I don't find any details if java doc that PrintWriter buffers the output. The java doc for PrintWriter obtained from ServletResponse is as following, which clearly says that we need to call the flush() method explicitly to commit the response::


So I am also confused with above behavior.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2453
97
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay,

Good point!

By default a PrintWriter is created without automatic flushing (see constructor in API), but the same can be said about a ServletOutputStream, so the problem is caused by something else. If we read the exception message well it says: "getOutputStream() has already been called for this response"

I tested it and it looks like the problem appears in the initialization of the forwarded JSP. It seems that the initialization of the JSP does a getOutputStream() and the container detects that this has already been done.

Bottom line: don't do anything with the response if you are going to do a forward. As the specs don't say what should happen: it might work but it also might not work...

Regards,
Frits
 
Aditya Narayan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay ,
As Frits Walraven correctly told , when you are forwarding the request using request dispatcher , donot play with the response. The purpose of request dispatcher is that if the current servlet or jsp cannot handle the task on its own, delegate it to another jsp or even servlet using request dispatcher . Hence let the target jsp or servlet create the required response. Yeah there are absurdities produced when you try to work otherwise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic