1.自定义的HttpFilter,实现Filter接口
HttpFilter 
package com.aff.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //自定义的HttpFilter,实现Filter接口
public abstract class HttpFilter implements Filter {
/**
* 用于保存FilterConfig 对象
*/ private FilterConfig filterConfig; // 不建议子类直接覆盖,若直接覆盖将导致 FilterConfig 成员变量初始化失败
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
init();
} // 供子类直接继承的初始化方法,可以通过 getFilterConfig() 获取 FilterConfig 对象
protected void init() {
// 直接返回init(ServletConfig) 的FilterConfig 对象
} public FilterConfig getFilterConfig() {
return filterConfig;
} /*
* 原生的doFilter 方法, 在方法内部把ServletRequest 和ServletResponse 转为了
* HttpServletRequest 和 HttpServletResponse, 并调用了
* dofilter(HttpServletRequest request, HttpServletResponse response,FilterChain chain)
*
* 若编写 Filter 的过滤发不能发 不见一丝直接继承该方法,
* 而建议继承 doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
*/
@Override
public void doFilter(ServletRequest req, ServletResponse reqs, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) reqs; doFilter(request, response, chain);
} // 抽象方法,为http请求指定,必须实现的方法
public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException;
// 空的destroy()
@Override
public void destroy() {
}
}

2.理解多个Filter代码的执行顺序

HelloFilter

package com.aff.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 HelloFilter implements Filter { @Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("init"); } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("1. Before HelloFilter's chain.doFilter... "); chain.doFilter(request, response);
System.out.println(" 2.After HelloFilter's chain.doFilter... "
);
} @Override
public void destroy() {
System.out.println("destroy"); } }

SecondFilter
package com.aff.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 { @Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("seond init。。。");
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println(" 3.Before SecondFilter's chain.doFilter... "); // 放行
chain.doFilter(request, response);
system.out.println();
System.out.println(" 4.After SecondFilter's chain.doFilter... "
);
} @Override
public void destroy() {
System.out.println("second destroy。。。"); }
}

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Test Page</h3>
<%
System.out.print("5.Test.jsp");
%>
</body>
</html>

以上的代码执行顺序为

1. Before  HelloFilter's  chain.doFilter...
3.Before SecondFilter's chain.doFilter...
5.Test.jsp
4.After SecondFilter's chain.doFilter...
2.After HelloFilter's chain.doFilter...

创建HttpFilter与理解多个Filter代码的执行顺序-LMLPHP

05-11 05:00