• web.xml中自定义Listener


    Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应。 常用的Web事件的监听接口如下:

    ServletContextListener:用于监听Web的启动及关闭 ServletContextAttributeListener:

    用于监听ServletContext范围内属性的改变 ServletRequestListener:

    用于监听用户请求 ServletRequestAttributeListener:

    用于监听ServletRequest范围属性的改变 HttpSessionListener:

    用于监听用户session的开始及结束 HttpSessionAttributeListener:

    用于监听HttpSession范围内的属性改变:

    栗子:参考https://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example#servlet-listener-example

    import java.sql.Connection;

    public class DBConnectionManager {

    private String dbURL;
    private String user;
    private String password;
    private Connection con;

    public DBConnectionManager(String url, String u, String p){
    this.dbURL=url;
    this.user=u;
    this.password=p;
    //create db connection now

    }

    public Connection getConnection(){
    return this.con;
    }

    public void closeConnection(){
    //close DB connection here
    }
    }

    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    import javax.servlet.annotation.WebListener;

    @WebListener
    public class AppContextAttributeListener implements ServletContextAttributeListener {

    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
    System.out.println("ServletContext attribute added::{"+servletContextAttributeEvent.getName()+","+servletContextAttributeEvent.getValue()+"}");
    }

    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
    System.out.println("ServletContext attribute replaced::{"+servletContextAttributeEvent.getName()+","+servletContextAttributeEvent.getValue()+"}");
    }

    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
    System.out.println("ServletContext attribute removed::{"+servletContextAttributeEvent.getName()+","+servletContextAttributeEvent.getValue()+"}");
    }

    }

    @WebListener
    public class AppContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext ctx = servletContextEvent.getServletContext();

    String url = ctx.getInitParameter("DBURL");
    String u = ctx.getInitParameter("DBUSER");
    String p = ctx.getInitParameter("DBPWD");

    //create database connection from init parameters and set it to context
    DBConnectionManager dbManager = new DBConnectionManager(url, u, p);
    ctx.setAttribute("DBManager", dbManager);
    System.out.println("Database connection initialized for Application.");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    ServletContext ctx = servletContextEvent.getServletContext();
    DBConnectionManager dbManager = (DBConnectionManager) ctx.getAttribute("DBManager");
    dbManager.closeConnection();
    System.out.println("Database connection closed for Application.");

    }

    }

    import javax.servlet.ServletRequest;
    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.annotation.WebListener;

    @WebListener
    public class MyServletRequestListener implements ServletRequestListener {

    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
    ServletRequest servletRequest = servletRequestEvent.getServletRequest();
    System.out.println("ServletRequest destroyed. Remote IP="+servletRequest.getRemoteAddr());
    }

    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
    ServletRequest servletRequest = servletRequestEvent.getServletRequest();
    System.out.println("ServletRequest initialized. Remote IP="+servletRequest.getRemoteAddr());
    }

    }


    @WebListener
    public class MySessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent sessionEvent) {
    System.out.println("Session Created:: ID="+sessionEvent.getSession().getId());
    }

    public void sessionDestroyed(HttpSessionEvent sessionEvent) {
    System.out.println("Session Destroyed:: ID="+sessionEvent.getSession().getId());
    }

    }

    @WebServlet("/MyServlet")
    public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("begin to handle request....");
    ServletContext ctx = request.getServletContext();
    ctx.setAttribute("User", "Pankaj");
    String user = (String) ctx.getAttribute("User");
    ctx.removeAttribute("User");

    HttpSession session = request.getSession();
    session.invalidate();

    PrintWriter out = response.getWriter();
    out.write("Hi "+user);
    }

    }

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>ServletListenerExample</display-name>

    <context-param>
    <param-name>DBUSER</param-name>
    <param-value>pankaj</param-value>
    </context-param>
    <context-param>
    <param-name>DBPWD</param-name>
    <param-value>password</param-value>
    </context-param>
    <context-param>
    <param-name>DBURL</param-name>
    <param-value>jdbc:mysql://localhost/mysql_db</param-value>
    </context-param>

    <listener>
    <listener-class>com.journaldev.listener.AppContextListener</listener-class>
    </listener>
    <listener>
    <listener-class>com.journaldev.listener.AppContextAttributeListener</listener-class>
    </listener>
    <listener>
    <listener-class>com.journaldev.listener.MySessionListener</listener-class>
    </listener>
    <listener>
    <listener-class>com.journaldev.listener.MyServletRequestListener</listener-class>
    </listener>
    </web-app>

    启动时:

    ServletContext attribute added::{DBManager,com.journaldev.db.DBConnectionManager@18140878}
    Database connection initialized for Application.
    ServletContext attribute added::{org.apache.jasper.compiler.TldLocationsCache,org.apache.jasper.compiler.TldLocationsCache@45cc5809}

    浏览器输入:http://localhost:8084/ServletListenerExample/MyServlet

    输出:

    ServletRequest initialized. Remote IP=127.0.0.1
    begin to handle request....
    ServletContext attribute added::{User,Pankaj}
    ServletContext attribute removed::{User,Pankaj}
    Session Created:: ID=349003AB937F7C54BCCB4DE4506F5958
    Session Destroyed:: ID=349003AB937F7C54BCCB4DE4506F5958
    ServletRequest destroyed. Remote IP=127.0.0.1

    关闭时:Database connection closed for Application.

  • 相关阅读:
    Linux同一机器设置多个IP2019-7-6
    使用Apache服务部署静态网站2019-7-5
    系统状态检测命令2019-7-5
    简单的shell脚本
    常用的系统工作命令2019-7-4
    Lnmp架构部署动态网站环境.2019-7-3-1.4
    Lnmp架构部署动态网站环境.2019-7-3-1.3
    Linux安装ftp服务-详细步骤
    循环删除List集合的元素
    反射-父类获取子类属性并赋值
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8944977.html
Copyright © 2020-2023  润新知