Struts Application with Tomcat Web Server

1) Address.jsp ( this page is a form basically )
 
 
 
   <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
   <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
   <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
 
   <html:html >
 
   <head>
 
   <title>Student Record Form</title>
 
 
   <html:base/>
   <link rel="stylesheet" type="text/css" href="../../css/example.css" />
   </head>
 
   <body bgcolor=#FFF68F>
 
   <html:form action="/Address">
 
   <!--  <html:errors/>  -->
 
   <table>
 
        <tr>
 
          <td align="center" colspan="2">
               <font size="4"><u> STUDENT RECORD FORM </u></font>
        </tr>
 
        <tr>
          <td align="right">
            Name :
          </td>
          <td align="left">
            <html:text property="name" size="30" maxlength="30"/>
          </td>
        </tr>
 
        <tr>
          <td align="right">
            Address :
          </td>
          <td align="left">
            <html:text property="address" size="30" maxlength="30"/>
          </td>
        </tr>
 
        <tr>
          <td align="right">
            E-mail address :
          </td>
          <td align="left">
            <html:text property="emailAddress" size="30" maxlength="30"/>
          </td>
        </tr>
 
<tr>
          <td align="right">
           Father's Name :
          </td>
          <td align="left">
            <html:text property="fatherName" size="30" maxlength="30"/>
          </td>
        </tr>
 
 
<tr>
          <td align="right">
            Date Of Birth :
          </td>
          <td align="left">
            <html:text property="dateOfBirth" size="30" maxlength="30"/>
          </td>
        </tr>
 
<tr>
          <td align="right">
            Sex :
          </td>
          <td align="left">
            <html:radio property="sex" value="M"/> Male
            <html:radio property="sex" value="F"/> Female
          </td>
        </tr>
       
 
<tr>
          <td align="right">
            <html:submit>Save</html:submit>
          </td>
          <td align="left">
            <html:cancel>Cancel</html:cancel>
          </td>
        </tr>
 
 
</table>
   </html:form>
   </body>
</html:html>
 
saving this page in  \webapps\ashu3\jsp\ashu
 
***********************************************************************
 
open this form by typing  http://localhost:8080/ashu3/Address.do
 
***********************************************************************
 
for this mapping first we created an index file in the folder \webapps\ashu3.
 
contents of index.jsp are as follows:-
 
 
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Redirecting Page</title>
<html:xhtml/>
<html:base/>
<link href="css/example.css" rel="stylesheet" type="text/css" />
</head>
<body>
 
<table width="85%" border="0" cellpadding="2" cellspacing="5">
 
<tr valign="top">
    <td><b><u>Ashu's & Asif's ActionForm</u></b></td>
    <td>
                        <html:link action="/Address">Execute</html:link>
        </td>
   
  </tr>
 
 
</table>
 
</body>
 
</html>
 
 
now look at <html:link action="/Address">Execute</html:link> ** action="/address" this entry has been mapped in struts-config.xml  in action-mappings tag as :-
 
 
 
<action  path="/Address"
         type="ashu.AddressAction"
         name="AddressForm"
         scope="request"
         validate="true"
         input="/jsp/ashu/Address.jsp">
 
 
 
    <forward name="success" path="/jsp/ashu/success.jsp"/>
    <forward name="failure" path="/jsp/ashu/failure.jsp"/>
 
 
 
</action>
 
=======================================================================
 
now before giving the code for the pages success.jsp and failure.jsp moving to the controller and action form...
 
 
AddressForm.java
 
 
 
 
 
package ashu;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.*;
 
 
public class AddressForm extends ActionForm
{
  private String name=null;
  private String address=null;
  private String emailAddress=null;
  private String sex=null;
  private String fatherName=null;
  private String dateOfBirth=null;
 
 
public void setName(String name){
    this.name=name;
  }
 
  public String getName(){
    return this.name;
  }
 
  public void setAddress(String address){
    this.address=address;
  }
 
  public String getAddress(){
    return this.address;
  }
 
 
  public void setEmailAddress(String emailAddress){
    this.emailAddress=emailAddress;
  }
 
  public String getEmailAddress(){
    return this.emailAddress;
  }
 
  public void setSex(String sex){
    this.sex=sex;
  }
 
  public String getSex(){
    return this.sex;
  }
 
 
  public void setFatherName(String fatherName){
    this.fatherName=fatherName;
  }
 
  public String getFatherName(){
    return this.fatherName;
  }
 
 
 
  public void setDateOfBirth(String dateOfBirth){
    this.dateOfBirth=dateOfBirth;
  }
 
  public String getDateOfBirth(){
    return this.dateOfBirth;
  }
 
 
 
public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.name=null;
    this.address=null;
    this.emailAddress=null;
    this.sex=null;
    this.fatherName=null;
    this.dateOfBirth=null;
    }
 
 
