Javatpoint Logo
Javatpoint Logo

Struts 2 Login and Logout Example

Before creating the login and logout application using struts 2, you must clear the concepts of aware interfaces in struts 2. In this example, we have used the SessionAware interface to put the information in the session scope and ServletActionContext class to get the information from the session scope.

This example contains three links login, logout and profile. The end user cannot click on the profile page until he/she is logged in. After getting logged in, he/she may go the profile page. If the end user clicks on the logout page, he will not be able to access the profile page.

Here, we assume that you have a table in oracle database named user3333 that contains records. The table query is:

Example of creating login and logout application using struts 2

In this example, we are need following pages

  1. index.jsp for providing links to the login, logout and profile.
  2. struts.xml for defining the result and action.
  3. for defining login and logout logic.
  4. for matching username and password in the database.
  5. for checking if the user is logged in or not.
  6. View components for the displaying results.

1) Create index.jsp for input

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


2) 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.


3) 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.

4) Create the Dao class to authenticate user

This class simply validates the user from the table stored in the oracle database.

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.

6) Create view components

There are many view components:

  • login.jsp
  • loginsuccess.jsp
  • loginerror.jsp
  • logoutsuccess.jsp
  • profilesuccess.jsp
  • profileerror.jsp

view components for login


This page creates the login form.


This page prints the welcome message with the username.


This page displays the error message.

view components for logout


This page simply displays the successfully logged out message.

view components for profile


This page prints the welcome to profile message.


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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA