码迷,mamicode.com
首页 > 其他好文 > 详细

监听器 ServletRequestAttributeListener&ServletRequestListener详解

时间:2017-12-29 23:38:20      阅读:563      评论:0      收藏:0      [点我收藏+]

标签:cti   map   getc   servlet   body   path   详解   mapping   src   

在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对象销毁了 ......

 

监听器 ServletRequestAttributeListener&ServletRequestListener详解

标签:cti   map   getc   servlet   body   path   详解   mapping   src   

原文地址:https://www.cnblogs.com/weishao-lsv/p/8146489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!