6)ServletRequestAttributeListener:
ServletRequestAttributeListener能监测到有人正在往ServletRequest里添加属性。你可以采取相应的措施。
例 2.2.6.1
package com;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("增加了" + arg0.getName() + " " +
arg0.getValue() );
}
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("去除了" + arg0.getName() + " " +
arg0.getValue() );
}
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("取代了" + arg0.getName() + " " +
arg0.getValue()+"现在的新值是"+arg0.getServletRequest().getAttribute(arg0.getName()) );
}
}
web.xml加入下面这段话:
<listener>
<listener-class>com.MyServletRequestAttributeListener</listener-class>
</listener>
用下面这个servlet测试:
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("add attribute");
request.setAttribute("name", "马克-to-win");
System.out.println("replace attribute");
request.setAttribute("name", "mark-to-win");
System.out.println("remove attribute");
request.removeAttribute("name");
}
}
相关文章
- 03-14bs4和xpath的用法
- 03-14扩展Mongoose Model的静态方法和实例方法
- 03-14.net Strings.Split静态方法和mystring.Split 实例方法在处理空字符串时的不同
- 03-14MFC基础--CString的Tokenize()和_tcstok()的用法对比
- 03-14C++ mem_fun 和 mem_fun_ref 的用法
- 03-14关于Unity中的刚体和碰撞器的相关用法(二)
- 03-14C++ sizeof操作符的用法和strlen函数的区别
- 03-14vuex的属性和基本用法
- 03-14vuex的用法和一些容易被忽略的地方
- 03-14Python3中reduce和lambda的用法