Servlet3.0 Filter

package com.lee;

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;
import javax.servlet.http.HttpServletRequestWrapper;

/*
 * 
 *  <filter>
 		<filter-name>log</filter-name>
 		<filter-class>com.lee.LogFilter</filter-class>
 	</filter>
 	<filter-mapping>
 		<filter-name>log</filter-name>
 		<url-pattern>/user/*</url-pattern>
 	</filter-mapping>
 
属性                                                是否必须                              说明
asyncSupported        否				 该Filter是否支持异步操作模式
dispatcherType        否    			 指定Filter对那种dispatcher模式进行过滤 该属性支持 Async,Error Forward,include,request 
displayName           否				 File 显示的名称
filterName            是				Filter的名称
initParams		                 否				Filter的配置参数
servletNames          否                                  可以指定多个,表示对这几个特定的的serlet 进行过滤
urlPatterns/value     否 				指定 Filter拦截的 URL
       
 	
 	
 
@WebFilter(filterName="log",urlPatterns={"/*"})*/
public class LogFilter implements Filter {

	private FilterConfig config;
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		this.config=null;
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		ServletContext context= this.config.getServletContext();
		
		long before=System.currentTimeMillis();
		System.out.println("开始过滤");
		//HttpServletRequestWrapper hrequest=(HttpServletRequestWrapper)arg1;
		
		HttpServletRequest hrequest=(HttpServletRequest)arg0;
		System.out.println("Filter 已经获取到了用户请求的地址:"+hrequest.getServletPath());
		chain.doFilter(arg0, arg1);
		long after=System.currentTimeMillis();
		System.out.println("过滤结束");
		System.out.println("请求被定位到"+hrequest.getRequestURI()+" 所花的时间为:"+(after-before));
		
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config=config;
	}

}

package com.lee;

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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

/*
@WebFilter(
		filterName="authority",
		urlPatterns={"/*"},
		initParams={
				@WebInitParam(name="encoding",value="UTF-8"),
				@WebInitParam(name="loginPage",value="/getPostParam.jsp"),
				@WebInitParam(name="proLogin",value="/proLog.jsp")
		}
		)*/
public class AuthorityFilter implements Filter {

	private FilterConfig config;
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		config=null;
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		String encoding=config.getInitParameter("encoding");
		String loginPage=config.getInitParameter("loginPage");
		String proLogin=config.getInitParameter("proLogin");
		arg0.setCharacterEncoding(encoding);
		
		HttpServletRequest  request=(HttpServletRequest)arg0;
		HttpSession session=request.getSession(true);
		String requestPath=request.getServletPath();
		if(session.getAttribute("user")==null
				&& !requestPath.endsWith(loginPage)
				&& !requestPath.endsWith(proLogin)){
			request.setAttribute("tip", "您还没有登录");
			request.getRequestDispatcher(loginPage).forward(arg0, arg1);
		}else{
			arg2.doFilter(arg0, arg1);		
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		this.config=arg0;
	}

}

你可能感兴趣的:(filter)