Java Web之过滤器注解@WebFilter 的使用

@WebFilter 用于将一个类声明为过滤器,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器。该注解具有下表给出的一些常用属性 ( 以下所有属性均为可选属性,但是 value、urlPatterns、servletNames 三者必需至少包含一个,且 value 和 urlPatterns 不能共存,如果同时指定,通常忽略 value 的取值 ):

@WebFilter 的常用属性

Java Web之过滤器注解@WebFilter 的使用_第1张图片

1.日志过滤器,采用ServletContext实现:


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
@WebFilter(filterName="LogFilter200",urlPatterns="/*")
public class LogFilter200 implements Filter {
	private FilterConfig filterConfig;
	public void destroy() {
		this.filterConfig = null;
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		ServletContext context = this.filterConfig.getServletContext();
		long before = System.currentTimeMillis();
		System.out.println("LogFilter200开始执行过滤操作");
		HttpServletRequest httpReq = (HttpServletRequest)request;
		context.log("LogFilter200已经截获到用户的请求地址:"+httpReq.getServletPath());
		chain.doFilter(request, response);
		long after = System.currentTimeMillis();
		context.log("LogFilter200过滤执行完毕");
		context.log("请求被定为到:"+httpReq.getRequestURI()+"所花的时间为:"+(after-before));
	}
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("====LogFilter200过滤器初始化完成-------");
		this.filterConfig = fConfig;
	}
}

2.字符编码过滤器:


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.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(
filterName="filterForCharacterEncoding",urlPatterns="/*",
initParams= {@WebInitParam(name="characterEncoding",value="utf-8"),
			 @WebInitParam(name="login",value="login.jsp")})
public class CharacterEncodingFilter200 implements Filter {
	private FilterConfig filterConfig;
	private String characterEncoding;
   public void destroy() {
	   System.out.println("CharacterEncodingFilter200销毁");
   }
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("CharacterEncodingFilter200--进行过滤操作");
		if(null!=characterEncoding) {
			request.setCharacterEncoding(characterEncoding);
			response.setCharacterEncoding(characterEncoding);
		}
		chain.doFilter(request, response);
	}
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("CharacterEncodingFilter200初始化完成");
		this.filterConfig = fConfig;
		characterEncoding = this.filterConfig.getInitParameter("characterEncoding");
		System.out.println("filterName=="+fConfig.getFilterName());
	}
}

3.用户登陆认证权限管理过滤器:


import java.io.IOException;

import javax.servlet.DispatcherType;
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.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@WebFilter(filterName="AuthorityFilter200",urlPatterns="/admin/*",
dispatcherTypes=DispatcherType.FORWARD,
initParams= {@WebInitParam(name="login",value="login.jsp")})
public class AuthorityFilter200 implements Filter {
	private FilterConfig filterConfig;
	public void destroy() {
		this.filterConfig = null;
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		String login= this.filterConfig.getInitParameter("login");
		HttpServletRequest httpR = (HttpServletRequest)request;
		HttpSession session = httpR.getSession();
		String rPath = httpR.getServletPath();
		System.out.println("session==="+session.getAttribute("user"));
		if(null==session.getAttribute("user")&&!rPath.endsWith(login)) {
			request.setAttribute("tips", "您当前没有登录,请先登录!");
			System.out.println("tips==============");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else {
			chain.doFilter(request, response);
		}
	}
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("AuthorityFilter200权限管理认证登录过滤器初始化成功");
		this.filterConfig = fConfig;
	}

}

 

你可能感兴趣的:(#,Java,Web开发技术)