中文乱码(编码解码)问题的解决办法【转】

 

中文乱码(编码解码)问题的解决办法

个人小结:中文乱码(编码解码)问题的解决办法
在使用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

 

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代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>cottsoft</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.jsp</welcome-file>  
  6.   </welcome-file-list>  
  7.   <filter>  
  8.     <filter-name>CharacterEncodingFilter</filter-name>  
  9.     <filter-class>com.cottsoft.common.filter.CharacterEncodingFilter</filter-class>  
  10.     <init-param>  
  11.         <param-name>encoding</param-name>  
  12.         <param-value>GBK</param-value>  
  13.     </init-param>  
  14.   </filter>  
  15.   <filter-mapping>  
  16.     <filter-name>CharacterEncodingFilter</filter-name>  
  17.     <url-pattern>/*</url-pattern>  
  18.   </filter-mapping>  
  19. </web-app>  
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值