I am newbie to struts2 , I am developing a application using struts 2 how can i implement log out functionality in the application and after session time out redirect to login page.
thanks in advance.
I am newbie to struts2 , I am developing a application using struts 2 how can i implement log out functionality in the application and after session time out redirect to login page.
thanks in advance.
In Struts2 we have something called SessionAware interface which allows you to keep data in session and retrieve when you in need...
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
public class Login extends ActionSupport implements SessionAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private SessionMap<String,Object> sessionmap;
public String execute() {
System.out.println("Inside Login Action");
//Now Keeping required data in sessionMap
//Retrieve User Object from Database
sessionmap.put("Current User",User);
//Note User is a object or record retrieved from DB
return SUCCESS;
}
@Override
public void setSession(Map<String, Object> map) {
System.out.println("INSIDE SESSION Login");
sessionmap=(SessionMap)map;
}
}
Now In Logout Class
sessionmap.invalidate();
//Removes the user object from session :)
To check whether user have logged in or session expires....
User user = (User)sessionmap.get("Current User");
if user is null session is expired or user haven't logged in...:)
web.xml
<session-config>
<session-timeout>20</session-timeout>
</session-config>
Log Out Functionality- Create an action named LogoutAction and on the JSP logout button call this action. clear the session object in your method and redirect to the login page.
for session time out, I think you can specify this in your web.xml. More details here
This is the working code for me, Try this one
Added this code in header section of jsp page
<meta http-equiv="refresh" content="<%=(session.getMaxInactiveInterval())-30%>; url=Login.jsp" />