wood burning stoves*
The moose likes Servlets and the fly likes response.redirect issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "response.redirect issue" Watch "response.redirect issue" New topic
Author

response.redirect issue

Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Hi All,
I have a session and I set the time limit for the session as 30 second.
When the session time period expired the page should automatically redirect.
For that I used the following code.

But it shows an error
description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:367)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.IllegalStateException
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:433)
org.apache.jsp.CPannel.ViewContact_jsp._jspService(ViewContact_jsp.java:124)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

Java Best Practices
Linux Best Practices
Amortization Calculator

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

The error you got is related to response.sendRedirect() usage.
It would be very great if you post your code.
Thank you.
[ December 18, 2008: Message edited by: Sudipto Shekhar ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The IllegalStateException is almost always associated with trying to change a response headers when the response is already committed.

So in your example you have 2 sendRedirect() calls. The sendRedirect is a header response that tells the client that it should go to a different page. It should be called BEFORE any other response is sent to the client. So if you have any other output to the response before the sendRedirect occurs, then you will get this error.

The sendRedirect will also commit the response (send a response to the client), so no changes can be made to the response AFTER sending the response. This comes into play at least twice in your code:



So if there is an exception that occurs when session.getAttribute gets called and the value is cast to an Integer, then a response.sendRedirect() will be sent to the client, but then the catch statement ends and the method will continue, bringing you to:



Where the comparison may be made and found to be true and a second sendRedirect() could be called, causing the same error.

Finally, what if you do get an exception when getting the user id, but the user is not equal to 0 (which it may not be, it may be null)? Then the rest of the method still runs and tries to send a response to the client, ending in the same error message.

Your code is better off with either if/else to make sure only one execution path is followed:


I would guess that the error is coming because you are using the sendRedirect after already sending some data to the client rather than before. When you sendRedirect before sending data to the client the error usually is hidden in the log files where you aren't likely to see it, though other weird behavior may come up.

So make sure you do the sendRedirect first thing, do nothing before doing the sendRedirect, and in the case when sendRedirect is used, make sure nothing else happens after the sendRedirect.


Steve
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Hi,
Thanks for your reply. I used these code as a first one and I used try and catch because There is an exception occur when the session.getAttribute("userId") is returns null.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by vinoth thirunavukarasu:
Hi,
Thanks for your reply. I used these code as a first one and I used try and catch because There is an exception occur when the session.getAttribute("userId") is returns null.


Right, because you are trying to convert it to an int from an Integer right off the bat. You won't need to do that if you treat the user id as an Integer (the object) the entire time, rather than an int (the primitive). You probably don't need an int value anyway - you generally don't need to do math on user ids.
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Hi,
Yes I always used session(userId) as a int object.

Regards,
Vinoth.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Relying upon an exception for this is insane. Check to see if the scoped variable is null before trying to convert it to an integer.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by vinoth thirunavukarasu:
For that I used the following code.


Your code logic is wrong. The sendRedirect() doesn't immediatelty stop the complete thread and jump out of the method block. The method just continues until it returns or ends. It is a common misunderstanding under greenhorns.

Rearrange your logic to something like:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.redirect issue