package com.example; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServletContextType */ @WebServlet("/TestServletContextType") public class TestServletContextType extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestServletContextType() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("a","b"); request.setAttribute("a", "c"); request.removeAttribute("a"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
监听者类:
package com.example; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; public class MyservletAttributeListen implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent ev) { System.out.println("A:"+ev.getName()+"->"+ev.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent ev) { System.out.println("M:"+ev.getName()+"->"+ev.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent ev) { System.out.println("P:"+ev.getName()+"->"+ev.getValue()); } }
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>FirstListener</display-name> <servlet> <servlet-name>ListenerTest</servlet-name> <servlet-class>com.example.ListenerTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListenerTest</servlet-name> <url-pattern>/ListenTest.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>TestServletContextType</servlet-name> <servlet-class>com.example.TestServletContextType</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServletContextType</servlet-name> <url-pattern>/testtype.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>TestServletContextType2</servlet-name> <servlet-class>com.example.TestServletContextType2</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServletContextType2</servlet-name> <url-pattern>/testtype1.do</url-pattern> </servlet-mapping> <context-param> <param-name>breed</param-name> <param-value>Greet Dane</param-value> </context-param> <listener> <listener-class>com.example.MyservletAttributeListen</listener-class> </listener> </web-app>
测试结果:
注:如果属性被替换,getValue方法返回的是原来的属性。
转载于:https://www.cnblogs.com/zoeyqq/p/6861211.html