Friday, 2 October 2015

Improving Servlet performance to fetch records from database

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.
  1. CREATE TABLE  "CSUSER"   
  2.    (    "USERID" NUMBER,   
  3.     "USERNAME" VARCHAR2(4000),   
  4.     "USERPASS" VARCHAR2(4000),   
  5.     "USEREMAIL" VARCHAR2(4000),   
  6.     "USERCOUNTRY" VARCHAR2(4000),   
  7.     "CONTACT" NUMBER,   
  8.      CONSTRAINT "CSUSER_PK" PRIMARY KEY ("USERID") ENABLE  
  9.    )  
  10. /  

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.
  1. <a href="servlet1">first servlet</a>|  
  2. <a href="servlet2">second servlet</a>  

2) User.java
This is simple bean class containing 3 properties with its getters and setters. This class represents the table of the database.
  1. public class User {  
  2. private int id;  
  3. private String name,password;  
  4.   
  5. public int getId() {  
  6.     return id;  
  7. }  
  8. public void setId(int id) {  
  9.     this.id = id;  
  10. }  
  11. public String getName() {  
  12.     return name;  
  13. }  
  14. public void setName(String name) {  
  15.     this.name = name;  
  16. }  
  17. public String getPassword() {  
  18.     return password;  
  19. }  
  20. public void setPassword(String password) {  
  21.     this.password = password;  
  22. }  
  23.   
  24.   
  25. }  
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.
  1. import javax.servlet.ServletContext;  
  2. import javax.servlet.ServletContextEvent;  
  3. import javax.servlet.ServletContextListener;  
  4. import java.sql.*;  
  5. import java.util.ArrayList;  
  6.   
  7. public class MyListener implements ServletContextListener{  
  8.   
  9.  public void contextInitialized(ServletContextEvent e) {  
  10.           
  11.   ArrayList list=new ArrayList();  
  12.    try{  
  13.     Class.forName("oracle.jdbc.driver.OracleDriver");  
  14.     Connection con=DriverManager.getConnection(  
  15.      "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  16.               
  17.     PreparedStatement ps=con.prepareStatement("select * from csuser");  
  18.     ResultSet rs=ps.executeQuery();  
  19.     while(rs.next()){  
  20.      User u=new User();  
  21.      u.setId(rs.getInt(1));  
  22.      u.setName(rs.getString(2));  
  23.      u.setPassword(rs.getString(3));  
  24.      list.add(u);  
  25.     }  
  26.     con.close();  
  27.               
  28.    }catch(Exception ex){System.out.print(ex);}  
  29.   
  30.    //storing the ArrayList object in ServletContext       
  31.    ServletContext context=e.getServletContext();  
  32.    context.setAttribute("data",list);  
  33.           
  34.  }  
  35.  public void contextDestroyed(ServletContextEvent arg0) {  
  36.     System.out.println("project undeployed...");  
  37.  }  
  38.   
  39. }  
4) MyServlet1.java
This servlet gets the information from the servlet context object and prints it.
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9.   
  10. import javax.servlet.ServletContext;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16.   
  17. public class MyServlet1 extends HttpServlet {  
  18.  public void doGet(HttpServletRequest request, HttpServletResponse  
  19.    response)throws ServletException, IOException {  
  20.   
  21.     response.setContentType("text/html");  
  22.     PrintWriter out = response.getWriter();  
  23.       
  24.     long before=System.currentTimeMillis();  
  25.           
  26.     ServletContext context=getServletContext();  
  27.     List list=(List)context.getAttribute("data");  
  28.           
  29.     Iterator itr=list.iterator();  
  30.     while(itr.hasNext()){  
  31.      User u=(User)itr.next();  
  32.      out.print("<br>"+u.getId()+" "+u.getName()+" "+u.getPassword());  
  33.     }  
  34.           
  35.     long after=System.currentTimeMillis();  
  36.     out.print("<br>total time :"+(after-before));  
  37.           
  38.     out.close();  
  39.     }  
  40.   
  41. }  
5) MyServlet2.java
It is same as MyServlet1. This servlet gets the information from the servlet context object and prints it.
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9.   
  10. import javax.servlet.ServletContext;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16.   
  17. public class MyServlet2 extends HttpServlet {  
  18.  public void doGet(HttpServletRequest request, HttpServletResponse  
  19.    response)throws ServletException, IOException {  
  20.   
  21.     response.setContentType("text/html");  
  22.     PrintWriter out = response.getWriter();  
  23.       
  24.     long before=System.currentTimeMillis();  
  25.           
  26.     ServletContext context=getServletContext();  
  27.     List list=(List)context.getAttribute("data");  
  28.           
  29.     Iterator itr=list.iterator();  
  30.     while(itr.hasNext()){  
  31.      User u=(User)itr.next();  
  32.      out.print("<br>"+u.getId()+" "+u.getName()+" "+u.getPassword());  
  33.     }  
  34.           
  35.     long after=System.currentTimeMillis();  
  36.     out.print("<br>total time :"+(after-before));  
  37.           
  38.     out.close();  
  39.     }  
  40.   
  41. }  
6) web.xml
Here we are containing the information about servlets and listener.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.    
  8.  <listener>  
  9.  <listener-class>MyListener</listener-class>  
  10.  </listener>  
  11.    
  12.  <servlet>  
  13.     <servlet-name>MyServlet1</servlet-name>  
  14.     <servlet-class>MyServlet1</servlet-class>  
  15.       
  16.   </servlet>  
  17.   <servlet>  
  18.     <servlet-name>MyServlet2</servlet-name>  
  19.     <servlet-class>MyServlet2</servlet-class>  
  20.       
  21.   </servlet>  
  22.     
  23.    <servlet-mapping>  
  24.     <servlet-name>MyServlet1</servlet-name>  
  25.     <url-pattern>/servlet1</url-pattern>  
  26.   </servlet-mapping>  
  27.   <servlet-mapping>  
  28.     <servlet-name>MyServlet2</servlet-name>  
  29.     <url-pattern>/servlet2</url-pattern>  
  30.   </servlet-mapping>  
  31.     
  32. </web-app>  

No comments:

Post a Comment