博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
开发过程遇到的中文乱码问题如何解决
阅读量:7048 次
发布时间:2019-06-28

本文共 3311 字,大约阅读时间需要 11 分钟。

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

解决方法:

首先查看数据库编码,输入:

 
  1. show variables like "%char%"

确认编码一致,如果不一致,可输入:

 
  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. <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 

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

 
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   

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

在数据库连接时

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

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

4.在使用struts2可使用过滤器:

先变写一个过滤器

 
  1. package com.oumyye.util; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import javax.servlet.Filter; 
  6. import javax.servlet.FilterChain; 
  7. import javax.servlet.FilterConfig; 
  8. import javax.servlet.ServletException; 
  9. import javax.servlet.ServletRequest; 
  10. import javax.servlet.ServletResponse; 
  11.  
  12. public class CharacterEncodingFilter implements Filter{ 
  13.  
  14.     protected String encoding = null
  15.     protected FilterConfig filterConfig = null
  16.  
  17.     public void init(FilterConfig filterConfig) throws ServletException { 
  18.         this.filterConfig = filterConfig; 
  19.         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 = nullthis.filterConfig = null
  20.     } 

在web.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_3_0.xsd" version="3.0"
  3.   <display-name>0001web</display-name> 
  4.   <!-- 中文编码过滤器 --> 
  5.     <filter> 
  6.         <filter-name>CharacterEncodingFilter</filter-name> 
  7.         <filter-class>com.oumyye.util.CharacterEncodingFilter</filter-class
  8.         <init-param> 
  9.             <param-name>encoding</param-name> 
  10.             <param-value>UTF-8</param-value> 
  11.         </init-param> 
  12.     </filter> 
  13.     <filter-mapping> 
  14.         <filter-name>CharacterEncodingFilter</filter-name> 
  15.         <url-pattern>/*</url-pattern> 
  16.         <dispatcher>REQUEST</dispatcher> 
  17.         <dispatcher>FORWARD</dispatcher> 
  18.     </filter-mapping> 

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

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

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

附:JSP中的编码设置

1. pageEncoding:<%@ page pageEncoding="UTF-8"%>

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

2. contentType: <%@ page contentType="text/html; charset=UTF-8"%>

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

3. html页面charset:<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

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

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

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

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

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

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

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

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

与<META http-equiv="Content-Type" content="text/html; charset=UTF-8">一样

作者:偶my耶

来源:51CTO

转载地址:http://secol.baihongyu.com/

你可能感兴趣的文章
MVC验证12-使用DataAnnotationsExtensions对整型、邮件、最小值、文件类型、Url地址等验证...
查看>>
Source not found
查看>>
【CLRS】《算法导论》读书笔记(一):堆排序(Heapsort)
查看>>
支持类型过滤的枚举器
查看>>
HDU 4275 Color the Tree(树同构)
查看>>
php里Array2xml
查看>>
以boost::function和boost:bind取代虚函数
查看>>
oracle监听器(listener)配置心得
查看>>
wince -- RS485半双工实现
查看>>
nginx 源码学习笔记(二)——nginx精粹-模块
查看>>
DirectX截图黑屏的解决办法
查看>>
Expanding Cat5e LAN segments over 100 metres using OUTREACH Ethernet LAN extenders
查看>>
关于Oracle冷备份与热备份的对比
查看>>
C#条件判断-嵌套if结构
查看>>
Selenium:Hello,World!
查看>>
HibernateTemplate 查询
查看>>
ListView控件的基本操作
查看>>
jQuery 参考手册 - 属性操作
查看>>
C–gcc命令行下的参数
查看>>
mysql 连接查询 和 子查询
查看>>