Written using Servlet Filter

The use of the filter 1.servlet

Before accessing the back-end resources to the client's request, to intercept these requests.

Before being sent back to the client in the server's response, process the response.

2.Servlet filter method

(1)public void doFilter (ServletRequest, ServletResponse, FilterChain)

The method performs the actual filtering operation, when a client requests a method is provided that matches the filter URL, Servlet container will first call the method doFilter filter. FilterChain subsequent user access to the filter.

(2)public void init(FilterConfig filterConfig)

When the web application starts, web server will create an instance of Filter, and call its init method, read the web.xml configuration, complete object initialization functions to prepare to intercept user requests for subsequent preparation (filter objects only will be created once, init method is executed only once). Developers parameter init method, obtained on behalf of FilterConfig objects in the current filter configuration information.

(3)public void destroy()

Servlet container calls this method before the destruction of the filter instance, release resources occupied Servlet filters in the process.

3.Servlet filter process

Request ----> matched filter -> doFilter () ---> filtered response filter chain strand (servlet response) ---> servlet response before the servlet

4. Annotation arranged:

@WebFilter(filterName = "loginFilter",   
    urlPatterns = "/*",   
    initParams = {  
            @WebInitParam(name = "loginUI", value = "/home/loginUI"),  
            @WebInitParam(name = "loginProcess", value = "home/login"),  
            @WebInitParam(name = "encoding", value = "utf-8")  
}) 
 

5.FilterConfig use

Filter the init method provides a FilterConfig objects, such as web.xml file configured as follows:

<filter>
    <filter-name>LogFilter</filter-name>
    <filter-class>com.sxt.test.LogFilter</filter-class>
    <init-param>
        <param-name>Site</param-name>
        <param-value>sxt教程</param-value>
</init-param>
 </filter>

在 init 方法使用 FilterConfig 对象获取参数:

public void  init(FilterConfig config) throws ServletException {
    // 获取初始化参数
    String site = config.getInitParameter("Site"); 
    // 输出初始化参数
    System.out.println("网站名称: " + site); 
}

6.web.xml配置说明:

<filter>指定一个过滤器。

<filter-name>用于为过滤器指定一个名字,该元素的内容不能为空。

<filter-class>元素用于指定过滤器的完整的限定类名。

<init-param>元素用于为过滤器指定初始化参数,它的子元素<param-name>指定参数的名字,<param-value>指定参数的值。

在过滤器中,可以使用FilterConfig接口对象来访问初始化参数。

<filter-mapping>元素用于设置一个 Filter 所负责拦截的资源。一个Filter拦截的资源可通过两种方式来指定:Servlet 名称和资源访问的请求路径

<filter-name>子元素用于设置filter的注册名称。该值必须是在<filter>元素中声明过的过滤器的名字

<url-pattern>设置 filter 所拦截的请求路径(过滤器关联的URL样式)

<servlet-name>指定过滤器所拦截的Servlet名称。

<dispatcher>指定过滤器所拦截的资源被 Servlet 容器调用的方式,可以是REQUEST,INCLUDE,FORWARD和ERROR之一,默认REQUEST。用户可以设置多个<dispatcher>子元素用来指定 Filter 对资源的多种调用方式进行拦截。

<dispatcher>子元素可以设置的值及其意义

Guess you like

Origin www.cnblogs.com/WhiperHong/p/11025460.html