Tomcat:设置filter过滤器

filter可以在http请求到达servlet之前做一些事情,也可以在servlet生成响应后做一些事情。

下面是一个示例。

使用netbeans8创建web项目HelloJSP,项目完成后结构如下:

Tomcat:设置filter过滤器

我们使用了 两个Filter

web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>FirstFilter</filter-name>
        <filter-class>letian.filter.FirstFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>FirstFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name>SecondFilter</filter-name>
        <filter-class>letian.filter.SecondFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecondFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>



letian/filter/FirstFilter.java内容如下:

package letian.filter;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FirstFilter implements Filter {
    
    //tomcat启动,或者context重新加载的时候调用init(先destroy再init)  
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("FirstFilter init...");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        //request到达的时候执行下面的代码  
        System.out.println("FirstFilter request>>>>>>>>>>>>");

        //转给过滤器链中的下一个filter,如果是最后一个filter,调用要访问的资源  
        chain.doFilter(request, response);

        //要访问的资源访问完以后,response到达的时候执行下面的代码  
        System.out.println("FirstFilter response>>>>>>>>>>>>");
 
    }
    
    //tomcat关闭或者context重新加载的时候调用destroy  
    public void destroy() {
        System.out.println("FirstFilter destroy...");
    }
}



letian/filter/SecondFilter.java内容如下:

package letian.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SecondFilter implements Filter {
    
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("SecondFilter init...");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        
        System.out.println("SecondFilter request>>>>>>>>>>>>");
 
        request.setAttribute("username", "letian");
        chain.doFilter(request, response);

        System.out.println("SecondFilter response>>>>>>>>>>>>");
        
    }
    
    public void destroy() {
        System.out.println("SecondFilter destroy...");
    }
}



letian/servlet/Home.java内容如下:

package letian.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "Home", urlPatterns = {"/"})
public class Home extends HttpServlet {
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("I am home!"); // 输出到控制台
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<h1>"+ request.getAttribute("username") +"</h1>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


}




效果:


启动tomcat,可以看到Tomcat输出:
FirstFilter init...
SecondFilter init...



在浏览器中访问http://localhost:8080/HelloJSP/:
Tomcat:设置filter过滤器
此时,tomcat输出:
FirstFilter request>>>>>>>>>>>>
SecondFilter request>>>>>>>>>>>>
I am home!
SecondFilter response>>>>>>>>>>>>
FirstFilter response>>>>>>>>>>>>



关闭tomcat时,输出:
FirstFilter destroy...
SecondFilter destroy...



参考:

Tomcat 7源码学习笔记 -8 Filter过滤器解读:http://blogzhoubo.iteye.com/blog/1738358
Apache Tomcat 8 Configuration Reference Container Provided Filters: http://tomcat.apache.org/tomcat-8.0-doc/config/filter.html














你可能感兴趣的:(Tomcat:设置filter过滤器)