📜  提高Servlet性能以从数据库获取记录

📅  最后修改于: 2021-01-09 12:59:01             🧑  作者: Mango

改善Servlet性能以从数据库中获取记录

在此示例中,我们将提高Web应用程序从数据库中获取记录的性能。为此,我们将表的数据存储在一个集合中,然后在我们的servlet中重用该集合。因此,我们不会直接一次又一次地访问数据库。这样,我们正在改善性能。

要运行此应用程序,您需要创建带有一些记录的下表。

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
   )
/

改善Servlet从数据库获取记录的性能的示例

在此示例中,我们创建了6个页面。

  • index.html
  • User.java
  • MyListener.java
  • MyServlet1.java
  • MyServlet2.java
  • web.xml

该html文件包含两个将请求发送到servlet的链接。

这是简单的bean类,包含3个具有其getter和setter的属性。此类表示数据库表。


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;
}


}

这是侦听器类。部署项目时,默认情况下会调用ServletContextListener的contextInitialized方法。在这里,我们获取表的记录并将其存储在添加到ArrayList类对象中的User类对象中。最后,表的所有记录将存储在ArrayList类对象(集合)中。最后,我们将ArrayList对象作为属性存储在ServletConext对象中,以便我们可以在Servlet中获取它并使用它。


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...");
 }

}

该Servlet从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("
"+u.getId()+" "+u.getName()+" "+u.getPassword()); } long after=System.currentTimeMillis(); out.print("
total time :"+(after-before)); out.close(); } }

与MyServlet1相同。该Servlet从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 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("
"+u.getId()+" "+u.getName()+" "+u.getPassword()); } long after=System.currentTimeMillis(); out.print("
total time :"+(after-before)); out.close(); } }

在这里,我们包含有关servlet和侦听器的信息。



 
 
 MyListener
 
 
 
    MyServlet1
    MyServlet1
    
  
  
    MyServlet2
    MyServlet2
    
  
  
   
    MyServlet1
    /servlet1
  
  
    MyServlet2
    /servlet2