Create table:

create table employeregsiter(empid int primary key,ename varchar(20),mobilenumber int,designation varchar(20));

Step1: develop a  index.jsp form 

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="reg">
    <h3>REGISTRATION FORM</h3>
    <html:errors/>
    <table background="#hff4" color="red">
        <tr>
            <td>ID:</td>
            <td><html:text property="empid"/></td>
         </tr>   
         <tr>
            <td> Name:</td>
            <td><html:text property="ename"/></td> 
        </tr>
        <tr>
            <td> Mobile:</td>
            <td><html:text property="mobilenumber"/></td> 
        </tr>
        <tr>
            <td> Designation:</td>
            <td><html:text property="designation"/></td> 
        </tr>    
  <tr>
 <td colspan="2"><html:submit value="Register"/></td> 
        </tr>
    </table> 
</html:form>

Step3: develop a formbean class name EmployeeRegister.java

package formbean;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class EmployeeRegister extends org.apache.struts.action.ActionForm {
    private Integer empid;
    private String ename;
    private Integer mobilenumber;
    private String designation;
public String getDesignation() {
        return designation;
    }
public void setDesignation(String designation) {
        this.designation = designation;
    }
public Integer getEmpid() {
        return empid;
    }
public void setEmpid(Integer empid) {
        this.empid = empid;
    }
public String getEname() {
        return ename;
    }
public void setEname(String ename) {
        this.ename = ename;
    }
public Integer getMobilenumber() {
        return mobilenumber;
    }
public void setMobilenumber(Integer mobilenumber) {
        this.mobilenumber = mobilenumber;
    }
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
       
         if (getEmpid() == null || getEmpid()< 1) {
            errors.add("empid", new ActionMessage("error.eid.required"));
        }
       
        if (getEname() == null || getEname().length() < 1) {
            errors.add("ename", new ActionMessage("error.ename.required"));
        }
        return errors;
    }
}

Configuring a formbean class into struts-config.xml file

<form-beans>
 <form-bean name="EmployeeRegister" type="formbean.EmployeeRegister"/>
    </form-beans>


Step4: develop a action class name EmployeeRegisterAction.java

package controller;
import formbean.EmployeeRegister;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import pojo.Employeregister;
public class EmployeeRegisterAction extends org.apache.struts.action.Action {
private static final String SUCCESS = "success";
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        EmployeeRegister er=(EmployeeRegister)form;
        Integer eid1=er.getEmpid();
        String ename=er.getEname();
        Integer mobile=er.getMobilenumber();
        String design=er.getDesignation();
    Session s=new Configuration().configure().buildSessionFactory().openSession();
        Transaction tx=s.beginTransaction();
        request.setAttribute("eid",eid1);
        Employeregister e=new Employeregister(eid1, ename, mobile, design);
        s.save(e);
         tx.commit();
          return mapping.findForward(SUCCESS);
    }
}

Configuring action class into struts-config.xml file

<action-mappings>
        <action input="/index.jsp" name="EmployeeRegister" path="/reg" scope="request"        type="controller.EmployeeRegisterAction" validate="true">
            <forward name="success" path="/success.jsp"/></action>  </action-mappings>


Step5: develop a success page success.jsp






HI ! <font color="green"><b><%=request.getAttribute("eid")%></b></font> &nbsp; <br></br>
you are successfully registered....<br></br>
Click Here For<a href="index.jsp">Register</a>

  
Step6:create hibernate configuration file and mapping file and pojo class



a). Employeregister.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 7, 2013 11:11:29 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="pojo.Employeregister" table="employeregister" catalog="ashok">
        <id name="empid" type="int">
            <column name="empid" />
            <generator class="assigned" />
        </id>
        <property name="ename" type="string">
            <column name="ename" length="20" />
        </property>
        <property name="mobilenumber" type="int">
            <column name="mobilenumber"/>
        </property>
        <property name="designation" type="string">
            <column name="designation" length="30" /> </property>
</class></hibernate-mapping>



b). hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ashok</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="pojo/Employeregister.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

c). Employeregister.java (pojo class)

package pojo;
// Generated Dec 7, 2013 11:11:23 AM by Hibernate Tools 3.2.1.GA
public class Employeregister  implements java.io.Serializable {
  private int empid;
     private String ename;
     private int mobilenumber;
     private String designation;
public Employeregister() {
    }
public Employeregister(int empid) {
        this.empid = empid;
    }
    public Employeregister(int empid, String ename, int mobilenumber, String designation) {
       this.empid = empid;
       this.ename = ename;
       this.mobilenumber = mobilenumber;
       this.designation = designation;
    }
   public int getEmpid() {
        return this.empid;
    }
    public void setEmpid(int empid) {
        this.empid = empid;
    }public String getEname() {
        return this.ename;
    } public void setEname(String ename) {
        this.ename = ename;
    }public int getMobilenumber() {
        return this.mobilenumber;
    } public void setMobilenumber(int mobilenumber) {
        this.mobilenumber = mobilenumber;
    }
    public String getDesignation() {
        return this.designation;
    }
 public void setDesignation(String designation) {
        this.designation = designation;
    }
}

hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="ashok"/>
  <table-filter match-name="employeregister"/>
</hibernate-reverse-engineering>




Output:

After entering a values


  we can get success page







DOWNLOAD WAR FILE  CLICK HERE

DOWNLOAD RAR FILE  CLICK HERE

1 comments:

  1. in your database mobile number is inserted as '0'.. Can u please tell how to rectify it..???

    ReplyDelete