VOOZH about

URL: https://www.geeksforgeeks.org/java/servlet-crud-operation-with-example/

⇱ Servlet - CRUD Operation with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Servlet - CRUD Operation with Example

Last Updated : 18 May, 2026

CRUD stands for Create, Read, Update, and Delete, which are the basic operations used to manage data in a database. In this example, we will build a simple User Registration application using Servlet, MySQL, and JDBC to perform all CRUD operations.

  • Used to manage user data such as adding, viewing, updating, and deleting records.
  • Demonstrates how Servlet interacts with MySQL database using JDBC.
  • Commonly used in real-world web applications for data management.

Prerequisites

Steps to Impement Servlet - CRUD Operation

Step 1: Create Database (MySQL)

First, we create the database and table in MySQL and appuserdb is the database user table stores user detailsi and id is auto-incremented.

👁 Image

Step 2: Create Project (IntelliJ + Tomcat)

After that we set up our project, for this example am using Intellij IDE, 

  • Create a new project by selecting File( it will show you various options)
  • Select New, then project
  • Click on Java Enterprise and choose web application as your project Template
👁 Image

Setting Up Application server

  • Select the application and choose new
  • Select Tomcat base directory(The one saved in your working directory)

This is what the project structure will look like:

👁 Image

Step 3: Create Model Class (User.java)

Represents database table structure it is Used to transfer user data between layers

Step 4: Create Database Connection (UserDaoHandler.java)

Then we create a class that helps to perform CRUD operations on our database,

Step 5: Create servlet class (AddUser)

AddUser.java for adding users to the database.

Step 6: Create servlet class (UpdateUser)

This method gets the user id and matches it to the one in the database, then does an update on it.

Step 7: Create servlet class (PatchUser)

This method simply exchanges information on the database to the input parameters gotten from the web page and saves it into the database.

Step 8: Create servlet class (ViewUser)

This method gets all users from the database and displays it in a simple table format.

Step 9: Create servlet class (Delete)

The method below deletes the user from the database and sends the user view response, displaying the current users in the database.

Step 10: Run Your application

  • Start the Apache Tomcat server from IntelliJ IDEA.
  • Right-click the project and select Run to deploy the application on the server.
  • Open the browser and access the application using the Tomcat URL:

http://localhost:8080/appuser_war_exploded/

Output:

The Java servlet renders the Java server page anytime a user calls.

👁 Output

when user succesfully inserted the data and Record saved succesfully message shown on the browser screen.

👁 Output

When we want to add a user The browser show the page

👁 Output

Explanation: The application uses Servlets to handle user requests and JDBC to interact with the MySQL database. Each servlet performs a specific CRUD operation such as adding, viewing, updating, or deleting users. The DAO class manages all database queries, while the User model stores user data throughout the application flow.

Comment
Article Tags:
Article Tags: