过滤器(解决中文乱码get/post)

过滤器(解决中文乱码get/post)

Filter

必须在web.xml中注册

作用:在请求和响应之间进行拦截数据,对数据进行加工处理。

原理:请求à过滤器1(doFilter)à过滤器2à 处理  à过滤器2à过滤器1à响应

多个过滤器组成的叫做过“滤器链”。

编写:(分析一下生命周期)

1.      让一个类实现Filter接口

2.      重写里面的方法,最重要的是doFilter方法

3.      在web.xml注册

过滤器(解决中文乱码get/post)_第1张图片

过滤器(解决中文乱码get/post)_第2张图片

过滤器(解决中文乱码get/post)_第3张图片

生命周期

过滤器(解决中文乱码get/post)_第4张图片

中文乱乱处理:

 post提交中文乱码:

 全局post提交中文乱码过滤器

 get提交中文乱码:

主要利用:

过滤器(解决中文乱码get/post)_第5张图片

在过滤器中:我们继承HttpServletRequestWrapper类,通过重写里面的方法来达到目的:

过滤器(解决中文乱码get/post)_第6张图片


过滤器(解决中文乱码get/post)_第7张图片


过滤器(解决中文乱码get/post)_第8张图片

 

完整过滤器代码如下:

 

package com.crm.web.filter;

 

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

import java.util.Map.Entry;

 

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequestWrapper;

 

public class MyFilter implements Filter {

 

         @Override

         public void destroy() {

                   System.out.println("过滤器销毁.......");

 

         }

 

         @Override

         public void doFilter(ServletRequest request, ServletResponse response,

                            FilterChain chain) throws IOException, ServletException {

                   System.out.println("进行过滤器开发.......");

                   // 只要在这里加

                   request.setCharacterEncoding("utf-8");// 请求

                   response.setCharacterEncoding("utf-8");// 响应

                   response.setContentType("text/html;charset=utf-8");// 响应

                   // get提交乱码处理

                   chain.doFilter(new MyRequest((HttpServletRequest) request), response);

 

         }

 

         @Override

         public void init(FilterConfig filterConfig) throws ServletException {

                   System.out.println("过滤器初始化.......");

 

         }

 

         class MyRequest extends HttpServletRequestWrapper {

 

                   HttpServletRequest request;

 

                   public MyRequest(HttpServletRequest request) {

                            super(request);

                            this.request = request;

 

                   }

 

                   @Override

                   public String getParameter(String name) {

                            String value = request.getParameter(name);

                            // 先判断是否为空

                            if (value == null) {

                                     return null;

                            }

                            // 再判断提交方法

                            if (!request.getMethod().equals("GET")) {

                                     return value;

                            }

                            // get提交

 

                            try {

                                     value = new String(value.getBytes("iso8859-1"), "utf-8");

                            } catch (UnsupportedEncodingException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            }

                            return value;

                   }

 

                   @Override

                   public String[] getParameterValues(String name) {

                            String[] oldValues = request.getParameterValues(name);

                            String[] newValues = null;

                            if (oldValues != null) {

                                     newValues = new String[oldValues.length];

                                     if (!request.getMethod().equals("GET")) {

                                               return oldValues;

                                     }

                                     if (newValues != null) {

                                               try {

                                                        for (int i = 0; i < oldValues.length; i++) {

                                                                 String value = oldValues[i];

                                                                 value = new String(value.getBytes("iso8859-1"),

                                                                                    "utf-8");

                                                                 newValues[i] = value;

                                                        }

                                               } catch (UnsupportedEncodingException e) {

                                                        // TODO Auto-generated catch block

                                                        e.printStackTrace();

                                               }

                                     }

                            }

                            return newValues;

                   }

 

                   @Override

                   public Map getParameterMap() {

                            // 1.拿到客户端的值

                            Map oldMap = request.getParameterMap();

                            // 2.新map

                            Map newMap = new HashMap();

                            // 3.拿出oldMap

                            Set> entrySet = oldMap.entrySet();

                            for (Entry entry : entrySet) {

                                     // 4.获取键

                                     String key = entry.getKey();

                                     // 5.获取值

                                     String[] values = entry.getValue();

                                     // 新的数组和原数组一样大

                                     String[] newValues = new String[values.length];

                                     // 6.判断值是否为空

                                     if (values == null) {

                                               newMap.put(key, new String[] {});// 初始化

                                               continue;

                                     }

                                     // 7.取出values里面每一个值进行转码

                                     try {

                                               for (int i = 0; i < values.length; i++) {

                                                        String value = values[i];

                                                        value = new String(value.getBytes("iso8859-1"), "utf-8");

                                                        // value = new String(value.getBytes("iso8859-1"),

                                                        // request.getCharacterEncoding());

                                                        newValues[i] = value;

                                               }

                                               newMap.put(key, newValues);

 

                                     } catch (UnsupportedEncodingException e) {

                                               // TODO Auto-generated catch block

                                              e.printStackTrace();

                                     }

 

                            }

                            return newMap;

                   }

 

         }

 

}

 


你可能感兴趣的:(Java)