?
Solved

IllegalStateException is raised when we attempt to forward the request.

Posted on 2013-01-25
5
Medium Priority
?
255 Views
Last Modified: 2013-02-17
I was reading as follows

if the output is not buffered and/or if the
response is already committed by the forwarding resource, then a java.lang.
IllegalStateException is raised when we attempt to forward the request.


please advise.
Any links resources, sample code, ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:tvedtem
ID: 38821534
The key to this error is the meaning of 'committed response'.

Once a response is committed, it is too late to forward it on.

An HTTP response looks a bit like this:

Status: HTTP/1.1 200 OK
Server:	Apache	
Cache-Control:	private, max-age=0, must-revalidate	
Content-Type:	text/html	
Date:	Sat, 26 Jan 2013 06:34:53 GMT	
Expires:	Sat, 26 Jan 2013 06:34:53 GMT	
Access-Control-Allow-Origin:	*	
Content-Length:	141150	

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html...

Open in new window



That it, it's a series of headers followed by the content (HTML) of the page.

If you are buffering the response, you will be able to assemble the first chunk of the response (say, 8k) before it is written.  By 'written', I mean the bytes are starting to be sent to the client (you can imagine a very long HTML page, which you see appear on a browser gradually, a bit at a time).  
At this point we say the response is 'committed'.  It is too late now to send the request somewhere else, we've already started to send the response.
(if you are not buffering the response, it's committed as soon as the first bit of the header has been written!)

If you forward before this happens (before the response has been committed) then this will work fine, any data in the buffer will be abandoned and the request can be forwarded on.

But if you, for example, put some scriptlet halfway down a JSP which forwards the response in certain circumstances, well you're probably too late.  The headers and the first bit of the page have already been sent (and may even be appearing on a users browser).




I remember a very good book from way back called 'Core Servlets'
It is well worth a read even now:
http://pdf.coreservlets.com/
0
 
LVL 7

Author Comment

by:gudii9
ID: 38873658
>>if you are not buffering the response, it's committed as soon as the first bit of the header has been written

can you please elaborate on this. That point is not clear to me.
0
 
LVL 4

Expert Comment

by:tvedtem
ID: 38874328
Sorry, I should've said "first bit following the header"

Unless you have done something deliberately to stop it, by default your response will be buffered. That means that (say) 8k of the response will be assembled in the buffer, before being sent back to the caller (and once it has been sent your respone will be committed and no longer forwardable).  The default size of the buffer depends on your servlet container.

According to the JSP spec, if you are not using a buffer (ie you've set it to 0 size) , your response will be committed whe the first content is encountered.  That means the first byte following the headers. (I think it's actually the first println, which is effectively the first CR if you are using JSPs)

It stands to reason that you cannot do anything with the headers once they've been sent to the user.

You could imagine a bit of code in your container that outputs all the headers and marks the response as committed, once it starts writing the content of the page.
0
 
LVL 7

Author Comment

by:gudii9
ID: 38881164
>>>According to the JSP spec, if you are not using a buffer (ie you've set it to 0 size) , your response will be committed whe the first content is encountered.  That means the first byte following the headers. (I think it's actually the first println, which is effectively the first CR if you are using JSPs)


where do we configure to use or not to use buffer?
Please advise
0
 
LVL 4

Accepted Solution

by:
tvedtem earned 2000 total points
ID: 38882977
Bear in mind that although for the most part they'll be as you expect, the J2EE classes are provided by your application server.

Here are the docs for Tomcat (setBufferSize).
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletResponse.html#setBufferSize%28int%29

Also bear in mind that changing the buffer size is a fairly broad brush.  
If you can think of a way to figure out whether the request should be forwarded *before* you start assembling the response, that's preferable from an architectural point of view... that said, changing the buffer size might work fine for you.

The usual pattern here is to write a servlet controller which makes any forwarding or redirect decisions before you choose JSPs/templates to display.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question