Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JSP Hibernate Tutorial

In this section, you will learn how to use JSP with hibernate. This section contains pagination example using JSP & Hibernate.


JSP Hibernate Tutorial

In this section, you will learn how to use JSP with hibernate. This section contains pagination example using JSP & Hibernate.



<?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" />



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

package devmanuals.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 devmanuals.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

public class DAO {
private static int pageSize = 3;
public static List getData(int pageNumber) {
SessionFactory sessionFactory = 
new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
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", devmanuals.dao.DAO.getData(pageNumber));
String myUrl = "contact.jsp?page=" + nextPage;
pageContext.setAttribute("myUrl", myUrl);
<h2 align="center">Showing Table Records</h2>
<jsp:useBean id="EmpList" scope="session" type="java.util.List"></jsp:useBean>
<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>


Download Source Code


Share on Google+Share on Google+

JSP Hibernate Tutorial

Posted on: December 23, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JSP Hibernate Tutorial  

Post your Comment

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