中文乱码(编码解码)问题的解决办法
个人小结:中文乱码(编码解码)问题的解决办法
在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱码问题以及解决方法。
1.JSP页面乱码
这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要在页面开始地方用下面代码指定字符集编码即可,
一般来说如果要兼容多种字符编码的话,最好用UTF-8编码。注意,UTF-8最好是要大写。否则有可能会出现未知的异常。
2、数据库乱码
这种乱码会使你插入数据库的中文变成乱码,或者读出显示时也是乱码,解决方法如下:
在数据库连接字符串中加入编码字符集
String Url="jdbc:mysql://localhost/digitgulf?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
并在页面中使用如下代码:
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
3、中文作为参数传递乱码
当我们把一段中文字符作为参数传递个另一页面时,也会出现乱码情况,解决方法如下:
在参数传递时对参数编码,比如在JSP中传递,传递前应编码,获取时作相应的解码
这里只写出兼容多种浏览器的方法(IE和火狐等)
JAVA写法:
编码:<%@ page import="java.net.URLEncoder" %>
String name = "中国";
String encodeName = URLEncoder.encode(URLEncoder.encode(name, "utf-8"),"utf-8");
解码:<%@ page import="java.net.URLDecoder" %>
String name = request.getParameter("name");
String decodeName = URLDecoder.decode(URLDecoder.decode(name, "utf-8"),"utf-8");
JS中的写法:
编码:encodeURI(encodeURI(originalStr,"utf-8"),"utf-8");
解码:decodeURI(decodeURI(originalStr, "utf-8"), "utf-8");
注意:以下字符不能被解码 ! @ # $ & * ( ) = : / ; ? + '
在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱码问题以及解决方法。
1.JSP页面乱码
这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要在页面开始地方用下面代码指定字符集编码即可,
一般来说如果要兼容多种字符编码的话,最好用UTF-8编码。注意,UTF-8最好是要大写。否则有可能会出现未知的异常。
2、数据库乱码
这种乱码会使你插入数据库的中文变成乱码,或者读出显示时也是乱码,解决方法如下:
在数据库连接字符串中加入编码字符集
String Url="jdbc:mysql://localhost/digitgulf?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
并在页面中使用如下代码:
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
3、中文作为参数传递乱码
当我们把一段中文字符作为参数传递个另一页面时,也会出现乱码情况,解决方法如下:
在参数传递时对参数编码,比如在JSP中传递,传递前应编码,获取时作相应的解码
这里只写出兼容多种浏览器的方法(IE和火狐等)
JAVA写法:
编码:<%@ page import="java.net.URLEncoder" %>
String name = "中国";
String encodeName = URLEncoder.encode(URLEncoder.encode(name, "utf-8"),"utf-8");
解码:<%@ page import="java.net.URLDecoder" %>
String name = request.getParameter("name");
String decodeName = URLDecoder.decode(URLDecoder.decode(name, "utf-8"),"utf-8");
JS中的写法:
编码:encodeURI(encodeURI(originalStr,"utf-8"),"utf-8");
解码:decodeURI(decodeURI(originalStr, "utf-8"), "utf-8");
注意:以下字符不能被解码 ! @ # $ & * ( ) = : / ; ? + '
jsp路径参数: encodeURI(encodeURI(中文,"utf-8"),"utf-8")
action: URLDecoder.decode(URLDecoder.decode(request.getParameter("中文"), "utf-8"), "utf-8")
=======================================================================================
=======================================================================================
=======================================================================================
=======================================================================================
第二种 过滤器:
在国内项目中,经常会遇到页面传值为中文的情况,经常会遇到乱码。
现将我之前的一个过滤器放上来,供大家参考。(注:版权声明中的CottSoft.com为本人的项目,所以大家放心用。)
定义过滤器:
CharacterEncodingFilter.java
/*
**************************************************************************
* Confidentiality Information:
*
* This module is the confidential and proprietary information of
* CottSoft.com; it is not to be copied, reproduced, or
* transmitted in any form, by any means, in whole or in part,
* nor is it to be used for any purpose other than that for which
* it is expressly provided without the written permission of
* CottSoft.com.
*
**************************************************************************
*
* PROGRAM DESCRIPTION:
*
* This filter is processing Chinese code.
*
*
*
**************************************************************************
* CHANGE HISTORY:
*
* Date: by: Reason:
* YYYY-MM-DD IN Reason text.
*
* 2010-11-28 Simon Hoo Initial Version.
*************************************************************************
*/
package com.cottsoft.common.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;
public class CharacterEncodingFilter implements Filter {
private String encoding = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding);
filterChain.doFilter(request, response);
}
public void destroy() {
// destroy code.
}
}
配制web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>cottsoft</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>com.cottsoft.common.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>GBK</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>