This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException

 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My test.jsp is :
<%@page import="bean.Phone;"%>
<html>
<body>
<form method="get" action="contextListenerServlet">
Name:<input type="text" name="name" /> <br />
<input type="submit" value="Click" />
<%
ServletOutputStream sos= response.getOutputStream();----------(1)
%>
</form>
</body>
</html>
This jsp throwing :
java.lang.IllegalStateException: getOutputStream() has already been called for this response

What is the problem at (1)?
 
winay Kumar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anand,

That means response is already committed.
 
Kunal Jag
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Propagation of IllegalStateException is a common point of confusion among many exam aspirants. Following are some scenarios which can land you in trouble:

RequestDispatcher
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.


Regards,
 
Fola Fadairo
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Fola,
I just change page directive as: <%@page autoFlush="false" %>,making autoFlush="true" ,but it still throwing the same exception.
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Kunal Jag,
Your content is very helpful.Thanks for sharing it with us.
 
V Sahdev
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Fola,
To correct you here response is one out of nine JSp implicit oject and of type HttpServletResponse.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable comments,specailly Charles.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic