aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes SendError function not throwing IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "SendError function not throwing IllegalStateException" Watch "SendError function not throwing IllegalStateException" New topic
Author

SendError function not throwing IllegalStateException

Adri Smith
Ranch Hand

Joined: Jun 07, 2004
Posts: 76
I am surprise that this code is working well without throwing any exception:

public class errorServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("Hello world");
out.println(" </BODY>");
out.println("</HTML>");

response.flushBuffer();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
out.flush();
out.close();
}
}

I think this should raise the exception IllegalStateException because the method flushBuffer commits the response.

If we take a look in the API we can see this:

flushbuffer => A call to this method automatically commits the response
sendError => If the response has already been committed, this method throws an IllegalStateException

So why is this not happening ?

I am using Tomcat 5.0. is it Tomcat not compliant or I am misunderstandign something ?

thanks in advance,
ADri
Sivasundaram Umapathy
Ranch Hand

Joined: Aug 10, 2002
Posts: 360
Your servlet on resin produces "java.lang.IllegalStateException: forward() not allowed after buffer has committed." where as it runs well on Tomcat.

So this may be an instance of one more tomcat issue.

Infact, I suggest you to make a note of these violations and report to tomcat mailing lists(Offcourse check the bug database before to ensure that it is not reported already)


Siva
Co-Author - SCMAD Exam Guide - ISBN:9780070077881
Author - Java certification success, Part 4: SCEA
Adri Smith
Ranch Hand

Joined: Jun 07, 2004
Posts: 76
Thx to gave me your opinion about this point.

This help to make me more confindent I am not missing some points.

I will try after taking the exam to contact Tomcat if this is not reported yet.

Adri
Sivasundaram Umapathy
Ranch Hand

Joined: Aug 10, 2002
Posts: 360
All the best with your exam preparation
Adri Smith
Ranch Hand

Joined: Jun 07, 2004
Posts: 76
Thanks a lot

You are helping me very much in making things clear

I wish you all the best

This forum is wonderful !

Adri
 
jQuery in Action, 2nd edition
 
subject: SendError function not throwing IllegalStateException
 
Similar Threads
doubt in Exception occured by sendError()
Servlet getParameter
about sendRedirect
RequestDispatcher.forward()
Confused with flush() behavior