Servlet + JSP + JDBC + MySQL Example

In this article, we will build a simple Employee Registration Form example using a combination of JSP, ServletJDBC and MySQL database.

Get source code of this tutorial on my GitHub Repository.

In this example, we will develop below flow:
1. Creating an Employee Registration form using JSP
2. Submit Employee Registration form with a POST request and URL - /register
3. After form submission corresponding servlet will get called -
4. EmployeeServlet class handles all the request parameters and send to EmployeeDao class to save this data to the database.

Below diagram shows the Employee Registration JSP page:

Let's develop a complete step by step web application using a combination of JSP, Servlet, JDBC and MySQL database.

Let me list out the tools and technologies that I have used to develop this web application. 

Tools and technologies used

  • JSP - 2.2 +
  • IDE - STS/Eclipse Neon.3
  • JDK - 1.8 or later
  • Apache Tomcat - 8.5
  • JSTL - 1.2.1
  • Servlet API - 2.5
  • MySQL - mysql-connector-java-8.0.13.jar

Development Steps

  1. Create an Eclipse Dynamic Web Project
  2. Add Dependencies
  3. Project Structure
  4. MySQL Database Setup
  5. Create a JavaBean -
  6. Create a
  7. Create a
  8. Create a employeeregister.jsp
  9. Create a employeedetail.jsp
  10. Demo

1. Create an Eclipse Dynamic Web Project

To create a new dynamic Web project in Eclipse:
1. On the main menu select File > New > Project....
2. In the upcoming wizard choose Web > Dynamic Web Project.

3. Click Next.
4. Enter project name as "jsp-servlet-jdbc-mysql-example";
5. Make sure that the target runtime is set to Apache Tomcat with the currently supported version. 

2. Add Dependencies

Add the latest release of below jar files to the lib folder.
  • jsp-api.2.3.1.jar
  • servlet-api.2.3.jar
  • mysql-connector-java-8.0.13.jar
  • jstl-1.2.jar

3. Project Structure

Standard project structure for your reference -

4. MySQL Database Setup

Let's create a database named "mysql_database" in MySQL. Now, create an employee table using below DDL script:
CREATE TABLE `employee` (
   `id` int(3) NOT NULL,
   `first_name` varchar(20) DEFAULT NULL,
   `last_name` varchar(20) DEFAULT NULL,
   `username` varchar(250) DEFAULT NULL,
   `password` varchar(20) DEFAULT NULL,
   `address` varchar(45) DEFAULT NULL,
   `contact` varchar(45) DEFAULT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
SELECT * FROM mysql_database.employee; 

5. Create a JavaBean -

Let's create an Employee JavaBean class which we will use in JSP action tags.
package net.javaguides.jsp.jdbc.bean;


 * JavaBean class used in jsp action tags.
 * @author Ramesh Fadatare
public class Employee implements Serializable {
    private static final long serialVersionUID = 1 L;
    private String firstName;
    private String lastName;
    private String username;
    private String password;
    private String address;
    private String contact;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    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;
    public String getAddress() {
        return address;
    public void setAddress(String address) {
        this.address = address;
    public String getContact() {
        return contact;
    public void setContact(String contact) { = contact;

6. Create an

Let's create EmployeeDao class which contains JDBC code to connect with MySQL database. Add the following code to an EmployeeDao class:
package net.javaguides.jsp.jdbc.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import net.javaguides.jsp.jdbc.bean.Employee;

public class EmployeeDao {

    public int registerEmployee(Employee employee) throws ClassNotFoundException {
        String INSERT_USERS_SQL = "INSERT INTO employee" +
            "  (id, first_name, last_name, username, password, address, contact) VALUES " +
            " (?, ?, ?, ?, ?,?,?);";

        int result = 0;


        try (Connection connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/mysql_database?useSSL=false", "root", "root");

            // Step 2:Create a statement using connection object
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
            preparedStatement.setInt(1, 1);
            preparedStatement.setString(2, employee.getFirstName());
            preparedStatement.setString(3, employee.getLastName());
            preparedStatement.setString(4, employee.getUsername());
            preparedStatement.setString(5, employee.getPassword());
            preparedStatement.setString(6, employee.getAddress());
            preparedStatement.setString(7, employee.getContact());

            // Step 3: Execute the query or update query
            result = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            // process sql exception
        return result;

    private void printSQLException(SQLException ex) {
        for (Throwable e: ex) {
            if (e instanceof SQLException) {
                System.err.println("SQLState: " + ((SQLException) e).getSQLState());
                System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
                System.err.println("Message: " + e.getMessage());
                Throwable t = ex.getCause();
                while (t != null) {
                    System.out.println("Cause: " + t);
                    t = t.getCause();
You can learn complete JDBC at JDBC Tutorial

7. Create an

 Let's create an EmployeeServlet class to process HTTP request parameters and redirect to the appropriate JSP page after request data stored in the database:
package net.javaguides.employeemanagement.web;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.javaguides.employeemanagement.dao.EmployeeDao;
import net.javaguides.employeemanagement.model.Employee;

 * @email Ramesh Fadatare

public class EmployeeServlet extends HttpServlet {
    private static final long serialVersionUID = 1 L;
    private EmployeeDao employeeDao;

    public void init() {
        employeeDao = new EmployeeDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        String contact = request.getParameter("contact");

        Employee employee = new Employee();

        try {
        } catch (Exception e) {
            // TODO Auto-generated catch block


8. Create a employeeregister.jsp

Let's design employee registration HTML form with the following fields:
  • firstName
  • lastName
  • username
  • password
  • address
  • contact
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
 <div align="center">
  <h1>Employee Register Form</h1>
  <form action="<%= request.getContextPath() %>/register" method="post">
   <table style="with: 80%">
     <td>First Name</td>
     <td><input type="text" name="firstName" /></td>
     <td>Last Name</td>
     <td><input type="text" name="lastName" /></td>
     <td><input type="text" name="username" /></td>
     <td><input type="password" name="password" /></td>
     <td><input type="text" name="address" /></td>
     <td>Contact No</td>
     <td><input type="text" name="contact" /></td>
   <input type="submit" value="Submit" />

9. Create an employeedetails.jsp

After an employee successfully registered then this page show a successful message on screen:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@page import="net.javaguides.employeemanagement.dao.*"%>
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
 <h1>User successfully registered!</h1>
Note that in the above page, we have used JSP action tags. Read more about action tags here.

10. Demo

It's time to see a demo of the above development. Deploy this web application in tomcat server.

Employee Registration

Once you deploy this application successfully then hit this link into a browser - http://localhost:8080/jsp-servlet-jdbc-mysql-example/employeeregister.jsp

Registration Success Page

10. Related Articles


  • JSP + JDBC + MySQL Example -  In this article, we will build a simple Employee Registration module using JSPJDBC, and MySQL database.

Servlet Articles

In the next article, we will look into JSP Login Form + JDBC + MySQL Example.
Get source code of this tutorial on my GitHub Repository.