监听器 ServletRequestAttributeListener&ServletRequestListener详解
2024-08-31 14:42:39
在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对象销毁了 ......
最新文章
- ios中@class和 #import区别
- vs2010下C++调用lib或dll文件
- protobuf-net 与 C#中几种序列化的比较
- 错误解决一_call time pass-by-reference removed
- 解决子元素用css float浮动后父级元素高度自适应高度
- Spring IOC容器分析(2) -- BeanDefinition
- 通过net time同步电脑时间
- 【学习】Linux Shell脚本实例之一
- Django REST framework基础:分页
- ryzen nvidia hackintosh
- 如何配置nginx负载均衡配置(轮询,权重,ip绑定)
- ecshop 添加后台页面以及设置权限
- 内网服务器通过Squid代理访问外网
- BERT的开源实现的使用
- u-boot的配置
- Linux查看文件大小命令
- C# WEB.API 多图上传
- 启动tomcat时为tomcat指定JDK
- STC12LE5620AD RAM问题
- Vue-cli 3.0 使用Sass Scss Less预处理器