在web开发中,监听器不仅可以对Application监听,同时还可以对seesion和request对象进行监听;

该文章主要演示的是对request对象的创建和request属性的监听。

项目结构(红叉不需要关注,是maven环境的问题,不影响我们的主线)

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>gzipfilter</display-name>
  <listener>
    <listener-class>request.RequestListener</listener-class>
    <listener-class>request.RequestAttrListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>web.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/servlet/test</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

pom.xml

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

RequestAttrListener

public class RequestAttrListener implements ServletRequestAttributeListener{

    public void attributeAdded(ServletRequestAttributeEvent event) {
        System.out.println("request域添加了属性:" + event.getName() + "=" + event.getValue());
    }

    public void attributeRemoved(ServletRequestAttributeEvent event) {
        System.out.println("request域删除了属性:" + event.getName() + "=" + event.getValue());
    }

    public void attributeReplaced(ServletRequestAttributeEvent event) {
        System.out.println("request域修改了属性(这里展示的是被替换的):" + event.getName() + "=" + event.getValue());
    }

}

RequestListener

public class RequestListener implements ServletRequestListener{

    public void requestDestroyed(ServletRequestEvent even) {
        System.out.println("request对象销毁了 ......");
    }

    public void requestInitialized(ServletRequestEvent even) {
        System.out.println("request对象创建了 ......");
    }

}

TestServlet

public class TestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         request.setAttribute("a", "aaaa");
         request.setAttribute("a", "bbbb");
         request.removeAttribute("a");
         response.getOutputStream().print("over");
    }
}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
  <a href="servlet/test">点点</a>
</body>
</html>

访问测试:

  

http://localhost:8080/listener/

结果解析:

  

这三行是http://localhost:8080/listener/生成的request对象创建了 ......
request域修改了属性(这里展示的是被替换的):org.apache.catalina.ASYNC_SUPPORTED=true
request对象销毁了 ......

//以下是点击了index.jsp页面超链接生成的
request对象创建了 ......
request域修改了属性(这里展示的是被替换的):org.apache.catalina.ASYNC_SUPPORTED=true
request域添加了属性:a=aaaa
request域修改了属性(这里展示的是被替换的):a=aaaa
request域删除了属性:a=bbbb
request对象销毁了 ......

最新文章

  1. ios中@class和 #import区别
  2. vs2010下C++调用lib或dll文件
  3. protobuf-net 与 C#中几种序列化的比较
  4. 错误解决一_call time pass-by-reference removed
  5. 解决子元素用css float浮动后父级元素高度自适应高度
  6. Spring IOC容器分析(2) -- BeanDefinition
  7. 通过net time同步电脑时间
  8. 【学习】Linux Shell脚本实例之一
  9. Django REST framework基础:分页
  10. ryzen nvidia hackintosh
  11. 如何配置nginx负载均衡配置(轮询,权重,ip绑定)
  12. ecshop 添加后台页面以及设置权限
  13. 内网服务器通过Squid代理访问外网
  14. BERT的开源实现的使用
  15. u-boot的配置
  16. Linux查看文件大小命令
  17. C# WEB.API 多图上传
  18. 启动tomcat时为tomcat指定JDK
  19. STC12LE5620AD RAM问题
  20. Vue-cli 3.0 使用Sass Scss Less预处理器

热门文章

  1. MySQL简版(一)
  2. Linux下升级安装Python-3.6.9版本
  3. day02项目配置代码
  4. 【NOIP2016提高A组8.12】奇袭
  5. c++ printf和cout的性能
  6. FileUtils (从磁盘下载,从网络下载)
  7. 虚拟机安装Windows系统,再安装orcale
  8. tweenMax+如何让数字由初始值动画到结束的值
  9. 解决webstorm卡顿问题,下面详细设置方法,使得webstorm快速打开
  10. HTML中的表单&lt;form&gt;标签