Java Guides

This tutorial demonstrates how to create a simple web application using Servlet, JSP, JDBC, and MySQL. In this tutorial, we will create an Employee Register Form using the latest JSP, Servlet Jakarta API, and MySQL database.

Development Steps

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

Step 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 the project name as jsp-servlet-jdbc-mysql-example .
  5. Ensure that the target runtime is set to Apache Tomcat with the currently supported version.

Step 2: Add Dependencies

Add the latest release of the below JAR files to the lib folder:

Add the following dependencies to your pom.xml :

 4.0.0 net.javaguides.servlet.tutorial java-servlet-tutorial war 0.0.1-SNAPSHOT java-servlet-tutorial Maven Webapp http://maven.apache.org  jakarta.servlet jakarta.servlet-api 6.1.0 provided  jakarta.servlet.jsp.jstl jakarta.servlet.jsp.jstl-api 3.0.0  mysql mysql-connector-java 8.0.13   java-servlet-tutorial   

Step 3: Project Structure

Here is the standard project structure for your reference:

Servlet + JSP + JDBC + MySQL Example

Step 4: MySQL Database Setup

Create a database named mysql_database in MySQL and then create an employee table using the following DDL script:

CREATE TABLE `employee` ( `id` int(3) NOT NULL AUTO_INCREMENT, `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; 

Step 5: Create a JavaBean - Employee.java

package net.javaguides.employeemanagement.bean; import java.io.Serializable; /** * JavaBean class used in JSP action tags. */ public class Employee implements Serializable < private static final long serialVersionUID = 1L; private String firstName; private String lastName; private String username; private String password; private String address; private String contact; // Getters and Setters 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) < this.contact = contact; >> 

Step 6: Create EmployeeDao.java

package net.javaguides.employeemanagement.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import net.javaguides.employeemanagement.bean.Employee; public class EmployeeDao < public int registerEmployee(Employee employee) throws ClassNotFoundException < String INSERT_USERS_SQL = "INSERT INTO employee (first_name, last_name, username, password, address, contact) VALUES (?, ?, ?, ?, ?, ?);"; int result = 0; Class.forName("com.mysql.cj.jdbc.Driver"); try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_database?useSSL=false", "root", "root"); PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) < preparedStatement.setString(1, employee.getFirstName()); preparedStatement.setString(2, employee.getLastName()); preparedStatement.setString(3, employee.getUsername()); preparedStatement.setString(4, employee.getPassword()); preparedStatement.setString(5, employee.getAddress()); preparedStatement.setString(6, employee.getContact()); System.out.println(preparedStatement); result = preparedStatement.executeUpdate(); >catch (SQLException e) < printSQLException(e); >return result; > private void printSQLException(SQLException ex) < for (Throwable e: ex) < if (e instanceof SQLException) < e.printStackTrace(System.err); 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(); >> > > > 

Step 7: Create EmployeeServlet.java

package net.javaguides.employeemanagement.web; import java.io.IOException; 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 net.javaguides.employeemanagement.dao.EmployeeDao; import net.javaguides.employeemanagement.bean.Employee; @WebServlet("/register") public class EmployeeServlet extends HttpServlet < private static final long serialVersionUID = 1L; 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(); employee.setFirstName(firstName); employee.setLastName(lastName); employee.setUsername(username); employee.setPassword(password); employee.setAddress(address); employee.setContact(contact); try < employeeDao.registerEmployee(employee); >catch (Exception e) < e.printStackTrace(); >response.sendRedirect("employeedetails.jsp"); > > 

Step 8: Create employeeregister.jsp

     Insert title here   

Employee Register Form

First Name
Last Name
UserName
Password
Address
Contact No

Step 9: Create employeedetails.jsp

     Insert title here  

User successfully registered!

Step 10: Demo

After following all the steps, you should be able to run your web application. Open your browser and navigate to http://localhost:8080/jsp-servlet-jdbc-mysql-example/employeeregister.jsp . You should see the Employee Register Form, as shown in the image below:

Fill out the form and submit it. The form data will be stored in the MySQL database, and you will be redirected to employeedetails.jsp with a success message.

This example demonstrates a simple web application using Servlet, JSP, JDBC, and MySQL, updated to use the latest Servlet Jakarta API.

Related Servlet Posts