码迷,mamicode.com
首页 > 编程语言 > 详细

Java WEB应用中文乱码处理总结

时间:2015-06-15 13:10:08      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:中文乱码   编码   tomcat   字符集设置   get与post乱码处理   

处理服务器乱码的四种方式

1.jsp编码设置错误导致乱码
在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page language="java" contentType="text/html;charset=utf-8" %>

2.在接受request参数时进行,response响应时设置编码格式
request.setCharacterEncoding( "UTF-8" )
response.setCharacterEncoding( "UTF-8")

3.使用String设置字符集的方式处理乱码
indexString = java.net.URLDecoder.decode(indexString , "UTF-8");
indexString =  new String(indexString.getBytes("UTF-8"), "UTF-8");

4.使用自定义过滤器

package com.cdu.yige.filter;
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;
import javax.servlet.http.HttpServletRequest;

public class EncodingFilter implements Filter {
     private String charset = "UTF-8";
     @Override
     public void destroy() {}
     @Override
     public void doFilter(ServletRequest request, ServletResponse response,
               FilterChain chain) throws IOException, ServletException {
          HttpServletRequest req = (HttpServletRequest) request;
          if(req.getMethod().equalsIgnoreCase("GET")) {
               if(!(req instanceof GetRequest)) {
                    req = new GetRequest(req, charset);//处理get请求编码
               }
          } else {
               req.setCharacterEncoding(charset);//处理post请求编码
          }
          chain.doFilter(req, response);
     }

     @Override
     public void init(FilterConfig fConfig) throws ServletException {
          String charset = fConfig.getInitParameter("charset");
          if(charset != null && !charset.isEmpty()) {
               this.charset = charset;
          }
     }
}
对get方式的过滤
package com.cdu.yige.filter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 对GET请求参数加以处理!
* @author lucien
*
*/
public class GetRequest extends HttpServletRequestWrapper {
     private HttpServletRequest request;
     private String charset;
     
     public GetRequest(HttpServletRequest request, String charset) {
          super(request);
          this.request = request;
          this.charset = charset;
     }

     @Override
     public String getParameter(String name) {
          // 获取参数
          String value = request.getParameter(name);
          if(value == null) return null;//如果为null,直接返回null
          try {
               // 对参数进行编码处理后返回
               return new String(value.getBytes("ISO-8859-1"), charset);
          } catch (UnsupportedEncodingException e) {
               throw new RuntimeException(e);
          }
     }
     
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Override
     public Map getParameterMap() {
          Map<String,String[]> map = request.getParameterMap();
          if(map == null) return map;
          // 遍历map,对每个值进行编码处理
          for(String key : map.keySet()) {
               String[] values = map.get(key);
               for(int i = 0; i < values.length; i++) {
                    try {
                         values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
                    } catch (UnsupportedEncodingException e) {
                         throw new RuntimeException(e);
                    }
               }
          }
          // 处理后返回
          return map;
     }
     @Override
     public String[] getParameterValues(String name) {
          String[] values = super.getParameterValues(name);
          for(int i = 0; i < values.length; i++) {
               try {
                    values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
               } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
               }
          }
          return values;
     }
}
过滤器web.xml文件配置
      < filter>
            <filter-name >EncodingFilter </filter-name >
           <filter-class >com.cdu.yige.filter.EncodingFilter </filter-class >
      </filter >
      <filter-mapping >
            <filter-name >EncodingFilter </filter-name >
            <url-pattern >/* </url-pattern >
      </filter-mapping >

5.修改tomcat服务器server.xml的字符集配置,对get方式和post方式的乱码终极处理。
  <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
    URIEncoding="UTF-8"
    useBodyEncodingForURI="true"

    redirectPort="8443" />

6.get请求方式和post请求方式产生的乱码处理方式略有不同,请自行百度参看。
其他方法参看:

Java WEB应用中文乱码处理总结

标签:中文乱码   编码   tomcat   字符集设置   get与post乱码处理   

原文地址:http://blog.csdn.net/jeffsmish/article/details/46502313

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!