标签:Servle 方法 跳转 system abc 条件 service 配置 详解
filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标资源访问前后进行逻辑处理。
步骤:
举个??子:
1 package com.boss.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 public class MyFilter implements Filter { 13 14 @Override 15 //Filter创建的时候执行init方法 16 public void init(FilterConfig filterConfig) throws ServletException { 17 //1、获得web.xml中filter的名称 18 System.out.println(filterConfig.getFilterName());//MyFilter 19 //2、获得当前filter的初始化参数 20 //System.out.println(filterConfig.getInitParameter("code"));//1 21 //3、获得servletContext 22 filterConfig.getServletContext(); 23 24 System.out.println("init..."); 25 } 26 27 @Override 28 //doFilter是Filter的核心过滤的方法 29 //request: 内部封装的是客户端http请求的内容 30 //response: 代表的是响应 31 //FilterChain: 过滤器链对象 32 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 33 throws IOException, ServletException { 34 35 System.out.println("running..."); 36 //放行请求 37 chain.doFilter(request, response); 38 } 39 40 @Override 41 //Filter对象销毁的时候执行destory方法 42 public void destroy() { 43 System.out.println("destroy..."); 44 } 45 46 }
web.xml:
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.boss.filter.MyFilter</filter-class> <!-- <init-param> <param-name>code</param-name> <param-value>1</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
其中的参数ServletRequest/ServletResponse是每次在执行doFilter方法时web容器负责创建一个request和一个response对象作为doFilter的参数传递进来。该request与response就是在访问目标资源的service方法时的request和response。FilterChain是过滤器链对象,通过该对象的doFilter方法可以放行该请求。chain对象根据配置的filter-mapping顺序依次执行filter。
注意:url-pattern可以使用servlet-name替代,也可以混用。
举个??子:
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.boss.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
测试代码:
1 package com.boss.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestFilter extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 13 IOException { 14 request.getRequestDispatcher("/index.jsp").forward(request, response); 15 } 16 17 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, 18 IOException { 19 doGet(request, response); 20 } 21 }
此时当访问TestFilter时,过滤器只会执行一次,而不是两次。转发时不会执行过滤器,但是重定向会执行两次。
测试代码:
1 package com.boss.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestFilter extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 13 IOException { 14 response.sendRedirect(request.getContextPath() + "/index.jsp"); 15 } 16 17 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, 18 IOException { 19 doGet(request, response); 20 } 21 }
1 package com.boss.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.HttpServletRequest; 12 13 public class EncodingFilter implements Filter { 14 15 @Override 16 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 17 throws IOException, ServletException { 18 19 //request.setCharacterEncoding("UTF-8"); 20 21 //在传递request之前对request的getParameter方法进行增强 22 /* 23 * 装饰者模式(包装) 24 * 25 * 1、增强类与被增强的类要实现统一接口 26 * 2、在增强类中传入被增强的类 27 * 3、需要增强的方法重写 不需要增强的方法调用被增强对象原来的 28 * 29 */ 30 31 //被增强的对象 32 HttpServletRequest req = (HttpServletRequest) request; 33 //增强对象 34 EnhanceRequest enhanceRequest = new EnhanceRequest(req); 35 chain.doFilter(enhanceRequest, response); 36 } 37 38 @Override 39 public void destroy() { 40 } 41 42 @Override 43 public void init(FilterConfig filterConfig) throws ServletException { 44 } 45 }
1 package com.boss.filter; 2 3 import java.io.UnsupportedEncodingException; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletRequestWrapper; 7 8 //HttpServletRequestWrapper类是HttpServletRequest类的装饰类 9 public class EnhanceRequest extends HttpServletRequestWrapper { 10 private HttpServletRequest request; 11 12 public EnhanceRequest(HttpServletRequest request) { 13 super(request); 14 this.request = request; 15 } 16 17 //对getParameter增强 18 @Override 19 public String getParameter(String name) { 20 String param = request.getParameter(name);//乱码 21 try { 22 param = new String(param.getBytes("iso8859-1"), "UTF-8"); 23 } catch (UnsupportedEncodingException e) { 24 e.printStackTrace(); 25 } 26 return param; 27 } 28 29 }
web.xml
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.boss.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
标签:Servle 方法 跳转 system abc 条件 service 配置 详解
原文地址:https://www.cnblogs.com/icehand1214/p/9621096.html