aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes why am I getting IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "why am I getting IllegalStateException" Watch "why am I getting IllegalStateException" New topic
Author

why am I getting IllegalStateException

A. Aka
Ranch Hand

Joined: Sep 08, 2012
Posts: 44
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
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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

Joined: Sep 08, 2012
Posts: 44
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
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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

Joined: Mar 02, 2006
Posts: 37

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.


Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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

Joined: Sep 17, 2012
Posts: 17
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why am I getting IllegalStateException
 
Similar Threads
RequestDispatcher showing error??
clarification regarding RequestDispatcher usage
problem with the request dispatcher
Tomcat 7 not loading classes/jars in Web-Inf
Opening a zip file from JSP page