This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.redirect issue

 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes I always used session(userId) as a int object.

Regards,
Vinoth.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic