SessionAware Interface Example

The SessionAware interface must be executed 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) swaggers system calls this strategy by passing the case of SessionMap class.

SessionMap class

The swaggers 2 system passes the occurrence of org.apache.struts2.dispatcher.SessionMap. It expands the java.util.AbstractMap class which executes 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:

MethodsDescription
public Object put(Object key, Object value) stores a trait 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 characteristics 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 swaggers 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. Login.java for characterizing login and logout logic.
  5. Profile.java 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.

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.javacodegeeks.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.javacodegeeks.Login" method="logout">  
  19. <result name="success">/logout-success.jsp</result>  
  20. </action>  
  21.    
  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