public ActionErrors validate(
      ActionMapping mapping, HttpServletRequest request ) {
      ActionErrors errors = new ActionErrors();
     
      if( getName() == null || getName().length() < 1 ) {
        errors.add("name",new ActionMessage("error.name.required"));
      }
      if( getAddress() == null || getAddress().length() < 1 ) {
        errors.add("address",new ActionMessage("error.address.required"));
      }
      if( getEmailAddress() == null || getEmailAddress().length() < 1 ) {
        errors.add("emailaddress",new ActionMessage("error.emailaddress.required"));
      }
 
      return errors;
  }
 
}
 
 
 
this java file is a bean which consist of the setter and getter methods simply............
 
save this file in \webapps\ashu3\WEB-INF\classes\ashu
 
compile this file
 
***********************************************************************
 
AddressActon.java  ( this is action form that consist of execute method)
 
 
 
package ashu;
 
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.*;
 
public class AddressAction extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
     
      AddressForm a = (AddressForm) form;              // created an object of AddressForm in the action form then using it
 
       try
       {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
       
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=/india.mdb";
        
 
        Connection con = DriverManager.getConnection(database, "", "");
 
        // try and create a java.sql.Statement so we can run queries
        Statement s = con.createStatement();
System.out.println("insert into mytable values(" + a.getName() +"," + a.getAddress() + "," + a.getEmailAddress() + "," +
                                                  a.getSex() + "," + a.getFatherName() + "," + a.getDateOfBirth() + ")");
        s.execute("insert into mytable values('" + a.getName() +"','" + a.getAddress() + "','" + a.getEmailAddress() + "','" +
                                                  a.getSex() + "','" + a.getFatherName() + "','" + a.getDateOfBirth() + "');");
       
        return mapping.findForward("success");
       }
       catch(Exception err)
      {
        System.out.println("ERROR: " + err);
      }
      return mapping.findForward("failure");
  }
}
 
 
isme hum database ki queries write karenge this is known as the controller, save this java file in
 
\webapps\ashu3\WEB-INF\classes\ashu , compile it
 
***********************************************************************
 
 
 
now for the AddressForm.class this is working as the bean
we have to give an entry for this bean in our struts-config.xml file as:-
 
 
<!-- Simple AddressAction Bean -->
 
        <form-bean name="AddressForm" type="ashu.AddressForm"/>
 
 
under Form-Beans tag.
 
 
also we are using the struts-html taglib in our jsp pages, so donot forget to include tld file entries in web.xml file
 
***********************************************************************
 
code for success.jsp  ( save in \webapps\ashu3\jsp\ashu )
 
  <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
   <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
   <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
 
<html:html>
<head>
<title>  Success </title>
 
<html:base/>
</head>
<body>
 
 
<b><h1>  One Row Added Succesfully </h1></b>
<button style="background-color:transparent"  onClick="window.location='/ashu3/index.jsp' ">go home </button>
<button style="background-color:transparent"  onClick="window.location='/ashu3/jsp/ashu/First.jsp' ">Qyery DataBase </button>
 
</body>
</html:html>
 
its doing two work
1) redirecting to index page
2) generating a report ( fetching data basically   '/ashu3/jsp/ashu/First.jsp' )
 
 
 
code for fetching data i.e /ashu3/jsp/ashu/First.jsp
 
 
 
<%@ page import="java.sql.*"%>
 
<%
java.sql.Connection con;
java.sql.PreparedStatement pstmt;
java.sql.ResultSet rst;
 
 
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=/india.mdb","","");
pstmt = con.prepareStatement("select * from mytable");
rst = pstmt.executeQuery();
 
int i = 1;
if(rst.next())
do
{
 
out.println("<br>" + i++ + ")   "  +  "   " + rst.getString("name") + "  " + rst.getString("address") + "   " + rst.getString("emailaddress") + "   " + rst.getString("sex")  + "    " + rst.getString("fname") +  "  " + rst.getString("dob") );
 
}while (rst.next() );
}
catch(Exception e)
{
out.println("Error "+e);
}
 
%>
 
 
 
 
 
 
 
 
***********************************************************************
 
 
 
 
Code for the file failure.jsp ( save in \webapps\ashu3\jsp\ashu )
 
<html>
<title>  Failure in Filling Data </title>
<body>
 
<b><h1>   Error in Filling data in to DATA BASE </h1></b>
 
</body>
</html>
 
***********************************************************************
 
 
now in this application i have not used any validation using struts and also ne javascript validation..
 
i have also used a bit of css , but u can ignore that by deleting the corresponding line
 
 
 
***********************************************************************
 
 
U have to create a database in ms-access and name it as india.mdb, now create a table in that and name it as mytable
 
with 6 columns as specified in the Address.jsp file, also do not forget to create a dsn connection..
 
FOR MORE JAVA TUTORIALS PLEASE VISIT http://sharma.ashutosh84.googlepages.com

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: