Java学习笔记之:Java Servlet 过滤器配置

一、步骤

1.创建一类实现Filter接口,重写父类的方法

public class SimpleFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        // TODO Auto-generated method stub
        
    }

    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        
    }

}

2.配置web.xml文件,

<!-- filter配置 -->
<filter>
<filter-name>filter</filter-name>
<filter-class>com.mxp.filter.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<!-- <url-pattern>*.do</url-pattern> --><!-- url的配置方式 -->
<url-pattern>/*</url-pattern><!-- 通配过滤的是所有url -->
</filter-mapping>

 3.在dofilter()方法中处理可以放行的请求

二、过滤器的生命周期

 1.服务器启动的时候执行初始化方法,它的初始化方法在servlet的init()方法之前执行

 2.在用户每一次发起url请求的时候会执行dofilter()方法,在doget()和dopost()方法之前执行.需要注意的是filter过滤的是url,而servlet的doget和dopost方法处理的是request请求

 3.在服务器销毁的时候执行destroy()方法

三、实例

1.index.jsp添加一个超链接

<a href="login.do">测试</a> <br>

2.新建servlet文件,用来处理超链接的请求

public class UserServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub

        System.out.println("servletinit");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("servletdo");
        req.getRequestDispatcher("main.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

3.添加过滤器

public class UserFilte implements Filter {

    // 销毁
    public void destroy() {
        System.out.println("destroy");
    }

    // 过滤
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        System.out.println("UserFilte do");
        arg2.doFilter(arg0, arg1);//相当于放行
    }

    // 初始化
    public void init(FilterConfig arg0) throws ServletException {

        System.out.println("UserFilteinit");
    }

}

4.web.xml配置

    <servlet>
        <servlet-name>USER</servlet-name>
        <servlet-class>com.hgd.servlet.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>USER</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>filter</filter-name>
        <filter-class>com.hgd.filter.UserFilte</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
    

5.部署并打开主页,在控制台可以看到当服务器运行过滤器已经初始化了

6.点击“测试”超链接,从控制台输出可以看出只有当过滤器请求通过了才会执行servlet中的dopost或doget方法

 

 

posted @ 2016-10-25 11:02  HuTiger  阅读(880)  评论(0编辑  收藏  举报