Creating a form based application in Struts


  1. Create the JSP to contain the form.
  2. Create a form-bean to contain values of form fields.
  3. Create a form-action to validate input of user and to provide appropriate forwards names to controlling servlets.
  4. Provide information in Struts-config.xml.
  5. Create a page for each forward.

Creation of form bean

  1. Create a class in a package by inheriting org.apache.Struts.action.ActionForm.
  2. Provide variables in the form-bean class as the properties.Name of these variables must match with the value of property attributes present in form field tags.
  3. Provide setter and getter methods for each of the property.
  4. Optionally override the reset() method to provide default values to the properties.

Creation of form action

  1. Create a class in the same package where form-bean exists and this class must inherit org.apache.Struts.action.Action class.
  2. Override the execute() method to implement validation logic and to return a forward.Return type of this method must be org.apache.Struts.ActionForward.This method accept four parameters as follows below. This represents all forward name and present in Struts-config.xml and helps to create object of actionforward. This represent form bean.
    c.javax.servlet.http.HttpServletRequest:- This represent user request.
    d.javax.servlet.http.HttpServletResponse:- This represent service response.

Compilation of bean and action: - Compile formbean and action file simultaneously by using servlet-api.jar and Struts-core-1.3.8.jar in classpath.The jar file of Struts(Struts-core-1.3.8.jar) can be found in lib folder of the context,after installation of Structs.

--- \classes\logs\javac – classpath servlet-api.jar ;Struts-core-1.3.8.jar *.java

Here "logs" is the package folder where we stored both the form bean and fom action files.

Providing information in Struts-config.xml file

  1. Provide information about formbean by using the following tags in Struts-config.xml as

    <form-bean name="name to map formbean with form action" type="class name with package name of the form bean" />
  2. Supply information about form-action and forwards by using the following tags in Struts-config.xml file


    <action name="mapping name specified in formbean" path="value of action attribute present in from" type="class name of form action" >
    <forward name="name of forward used by form action" path="name of the page to be visible" />
    <forward name=" name of forward used by form action " path=" name of the page to be visible " />


Using form tag of Struts


This contains a name to refer the form-action in the configuration fle. This is a mandatory attribute. Whenever the form gets submitted then the extension specified in the url-pattern of the controlling servlet gets added with the attribute value.


This contains a value as get or post to specify the process of data submission.

Creating a login form using Struts

Here we use Type-1 driver (JDBC-ODBC bridge)

Creation of dsn(database source name) for Oracle

Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

Note: - Here Username=system, Password=pintu and Dsn name=dsn1

Table Creation with data

create table userinfo(userid varchar(50),pass varchar(50))

insert into userinfo values('Raj','raj123')
insert into userinfo values('Ravi','ravi123')
insert into userinfo values('Rahul','rahul123')

Creation of a context file

Create any folder in any drive as (E:\Strutsproject). Inside that folder store your .JSP files. Give the Context path name as javaStruts and docBase as E:\Strutsproject, here docBase means the total path where we are storing our .JSP files. Store the java bean file inside 'classes' folder of the context for predefined context (root) the classes folder required to be created inside the WEB-INF folder. These changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Creation of form bean

Create a package folder (pack1) inside the classes folder of the context (E:\Strutsproject\WEB-INF\classes). Store all the managed bean files inside the package folder (logs).

//form bean
package logs;
import javax.servlet.http.*;
import org.apache.Struts.action.*;
public class logbean extends ActionForm
String uid,pass;
public void setUid(String u)
public void setPass(String p)
public String getUid()
return uid;
public String getPass()
return pass;
public void reset(ActionMapping map,HttpServletRequest res)
} }

//form action
package logs;
import javax.servlet.http.*;
import org.apache.Struts.action.*;
import java.sql.*;
public class logaction extends Action
public ActionForward execute(ActionMapping map,ActionForm frm,HttpServletRequest req,HttpServletResponse res) throws Exception
logbean b=(logbean)frm;
String u=b.getUid();
String p=b.getPass();
boolean isexist=false;
Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from userinfo where userid='"+u+"' and pass='"+p+"'");

//Fetching data from ResultSet and display
catch(Exception e)

ActionForward af=map.findForward("valid");
return af;
ActionForward af=map.findForward("invalid");
return af;
} }

Compile both the files simultaneously

Javac –cp Struts-core-1.3.8.jar; servlet-api.jar*.java

Creating the JSP form field tags and respective forward JSP pages

Store all the JSP pages inside the context folder (E:\Strutsproject)

Here there are three JSP pages they are login1.JSP, welcome.JSP and error.JSP.


<%@ taglib uri="/WEB-INF/Struts-html.tld" prefix="ht" %>
<h1 align="center">
<ht:form action="/logact" >
<h2>User Id<ht:text property="uid" value="" /></h2>
<h2>Password<ht:password property="pass" value="" /></h2>
<ht:submit />




<h1>Invalid data</h1>
<JSP:include page="/login1.JSP" />

web.xml setting

<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "">


Struts-config settings

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE Struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

<form-bean name="form1" type="logs.logbean" />

<forward name="form1" path="/welcome.JSP" />


<action name="form1" path="/logact" type="logs.logaction" >
<forward name="valid" path="/welcome.JSP" />
<forward name="invalid" path="/error.JSP" />



Running the application

Run the tomcat then write the below line in the URL

Here javaStruts is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.