SessionAware Interface Example

Struts 2 SessionAware interface example. Let's see how to store the information in session scope by using struts action class and get this information from another action.

SessionAware Interface Example

SessionAware Interface Example

The SessionAware interface must be implemented by the Action class to store the information in the session scope.

Method of SessionAware interface

It contains only one method setSession.

Public and abstract Method Description
void setSession(Map<String,Object> map) struts framework calls this method by passing the instance of SessionMap class.

SessionMap class

The struts 2 framework passes the instance of org.apache.struts2.dispatcher.SessionMap. It extends the java.util.AbstractMap class which implements the java.util.Map.SessionMap. There are many useful methods of SessionMap class.

Methods of SessionMap class

The commonly used methods of SessionMap class are as follows:

Methods Description
public Object put(Object key, Object value) stores an attribute in the HttpSession object.
public Object remove(Object key) removes the attribute for the specified key and returns the attribute value.
public Object get(Object key) return a value for the corresponding key from the HttpSession object.
public Set entrySet() returns a set object containing all the key and value objects set in the HttpSession object.
public void invalidate() invalidates the current HttpSession object.
public void clear() removes all the attributes set in the HttpSession object.

Example of SessionAware interface

This example contains three links login, logout, and profile. You cannot click on the profile page until you are logged in. After getting logged in, you may go to the profile page. If the end-user clicks on the logout link, he will not be able to access the profile page.

Steps are as follows:

  1. add struts and servlet library
  2. index.jsp for providing links to the login, logout, and profile.
  3. struts.xml for defining the result and action.
  4. Login.java for defining login and logout logic.
  5. Profile.java for checking if the user is logged in or not.
  6. View components for the displaying results.

1) Add struts and servlet library

You need to add struts 2 and a servlet library.


2) Create index.jsp for input

This JSP page creates three links for login, logout, and profile.

index.jsp

  1. <a href="loginform">Login</a>|  
  2. <a href="logout">Logout</a>|  
  3. <a href="profile">Profile</a>  

3) Define action and result in struts.xml

This xml file defines one package and 4 actions. Each action defines at least one result page.

For the login process and logout actions, we are using the same action class but their invocation methods are different.

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5. <constant name="struts.devMode" value="true"></constant>  
  6.   
  7. <package name="abc" extends="struts-default" >  
  8.   
  9. <action name="loginform">  
  10. <result>login.jsp</result>  
  11. </action>  
  12.   
  13. <action name="login" class="com.hpnmaratt.Login" method="execute">  
  14. <result name="success">/login-success.jsp</result>  
  15. <result name="login">/login-error.jsp</result>  
  16. </action>  
  17.   
  18. <action name="logout" class="com.hpnmaratt.Login" method="logout">  
  19. <result name="success">/logout-success.jsp</result>  
  20. </action>  
  21.    
  22.  <action name="profile" class="com.hpnmaratt.Profile" method="execute">  
  23. <result name="success">/profile-success.jsp</result>  
  24. <result name="login">/profile-error.jsp</result>  
  25. </action>  
  26. </package>  
  27. </struts>      

4) Create the action class for login and logout

This action class implements the SessionAware interface and overrides the setSession method to store the information in the session scope.

For logout, we are simply calling the invalidate() method of SessionMap.

Login.java

  1. package com.hpnmaratt;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.apache.struts2.dispatcher.SessionMap;  
  6. import org.apache.struts2.interceptor.SessionAware;  
  7.   
  8. public class Login implements SessionAware{  
  9. private String name,password;  
  10. private SessionMap<String,Object> sessionMap;  
  11.   
  12. //getters and setters  
  13.   
  14. @Override  
  15. public void setSession(Map<String, Object> map) {  
  16.     sessionMap=(SessionMap)map;  
  17. }  
  18.   
  19. public String execute(){  
  20. if(password.equals("admin")){  
  21.     sessionMap.put("login","true");  
  22.     sessionMap.put("name",name);  
  23.       
  24.     return "success";  
  25. }  
  26. else{  
  27.     return "login";  
  28. }  
  29. }
  30. public String logout(){  
  31.     if(sessionMap!=null){  
  32.         sessionMap.invalidate();  
  33.     }  
  34.     return "success";  
  35. }  
  36. }  

5) Create the Profile class

This class gets the information from the session scope, if any information is found in the session scope with login name, it returns success otherwise false.

Profile.java

  1. package com.hpnmaratt;  
  2.   
  3. import javax.servlet.http.HttpSession;  
  4. import org.apache.struts2.ServletActionContext;  
  5.   
  6. public class Profile{  
  7.   
  8.     public String execute(){  
  9.         HttpSession session=ServletActionContext.getRequest().getSession(false);  
  10.         if(session==null || session.getAttribute("login")==null){  
  11.             return "login";  
  12.         }  
  13.         else{  
  14.             return "success";  
  15.         }  
  16.     }  
  17. }  

6) Create view components

There are many view components:

  • login.jsp
  • login-success.jsp
  • login-error.jsp
  • logout-success.jsp
  • profile-success.jsp
  • profile-error.jsp

view components for login

login.jsp

This page creates the login form.

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <s:password name="password" label="Password"></s:password>  
  6. <s:submit value="login"></s:submit>  
  7. </s:form>  

login-success.jsp

This page prints the welcome message with the username.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <hr>  
  3. <%@ taglib uri="/struts-tags" prefix="s" %>  
  4.   
  5. Welcome, <s:property value="#session.name"/>  

login-error.jsp

This page displays the error message.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <hr>  
  3. Sorry, username or password error!  
  4. <jsp:include page="login.jsp"></jsp:include>  

view components for logout

logout-success.jsp

This page simply displays the successfully logged out message.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <hr>  
  3. <p>You are successfully logged out!</p>  

view components for profile

profile-success.jsp

This page prints the welcome to profile message.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <hr>  
  3. <%@ taglib uri="/struts-tags" prefix="s" %>  
  4.   
  5. Welcome to Profile, <s:property value="#session.name"/>  

profile-error.jsp

This page prints the message to login first and includes the login.jsp page.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <hr>  
  3. Sorry, username or password error!  
  4. <jsp:include page="login.jsp"></jsp:include>  

Output

If you click on the profile link, you will be forwarded to profile-error.jsp

Now write your name for name and admin for the password.

Now, you are successfully logged in

Now, click on the profile.