Login form using struts

Here we develop a login form using struts framework.
 Our requirements are:
  • Develop a login form with two fields User Name and Password.
  • User Name and Password are mandatory.(validation purpose)
  • User Name and Password at least 5 digits. (validation purpose)
  • If User Name and Password are match then go to home page with welcome message otherwise give error message Invalid User Name or Password. 
So our requirements are very clear what we have to develop.Since we are developing in struts so we need some jar files and tld files:

Jar Files:
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar
  • commons-fileupload.jar (your choice for uploading file)
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar
  • struts-legacy.jar
Tld files:
  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-nested.tld
  • struts-template.tld
  • struts-tiles.tld
All these jar and tld files are free available so you can download and copy to all these jar files in your lib and tld in WEB-INF.

Here project name is StrutsApp Now we need following files:
  1. LoginForm.java
  2. LoginAction.java
  3. Application.properties
  4. login.jsp
  5. home.jsp
  6. web.xml
  7. struts-config.xml
So our project structure is like this

Now we write the code of each file as 


package com.javaforecast.struts1;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
 return username;
public void setUsername(String username) {
 this.username = username;
public String getPassword() {
 return password;
public void setPassword(String password) {
 this.password = password;
public ActionErrors validate(ActionMapping am,HttpServletRequest req){
 ActionErrors errors=new ActionErrors();
  errors.add("username", new ActionError("errors.login.un.required"));
 }else if(username.length()<5||username.length()>10){
  errors.add("username", new ActionError("errors.login.un.length"));
  errors.add("password", new ActionError("errors.login.pw.required"));
 }else if(password.length()<5||password.length()>10){
  errors.add("password", new ActionError("errors.login.pw.length"));
 return errors;


package com.javaforecast.struts1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

public class LoginAction extends Action {
public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception{
String result="login";
LoginForm lf=(LoginForm)af;
String un=lf.getUsername();
String pw=lf.getPassword();
req.setAttribute("UN", un);
req.setAttribute("EMSG", "Invalid Username or Password");
return am.findForward(result);


errors.login.un.required=<font color=red size=3>Username is required</font>
errors.login.un.length=<font color=red size=3>Min length is 5-digit</font>
errors.login.pw.required=<font color=red size=3>Password is required</font>
errors.login.pw.length=<font color=red size=3>Min length is 5-digit</font>


<%@taglib uri="WEB-INF/struts-html.tld" prefix="html"%>
<body background=green >
<h1>Java Forecast</h1>
<h2><font color=red size=5>LoginForm</font></h2>
<font color=red size=5>${EMSG }</font>
<html:form action="/loginSubmit">

<td><html:text  property="username"/><html:errors property="username"/></td>


<td><html:password  property="password"/><html:errors property="password"/></td>


<td> <html:submit value ="login"/></td>



<h1>Java Forecast</h1>
<h1>Mr.${UN }! Welcome to JFC Home Page</h1>


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Java Forecast</display-name>
<!-- Here the param value's content(struts-config.xml) must be your struts -config file(any name xx-config.xml)-->


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

<form-bean name="loginForm" type="com.javaforecast.struts1.LoginForm" />
<action path="/loginSubmit" 
  type="com.javaforecast.struts1.LoginAction" scope="request" 
<forward name="login" path="/login.jsp" />
<forward name="home" path="/home.jsp" />
<message-resources parameter="com.javaforecast.struts1.Application" />

After successfully follow these above steps and run the project then you will get as below mentioned output:

Login without submitting user name and password.

Login with user name and password whose length is less than 5-digits.

Login with invalid user name and password

Now finally user name and password same we get final requirement as

Thanks, I hope you enjoyed with this tutorial.


Post a Comment