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.
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.
Joined: Feb 16, 2004
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.
Joined: May 30, 2007
@Fola, To correct you here response is one out of nine JSp implicit oject and of type HttpServletResponse.
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 / AmazonAmazon UK )
Joined: May 30, 2007
Thanks for your valuable comments,specailly Charles.