ServletContextEvent class gives notifications about changes to the servlet context of a web application. ServletContextListener receives the notifications about changes to the servlet context and perform some action. ServletContextListener is used to perform important task at the time when context is initialized and destroyed. In short, ServletContextEvent and ServletContextListener works in pair, whenever Servlet COntext changes, ServletContextEvent publishes a notification which is received by ServletContextListener and then, based on that certain tasks are performed by it.
ServletContextEvent类提供有关Web应用程序Servlet上下文更改的通知。 ServletContextListener接收有关Servlet上下文更改的通知,并执行一些操作。 ServletContextListener用于在初始化和销毁上下文时执行重要任务。 简而言之,每当Servlet COntext更改时,ServletContextEvent和ServletContextListener便会成对工作,ServletContextEvent发布一个通知,该通知由ServletContextListener接收,然后基于它执行的某些任务。
ServletContextListener接口的方法 (Methods of ServletContextListener Interface)
Methods | Description |
---|---|
void contextDestroyed(ServletContextEvent e) | is invoked when the application is destroyed. |
void contextInitialized(ServletContextEvent e) | is invoked when the application is initialized. |
方法 | 描述 |
---|---|
void contextDestroyed(ServletContextEvent e) | 销毁应用程序时调用。 |
void contextInitialized(ServletContextEvent e) | 在初始化应用程序时调用。 |
制作和使用上下文监听器 (Making and Using a context listener)
Context listener is not a servlet or JSP, it's a class that implements ServletContextListener interface and provides definition of contextDestroyed()
and contextInitialized()
.
上下文侦听器不是servlet或JSP,它是一个实现ServletContextListener接口并提供contextDestroyed()
和contextInitialized()
定义的类。
![Using a ServletContextListener](https://i-blog.csdnimg.cn/blog_migrate/cd44350949c8d16fe281ae1582db7809.png)
删除示例ServletContextListener的用法 (Example demontrating usage of ServletContextListener)
index.html
index.html
<a href="Counter">Total Page views</a>
web.xml
web.xml
![Using a ServletContextListener in web xml](https://i-blog.csdnimg.cn/blog_migrate/e2387aea03c618282259057ac87728b5.jpeg)
For this example we will have to create a table named counter with a column named pageview to save the number of pageviews.
在此示例中,我们将创建一个名为counter的表,该表具有一个名为pageview的列以保存页面浏览量。
MyListener.java
MyListener.java
import java.sql.*;
import javax.servlet.*;
public class MyListener implements ServletContextListener
{
ServletContext ctx;
Connection con;
Statement s;
PreparedStatement ps;
ResultSet rs;
int count;
public void contextInitialized(ServletContextEvent sce) {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","user","password");
s = con.createStatement();
// fetching pageviews value from table counter
rs = s.executeQuery("select pageview from counter");
while(rs.next()) {
count = rs.getInt(1);
}
ctx = sce.getServletContext();
ctx.setAttribute("pcount", count);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce) {
try
{
ctx = sce.getServletContext();
count = (Integer)ctx.getAttribute("pcount");
ps = con.prepareStatement("update counter set pcount='"+count+"'");
ps.executeUpdate();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Counter.java
Counter.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Counter extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext ctx = getServletContext();
Integer count = (Integer)ctx.getAttribute("pcount");
out.println(count+": pageview");
ctx.setAttribute("pcount", ++count);
}
}
翻译自: https://www.studytonight.com/servlet/servlet-context-listener.php