Login Form Example With Struts

This post will show you how a form process in struts 1 framework. We will create a login form and then using struts 1 we will verify the authentication of the user. This example will take following steps :

  1. First of all we will create a form bean (LoginForm.java) that will hold the form values provided by the user.
  2. Create a jsp page (Login.jsp) which will contain the form to be displayed to the user.
  3. Create a success page (Success.jsp) and failure page (Failure.jsp) for providing feedback to the user on their form submission.
  4. Create a controller helper class (LoginAction.java) that will check for the user input and decide which view to be respond to the users (Success.jsp or Failure.jsp).
  5. And finally we will configure our form bean and action classes in struts-config.xml.


Following is the code in LoginForm.java file :

package com.raistudies.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {

    private static final long serialVersionUID = -3491637470205228033L;

    private String username = null;
    private String password = null;

    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 void reset(ActionMapping mapping, HttpServletRequest request) {
        this.password = null;

There are two fields in this form bean “username” and “password”, that will hold the value of two fields in the login form. One new goods here is the method “reset” which is overwritten by our form bean. “reset” method is called at the end of the every request processed by the struts. In reset method we have set the value of password as null which means every time the user will open the login jsp in browser it will show the last username but will not show the value of password.


Bellow are the content of Login.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Form</title>
    <html:form action="/Login" focus="username">
        Username : <html:text property="username"/><br/>
        Password : <html:password property="password"/><br/>
        <html:submit value="Login"/>

This jsp will render a form with two fields, “username” and “password”. Struts HTML taglib has been used to create the form. <html:form action=”/Login” focus=”username”> will render as a html form with submit url as “/Login.do”, so our action must use this path in configuration to be run. <html:text property=”username”/> will be render as html input tag and it’s value will be put in the “username” field of the form bean LoginForm. Same with the property “password”.


Lets look inside the code in LoginAction.java :

public class LoginAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        LoginForm loginForm = (LoginForm)form;
        if(loginForm.getUsername() == null || loginForm.getPassword() == null ||
        !loginForm.getUsername().equalsIgnoreCase("rahul") || !loginForm.getPassword().equals("abc")){
            return mapping.findForward("failure");
            return mapping.findForward("success");

As you can see first of all the ActionForm instance is typecast to LoginForm in the execute method of LoginAction and then logic to verify the username and password will decide which view to be send back to the user. In this case, username must be “rahul” ans password must be “abc” to go to the view associated with the “success”, otherwise view associated with “failure” will be returned. We can see the jsp files associated with “success” and “failure”.


Entries of form beans and actions aer done in struts-config.xml as follows :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
    <!-- ========== Form Bean Definitions ================================== -->
        <form-bean name="loginForm" type="com.raistudies.forms.LoginForm"/>
    <!-- ========== Action Mapping Definitions ============================= -->
            <action name="loginForm" path="/Login" type="com.raistudies.actions.LoginAction" scope="request" input="/Login.jsp">
                <forward name="failure" path="/Failure.jsp" redirect="true"/>
                <forward name="success" path="/Success.jsp" redirect="true"/>

Our LoginForm class has been added as a form bean named “loginForm” and associated with the action class LoginAction. Attributes  associated with the action class are specified as with the following reason :

  1. name=”loginForm” : Struts will instantiate the LoginForm class will set the value of form properties.
  2. path=”/Login” : The action class will be associated with the request path “/Login.do”. Hence the Login.jsp contains “/Login” as action attribute value.
  3. input=”/Login.jsp” : Form inputs will be taken from Login.jsp.

There are two action forwards also registered with our action which tells struts that if “failure” is return by action then the response view will be “/Failure.jsp” and if “success” is returned by action then the response view will be “/Success.jsp”.

Deploy the war file in Tomcat 6 and hit the url in your browser you will get the following login form :

Login FormNow, put “Rahul” as username and “abc” as password you will be carried out to the Success.jsp page :

Login Successful Form

Login Successful Form

Now, try with the username as “scote” and “tiger”. It will show you Failure.jsp page :

Login Failure

Login Failure

If you import the code provided in bellow link in Eclipse. The project directory will be like this :

Product Hierarchi

Project Hierarchi in Eclipse

You can download code from bellow links :

Source Code : Download

War File : Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>