厚积薄发系列之JavaWeb的Filter
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了厚积薄发系列之JavaWeb的Filter相关的知识,希望对你有一定的参考价值。
1.Filter的使用配置
- 创建一个类使其实现javax.servlet.Filter接口
- 实现接口方法 destroy() doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) init(FilterConfig arg0)
- 配置web.xml
-
<filter> <filter-name>TestFilter</filter-name> <filter-class>filter.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.Filter的运行流程(两张图来展示)
3.Filter接口的方法
- init(FilterConfig config):初始化操作
- doFilter(ServletRequest request, ServletResponse response, FilterChain chain):处理业务逻辑
- destroy() :销毁操作
4.Filter的声明周期
- 初始化操作 在服务器启动的时候 根据web.xml配置文件创建Filter实例 调用init方法 实现初始化操作
- 处理业务逻辑 在用户的一次请求 创建一个新的线程 根据路径调用相应的过滤器的doFilter执行业务逻辑
- 结束阶段 当Filter被移除的时候或者服务器正常关闭的时候 调用destory方法 销毁该实例
- 注意Filter是单实例多线程的
5.Filter的常见用法
- post和get请求统一utf-8编码 web.xml路径配置为/*
-
1 import java.io.IOException; 2 import java.io.UnsupportedEncodingException; 3 import java.util.Map; 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 import javax.servlet.http.HttpServletRequestWrapper; 13 import javax.servlet.http.HttpServletResponse; 14 /** 15 * 统一编码 16 * @author Administrator 17 * 18 */ 19 public class EncodingFilter implements Filter { 20 21 @Override 22 public void init(FilterConfig filterConfig) throws ServletException { 23 // TODO Auto-generated method stub 24 25 } 26 27 @Override 28 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { 29 //1.强转 30 HttpServletRequest request=(HttpServletRequest) req; 31 HttpServletResponse response=(HttpServletResponse) resp; 32 33 //2.放行 34 chain.doFilter(new MyRequest(request), response); 35 } 36 37 @Override 38 public void destroy() { 39 // TODO Auto-generated method stub 40 41 } 42 43 } 44 class MyRequest extends HttpServletRequestWrapper{ 45 private HttpServletRequest request; 46 private boolean flag=true; 47 48 49 public MyRequest(HttpServletRequest request) { 50 super(request); 51 this.request=request; 52 } 53 54 @Override 55 public String getParameter(String name) { 56 if(name==null || name.trim().length()==0){ 57 return null; 58 } 59 String[] values = getParameterValues(name); 60 if(values==null || values.length==0){ 61 return null; 62 } 63 64 return values[0]; 65 } 66 67 @Override 68 /** 69 * hobby=[eat,drink] 70 */ 71 public String[] getParameterValues(String name) { 72 if(name==null || name.trim().length()==0){ 73 return null; 74 } 75 Map<String, String[]> map = getParameterMap(); 76 if(map==null || map.size()==0){ 77 return null; 78 } 79 80 return map.get(name); 81 } 82 83 @Override 84 /** 85 * map{ username=[tom],password=[123],hobby=[eat,drink]} 86 */ 87 public Map<String,String[]> getParameterMap() { 88 89 /** 90 * 首先判断请求方式 91 * 若为post request.setchar...(utf-8) 92 * 若为get 将map中的值遍历编码就可以了 93 */ 94 String method = request.getMethod(); 95 if("post".equalsIgnoreCase(method)){ 96 try { 97 request.setCharacterEncoding("utf-8"); 98 return request.getParameterMap(); 99 } catch (UnsupportedEncodingException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } 103 }else if("get".equalsIgnoreCase(method)){ 104 Map<String,String[]> map = request.getParameterMap(); 105 if(flag){ 106 for (String key:map.keySet()) { 107 String[] arr = map.get(key); 108 //继续遍历数组 109 for(int i=0;i<arr.length;i++){ 110 //编码 111 try { 112 arr[i]=new String(arr[i].getBytes("iso8859-1"),"utf-8"); 113 } catch (UnsupportedEncodingException e) { 114 e.printStackTrace(); 115 } 116 } 117 } 118 flag=false; 119 } 120 //需要遍历map 修改value的每一个数据的编码 121 122 return map; 123 } 124 125 return super.getParameterMap(); 126 } 127 128 }
- 登陆验证 判断用户是否登陆 web.xml路径配置自己想要用户登陆才能登陆登陆才能访问的页面
-
1 public void doFilter(ServletRequest arg0, ServletResponse arg1, 2 FilterChain arg2) throws IOException, ServletException { 3 4 HttpServletRequest request=(HttpServletRequest) arg0; 5 HttpServletResponse response=(HttpServletResponse) arg1; 6 HttpSession session=request.getSession(); 7 8 String path=request.getRequestURI(); 9 10 Integer uid=(Integer)session.getAttribute("userid"); 11 12 if(path.indexOf("/login.jsp")>-1){//登录页面不过滤 13 arg2.doFilter(arg0, arg1);//递交给下一个过滤器 14 return; 15 } 16 if(path.indexOf("/register.jsp")>-1){//注册页面不过滤 17 arg2.doFilter(request, response); 18 return; 19 } 20 21 if(uid!=null){//已经登录 22 arg2.doFilter(request, response);//放行,递交给下一个过滤器 23 24 }else{ 25 response.sendRedirect("login.jsp"); 26 } 27 28 }
- 禁止浏览器缓存动态页面 web.xml路径配置为不想让浏览器缓存的页面
-
1 @Override 2 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 3 throws IOException, ServletException { 4 5 HttpServletRequest request = (HttpServletRequest) req; 6 HttpServletResponse response = (HttpServletResponse) resp; 7 8 response.setDateHeader("Expires",-1); 9 response.setHeader("Cache-Control","no-cache"); 10 response.setHeader("Pragma","no-cache"); 11 12 chain.doFilter(request, response); 13 14 } 15 16 @Override
以上是关于厚积薄发系列之JavaWeb的Filter的主要内容,如果未能解决你的问题,请参考以下文章