Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IllegalStateException is raised when we attempt to forward the request.

Posted on 2013-01-25
5
Medium Priority
?
250 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 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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

670 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