Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

IllegalStateException

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
My idea will be to just try the example out and see if it throws an IllegalStateException or not.
Jayanthi.
 
Bartender
Posts: 3647
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic