fbpx
Hero Illustration
1 Comments
Software Development

Step by Step: Making a Simple CRUD Application Using Java Servlet/JSP

What is CRUD?

CRUD is an acronym for CREATE, READ, UPDATE and DELETE which are basic functions of persistent storage. CRUD operations can use forms or an interface view to retrieve and return data from a database.

Procedures Function
CREATE This is a form view to create a new record in the database
READ Reads the table records based on the primary key within the input parameter.
UPDATE Updates the content of the table based on the specified primary key for a record.
DELETE Deletes a specified row in the table.

Create Application with CRUD Implementation

In this article we will create applications for personal records management with the following technologies:

  • Java Servlets and Java Server Pages (JSP)
  • JSP Standard Tag Library (JSTL)
  • Java Database Connectivity (JDBC)
  • MySQL database
  • Apache Tomcat Server

The following tools can be used for the development:

  1. Eclipse IDE for Java EE Developers (one of the newer versions is recommended)
  2. Apache Tomcat ver 8.5
  3. MySQL Community Server and MySQL Workbench (GUI Tool)
  4. MySQL Connector for Java

Let’s get started with the application, following the instructions below step by step:

1. Create Database

Execute the following MySQL script to create a database named “psmsdb” and a table named ”stuff” (of course, you can named your own database and table):

create database in CRUD Application using Java Servlet/JSP

2. Create Project with Maven

In Eclipse IDE, click File > New > Other and Maven Project. Name the project “PSMS”

Create Java Dynamic Web Project

In the Maven POM dialog, fill Group ID , Artifact ID and Description as shown below:

Create Java Dynamic Web Project

Then add the following dependencies to the pom.xml file:

Create Java Dynamic Web Project

3. Writing Model Class

Next, create a model package. We will use the package name com.mitrais.psms.model. Then we can create a Java class named Stuff.java to model a stuff entity in the database with the following code:

Stuff Java Image

4. Writing DAO Class

  • Create Datasource Class
    datasource class
  • Create a datasource properties file in folder resources:
    Datasources Properties File Image
  • Create DAO Interface
    DAO Interface Image
  • Create new interface class to implement DAO Interface
    Interface Class to Implement DAO Interface

We now need to implement a DAO (Data Access Object) class that provides CRUD (Create, Read, Update, Delete) operations for the table stuff in the database. Here’s the source code of the DAOStuff class:

Source code of the DaoStuff class

Source Code of the DaoStuff class 2

5. Create View/Form file

Create folders for css (in which to save all css/style files) and jsp (in which to save all jsp files) in WebContent folder :

View/Form File Image

CSS File

Create the file style.css in folder css

Style.css Image

JSP File

Create a page with JSP for displaying all information from the database. The following is code of the JSP file under the WebContentdirectory in the project:

Create a head.jsp file in folder jsp

Head JSP File Image

Create a file menu.jsp in folder jsp

Create a file StuffForm.jsp in folder jsp

Create a file StuffList.jsp in folder jsp

StuffList.JSP Image

StuffList.JSP File Image

6. Creating Controller Servlet Class

Now, we can implement a Java Servlet that acts as a page controller to handle all requests from the client. Let’s look at the code first:

Controller Servlet Class Image 1 controller servlet class 2

7. Deploying and Testing the Application

After we have completed the project code we need to deploy and test the application to check that it works.

Right click on project -> Run As -> Run On Server

Run Project Server Image

In the pop-up dialog choose Tomcat and then click finish

You can type the following url in the web browser to access the Personal Stuff Management Systems Application:

http://localhost:8080/PSMS

For the first time, the list is empty because there isn’t any information contained there yet:

Personal Stuff Management System Image

Click on the hyperlink Add New Stuff to begin adding a new stuff:

Add New Stuff Image

Enter stuff information (Name, Description, Quantity and Location) and click Save. The page will redirect to the list pages, as shown below:

Stuff Information Image

In this page you will see all items in database, and you can click on the link Edit and Delete to update and remove items.

References:

https://en.wikipedia.org/wiki/Create,_read,_update_and_delete

https://www.javatpoint.com/jsp-tutorial

https://www.javatpoint.com/crud-in-jsp

Contact us to learn more!

Please complete the brief information below and we will follow up shortly.

    ** All fields are required
    Leave a Reply to Asar Theen Cancel


    Asar Theen
    4 years ago

    how can i integrate to my admin portal

    Reply