This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "IllegalStateException" Watch "IllegalStateException" New topic


Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
My test.jsp is :
<%@page import="bean.Phone;"%>
<form method="get" action="contextListenerServlet">
Name:<input type="text" name="name" /> <br />
<input type="submit" value="Click" />
ServletOutputStream sos= response.getOutputStream();----------(1)
This jsp throwing :
java.lang.IllegalStateException: getOutputStream() has already been called for this response

What is the problem at (1)?
winay Kumar
Ranch Hand

Joined: Aug 13, 2007
Posts: 34
Hi Anand,

That means response is already committed.

Winay Kumar<br />SCJP 5.0
Kunal Jag
Ranch Hand

Joined: Jun 08, 2008
Posts: 31
Propagation of IllegalStateException is a common point of confusion among many exam aspirants. Following are some scenarios which can land you in trouble:

You can call an include action anytime, but the forward has to be called before the response is committed. Otherwise, an IllegalStateException exception will be thrown.

Session Tracking
All methods for storing, retrieving and removing attributes are called on an active session. If called on a session which has been invalidated, an IllegalStateException will be thrown.

Programmatically Setting Error Codes
If the response has already been committed, the sendError() method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Retrieving Request Data
You can read the request body as character data using ServletRequest.getReader() method which retrieves the body of the request as character data using a BufferedReader. Care should be taken that you can�t read request parameters using any of the getParameter() methods described in this section and then attempt to read the request with the getInputStream() or getReader() method or vice versa. Any such attempt will throw an IllegalStateException.

I hope this will help.


Kunal Jaggi<br />Author of <a href="" target="_blank" rel="nofollow">"SCWCD Exam Guide"</a>, SCWCD 5 (exam 310-083 and 310-084), <a href="" target="_blank" rel="nofollow">McGraw-Hill</a><br /><a href="" target="_blank" rel="nofollow"></a><br /><a href="" target="_blank" rel="nofollow"></a>
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
I think the problem is because of the implicit JspWriter object available to a JSP Page. The implicit object "out" exists, I am not sure you can get an outputstream under this circumstance.

PS: The autoFlush attribute of the page directive is set to "true" by default. Flush is called on the implicit object "out" automatically.
[ July 06, 2008: Message edited by: Fola Fadairo ]
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
HI Fola,
I just change page directive as: <%@page autoFlush="false" %>,making autoFlush="true" ,but it still throwing the same exception.
mimi chatterjee

Joined: Dec 03, 2007
Posts: 26
@ Kunal Jag,
Your content is very helpful.Thanks for sharing it with us.
V Sahdev

Joined: Jun 18, 2008
Posts: 14
You already have impilcit out object, courtsey JspWriter.
Use out object in your scriplets, although that would be making an explicit call. If you just , this will add to the output buffer,whatever value you are trying to print.
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
Hi Anand,
To the best of my knowledge, "response" is not an implicit object available to a JSP. I think the "response" available to you is from a service method of one of the classes or interfaces along the JSP hierarchy HttpJspPage. Like someone suggested if you need a writer, you had best stick with the implicit object "out". I hope this is helpful.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
To correct you here response is one out of nine JSp implicit oject and of type HttpServletResponse.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

I think we already have "out" ,implicitly available to JSP. This means we already have one stream to send out the response, so we can not have one more stream.

Please correct me if I am wrong.

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Remember, you can only invoke either response.getOutputStream() or response.getWriter(); trying to do both will cause an IllegalStateException to be thrown on the later invocation. Since the JSP uses a Writer for its page by default, you can't invoke response.getOutputStream() in the same request when using JSPs. Though since JSP content is textual, you wouldn't want to anyway, right?

You can call the same method multiple times to get a copy of the same Writer - so if you change your line to Writer w = response.getWriter(), it should work okay. Note that JSP pages use a JspWriter wrapper which buffers the content from the page, so writing directly to response.getWriter() will bypass the buffer and may write the output in an unexpected order. You should therefore always use the 'out' implicit object in JSPs.
[ July 10, 2008: Message edited by: Charles Lyons ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Thanks for your valuable comments,specailly Charles.
I agree. Here's the link:
subject: IllegalStateException
It's not a secret anymore!