File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Illegal State exception response.sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Illegal State exception response.sendRedirect" Watch "Illegal State exception response.sendRedirect" New topic

Illegal State exception response.sendRedirect

Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51

On some of my jsp pages response.sendRedirect throws an illegal State Exception.

Basically i validate user session on each page , when i ge a null value i have to redirect the user to error page , but on some pages it doesnt work.

String logId = (String)session.getAttribute("login");
Vector oData = (Vector)session.getAttribute("Data");
if((logId == null) || (oData == null)){

This code works on some pages and on one or two pages it gives illegal state exception.

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Check whether you have committed the response in those pages before doing sendRedirect(). Because this will result in IllegalStateException.
Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51
On these pages the very first thing it does is check for session and redirect if it doesnt exist. even though jsp forward works in these scenarios. But i dont prefer it .

response object is not commited before this code.

and structure of all pages are same check for session and redirect if not found. It works in most of them .
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13025
Redirect is done using headers so if the response headers are already sent you get illegal state exception. I bet there is some path by which the output is flushed, you just have not found it yet.
I agree. Here's the link:
subject: Illegal State exception response.sendRedirect
jQuery in Action, 3rd edition