Registration Page using JSP, Servlet, Hibernate, and MySQL Database

This tutorial will guide you through setting up a registration page using JSP and Servlets with Hibernate to manage user registration with a MySQL database. We will use the User entity for this example. The focus will be on setting up the environment and demonstrating the registration functionality.


Integrating JSP and Servlets with Hibernate allows for a clean separation of the presentation layer and the data access layer. Hibernate handles the database operations, while JSPs and Servlets manage the user interface and request handling. In this tutorial, we will:

  1. Set up a Maven project with necessary dependencies.
  2. Configure Hibernate and MySQL.
  3. Create an entity class (User).
  4. Create JSPs for the registration interface.
  5. Implement Servlets for handling registration requests.
  6. Use annotations instead of web.xml for configuration.

Step 1: Set Up Your Project

1.1 Create a Maven Project

Open your IDE and create a new Maven project.

1.2 Add Dependencies

Update your pom.xml file to include dependencies for Hibernate, MySQL, JSP, and Servlet API.

<project xmlns=""


        <!-- Hibernate ORM -->

        <!-- MySQL Connector -->

        <!-- Servlet API -->

        <!-- JSP API -->

        <!-- JSTL -->


Step 2: Configure Hibernate

2.1 Create hibernate.cfg.xml

Create a hibernate.cfg.xml file in the src/main/resources directory to configure database connection settings and Hibernate properties.

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="">update</property>
        <property name="hibernate.show_sql">true</property>

Replace hibernate_db, root, and password with your MySQL database name and credentials.


  • hibernate.dialect specifies the SQL dialect to be used.
  • hibernate.connection.driver_class specifies the JDBC driver class.
  • hibernate.connection.url specifies the JDBC URL for the database connection.
  • hibernate.connection.username and hibernate.connection.password specify the database credentials.
  • specifies the schema generation strategy.
  • hibernate.show_sql specifies whether to show SQL statements in the logs.

Step 3: Create the Entity Class

Create an entity class User that will be mapped to a table in the database. This class uses annotations to define the entity and its fields.

package com.example.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class User {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;

    // Getters and setters
    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;


  • The @Entity annotation specifies that the class is an entity and is mapped to a database table.
  • The @Id annotation specifies the primary key of the entity.
  • The @GeneratedValue(strategy = GenerationType.IDENTITY) annotation specifies that the primary key is auto-incremented.

Step 4: Create JSPs for User Interface

Create a JSP file named register.jsp in the src/main/webapp directory to provide the registration form.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
    <title>Registration Page</title>
    <form action="registerServlet" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username"><br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password"><br><br>
        <input type="submit" value="Register">

Create a success.jsp to display a success message upon successful registration.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
    <h1>Registration Successful</h1>
    <p>Welcome, ${username}!</p>


  • register.jsp provides a form to collect user credentials.
  • success.jsp displays a success message for the registered user.

Step 5: Implement Servlet for Handling Registration Requests

Create a servlet named RegisterServlet to handle registration requests.

5.1 Create RegisterServlet

package com.example.servlet;

import com.example.entity.User;
import com.example.util.HibernateUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();
            request.setAttribute("username", user.getUsername());
            request.getRequestDispatcher("success.jsp").forward(request, response);
        } catch (Exception e) {
            if (transaction != null) {
            request.setAttribute("message", "Error registering user");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        } finally {


  • The RegisterServlet class handles POST requests to register

the user using Hibernate and redirect to the appropriate JSP page based on the registration result.

Step 6: Configure Hibernate Utility Class

Create a utility class HibernateUtil to manage the Hibernate SessionFactory.

package com.example.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);

    public static SessionFactory getSessionFactory() {
        return sessionFactory;

    public static void shutdown() {


  • The HibernateUtil class provides a singleton SessionFactory and a method to shut it down.

Step 7: Run the Application

  1. Deploy the application to a servlet container like Apache Tomcat.
  2. Access the application at http://localhost:8080/registration-example/.
  3. Use the registration form to enter user credentials and test the registration functionality.

Sample Output

When you register with valid credentials, you should be redirected to success.jsp displaying a success message. If there is an error during registration, you should see an error message on the register.jsp.


In this tutorial, we have successfully set up a JSP Servlet Hibernate web application using a MySQL database and the User entity. We configured the project dependencies, Hibernate, created an entity class, implemented a servlet to handle registration requests, and demonstrated the use of JSPs to create a registration interface. This guide provides a solid foundation for building a web application with JSP, Servlets, and Hibernate, allowing you to extend the application further as needed.