File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes call to sendRedirect() and getSession()  after forward()  throws IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "call to sendRedirect() and getSession()  after forward()  throws IllegalStateException" Watch "call to sendRedirect() and getSession()  after forward()  throws IllegalStateException" New topic
Author

call to sendRedirect() and getSession() after forward() throws IllegalStateException

chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
Hi,
I am using a url rewrite filter from tuckey.org/urlrewrite; which using a configuration forwards request to a servlet depending on the pattern of requested URL. Lets say this servlet is AServlet.
Can this AServlet use sendRedirect() call ?

What I understand is that once a forward is executed, response is committed and sendRedirect will throw IllegalStateException?

Currently I am getting lot of IllegalStateException for getSession() calls and sendRedirect() calls in the AServlet.
I need to get rid of these exceptions.

Thanks

ck

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

You should either forward or redirect, why trying both? I think you are aware of the difference between them?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
You should either forward or redirect, why trying both?


The Urlrewrite library which is being used, does a forward internally. All requests are handled by urlrewrite filter at the first level. So I cannot avoid that.

My query is that just by calling forward on a request - the response gets committed ? even if nothing was written explicitly to the response.


To detail out how things are working at my end :

Lets say user accesses a short url http://test.myurl.com/section/News.

The request is handled by url-rewrite filter which forwards it to a resource(servlet) identified by mappings present with url-rewrite filter.
This resource , depending on some logic may call sendredirect.
Will there be an issue on this call?

Thanks

Chandrakant


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

chandrakant karale wrote:This resource , depending on some logic may call sendredirect. Will there be an issue on this call?

As long as you have not commited anything from that resource before the redirect, No problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call to sendRedirect() and getSession() after forward() throws IllegalStateException
 
Similar Threads
IllegalStateException
Regarding sendRedirect
2) getContext - absolute path
output of servlets with include() or forward()
Forwarding request to same servlet