欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Web------过滤器Filter

程序员文章站 2022-05-23 08:35:41
...

一、Filter简介

过滤器是一个程序,它先于与之相关的Servlet或JSP页面运行在服务器上。过滤器可附加到一个或多个Servlet或JSP页面上,并且可以检查进入这些支援的请求信息(WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、解决中文乱码问题、压缩响应信息等一些高级功能。)。
Servlet API中提供了一个Filter接口,开发web应用时,如果编写的Java类实现了这个接口,则把这个java类称之为过滤器Filter。他和用户及web资源的关系如下:
Java Web------过滤器Filter
演示:
Servlet: ServletDemo1

public class ServletDemo1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDemo1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("调用doGet方法");

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("调用doPost方法");
    }

}

Filter:FilterDemo01

public class FilterDemo01 implements Filter {

    /**
     * Default constructor. 
     */
    public FilterDemo01() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here

        // pass the request along the filter chain
        System.out.println("过滤前");
        chain.doFilter(request, response);
        System.out.println("过滤后");
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

在web. xml中配置过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Learning-JavaWeb</display-name>
  <filter>
    <filter-name>FilterDemo01</filter-name>
    <filter-class>servlet.FilterDemo01</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>FilterDemo01</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>servletDemo1</servlet-name>
    <servlet-class>servlet.ServletDemo1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servletDemo1</servlet-name>
    <url-pattern>/servletDemo1</url-pattern>
  </servlet-mapping>
</web-app>

在浏览器中输入:http://localhost:8080/Learning-JavaWeb/servletDemo1
Java Web------过滤器Filter

二、参考连接

【孤傲苍狼】:http://www.cnblogs.com/xdp-gacl/p/3948353.html