一句话概括:
AOP的编程模式就是使用filter拦截请求,分离主流业务和子业务。
比如filter可以拦截重复登录的请求。就像你想去见公司的大BOSS,可能要经过门卫,前台,秘书等层层拦截。filter也可以串联的多层拦截,满足一层以后到下一层。
package com.xzit.aop; 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 OneFilter implements Filter { private String ip = ""; @Override public void destroy() { // TODO Auto-generated method stub System.out.println("释放资源"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("执行过滤"); // if(ip.equals("127.0.0.1")) { // System.out.println("您被举报,暂时无法访问本网站的任何资源"); // }else { // // chain.doFilter(arg0, arg1); //执行链条驱动,交给下一个Filter(如果有) // } chain.doFilter(arg0, arg1); //执行链条驱动,交给下一个Filter(如果有) } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("初始化"); ip = arg0.getInitParameter("ip"); } }
package com.xzit.aop; 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 TwoFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("过滤器TwoFilter"); // chain.doFilter(arg0,arg1); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>aop2</display-name> <!-- 配置自定义过滤器 --> <filter> <filter-name>oneFilter</filter-name> <filter-class>com.xzit.aop.OneFilter</filter-class> <init-param> <param-name>ip</param-name> <param-value>127.0.0.1</param-value> </init-param> </filter> <!-- 配置Filter的拦截映射 --> <filter-mapping> <filter-name>oneFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>twoFilter</filter-name> <filter-class>com.xzit.aop.TwoFilter</filter-class> </filter> <!-- 配置Filter的拦截映射 --> <filter-mapping> <filter-name>twoFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ 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> index.jsp 页面 </body> </html>