Struts 2 Login Form Example

This tutorial is latest tutorial on Struts 2, which shows you how to develop Struts 2 Login Form Example.

Struts 2 Login Form Example

Struts 2 Login Form Example tutorial - Learn how to develop Login form in Struts 2 though this video tutorial

In this video tutorial I will explain you how you can create Login form in Struts 2 and validate the login action. In this example we are not validating the user against database. In future example I will show you how to validate user against database.

You can download the example code given here and then test on your computer. Eclipse Helios IDE is required to run the example. So, first download the code from our website.

Download the code discussed in this tutorial.

Here is the complete video of the process.

Let's start developing the Struts 2 Login Form Example

Step 1: Create a new dynamic project.

Step 2: Add the struts 2 library in the lib directory of the dynamic web application.

Step 3: Add the Struts 2 filter in the web.xml file.

Step 4: Copy the struts.xml file from downloaded source code into the src directory of the web application.

Step 5: Create the index.jsp file in the WebContent folder.

Step 6: Copy login.jsp and loginsuccess.jsp from the download source code into the dynamic web application.

Step 7: Copy Login.java action Java into the project

Step 8: Deploy and test the application on the server from the Eclipse Helios IDE.

Program explanation

Here is the code of action class (Login.java)

package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;

/**
* <p> Validate a user login. </p>
*/
public class Login extends ActionSupport {

public String execute() throws Exception {
System.out.println("Validating login");
if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
addActionError("Invalid user name or password! Please try again!");
return ERROR;
}else{
return SUCCESS;
} }

// ---- Username property ----
/**
* <p>Field to store User username.</p>
* <p/>
*/
private String username = null;

/**
* <p>Provide User username.</p>
*
* @return Returns the User username.
*/
public String getUsername() {
return username;
}
/**
* <p>Store new User username</p>
*
* @param value The username to set.
*/
public void setUsername(String value) {
username = value;
}

// ---- Username property ----
/**
* <p>Field to store User password.</p>
* <p/>
*/
private String password = null;
/**
* <p>Provide User password.</p>
*
* @return Returns the User password.
*/
public String getPassword() {
return password;
}

/**
* <p>Store new User password</p>
*
* @param value The password to set.
*/
public void setPassword(String value) {
password = value;
}

}

Here is the code of struts.xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/example" extends="struts-default">

<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="showLogin">
<result>/pages/login.jsp</result>
</action>

<action name="doLogin" class="net.roseindia.Login">
<result name="input">/pages/login.jsp</result>
<result name="error">/pages/login.jsp</result>
<result>/pages/loginsuccess.jsp</result>
</action>

</package>

<!-- Add packages here -->
</struts>

Here is the code of login.jsp file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Login Application!</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>
<s:form action="doLogin" method="POST">
<tr>
<td colspan="2">
Login
</td>
</tr>
<tr>
<td colspan="2">
<s:actionerror />
<s:fielderror />
</td>
</tr>

<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login" align="center"/>
</s:form>
</body>
</html>