---
title: "Java Web 开发过程中遇到中文乱码处理方法"
author: "susam"
date: "2015-04-01T14:02:00Z"
tags: []
categories: ["编程语言"]
slug: "java-web"
draft: false
lastmod: "2015-04-01T14:02:00Z"
id: 2197675
url: "https://susam.iteye.com/blog/2197675"
---

**1.数据库编码不一致导致乱码**

解决方法：

首先查看数据库编码，输入： 

1. show variables like "%char%";

 

![](http://dl2.iteye.com/upload/attachment/0107/1766/e21babe0-e03d-38b2-834f-ac0d25370cad.png)  
 

确认编码一致，如果不一致，可输入：

1. SET character\_set\_client='utf8';
2. SET character\_set\_connection='utf8';
3. SET character\_set\_results='utf8';

也可设置成gbk编码;

也可以在安装Mysql目录下修改my.ini文件

1. default-character-set=utf-8

**2.jsp页面乱码问题**

在myeclipse中jsp的默认编码为ISO-8859-8；

只需在页面头部修改为

1. &lt;%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %&gt;

在JSP页面头部加入下面这句话，告诉浏览器应该调用UTF-8的字符集。

1. &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;

**3.jsp连接数据库存入中文乱码**

在数据库连接时

1. jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

如果使用框架连接则把头文件都修改成UTF-8编码即可

**4.在使用struts2可使用过滤器：**

先变写一个过滤器

01. package com.oumyye.util;
02. import java.io.IOException;
03. import javax.servlet.Filter;
04. import javax.servlet.FilterChain;
05. import javax.servlet.FilterConfig;
06. import javax.servlet.ServletException;
07. import javax.servlet.ServletRequest;
08. import javax.servlet.ServletResponse;
09. public class CharacterEncodingFilter implements Filter{
10. protected String encoding = null;
11. protected FilterConfig filterConfig = null;
12. public void init(FilterConfig filterConfig) throws ServletException {
13. this.filterConfig = filterConfig;
14. this.encoding = filterConfig.getInitParameter("encoding"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (encoding != null) { request.setCharacterEncoding(encoding); response.setContentType("text/html; charset="+encoding); } chain.doFilter(request, response); } public void destroy() { this.encoding = null; this.filterConfig = null;
15. }
16. }

在web.xml中配置 

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;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\_3\_0.xsd" version="3.0"&gt;
03. &lt;display-name&gt;0001web&lt;/display-name&gt;
04. &lt;!-- 中文编码过滤器 --&gt;
05. &lt;filter&gt;
06. &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
07. &lt;filter-class&gt;com.oumyye.util.CharacterEncodingFilter&lt;/filter-class&gt;
08. &lt;init-param&gt;
09. &lt;param-name&gt;encoding&lt;/param-name&gt;
10. &lt;param-value&gt;UTF-8&lt;/param-value&gt;
11. &lt;/init-param&gt;
12. &lt;/filter&gt;
13. &lt;filter-mapping&gt;
14. &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
15. &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;
16. &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
17. &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
18. &lt;/filter-mapping&gt;

在表单中只能使用post传值,此方法对于get无效。

**5 处理单个字符串的中文乱码问题**

1. String newnewname=new String(name.getBytes("iso-8859-1"),"utf-8"))

 

**附：JSP中的编码设置**

**1. pageEncoding:&lt;%@ page pageEncoding="UTF-8"%&gt;**

设置JSP编译成Servlet时使用的编码    

**2. contentType: &lt;%@ page contentType="text/html; charset=UTF-8"%&gt;**

对服务器响应进行重新编码，即jsp的输出流在浏览器中显示的编码 

**3. html页面charset:&lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;**

网页的编码信息 ，说明页面制作所使用的编码

**4. request.setCharacterEncoding()  -- 可用在servlet和jsp页面中**

作用是设置对客户端请求进行重新编码的编码，即post方式提交的数据进行编码。

**5. response.setCharacterEncoding() -- 可用在servlet和jsp页面中**

对服务器响应进行重新编码，即jsp的输出流在浏览器中显示的编码，与&lt;%@ page contentType="text/html;charset=UTF-8"%&gt;一样

**6. response.setContentType() -- 可用在servlet和jsp页面中**

对服务器响应进行重新编码，即jsp的输出流在浏览器中显示的编码，与&lt;%@ page contentType="text/html;charset=UTF-8"%&gt;一样 

**7.response.setHeader("Content-Type","text/html;charset=UTF-8");   -- 可用在servlet和jsp页面中**

与&lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;一样