Hibernate Web Application Example

Web Application Example with Hibernate

In this section, we create a hibernate web Application. Here we are using a JSP file for presentation.

Example of creating a web application using hibernate

index.jsp

This page will show a registration form. It takes the input from the form and sends it to the associated (in this case, register.jsp) page.

Name: Mobile No: Password: Email ID:

register.jsp

The register.jsp will get all the information from index.jsp and stores it in the object of Student class.

<%@page import="com.hibernate.webap.StudentDao"%>
 
 
  
 <%
 int i=StudentDao.register(s);
 if(i>0)
 out.print("Registration is successfull");
 %> 

Student.java

It is a bean class. All the variables of bean class are set as private with public setter and getter methods.

public class Student {
  private int id,phnno;
  private String name,email,password;
  public int getId() { 
  return id;
  }
  public void setId(int id) {
  this.id = id; 
  }
  public int getPhnno() {
  return phnno;
  } 
  public void setPhnno(int phnno) {
  this.phnno = phnno;
  } 
  public String getName() {
  return name;
  } 
  public void setName(String name) {
  this.name = name;
  } 
  public String getEmail() {
  return email;
  } 
  public void setEmail(String email) {
  this.email = email;
  } 
  public void setPassword(String password) {
  this.password = password;
  }
 } 

StudentDao.java

It contains the method to store the object of Student class.

import org.hibernate.Session; 
 import org.hibernate.SessionFactory; 
 import org.hibernate.Transaction; 
 public class StudentDao { 
 public static int register(Student student){  
 int i=0; 
 Configuration cfg= new Configuration();
 cfg.configure("hibernate.cfg.xml");
 SessionFactory factory= cfg.buildSessionFactory(); 
 Session session = factory.openSession(); 
 Transaction t = session.beginTransaction(); 
 i=(Integer)session.save(student); 
 t.commit();  
 session.close(); 
 return i; 
  } 
  }  

student.hbm.xml

It maps the object of Student class with the database table.

 
 http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd"> 
   
  
  
   
  
  
  
  
  
  
   

hibernate.cfg.xml

It contains information about the mapping file and relational database.

 http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd"> 
 
 
 create
 org.hibernate.dialect.Oracle9Dialect 
 oracle.jdbc.driver.OracleDriver
 jdbc:oracle:thin:@localhost:1521:xe
 root //write your own username
 root //write your own password 
 
 
    

Output

Web Application Example with Hibernate
Web Application  Hibernate



ADVERTISEMENT
ADVERTISEMENT