aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes 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 "IllegalStateException " Watch "IllegalStateException " New topic
Author

IllegalStateException

Shalini Sher
Greenhorn

Joined: Sep 28, 2004
Posts: 22
For res.sendRedirect(), res.sendError(), and rd.forward, I am seeing inconsistent documentation with regards to if an IllegalStateException will be thrown or not, if the calls have already been made and if we write more to the response.

For instance, the code

public void do Post(req,res)
{
PrintWriter out = res.getWriter(); // line 1
out.print(" hello "); // line 2
res.sendError(res.SC_NOT_FOUND); // line 3
out.print(); // line 4
}

Will out.print() at line 4 throw an IllegalStateException?

What is the behavior the same for forward() and sendRedirect().


The servlet api has the following comments for sendError() - but this does not indicate if an exception will be thrown or not..It just mentions that the response should not be written to.

" If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to. "


I have seen totally inconsistent answers for this question in the different books/practice exams that I have read/taken. Could someone please clarify this.
Jayanthi Mani
Greenhorn

Joined: Dec 08, 2000
Posts: 25
Hello,
My idea will be to just try the example out and see if it throws an IllegalStateException or not.
Jayanthi.
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3211
    
    6
This may not work in real situation, because Writer object probably will have some buffer, and unless this buffer is not flushed, no Exception will be thrown.


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalStateException