Web Application with Hibernate (using XML)

Here, we will make a web application with rest. For making the web application, we are utilizing JSP for introduction rationale, Bean class for speaking to information and DAO class for database codes.

As we make the basic application in rest, we don't have to play out any additional tasks in sleep for making web application. In such case, we are getting the incentive from the client utilizing the JSP file.


Example to make web application utilizing hibernate

In this model, we are going to embed the record of the client in the database. It is essentially an enrollment form.

index.jsp

This page gets contribution from the client and sends it to the register.jsp record utilizing post method.

  1. <form action="register.jsp" method="post">  
  2. Name:<input type="text" name="name"/><br><br/>  
  3. Password:<input type="password" name="password"/><br><br/>  
  4. Email ID:<input type="text" name="email"/><br><br/>  
  5. <input type="submit" value="register"/>"  
  6.   
  7. </form>  

register.jsp

This record gets all solicitation parameters and stores this data into an object of User class. Further, it calls the register strategy for UserDao class passing the User class object.

  1. <%@page import="com.javacodegeeks.mypack.UserDao"%>  
  2. <jsp:useBean id="obj" class="com.javacodegeeks.mypack.User">  
  3. </jsp:useBean>  
  4. <jsp:setProperty property="*" name="obj"/>  
  5.   
  6. <%  
  7. int i=UserDao.register(obj);  
  8. if(i>0)  
  9. out.print("You are successfully registered");  
  10.   
  11. %>  

User.java

It is the simple bean class representing the Persistent class in hibernate.

  1. package com.javacodegeeks.mypack;  
  2.   
  3. public class User {  
  4. private int id;  
  5. private String name,password,email;  
  6.   
  7. //getters and setters  
  8.   
  9. }  

user.hbm.xml

It maps the User class with the table of the database.

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.  "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  4.  "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.  <hibernate-mapping>  
  7.  <class name="com.javacodegeeks.mypack.User" table="u400">  
  8.  <id name="id">  
  9.  <generator class="increment"></generator>  
  10.  </id>  
  11.  <property name="name"></property>  
  12.  <property name="password"></property>  
  13.  <property name="email"></property>  
  14.  </class>  
  15.          
  16.  </hibernate-mapping>  

UserDao.java

A Dao class, containing method to store the instance of User class.

  1. package com.javacodegeeks.mypack;    
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.    
  11. public class UserDao {    
  12.     
  13. public static int register(User u){    
  14.  int i=0;    
  15.   
  16.  StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  17.  Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
  18.   
  19. SessionFactory factory = meta.getSessionFactoryBuilder().build();  
  20. Session session = factory.openSession();  
  21. Transaction t = session.beginTransaction();   
  22.   
  23. i=(Integer)session.save(u);    
  24.   
  25. t.commit();    
  26. session.close();    
  27.     
  28. return i;    
  29.    
  30.  }    
  31. }    

hibernate.cfg.xml

It is a configuration file, containing informations about the database and mapping file.

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8. <session-factory>  
  9.  <property name="hbm2ddl.auto">create</property>  
  10.  <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  11.  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  12.  <property name="connection.username">system</property>  
  13.  <property name="connection.password">jtp</property>  
  14.  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  15.       
  16.  <mapping resource="user.hbm.xml"/>  
  17.  </session-factory>  
  18.   
  19. </hibernate-configuration>  

Output

Hibernate Web Application Example 1





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition