JavaWeb:ServletRequestAttributeListener
2020-12-26 16:31
标签:set pom setattr VID listener scope enc mode 部署 域对象中属性的监听器,可以监听属性的设置、改变和移除。 当 ServletRequestAttributeListener 监听器被注册后,有事件发生后, 当有属性被添加的时候被调用。 当有属性被移除的时候被调用。 当有属性被替换的时候被调用。 将该项目部署到服务器并启动,在客户端访问 http://localhost:8080/19java-web-servlet-request-attribute-listener/servlet01 项目地址,然后关闭 tomcat。 JavaWeb:ServletRequestAttributeListener 标签:set pom setattr VID listener scope enc mode 部署 原文地址:https://www.cnblogs.com/jiangbo44/p/13034738.html环境
概述
一共有三个,这里以 ServletRequestAttributeListener 为例学习。
ServletRequestAttributeListener 说明
会返回一个 ServletRequestAttributeEvent 事件对象。attributeAdded
public void attributeAdded(ServletRequestAttributeEvent srae);
attributeRemoved
public void attributeRemoved(ServletRequestAttributeEvent srae);
attributeReplaced
public void attributeReplaced(ServletRequestAttributeEvent srae);
ServletRequestAttributeListener 示例
pom.xml
Servlet01
package jiangbo.javaweb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Servlet01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service...");
req.setAttribute("username", "将波");
req.setAttribute("username", "波将");
req.removeAttribute("username");
}
}
Listener01
package jiangbo.javaweb;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class Listener01 implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("新增了属性 " + srae.getName() + ":" + srae.getValue());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("删除了属性 " + srae.getName() + ":" + srae.getValue());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("替换了属性 " + srae.getName() + ":" + srae.getValue() + "--->"
+ srae.getServletRequest().getAttribute(srae.getName()));
}
}
配置 web.xml
浏览器访问
控制台输出
替换了属性 org.apache.catalina.ASYNC_SUPPORTED:true--->false
service...
新增了属性 username:将波
替换了属性 username:将波--->波将
删除了属性 username:波将
下一篇:jquery总结
文章标题:JavaWeb:ServletRequestAttributeListener
文章链接:http://soscw.com/index.php/essay/38393.html