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. Login.java for defining login and logout logic.
  4. LoginDao.java for matching username and password in the database.
  5. Profile.java 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.

index.jsp

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.

struts.xml

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.

Login.java

4) Create the Dao class to authenticate user

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

LoginDao.java

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

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

login.jsp

This page creates the login form.

loginsuccess.jsp

This page prints the welcome message with the username.

loginerror.jsp

This page displays the error message.


view components for logout

logoutsuccess.jsp

This page simply displays the successfully logged out message.


view components for profile

profilesuccess.jsp

This page prints the welcome to profile message.

profileerror.jsp

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA