Solved

IllegalStateException is raised when we attempt to forward the request.

Posted on 2013-01-25
5
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 74
servlet and mdb, jms error 1 57
Select statement in @Query (JPA Repository) - SpringMVC 2 36
Java class and jar 3 41
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

756 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