一个过滤器实例

public class AuthFilter implements Filter {

	public void init(FilterConfig fc) throws ServletException {
		System.out.println("in filter,init()...");
	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String currentURL = request.getRequestURI();
		
		if(request.getSession().getAttribute("UISORFGAP")==null || "".equals(request.getSession().getAttribute("UISORFGAP"))){
			//拿到判断是否存在前置机和导入服务器标识
			request.getSession().setAttribute("UISORFGAP", UISOrFGAPMarkUtil.getFgapFlag());
		}
		
		if(currentURL.endsWith(".jsp")||currentURL.endsWith(".action")){
			if(currentURL.indexOf("verifyCodeImg.jsp") == -1){
				HttpSession session = request.getSession(true); // 没有返回一个新的.
				LoginUser loguser = (LoginUser) session.getAttribute("loginUser");
				String path = request.getContextPath();
				if(currentURL != null && (currentURL.indexOf("/login.jsp") == -1 && currentURL.indexOf("isLogin_log.action") == -1)){
					if(loguser == null && currentURL.indexOf("forword.jsp")==-1){
						response.sendRedirect(path + "/forword.jsp"); 
						return;
					}else if(currentURL.indexOf("forword.jsp")!=-1){
						filterChain.doFilter(request, response);
						return;
					}else if(loguser != null){
						filterChain.doFilter(request, response);
						return;
					}
				}else{
					filterChain.doFilter(request, response);
					return;
				}
			}else{
				//image过滤掉
				filterChain.doFilter(request, response);
				return;
			}
		}else{
			// js css 图片过滤 
			filterChain.doFilter(request, response);
			return;
		}
	}

	public void destroy() {
		System.out.println("in filter,destroy()...");
	}

}

 在web.xml中的配置

 

<filter>
		<filter-name>AuthFilter</filter-name>
		<filter-class>
			com.topwalk.fgap.utils.AuthFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>AuthFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

 

你可能感兴趣的:(过滤器)