1. 程式人生 > >servlet-過濾器

servlet-過濾器

//過濾器:每個servlet都要經過filter的處理
public class FilterTest implements Filter{

    @Override
    public void destroy() {
        System.out.println("destroy!1");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println(
"檢查!!"); chain.doFilter(request, response); System.out.println("完成!!"); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("init"); } }
<filter>
        <filter-name>filter</filter-name>
        <
filter-class>com.study.filter.FilterTest</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name>
      //*號代表所有servlet請求都要過濾 <url-pattern>/*</url-pattern> </filter-mapping>