监听器的使用,以及实现, 测试

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>MyFilter</display-name>
  
    <filter>
        <filter-name>Myfilter</filter-name>
        <filter-class>com.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Myfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
          <servlet-name>Test</servlet-name>
          <servlet-class>com.Test</servlet-class>
    </servlet>
    <servlet-mapping>
          <servlet-name>Test</servlet-name>
          <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

 

 1 //这是我的自定义监听
 2 import java.io.IOException;
 3 
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 
12 public class MyFilter implements Filter{
13 
14     @Override
15     public void destroy() {
16         
17     }
18 
19     @Override
20     public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
21         
22         HttpServletRequest request =( HttpServletRequest)arg0;
23         
24         
25         System.out.println("----->"+request.getRequestURI());
26         
27         if("/MyFilter/test".equals(request.getRequestURI())){
28             arg2.doFilter(arg0, arg1);
29         }
30     }
31 
32     @Override
33     public void init(FilterConfig arg0) throws ServletException {
34         
35     }
36 }
37 
38 
39 import javax.servlet.ServletRequest;
40 import javax.servlet.ServletResponse;
41 import javax.servlet.http.HttpServlet;
42 
43 public class Test extends HttpServlet{
44 
45     private static final long serialVersionUID = 1L;
46 
47     @Override
48     public void service(ServletRequest arg0, ServletResponse arg1){
49         System.out.println("我是MyFilter类的测试标识");
50         
51     }
52 }

主要是针对WEB开发的3大包装类的监听

posted @ 2014-05-19 17:13  ζ  简单ヾ°  阅读(580)  评论(0编辑  收藏  举报