aspose file tools*
The moose likes Servlets and the fly likes How to automatically display SessionExpired.jsp upon session timeout ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to automatically display SessionExpired.jsp upon session timeout ?" Watch "How to automatically display SessionExpired.jsp upon session timeout ?" New topic
Author

How to automatically display SessionExpired.jsp upon session timeout ?

Himanshoo Vijay
Greenhorn

Joined: Feb 14, 2006
Posts: 2
We have a Struts application in which we are setting the session timeout time in Web.xml and listening the session time out using a class implementing 'HttpSessionBindingListener'.

Webshphere Application Server 5.0.x

Upon session timeout 'valueUnbound' method gets called.
Requirement is to direct user to a particular 'SessionExpired.jsp' from this method.

Setting 'response' in 'session' in every Action that is called.

Inside 'vlaueUnbound(HttpSessionBindingEvent event)' I am doing the following:
1) HttpSession session = event.getSession();
2) Retrieve the 'response' from 'session'.
3) Call response.sendRedirect("/view/jsp/common/SessionExpired.jsp");

I am getting an exception on response.sendRedirect...

java.lang.IllegalStateException: Context has not been prepared for next connection
:
:

Any other method that I should call or any other technique that we should use. Objective can be achieved from JSP also but we want ot do this from server side only.
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Himanshu,

Since you are using struts you can handle session expiry in RequestProcessor Class.

Since all the requests are intercepted by processPreprocess() of RequestProcessor Class you can create a CustomRequestProcessor for your application and write the session expiry logic there.

Hope this helps.

Regards,
Vineela
Himanshoo Vijay
Greenhorn

Joined: Feb 14, 2006
Posts: 2
The issue is that I want to generate a response to client upon session expiration, but that resoponse should have proper context as is generated by Websphere container when it makes available a request and response object to Action's execute method.

So should I write a CustomRequestProcessor implementing HttpSessionBindignListener? The valueUnbound gets called automatically upon session expiration, but what to call next ?
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569

Inside 'vlaueUnbound(HttpSessionBindingEvent event)' I am doing the following:
1) HttpSession session = event.getSession();
2) Retrieve the 'response' from 'session'.
3) Call response.sendRedirect("/view/jsp/common/SessionExpired.jsp");


No, I believe you can only call response.sendRedirect() inside a servlet processing a request - not in
a Listener.


Upon session timeout 'valueUnbound' method gets called.
Requirement is to direct user to a particular 'SessionExpired.jsp' from this method.


Do you mean "pushing" some content to the client browser when the session expires. As far as I know, this is not possible with HTTP. You can only display session expired info whenever the client make a new request. You need to check for the existence of match HttpSesion object for every client request. And, as mentioned by Vineela, it is best done inside Struts' ActionServlet (i.e. by using RequestProcessor) since all request are intercepted by it.
Vinny Menon
Ranch Hand

Joined: Jan 10, 2006
Posts: 62

Hi Vineela/Alec/Himanshoo
Agreed to approach of implemneting the CustomRequestProcessor

further to this discussion,please comment if what i am suggesting below is a feasible solution.instead of keeping the response in session,we keep the request in session and use the requestDispatcher object to forward to the error jsp SessionExpired.jsp

Will this work?is this a good approach?
thanks in advance
cheers
vinny m


Regards,Vinny M.
proud Fan of European Champion CHELSEA FC
"If you don't see the bug where you're looking, perhaps you're looking in the wrong place" -James Gosling
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569

instead of keeping the response in session,we keep the request in session and use the requestDispatcher object to forward to the error jsp SessionExpired.jsp

You don't keep the response/request object in the session. They are automatically created (and destroyed) on each request by the container. What you should do in your CustomRequestProcessor might be something like this:

HttpSession session=request.getSession(false);
if (session==null) {
// test the request url to see if this is a constrained resource
// if yes, forward to SessionExpired.jsp/Login.jsp
// if no, create a new session and forward to that page.
}
...
saivenu madhav
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Hi

Could any one supply the code of CustomerRequestProcess..?


I need for the same requirement


ThanksInadvance
 
Don't get me started about those stupid light bulbs.
 
subject: How to automatically display SessionExpired.jsp upon session timeout ?