中文乱码分类:

(1)按照请求分类:

  GET请求乱码

  POST请求乱码

(2)按照乱码位置分类

  从前台传到后台的数据乱码(存储到数据库中的数据乱码)

  从后台传到前台的数据乱码(显示在页面的数据乱码)

 

乱码的解决方式:

(1)GET请求中请求参数乱码(注意:GET请求乱码,Spring的配置以及编码拦截器的配置是无效的)

方法一: 在后台处理编码,通过getBytes方法转换编码方式
String username = request.getParameter("username"); //获取请求参数值
username = new String(username.getBytes("iso8859-1"),"UTF-8");//将请求参数值转换为UTF-8编码格式

 
方法二:在客户端使用 URLEncoder.encode(name,”UTF-8”)对中文参数进行编码,在服务器端需要进行解码naem = java.net.URLDecoder.decode(name, “UTF-8”));
    /* jsp页面中使用超链接跳转:   响应删除笔记评论 */
    function deleteNoteComment(name){
        window.location.href="<%=request.getContextPath() %>/note/deleteComment.do?name="+URLEncoder.encode(name,"UTF-8”);
     }

  //Spring 的controller类中接收请求参数

  String  name = request.getParameter(“name”); //得到请求参数

       name = URLDecoder.decode(name, “UTF-8”)); //将请求参数转换为UTF-8编码格式

 

方法三:修改tomcat的server.xml文件:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>

 

(2)POST请求中文乱码解决方法

方法一:通过设置请求参数的编码

在方法的第一句添加语句 : request.setCharacterEncoding("UTF-8");

在方法的最后一句添加语句: response.setContentType("text/html;charset=UTF-8");

 

方法二:编写编码拦截器

拦截器类 CharacterEncodingFilter .java

package cn.dfx.share_record_SSM.dsum.util;

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;
import javax.servlet.http.HttpServletResponse;

public class CharacterEncodingFilter implements Filter{
    
    private String encoding=null;
    public void destroy() {
        
    }

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        
        request.setCharacterEncoding(encoding);
        chain.doFilter(request,response);
        response.setContentType("text/html;charset=UTF-8");
    }
    
    //得到配置文件中的初始化参数
    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

}

拦截器在web.xml中的配置

 <filter>
      <filter-name>character</filter-name>
      <filter-class>cn.dfx.share_record_SSM.dsum.util.CharacterEncodingFilter</filter-class>
      
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  
  <filter-mapping>
      <filter-name>character</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>   

 

方法三:直接使用Spring的编码拦截器,在web.xml文件中添加配置

 <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>
          org.springframework.web.filter.CharacterEncodingFilter
      </filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>
<filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

 

方法四:在Spring的Controller类中的RequestMapping配置

@RequestMapping(value="/goMain",produces="application/json;charset=UTF-8")

或者 :  @RequestMapping(value="/goMain",produces="text/html;charset=UTF-8")

 

方法五:在Spring的主配置文件中添加以下配置(这是绝招了!!!) spring-servlet.xml

 <!-- 处理spring的Controller类中返回json数据 中文乱码的配置 -->
     <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
         <property name="messageConverters"> <array> 
         <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> 
         <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </bean> </array>
          </property>
     </bean>

 

 posted on 2018-04-09 11:50  阿叮339  阅读(1542)  评论(0编辑  收藏  举报