aspose file tools*
The moose likes Struts and the fly likes Struts 2 and Session Timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 and Session Timeout" Watch "Struts 2 and Session Timeout" New topic
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
Sushant Gandhi
Greenhorn

Joined: Jun 24, 2013
Posts: 16
Ariram Kalimuthu wrote: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





This code is not running properly...actually i want the show the message after session time out....please help me out of this...........My LoginAction page coding is:---

package com;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


public class Login extends ActionSupport implements SessionAware{

private String username,userpass;
SessionMap<String,String> sessionmap;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getUserpass() {
return userpass;
}

public void setUserpass(String userpass) {
this.userpass = userpass;
}

public String execute(){

if(LoginDao.validate(username, userpass)){
return "success";
}
else{
return "error";
}
}

public void setSession(Map map) {
sessionmap=(SessionMap)map;
sessionmap.put("login","true");
}

public String logout(){
sessionmap.invalidate();
return "success";
}

}


.......
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8710
    
    6

Abhi Roy wrote: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.


The easiest thing to do is to use the security API that is already built into JEE servers. start reading here.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
Don't get me started about those stupid light bulbs.
 
subject: Struts 2 and Session Timeout
 
Similar Threads
Struts2 : Forwarding to the requested page after login
Session time out.
Help need to exclude list of bean classes from interceptors
Session checking with Struts 2
struts2 login interceptor not finding session attribute of user details.