Struts User and Session Management
This is a complete guide to build a User and Session Management application using struts2.
As part of this application we will understand the login and logout scenarios. This will also talk about Session management and redirect non logged in user to error or login page.
This tutorial will cover below topics in detail:
- Action class creation and redirection.
- Session management: user login and logout
- JSP test condition to check session
- Using ActionSupport class, validate() method and addFieldError to show errors on page.
As part of this tutorial we will be following below code hierarchy:
Below files will get create or Update as part of this application.
header.jsp → It will contain login menu with home, profile and logout page link. The header.jsp we will be including in home.jsp and profile.jsp
index.jsp → This will be a login page to enter username and password.
home.jsp → On successful login, user will redirect to home.jsp page.
profile.jsp → It is a profile page to show information from session object.
error.jsp → It is for showing error.
Login.java → Login action class is to handle login and logout request.
Profile.java → Profile action class is to handle profile related request.
Follow below steps to create complete application:
We are going to use Eclipse as an editor to develop Struts application.
Follow below steps to create our very first Struts application project.
- Open Eclipse.
- Click on File in top navigation menu and than click on new. There will be an option Dynamic Web Project start coming. As soon as we select select Dynamic Web Project below scree will start get appearing.
Provide below highlighted information color in red and click on Next.
3. Below screen will appear. Click on Next button as no changes are required.
4. In below screen select below highlighted checkbox to generate web.xml file also as part of this project.
Click on Finish will create struts project.
5. Open Struts official website link to download below highlighted dependency.
6. Unzip the download file and copy the libs folder. Paste and replace the same inside below libs folder highlighted red in color.
7. Create Login.java extending ActionSupport class to define field level validations and implementing SessionAware interface to create session.
validate() method from Action support class will help to create field level validations.
withSession(Map<String, Object> map) method from SessionAware interface will help us to set the session.
8. Create Profile.java action class to check if session exist or not. Once request will come to Profile action, it will go to profile page if session exist. It will go to an error page if a session doesn’t exist.
9. Create header.jsp to define header elements as Login, Logout and Profile.
10. Create index.jsp for user to login. It will be having field to enter name and password.
11. Create home.jsp file, this file will get load as soon as user login in the application.
12. Create profile.jsp file to load information related to profile.
13. Create error.jsp file to show error message.
14. Place below content in to struts.xmlweb.xml fine inside WEB-INF folder.
15. As it is a Struts application and require to create a struts.xml file inside src folder as shown below:
16. Right click on the project and select Run As and than Run on Server to start the application.
It will show below screen as soon as application gets started.
Click on Login button without entering name and Password will give below error.
Now, enter name and password as admin, as we are checking same in login() method of Login.java class. Click on login button will take to home page as shown below.
Click on Profile link will take us to the profile page.
Click on Logout link will take us to the Login index.jsp page.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.