| Author |
Struts 2 and Session Timeout
|
Abhi Roy
Greenhorn
Joined: Jun 23, 2009
Posts: 22
|
|
Hello,
I want to redirect the user to an error page if session will be timed out. How can I achive this? I have tried the following options.
1. Is there any inbuilt interceptor for checking session timed out , I believe no.
2. I have created one custom Interceptor, which simply checks whether the session is null or not before the execute() method will be executed. If the session is null the custome interceptor will throw exception. It was working fine , but in one jsp, where I'm using request obejct to put some data , found that the request object reference becomes null. But while not using the interceptor, the jsp is wokring fine, though the action class was requestaware.
3. Can I use any listner?
Please help me out.
Thanks
/Abhi
|
 |
Ariram Kalimuthu
Greenhorn
Joined: Jul 17, 2009
Posts: 19
|
|
Answer for Q1 - No (i am not sure)
Answer for Q2:
i also doing like that what you mentioned.
before that, you should configure the interceptor as default-interceptor
please do the following steps:
1). interceptor.
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext context = actionInvocation.getInvocationContext();
Map<String, Object> sessionMap = context.getSession();
//System.out.println(" retrived session "+ sessionMap);
if(sessionMap == null
|| sessionMap.isEmpty()
|| sessionMap.get("SESSION_SERVICE") == null) {
System.out.println(" session expired...");
return "SESSION_EXPIRED";
}
String actionResult = actionInvocation.invoke();
return actionResult;
}
SESSION_SERVICE - one of the attribute we are setting after user login.
if the session expired, session object will be null [if struts creates the session object our attribute won't be there]
2). struts.xml
<interceptors>
<interceptor name="tstSessionInterceptor" class="package.SessionInterceptor"/>
<interceptor-stack name="tstSessionCheckStack">
<interceptor-ref name="tstSessionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="tstSessionCheckStack"/>
</global-results>
<result name="sessionexpired" >sessionexpired.jsp</result>
</global-results>
thats what i have implemented.
it's working fine.
please revert back, if any issues.
thanks
|
ARIRAM K
SCJP 5.0, SCWCD 5.0 & Preparing for SCBCD 5.0
|
 |
Sachin Joshi
Ranch Hand
Joined: Aug 06, 2008
Posts: 83
|
|
I dont think there any interceptor like that. But you can easily write your own.
Checkout this Struts2 Interceptor
_______________________________________________
Try Struts 2
|
 |
 |
|
|
subject: Struts 2 and Session Timeout
|
|
|