• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2 and Session Timeout

 
Abhi Roy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic