Servlet Hibernate Connectivity Example
Previous Home Next

For creating the web application we are using NetBeans and MySQL server and appache tomcat for building the application. As we have discussed in our last application. In the last application we insert data into one table of the database but here in this application we will insert data into more than one application. So to create this application we need to go through some basic step to create application as we done in last application. This application will same as the last application.

Step 1. Create a database in MySQL database.

(a). create Database hibernate_pro; (Store all table into this database)

(b). use hibernate_pro; (here use means is activate means all database activity store in this database like as table)

create table InsertData
id int auto_increment not null primary key,
first_name varchar(50),
last_name varchar(50),
age varchar(50)


select * from InsertData;

Step 2. Prepration of application on NetBeans

1. Take a new Project name is 'HibernateApplicationUsingServlet'

First of all we will select to File Menu Wizard -> then click on NewProject -> Click on JavaWeb -> then Web Application -> click on Next -> Write the name of the Project -> select the server on which want to deploy the project -> click on Hibernate -> click on database connection -> hibernate_pro -> finish. Then given outlook will be the project on NetBeans


<?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">
    <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/hibernate_pro</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>

Step 3. Take a JSP page for the user view point. For this we need to go in File Menu Wizard -> click on New File Wizard -> click on web and then click on also jsp -> next -> jsp page name give here which you want -> click on finish.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <form action="MyServlet" method="post">
        First Name      <input type="text" name="fist_name"/>
        Last Name       <input type="text" name="last_name"/>
        Age             <select name="age">
                        <option>--Select Age--</option>
                        <option>0 to 10</option>
                        <option>10 to 20</option>
                        <option>20 to 30</option>
                        <option>30 to above</option>
        <input type="submit" value="Submit"/>

Step 4. Create a reveng file for the connectivity for java class to database and for creating the pojo class. for this go in file menu wizard -> new file -> search hibernate -> then click on opposite side given hibernate reverse engineering file-> next -> click on table which we create in the database -> add > finish .


 <?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">
  <schema-selection match-catalog="hibernate_pro"/>
  <table-filter match-name="InsertData"/>

Step 5. Now we need to create POJO class for sending the data into the data base. Remember -> Before creating the POJO class we need to create the package to store the POJO class into the package. Go in the File Menu wizard -> search hibernate -> then click on opposite side given hibernate POJO class file-> next -> both file should be in for the pojo class (hibernate.cfg & hibernate.revenge file ) -> slect the package > finish .

Step 6: Now we need to create a action page of servlet. for creating the servlet page we need to go in File Menu wizard-> new file-> web & opposite side search to servlet click on it -> mark on add information as given in the image below and then click on finish.

Action code of MyServlet

public class MyServlet extends HttpServlet 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String fist_name, last_name, age;
        fist_name = request.getParameter("fist_name");
        last_name = request.getParameter("last_name");
        age = request.getParameter("age");
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
            Session s = sf.openSession();
            Transaction tr = s.beginTransaction();
        InsertData id = new InsertData(fist_name, last_name, age, new Date());
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<title>Servlet MyServlet</title>");            
            out.println("<h1>Welcome " + fist_name + "</h1>");

Pints always need to remember (servlet file should be in the web.xml file as given below:)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

Step 8. After all doing the Now we need to build and run the project for this we need to go in the menu wizard -> click on run menu -> click on clean and build the project -> then click on the run when successfully build the project.

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Comment / Feedback: