在创建一个java类的时候,要实现Filter的一个接口,注意:要选择javax.servlet的
一、过滤器的实现
1.@WebFilter("/s01") 配置拦截的资源路径
2.doFilter() 方法中需要设置放行,否则请求无法到达资源 filterChain.doFilter(servletRequest,servletResponse);
3.如果是过滤器链(有多个过滤器Filter.java),则先配置的先执行(首字母在前的先执行);响应时,顺序相反。
package com.xxxx.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 拦截路径为s01的资源 */ @WebFilter("/s01") //@WebFilter("/*")//拦截所有的请求资源路径 public class Filter01 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter01 init.."); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("s01正在过滤..."); //一定要加上放行资源,不然请求无法到达资源 filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { System.out.println("Filter01 destory..."); } }
二、乱码问题
1.POST请求:任意版本的Tomcat都是会乱码的
解决:request.setCharacterEncoding("UTF-8");
2.GET请求:只有Tomcat7及以下的版本才会出现乱码问题
解决:new String(request.getParameter("参数名").getBytes("ISO-8859-1"),"UTF-8");
也可以直接拷入
package com.xxxx.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; @WebFilter("/*") public class AEncodingFilter implements Filter { @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { //基于HTTP HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; //处理请求乱码(处理POST请求) request.setCharacterEncoding("UTF-8"); //处理GET请求 String method = request.getMethod(); //如果是GET请求(不区分大小写) if("GET".equalsIgnoreCase(method)){ //获取服务器版本 Apache Tomcat/9.0.68 String serverInfo = request.getServletContext().getServerInfo(); //获取具体的版本号,获取'/'后面'.'前面的1位数字(注意只能取一个数字,如果是10的话就不行了) String versionStr = serverInfo.substring(serverInfo.indexOf(("/")+1,serverInfo.indexOf("."))); //判断服务器版本是否小于8 if(Integer.parseInt(versionStr)<8){ //定义一个内部类继承HttpServletRequest对象像 HttpServletRequest myRequest = new MyWapper(request); //放行资源 chain.doFilter(myRequest,response); return; } } chain.doFilter(request,response); return; } class MyWapper extends HttpServletRequestWrapper { // 定义HttpServletRequest对象,用来提升带参构造器中的request private HttpServletRequest request; public MyWapper(HttpServletRequest request) { super(request); this.request = request; // 提升作用域 } //重写getParameter()方法 @Override public String getParameter(String name) { String value = request.getParameter(name); if (value != null && !"".equals((value.trim()))) { try { value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return value; } } }View Code
三、实例
标签:request,public,WebFilter,过滤器,import,servlet,javax From: https://www.cnblogs.com/hmy22466/p/16842244.html