logo

Improving Servlet Performance To Fetch Records From Database


Show

We are going to refine the performance of web applications to fetch records from the database in this example. To Serve this we are putting down the data of the table in a collection and reprocessing this collection in our Servlet. So, we are not Directly striking the database again and again. We are enhancing the performance by this.

You need to make the table with the following records, to run this application.

CREATE TABLE  "CSUSER"   
   (    "USERID" NUMBER,   
    "USERNAME" VARCHAR2(4000),   
    "USERPASS" VARCHAR2(4000),   
    "USEREMAIL" VARCHAR2(4000),   
    "USERCOUNTRY" VARCHAR2(4000),   
    "CONTACT" NUMBER,   
     CONSTRAINT "CSUSER_PK" PRIMARY KEY ("USERID") ENABLE  
   )  
/  

Example of improving the performance of Servlet to fetch records from the database

In this following 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

In this html file there are two links that send requests to the servlet.

<a href="servlet1">first servlet</a>'  
<a href="servlet2">second servlet</a>  

2.User.java

Having 3 properties with its getters and setters, this is the simple bean class. This class simply represents the table of the database.

public class User {  
private int id;  
private String name,password;  
  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public String getPassword() {  
    return password;  
}  
public void setPassword(String password) {  
    this.password = password;  
}  
  
  
}  

3.MyListerner.java

It is the listener class. When the mission might be deployed, the contextInitialized technique of ServletContextListener is invoked by default. Here, we have become the records of the table and storing it withinside the User class object that's introduced withinside the ArrayList class object. At last, all of the information of the table might be saved withinside the ArrayList class object (collection). Finally, we're storing the ArrayList object withinside the ServletConext object as a characteristic in order that we are able to get it withinside the servlet and use it.

import javax.servlet.ServletContext;  
import javax.servlet.ServletContextEvent;  
import javax.servlet.ServletContextListener;  
import java.sql.*;  
import java.util.ArrayList;  
  
public class MyListener implements ServletContextListener{  
  
 public void contextInitialized(ServletContextEvent e) {  
          
  ArrayList list=new ArrayList();  
   try{  
    Class.forName("oracle.jdbc.driver.OracleDriver");  
    Connection con=DriverManager.getConnection(  
     "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
              
    PreparedStatement ps=con.prepareStatement("select * from csuser");  
    ResultSet rs=ps.executeQuery();  
    while(rs.next()){  
     User u=new User();  
     u.setId(rs.getInt(1));  
     u.setName(rs.getString(2));  
     u.setPassword(rs.getString(3));  
     list.add(u);  
    }  
    con.close();  
              
   }catch(Exception ex){System.out.print(ex);}  
  
   //storing the ArrayList object in ServletContext       
   ServletContext context=e.getServletContext();  
   context.setAttribute("data",list);  
          
 }  
 public void contextDestroyed(ServletContextEvent arg0) {  
    System.out.println("project undeployed...");  
 }  
  
}  

4.MyServlet1.java

Getting the Information from the Servlet Context objects and printing it is the Work of this Servlet.

import java.io.IOException;  
import java.io.PrintWriter;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.util.Iterator;  
import java.util.List;  
  
import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
  
public class MyServlet1 extends HttpServlet {  
 public void doGet(HttpServletRequest request, HttpServletResponse  
   response)throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
      
    long before=System.currentTimeMillis();  
          
    ServletContext context=getServletContext();  
    List list=(List)context.getAttribute("data");  
          
    Iterator itr=list.iterator();  
    while(itr.hasNext()){  
     User u=(User)itr.next();  
     out.print("<br>"+u.getId()+" "+u.getName()+" "+u.getPassword());  
    }  
          
    long after=System.currentTimeMillis();  
    out.print("<br>total time :"+(after-before));  
          
    out.close();  
    }  
  
}  

5.MyServlet2.java

It has the same work as MyServlet1.java has as this servlet gets the information from the ServletContext object and prints it.

import java.io.IOException;  
import java.io.PrintWriter;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.util.Iterator;  
import java.util.List;  
  
import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
  
public class MyServlet2 extends HttpServlet {  
 public void doGet(HttpServletRequest request, HttpServletResponse  
   response)throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
      
    long before=System.currentTimeMillis();  
          
    ServletContext context=getServletContext();  
    List list=(List)context.getAttribute("data");  
          
    Iterator itr=list.iterator();  
    while(itr.hasNext()){  
     User u=(User)itr.next();  
     out.print("<br>"+u.getId()+" "+u.getName()+" "+u.getPassword());  
    }  
          
    long after=System.currentTimeMillis();  
    out.print("<br>total time :"+(after-before));  
          
    out.close();  
    }  
  
}  

6.Web.xml

From here, we accommodate the information about Servlets and listeners.

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
   
 <listener>  
 <listener-class>MyListener</listener-class>  
 </listener>  
   
 <servlet>  
    <servlet-name>MyServlet1</servlet-name>  
    <servlet-class>MyServlet1</servlet-class>  
      
  </servlet>  
  <servlet>  
    <servlet-name>MyServlet2</servlet-name>  
    <servlet-class>MyServlet2</servlet-class>  
      
  </servlet>  
    
   <servlet-mapping>  
    <servlet-name>MyServlet1</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  
  <servlet-mapping>  
    <servlet-name>MyServlet2</servlet-name>  
    <url-pattern>/servlet2</url-pattern>  
  </servlet-mapping>  
    
</web-app>  

download this example (developed using Myeclipse IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)