wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpServletResponse X IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpServletResponse X IllegalStateException" Watch "HttpServletResponse X IllegalStateException" New topic
Author

HttpServletResponse X IllegalStateException

Rodrigo W Bonatto
Ranch Hand

Joined: Aug 20, 2004
Posts: 62
Hi,

I�m confused about when the response is commited and when the IllegalStateException is throw.
The Spec says: "Calling flush() on the PrintWriter commits the response.", but the container doesn't throw any exception in the following code:

package beanPackage;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("Servlet doGet Method put this content here!");
out.println("<br>before flush: "+resp.isCommitted());
out.flush();
resp.resetBuffer();
out.println("<br>after flush: "+resp.isCommitted());
}
}

I have the following result:

Servlet doGet Method put this content here!
before flush: false

The first isCommited() method returns correct boolean value(false), but if response is in commited state, the container shouldn't have to throw an IllegalStateException when I invoke response.resetBuffer() after flush() method?

Thanks in advanced.

Rodrigo
[ February 14, 2005: Message edited by: Rodrigo W Bonatto ]

SCJA, SCJP, SCJD, SCWCD, SCBCD5, SCDJWS, SCEA5
Deb Sadhukhan
Ranch Hand

Joined: Nov 05, 2003
Posts: 67
Method description says
Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.

So it will give you IllegalStateException.


_ __ _ __ _ __ _<br />SCJP 1.4 (95%) | SCWCD 1.4 (79%)<br />Artificial intelligence is no match for natural stupidity.
Rodrigo W Bonatto
Ranch Hand

Joined: Aug 20, 2004
Posts: 62
Hi Deb,

I agree with you, it should throw an exception.

But, I�m doing the test in Tomcat 5.0.28 and it doesn't throw the IllegalStateException.

What am I doing wrong?


Rodrigo

[ February 14, 2005: Message edited by: Rodrigo W Bonatto ]
[ February 14, 2005: Message edited by: Rodrigo W Bonatto ]
Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
Rodrigo, did you check tomcat logs to see if the container didn�t throw an java.lang.IllegalStateException?

The browser will show:

Servlet doGet Method put this content here!
before flush: false

and that�s it, everything that you try do write o the response object will throw an IllegalStateException. The browser doesn�t show the exception, because the exception occurred after the response was committed.

Marcos
Rodrigo W Bonatto
Ranch Hand

Joined: Aug 20, 2004
Posts: 62
Hello Marcos,

Thanks for the reply.

I've looked at Tomcat's logs and the exception was there!

Thanks for your explanation!

Best regards,

Rodrigo
[ February 23, 2005: Message edited by: Rodrigo W Bonatto ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpServletResponse X IllegalStateException
 
Similar Threads
RequestDispatcher and flushBuffer
Regarding File Download!!
doubt in Exception occured by sendError()
flush() before RequestDispatcher
Servlet Invocation