Share on Google+Share on Google+

Hibernate JSP

In this section, you will learn about Hibernate with JSP using simple pagination example.

Hibernate JSP

In this section, you will learn about Hibernate with JSP using simple pagination example.

In the below example, we will describe you how to do pagination using simple JSP and Hibernate.

The project hierarchy and the jar files used is given below :

The SQL query used to create table contact is given below:

CREATE TABLE `contacts` ( 
`id` int(11) NOT NULL auto_increment, 
`firstname` varchar(30) default NULL, 
`lastname` varchar(30) default NULL, 
`cell_no` varchar(15) default NULL, 
`email_id` varchar(30) default NULL, 
`website` varchar(150) default NULL, 
`birthdate` date default NULL, 
`created` timestamp NOT NULL default CURRENT_TIMESTAMP, 



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="devmanuals.model.Contact" />


package roseindia.model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

public class Contact implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long id;
private String firstName;
private String lastName;
private String emailId;
private String cellNo;
private Date birthDate;
private String website;
private Date created;

public Long getId() {
return id;
public String getFirstName() {
return firstName;
public String getLastName() {
return lastName;
public String getEmailId() {
return emailId;
public String getCellNo() {
return cellNo;
public Date getBirthDate() {
return birthDate;
public String getWebsite() {
return website;
public Date getCreated() {
return created;
public void setId(Long id) { = id;
public void setFirstName(String firstName) {
this.firstName = firstName;
public void setLastName(String lastName) {
this.lastName = lastName;
public void setEmailId(String emailId) {
this.emailId = emailId;
public void setCellNo(String cellNo) {
this.cellNo = cellNo;
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
public void setCreated(Date created) {
this.created = created;
public void setWebsite(String website) { = website;

package roseindia.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class DAO {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;
private static int pageSize = 3;
public static List getData(int pageNumber) {
try {
Configuration configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
Session session = sf.openSession();
List result = null;
try {
Query query = session.createQuery("from Contact");
query = query.setFirstResult(pageSize * (pageNumber - 1));
result = query.list();
} catch (Exception e) {
return result;


<%@ page contentType="text/html; charset=UTF-8"%>
<[email protected] uri="" prefix="c"%>
< contentType="text/html; charset=UTF-8" />

<link rel="stylesheet" type="text/css" href="css/screen.css" />
int pageNumber=1;
if(request.getParameter("page") != null) {
session.setAttribute("page", request.getParameter("page"));
pageNumber = Integer.parseInt(request.getParameter("page"));
} else {
session.setAttribute("page", "1");
String nextPage = (pageNumber +1) + "";
session.setAttribute( "EmpList", roseindia.dao.DAO.getData(pageNumber));
String myUrl = "contact.jsp?page=" + nextPage;
pageContext.setAttribute("myUrl", myUrl);
<h2>Showing Table Records</h2>
<jsp:useBean id="EmpList" scope="session" type="java.util.List"></jsp:useBean>
<table border="1">
<th>Web Site</th>
<th>Creation Date</th>

<c:forEach items="${EmpList}" var="emp" begin="0" end="10">
<td><c:out value="${}"></c:out>&nbsp;&nbsp;&nbsp;</td>
<td><c:out value="${emp.firstName}"></c:out></td>
<td><c:out value="${emp.lastName}"></c:out></td>
<td><c:out value="${emp.emailId}"></c:out></td>
<td><c:out value="${emp.cellNo}"></c:out></td>
<td><c:out value="${emp.birthDate}"></c:out></td>
<td><c:out value="${}"></c:out></td>
<td><c:out value="${emp.created}"></c:out></td>

<td colspan="2"></td>
<td colspan="2"><a href="${pageScope.myUrl}">Next Page</a></td>


After executing the above code, you will get the following page as output in the browser  :

Download Source Code


Posted on: March 31, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate JSP  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image