.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating form based application in struts

Posted By:satyapriyanayak       Posted Date: February 04, 2015    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will see how to Create a form based application in struts.

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 page for each forwards.

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 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 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.
a.org.apache.struts.action.Actionmappings:- This represents all forward name and present in struts-config.xml and helps to create object of actionforward.
b.org.apache.struts.action.Actionform:- 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 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 structs


This contains a name to refer the form-action in the configuration fle. This is 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 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" "http://java.sun.com/dtd/web-app_2_3.dtd">


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.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend