18 minutes

Login Application using Struts2 Tutorial

By Manas @manas, On Sep 17, 2023 Java 2.37K Views

This example explains step by step tutorials for building  Login Application using Struts2 Java Framework. The login workflow includes :

  • By default Login page will populate on running the project.
  • Show error message in case of invalid credentials without losing user enters text.
  • Redirect to welcome page on successful /valid credentials.

1. Software and tool requirements

This example us using JAVA 1.6 for compilation and and deployed on Tomcat 6.x as application server. We use Eclipse IDE for all of our development purpose.

  • JAVA 1.6
  • Tomcat 6
  • Eclipse 3.6

2. Struts 2 framework libraries

Download the latest version of struts 2 lib from with respect to you platform such as Windows or Linux. We are using struts- for this example.

After downloading extract the file to some location e.g. C:\struts-

3. Creating a new dynamic Web Project in eclipse as strutsweb

File -> New -> Dynamic Web Project.

Enter the Project Name as struts2web, select the target run-time as Apache Tomcat v6.0 and click on Finish.

New Dynamic Web Project

Now copy following jars from C:\struts- to \WEB-INF\libs

Struts2 Login Application Libs

4. Modify web.xml configuration to enable struts2 framework

Do the following changes in web.xml file to enable struts 2 framework features. You can locate web.xml file in \WEB-INF\web.xml in your project directory.

id="WebApp_ID" version="2.5">



<filter/>- This is filter dispatcher class act as controller. All requests go through this filter. It looks at the request and determines the appropriate action by looking up the struts configuration file i.e. struts.xml.

<filter-mapping/> – It is used to map the request to filter. All request action map to filter dispatcher.

<welcome-file-list/> – It is used to populate the default page.

5. Writing Java class to validate the login cridentials

Now, let us jump into validating login credenials from java class.  Create inside the following package com.javatechig.struts2web.actions. Note, you may have your own package name

public class LoginAuthAction extends ActionSupport {
    Private static final longserialVersionUID = 1L;
    private String userId;
    private String password;

    private Boolean error;

    public String execute() {
        if (userId.equals("")) {
            addActionError("Please Enter user id.");
            error = true;
        else if(password.equals("")) {
            addActionError("Please Enter password.");
            error = true;
        else if(!userId.equalsIgnoreCase(password)) {
            addActionError("Invalid userid or password.");
            error = true;
        } else {
            error = false;

        if (error) {
            return ERROR;
        } else {
            return SUCCESS;


    public String getUserId() {
        return userId;

    public void setUserId(String userId) {
        this.userId = userId;

    public String getPassword() {

    public void setPassword(String password) {
        this.password = password;

The LoginAuthAction extends ActionSupport to use core methods such as addActionError and static string ERROR, SUCCESS. The execute() is the default method which contains the logic for action class. In this example we are validating userId & password is empty or equals. If empty showing error “Please enter userId/password” else if equals then its validated.

6. Configuring action class in Struts configuration file

Now it’s time to configure the action class in Struts configuration file. Create struts2 configuration file as struts.xml inside project src directory

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"


Package – name – It can be any name but it is good practice to use project name.
Package – namespace –Default it is root(/). It is used for package modularization if the project has more than one module.
Action – It is for action class mapping with action name. I have three action name
login – which is mapped to default struts action dispatcher which populate login.jsp
welcome – which is mapped to default struts action dispatcher which populatewelcome.jsp
loginauth – which is mapped to LoginAuthActionclass which returns ERROR & SUCCESS.
ERROR – mapped to login.jsp to display the login errors with user entered credentials.
SUCCESS – Its redirect to “welcome” action.

You can see at the bottom of class I have created getter and setter method of declared variable. This is used by the interceptor. The interceptor will look for setFieldName while on every action request. The FieldName is the element name in the html form. E.g.
I am using text field in the login.jsp for userid . The name of textfield is userId.

When you click on login it request for loginauth action, which is goes through struts2 dispatcher filter which executes interceptor before/after the invoking of action class.  The interceptor will convert the field name userId to setUserId and then pass the textfield value to setUserId(val).  In the same way when dispatcher deliver the view i.e. jsp it uses getUserId(). E.g. when you enter wrong password and valid user id it throws the same page with error saying invalid password with existing user id values.

7. Creating jsp pages

In this example we will be needing two jsp pages. Once for login form and other is welcome screen. Welcome screen will appear once user credentials are authenticated

7.1. Login  form

<%@pagecontentType="text/html; charset=UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""">
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
.errors {
	border:1px solid #CC0000;
	list-style: none; 
			<tdcolspan="2"width="200"align="left"style="background:#eee">Login Here</td>
			<tdalign="left">User ID:</td><tdalign="left"><s:textfieldname="userId"/></td>

<s:form/> – Struts form element which enhanced with html form. The action is the action class to invoke on click on Login button.

<s:textfield/> – Struts textfiled element which is enhanced to <input type=”text”/>
<s:password/>- Struts textfiled element which is enhanced to <input type=”password”/>

The name attribute of textfield and password values are important. The value should be same as declared variable in the action class.
private String userId;
private String password;

7.2. Creating Welcome jsp page

This page contains the welcome text, and will be displayed, once user credentials are authenticated.

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""">
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Javatechig :: Struts 2 Tutorial</title>

7.3. Create Index.jsp

I am using this page to redirect to login.action. You can see it is configured as welcome-file in web.xml that means it invokes when you run the project with web-context (project-name) name i.e. http://localhost:8080/ and finally you will get login.jsp as default.

Now we are done with the sample and it is ready to run, before that pleas make sure all the created files in proper location as follows:

Struts2 Login Application Project Structure

8. Output

Struts2 Login Application Project Output

Struts2 Login Application Project Output

Struts2 Login Application Project Output


manas avtar


He is a java and web and enterprise application developer in Bangalore area, India. He has expertise in Servlets, JSP, Hibernet, Struts, JavaScript, Groovy, UML and other related tools and technologies.