ServletRequestAttributeListener的用法和实例

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");    
   }
}

更多请见:http://www.mark-to-win.com/index.html?content=Jsp/jspUrl.html&chapter=Jsp/jsp6_web.html#ServletRequestAttributeListener




 

ServletRequestAttributeListener的用法和实例ServletRequestAttributeListener的用法和实例 qq_44638460 发布了0 篇原创文章 · 获赞 0 · 访问量 431 私信 关注
上一篇:ServletContextAttributeListener的用法和实例


下一篇:spring aop中引介拦截器的用法