Javatpoint Logo
Javatpoint Logo

Improving Servlet performance to fetch records from database

In this example, we are going to improve the performance of web application to fetch records from the database. To serve this, we are storing the data of the table in a collection, and reusing this collection in our servlet. So, we are not directly hitting the database again and again. By this, we are improving the performance.

To run this application, you need to create following table with some records.

Example to Improve the performance of servlet to fetch records from database

In this example, we have created 6 pages.

  1. index.html
  2. User.java
  3. MyListener.java
  4. MyServlet1.java
  5. MyServlet2.java
  6. web.xml
1) index.html

This html file contains two links that sends request to the servlet.


2) User.java

This is simple bean class containing 3 properties with its getters and setters. This class represents the table of the database.

3) MyListener.java

It is the listener class. When the project will be deployed, contextInitialized method of ServletContextListener is invoked by default. Here, we are getting the records of the table and storing it in the User class object which is added in the ArrayList class object. At last, all the records of the table will be stored in the ArrayList class object (collection). Finally, we are storing the ArrayList object in the ServletConext object as an attribute so that we can get it in the servlet and use it.

4) MyServlet1.java

This servlet gets the information from the servlet context object and prints it.

5) MyServlet2.java

It is same as MyServlet1. This servlet gets the information from the servlet context object and prints it.

6) web.xml

Here we are containing the information about servlets and listener.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA