Filter也称之为过滤器,它是Servlet技术中实用技术之一,Web开发人员通过Filter技术,对web服务器管理的所有web资源进行拦截(例如Jsp, Servlet, 静态图片或html等),从而实现一些特殊的功能。
例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
它主要用于对请求进行预处理,也可以对响应进行后处理。
Filter的完整流程:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,
最后Filter再对服务器响应进行后处理。
1、在ServletRequest到达Servlet之前,拦截客户的ServletRequest。根据需要检查ServletRequest,也可以修改ServletRequest头和数据。
2 、在ServletResponse到达客户端之前,拦截ServletResponse。根据需要检查ServletResponse,也可以修改ServletResponse头和数据。
package com.usc.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 FilterLifeCycle implements Filter {
public FilterLifeCycle(){
System.out.println("Do my Constructer......");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Do my init......");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("Do my doFilter......");
System.out.println("请求向后面传递.....");
chain.doFilter(request, response);
System.out.println("请求传递回来了......");
System.out.println("Do my doFilter end......");
}
@Override
public void destroy() {
System.out.println("Do my destroy......");
}
}
Filter
filterTest
com.usc.filter.FilterLifeCycle
filterTest
/login.jsp
loginfilter
com.usc.filter.LoginCheckFilter
loginfilter
/login
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录界面
package com.usc.simulate;
/**
* 自定义MyFilter接口
*/
public interface MyFilter{
public void doFilter(MyRequest req,MyResponse resp,MyFilterChain chain);
}
package com.usc.simulate;
public class MyFilter1 implements MyFilter{
// 子类Filter1
public void doFilter(MyRequest req, MyResponse resp, MyFilterChain chain) {
System.out.println("MyFilter 1 start ......");
chain.doFilter(req, resp, chain);
System.out.println("MyFilter 1 finish ......");
}
}
package com.usc.simulate;
public class MyFilter2 implements MyFilter{
public void doFilter(MyRequest req, MyResponse resp, MyFilterChain chain) {
System.out.println("MyFilter 2 start ......");
chain.doFilter(req, resp, chain);
System.out.println("MyFilter 2 finish ......");
}
}
package com.usc.simulate;
public class MyFilter3 implements MyFilter{
public void doFilter(MyRequest req, MyResponse resp, MyFilterChain chain) {
System.out.println("MyFilter 3 start ......");
chain.doFilter(req, resp, chain);
System.out.println("MyFilter 3 finish ......");
}
}
package com.usc.simulate;
/**
* 自定义过滤器链
*
*/
public class MyFilterChain {
//过滤器数组,用来保存注册的Filter
private MyFilter[] filters;
//Servlet数组,保存注册的servlet
private MyServlet server;
//下标,用来表示链的顺序
private int index=0;
//构造方法,初始化数组
public MyFilterChain(MyFilter[] filters, MyServlet server){
this.filters=filters;
this.server=server;
}
//doFilter方法,循环处理
public void doFilter(MyRequest req,MyResponse resp,MyFilterChain chain){
if(index
package com.usc.simulate;
public class MyRequest {
}
package com.usc.simulate;
public class MyResponse {
}
package com.usc.simulate;
public class MyServlet {
public void doService(MyRequest req,MyResponse resp){
System.out.println("doService......");
}
}
package com.usc.simulate;
public class Test {
public static void main(String[] args) {
//构造过滤器数组
MyFilter[] filters ={new MyFilter1(),new MyFilter2(),new MyFilter3()};
//创建链对象
MyFilterChain chain = new MyFilterChain(filters, new MyServlet());
//点方法,模拟执行Filter链
chain.doFilter(new MyRequest(), new MyResponse(), chain);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
欢迎您<%=request.getAttribute("user") %>
package com.usc.filter;
import java.io.IOException;
import java.net.URLEncoder;
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 LoginCheckFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("LoginCheckFilter.......");
//设置所有请求编码
request.setCharacterEncoding("UTF-8");
//登录检查
String userName=request.getParameter("userName");
String passWord =request.getParameter("pssWord");
//如果校验不通过,则请求将不再往下传递
if(userName==null || "".equals(userName.trim()) ||
passWord==null || "".equals(passWord.trim())){
request.setAttribute("ErrorMsg", "用户名或密码填写不完整");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}else{
//校验通过,请求往下传递
// URLEncoder.encode(userName, "UTF-8");
request.setAttribute("user", userName);
chain.doFilter(request, response);
}
System.out.println("LoginCheckFilter end.......");
}
@Override
public void destroy() {
}
}
package com.usc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取参数
// String userName=req.getParameter("userName");
// String passWord =req.getParameter("pssWord");
// if(userName!=null){
// req.setAttribute("user", userName);
// }else{
// req.setAttribute("user", "UNKNOW");
// }
req.getRequestDispatcher("/welcome.jsp").forward(req, resp);
}
}
Filter
filterTest
com.usc.filter.FilterLifeCycle
filterTest
/login.jsp
loginfilter
com.usc.filter.LoginCheckFilter
loginfilter
/login
login
com.usc.servlet.LoginServlet
login
/login