ARTICLE

Creating a form based application in Struts

Posted by Satyapriya Nayak Articles | JSP October 03, 2011
Here you will see how to create a form based application in Struts.
Reader Level:
Download Files:
 

Requirements

  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.

    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 the following tags in Struts-config.xml as

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

    <action-mappings>

    <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 " />
    </action>

    </action-mappings>

Using form tag of Struts

Attribute

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.

Methods

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).

Logbean.java

//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)
{
uid=u;
}
public void setPass(String p)
{
pass=p;
}
public String getUid()
{
return uid;
}
public String getPass()
{
return pass;
}
public void reset(ActionMapping map,HttpServletRequest res)
{
uid=null;
pass=null;
} }

logaction.java

//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;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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
if(rs.next())
isexist=true;
}
catch(Exception e)
{
System.out.println(e);
}

if(isexist){
ActionForward af=map.findForward("valid");
return af;
}
else
{
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.

Login1.JSP

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

welcome.JSP

<html>
<body>
<h1>WELCOME</h1>
</body>
</html>

error.JSP

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

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">
<web-app>
<servlet>
<servlet-name>x</servlet-name>
<servlet-class>org.apache.Struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/Struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>x</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

Struts-config settings

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

<!DOCTYPE Struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/Struts/dtds/Struts-config_1_1.dtd">


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

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

<action-mappings>

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

</action-mappings>


</Struts-config>



Running the application

Run the tomcat then write the below line in the URL
http://localhost:8081/javaStruts/login1.JSP

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.

Output

Struts.gif





 

COMMENT USING

Trending up