提高 Servlet 性能以从数据库中获取记录
在这里,我们将提高 Web 应用程序从数据库中检索记录的能力。为此,我们将表的数据保存在一个集合中,并在我们的 servlet 中重用该集合。因此,我们不会重复查询数据库。因此,我们正在提高我们的绩效。为了开始,我们必须首先生成下表:
桌子:
CREATE TABLE gfguser
(
userid INT,
username VARCHAR(50),
userpass VARCHAR(50),
useremail VARCHAR(100),
usercountry VARCHAR(50),
contact INT,
PRIMARY KEY (USERID)
)
现在,在表中,添加以下记录。
实现:在这种情况下,我们生成了以下六个资源:
- 索引.html
- 用户。Java
- MainListenerGfg。Java
- 小服务程序1。Java
- servlet2。Java
- web.xml
index.html 文件中的两个链接将请求传输到 servlet。用户。 Java是一个简单的 bean 类,具有三个属性及其关联的 getter 和 setter。数据库表由此类表示。侦听器类是 MainListenerGfg。Java部署项目时默认调用ServletContextListener的contextInitialized方法。我们正在检索表的记录并将它们保存在 User 类对象中,该对象已添加到 ArrayList 类对象中。最后,ArrayList 类对象将用于保存表的所有记录(集合)。
最后,我们将 ArrayList 对象作为属性保存在 ServletContext 对象中,以便我们可以在 servlet 中使用它。小服务程序1。 Java从 servlet 上下文对象中检索和输出信息。小服务程序2。 Java与 Servlet1 相同。Java来自 servlet 上下文对象的信息由此 servlet 打印。有关 servlet 和侦听器的信息可以在 web.xml 中找到。
A. index.html
Java
GeeksForGeeks
This is first servlet| //first link
This is second servlet //second link
Java
// Java Program to Illustrate User Class
// Class
public class User {
// Declaring variables to
// access data from database
private int id;
private String name, password;
// Adding getters and setter for variables
// Getters and Setters
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;
}
}
Java
// Java Program to Illustrate MainListener Class
// Importing required classes
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
// Class
// implementing ServletContextListener interface
public class MainListenerGfg
implements ServletContextListener {
// Method 1
public void contextInitialized(ServletContextEvent e)
{
// Creating an empty ArrayList
ArrayList list = new ArrayList();
// Try block to check for exceptions
try {
// Loading drivers by
// connecting with database
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/geeksforgeeks",
"root", "root");
PreparedStatement ps = con.prepareStatement(
"select * from gfguser");
ResultSet rs = ps.executeQuery();
// Accessing all data until its empty
// using next() method
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);
}
// Closing the connections
// using close() method
con.close();
}
// Catch block to handle exceptions
catch (Exception ex) {
// Displaying the exceptions
System.out.print(ex);
}
// Storing the ArrayList object in ServletContext
ServletContext context = e.getServletContext();
context.setAttribute("data", list);
}
// Method 2
public void contextDestroyed(ServletContextEvent arg0)
{
// Print statement whenever this method is called
System.out.println("project undeployed...");
}
}
Java
// Java Program to Illustrate Servlet Class
// Importing required classes
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;
// Class
// Extending HttpServlet Class
public class Servlet1 extends HttpServlet {
// Method
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();
// Condition holds true till there is single element
// remaining in the List
while (itr.hasNext()) {
User u = (User)itr.next();
out.print("
" + u.getId() + " " + u.getName()
+ " " + u.getPassword());
}
// Closing the connections
// using close() method
out.close();
}
}
Java
// Java Program to Illustrate Servlet Class
// Importing required classes
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;
// Class
// Extending HttpServlet class
public class Servlet2 extends HttpServlet {
// Method
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
Iterator itr = list.iterator();
// Condition holds true till
// there is single element remaining
while (itr.hasNext()) {
User u = (User)itr.next();
out.print("
" + u.getId() + " " + u.getName()
+ " " + u.getPassword());
}
// Closing the connections
// using close() method
out.close();
}
}
XML
MainListenerGfg
Servlet1
Servlet1
Servlet2
Servlet2
Servlet1
/servlet1
Servlet2
/servlet2
B. 用户。Java
Java
// Java Program to Illustrate User Class
// Class
public class User {
// Declaring variables to
// access data from database
private int id;
private String name, password;
// Adding getters and setter for variables
// Getters and Setters
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;
}
}
C. MainListenerGfg。Java
Java
// Java Program to Illustrate MainListener Class
// Importing required classes
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
// Class
// implementing ServletContextListener interface
public class MainListenerGfg
implements ServletContextListener {
// Method 1
public void contextInitialized(ServletContextEvent e)
{
// Creating an empty ArrayList
ArrayList list = new ArrayList();
// Try block to check for exceptions
try {
// Loading drivers by
// connecting with database
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/geeksforgeeks",
"root", "root");
PreparedStatement ps = con.prepareStatement(
"select * from gfguser");
ResultSet rs = ps.executeQuery();
// Accessing all data until its empty
// using next() method
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);
}
// Closing the connections
// using close() method
con.close();
}
// Catch block to handle exceptions
catch (Exception ex) {
// Displaying the exceptions
System.out.print(ex);
}
// Storing the ArrayList object in ServletContext
ServletContext context = e.getServletContext();
context.setAttribute("data", list);
}
// Method 2
public void contextDestroyed(ServletContextEvent arg0)
{
// Print statement whenever this method is called
System.out.println("project undeployed...");
}
}
D.Servlet1。Java
Java
// Java Program to Illustrate Servlet Class
// Importing required classes
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;
// Class
// Extending HttpServlet Class
public class Servlet1 extends HttpServlet {
// Method
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();
// Condition holds true till there is single element
// remaining in the List
while (itr.hasNext()) {
User u = (User)itr.next();
out.print("
" + u.getId() + " " + u.getName()
+ " " + u.getPassword());
}
// Closing the connections
// using close() method
out.close();
}
}
E. Servlet2。Java
Java
// Java Program to Illustrate Servlet Class
// Importing required classes
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;
// Class
// Extending HttpServlet class
public class Servlet2 extends HttpServlet {
// Method
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
Iterator itr = list.iterator();
// Condition holds true till
// there is single element remaining
while (itr.hasNext()) {
User u = (User)itr.next();
out.print("
" + u.getId() + " " + u.getName()
+ " " + u.getPassword());
}
// Closing the connections
// using close() method
out.close();
}
}
F.web.xml
XML
MainListenerGfg
Servlet1
Servlet1
Servlet2
Servlet2
Servlet1
/servlet1
Servlet2
/servlet2
输出:
为了接收以下输出,运行 index.html 文件:
如果您单击“This is first servlet”链接,我们将得到以下结果:
当您单击“This is second servlet”链接时,将出现以下输出: