改善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个页面。
  1. index.html
  2. User.java
  3. MyListener.java
  4. MyServlet1.java
  5. MyServlet2.java
  6. web.xml
1)index.html
该html文件包含两个将请求发送到servlet的链接。
< a href="http://www.srcmini.com/servlet1"> first servlet< /a> | < a href="http://www.srcmini.com/servlet2"> second servlet< /a>

2) User.java
这是简单的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; }}

3) MyListener.java
【改善Servlet性能(从数据库中获取记录)】这是侦听器类。部署项目时, 默认情况下会调用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..."); }}

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

5) MyServlet2.java
与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("< br> "+u.getId()+" "+u.getName()+" "+u.getPassword()); } long after=System.currentTimeMillis(); out.print("< br> total time :"+(after-before)); out.close(); }}

6)web.xml
在这里, 我们包含有关servlet和侦听器的信息。
< ?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>

下载此示例(使用Myeclipse IDE开发)
下载此示例(使用Eclipse IDE开发)
下载此示例(使用Netbeans IDE开发)

    推荐阅读