• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServletResponse X IllegalStateException

 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Deb Sadhukhan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic