Solved

IllegalStateException is raised when we attempt to forward the request.

Posted on 2013-01-25
5
238 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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now