Servlet中的ServletContextEvent和ServletContextListener

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)

MethodsDescription
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

删除示例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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值