• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

response.redirect issue

 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Vt Guru
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Vt Guru
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Yes I always used session(userId) as a int object.

Regards,
Vinoth.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic