How to get data from database to servlet jdbc JSP

Here we will get data from the database to servlet JDBC and show data on the JSP page using Jakarta Server Pages and the JSTL library.

Technology used

  1. apache-tomcat-10.1.1
  2. JavaSE-17
  3. eclipse IDE for Enterprise Java and Web Developers

Steps to develop programs

  1. Create MySql Table and insert data
  2. Create A Dynamic Web Project
  3. Create package structure
  4. Create java files
  5. create jsp files

1 Create MySQL Table

Create a MySql table and insert a few data that will be retrieved from the database using our servlet program.

The script for creating the table is as below. Insert data as per need.

Php MyAdmin Student Table data
Fig: Php MyAdmin Student Table data

Creating Java Dynamic Web Project

Open Eclipse IDE and follow the below steps

  1. Click on File->New-> Dynamic Web Project.
  2. Give project Name
  3. Select Target runtime (here apache-tomcat-10.1.1)
  4. then next again next then finish.

This will create a new project.

Creating packages and files

  1. Create packages com.ebhor.controller, com.ebhor.dao and com.ebhor.model inside src/main/java
  2. Create servlet inside com.ebhor.controller
  3. Create ConnectionFactory and inside com.ebhor.dao
  4. Create inside com.ebhor.model
  5. Create a index.jsp and students.jsp inside webapp folder.

Project Explorer

External Jar

The following External jars are included in the apache tomcat lib folder

  1. mysql-connector-java-8.0.30
  2. jakarta.servlet.jsp.jstl-3.0.0
  3. jakarta.servlet.jsp.jstl-api-3.0.0

Servlet class to get data from DAO (

  1. Create a Dao object and access fetchAll(). This will return a List of students.
  2. Set the student list as an attribute in the request scope.
  3. Send the request and response to students.jsp using RequestDispatcher.

Student Model class

Create a student model class to hold values of student objects and provide a getter, setter, constructor, and toString method.

Connection with MySql and Getting Data from MySQL

Instead of specifying a database connection to each JDBC call a class is created to return the Connection object.

To access student’s data from MySQL table JDBC Prepared statement is used.

Steps to read data from MySQL

  1. Get the connection object
  2. Prepare the PreparedStatement
  3. Execute the query and get ResultSet
  4. Iterate ResultSet object
  5. Prepare student objects and assign to student ArrayList.
  6. Close the connection
  7. Return students

Index Page (index.jsp)

This page contains a link called Servlet.

List of students (students.jsp)

After fetching data from the database servlet send a request response to this page.

This page iterates request scope object students using c:forEach. JSTL core tag library is used here.


How To Get Data From Database To Servlet Jdbc
How To Get Data From Database To Servlet Jdbc
How To Get Data From Database To Servlet Jdbc
How To Get Data From Database To Servlet Jdbc
Q: jakarta.servlet.ServletException: java.lang.NoClassDefFoundError: jakarta/servlet/jsp/jstl/core/LoopTag

Along with jakarta.servlet.jsp.jstl-3.0.0 include jakarta.servlet.jsp.jstl-api-3.0.0

Q: org.apache.jasper.JasperException: The absolute uri: [] cannot be resolved in either web.xml or the jar files deployed with this application

Answer: Include jakarta.servlet.jsp.jstl-3.0.0 and supporting jar jakarta.servlet.jsp.jstl-api-3.0.0 to work it correctly.

Read More

Servlet url and class mapping using web.xml

Servlet Annotation WebServlet Example

ServletConfig to access initial parameter value

Servlet Maven Configuration Example

ServletContext getting parameter

Getting parameter values in Servlet getParameterNames

ServletContext getting multiple parameters

ServletConfig To Access Multiple Initial Parameter Value

getting all request parameters in servlet

Request dispatcher in Servlet User Login Example

Sending data to servlet using http get method