SessionAware Interface Example

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

Method of SessionAware interface

It contains just a single strategy setSession.

Public and unique MethodDescription
void setSession(Map<String,Object> map) struts system calls this strategy by passing the case of SessionMap class.

SessionMap class

The struts 2 system passes the occurrence of org.apache.struts2.dispatcher.SessionMap. It expands the java.util.AbstractMap class which implements the java.util.Map.SessionMap. There are numerous valuable techniques for SessionMap class.

Methods of SessionMap class

The generally utilized strategies for SessionMap class are as follows:

public Object put(Object key, Object value) stores a attribute in the HttpSession object.
public Object remove(Object key) expels the characteristic for the predefined key and returns the property value.
public Object get(Object key) return an incentive for the relating key from the HttpSession object.
public Set entrySet() restores a set article containing all the key and esteem objects set in the HttpSession object.
public void invalidate() nullifies the present HttpSession object.
public void clear() expels every one of the attributes set in the HttpSession object.

Example of SessionAware interface

This precedent contains three connections login, logout and profile. You can't tap on the profile page untill you are signed in. Subsequent to getting signed in, you may go to the profile page. On the off chance that the end client taps on the logout interface, he won't almost certainly get to the profile page.

Steps are as follows:

  1. add struts and servlet library
  2. index.jsp for giving connects to the login, logout and profile.
  3. struts.xml for characterizing the outcome and action.
  4. for characterizing login and logout logic.
  5. for checking if the client is signed in or not.
  6. View components for the showing results.

1) Add struts and servlet library

You need to add struts 2 and servlet library.

2) Create index.jsp for input

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

3) Define activity and result in struts.xml

This xml document characterizes one bundle and 4 activities. Each activity characterizes something like one outcome page.

For the loginprocess and logout activities, we are utilizing a similar activity class yet there summon strategies are different.

struts.xml 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 loginprocess and logout actions, we are using the same action class but there invocation methods are different.

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">  
  3. <struts>  
  5. <constant name="struts.devMode" value="true"></constant>  
  7. <package name="abc" extends="struts-default" >  
  9. <action name="loginform">  
  10. <result>login.jsp</result>  
  11. </action>  
  13. <action name="login" class="com.javacodegeeks.Login" method="execute">  
  14. <result name="success">/login-success.jsp</result>  
  15. <result name="login">/login-error.jsp</result>  
  16. </action>  
  18. <action name="logout" class="com.javacodegeeks.Login" method="logout">  
  19. <result name="success">/logout-success.jsp</result>  
  20. </action>  
  22.  <action name="profile" class="com.javacodegeeks.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>      

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition