`

JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决 5大配置点 使用UTF-8编码

阅读更多
1 struts2配置 2 数据库 3 页面 4 jdbc连接 5 tomcat

以下解决方法不分顺序,大家按照自己的配置对比.

1 struts2配置

struts2在配置struts.properties文件中加上struts.i18n.encoding=UTF-8 或者在 struts.xml里加上<constant name="struts.i18n.encoding" value="UTF-8" /> 有的朋友说加上struts.locale=zh_CN,但是我的struts.locale为en_GB没有问题.

2 数据库

2.1 mysql的表的编码方式,其DDL应该为: ENGINE=InnoDB DEFAULT CHARSET=utf8; 可以使用show create table tbname查看.

2.2 安装mysql数据库时就应该以utf8编码默认安装查看mysql数据库默认编码方式可以在其目录下my.ini文件中找到mysql和mysqld下default-character-set值为utf8或者在mysql数据库命令行下以"\s;"查看.

3 页面

每个页面上加上<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 这样在jsp页面里,点右键,查看编码方式则为UTF-8.

4 jdbc连接

设置characterencoding为UTF-8 如jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF8

5 tomcat

为了保证get/post数据都采用相同的UTF8编码,我们在server.xml中进行了如下设置: <Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"  URIEncoding="UTF-8" />

其中,当当使用IIS作为webserver转发servlet/jsp请求时IIS是通过AJP协议,把请求转发到Tomcat监听的8009端口,所以需要配置端口8009的URIEncoding为UTF-8.

     msyql数据库安装时候需要默认编码方式或者使用MySQL Server Instance Config Wizard 配置,

在设置Configure the MySQL Server 5.0 server instance那里的时候选择Manual Selected Default Character Set / Collation 为 UTF8 而不是默认的 LATIN1 在MYSQL里查看得 mysql> \s;

-------------- E:\MySQL\MySQL Server 5.0\bin\mysql.exe  Ver 14.12 Distrib 5.0.51a, for Win32 (ia32) Connection id:          2 Current database: Current user:          root@localhost SSL:                    Not in use Using delimiter:        ; Server version:        5.0.51a-community-nt MySQL Community Edition (GPL) Protocol version:      10 Connection:            localhost via TCP/IP Server characterset:    utf8 Db    characterset:    utf8 Client characterset:    utf8 Conn.  characterset:    utf8 TCP port:              3306 Uptime:                25 sec Threads: 1  Questions: 4  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.160

    my.ini文件内容为

[mysql] default-character-set=utf8 # SERVER SECTION # ---------------------------------------------------------------------- # # The following options will be read by the MySQL Server. Make sure that # you have installed the server correctly (see above) so it reads this # file. # [mysqld] # The TCP/IP Port the MySQL Server will listen on port=3306 #Path to installation directory. All paths are usually resolved relative to this. basedir="E:/MySQL/MySQL Server 5.0/" #Path to the database root datadir="E:/MySQL/MySQL Server 5.0/Data/" # The default character set that will be used when a new schema or table is # created and no character set is defined default-character-set=utf8 ...........
分享到:
评论

相关推荐

    Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决篇

    Struts Hibernate MyEclipse Tomcat MySQL JSP 乱码 编码出错 内码设定

    公告管理系统(JSP+Struts)(Java EE项目案例)

    (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),...

    Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决

    显示界面,JSP 2.0 JAVA容器:Tomcat 5.X MVC构架:Struts 1.2 ORM工具:Hibernate 3 数据库:MySQL 5.0 编译IDE:MyEclipse 5.0

    网上书店系统(Struts+Hibernate)(Java EE项目案例)

    (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),...

    网上文件管理系统(Struts)(Java EE项目案例)

    (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),...

    个性化定制系统(Ajax+Spring+Hibernate)(Java EE项目案例)

    (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),...

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    全书分4篇,共24章,其中,第1篇为技能学习篇,主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e...

    从J2SE到J2EE知识点介绍

    5. JSP页面通过URL传递中文参数的乱码问题 139 四、 Struts2 140 (一) 基于struts2的web应用的开发步骤 140 1. MVC设计模式() 140 2. 基于struts的web应用的开发步骤 141 3. 如何写自定义action,实体,以及业务类...

    《MyEclipse 6 Java 开发中文教程》前10章

    Tomcat JSP Web 开发中的乱码问题小结 164 第九章 开发Struts 1.x应用 166 9.1 介绍 166 9.2 创建Struts项目 168 9.2.1 创建Web项目 169 9.2.2 加入 Struts开发功能 169 9.3 使用Struts工具 171 9.3.1 Struts配置...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    基于JSP企业车辆管理系统设计软件程序源码+数据库+WORD毕业设计论文文档.zip

    此次设计主要采用MyEclipse加Tomcat后台服务器进行,设计过程中页面主要使用JSP技术完成,下面对MyEclipse、Tomcat和MySQL数据库进行简要介绍。 1.3.1.1 MyEclipse MyEclipse,是一个十分优秀的用于开发Java, J2EE的...

    Java Web应用详解.张丽(带详细书签).pdf

    11.3 Struts 开发中的中文乱码问题 11.4 Action 数据获取与传递 11.5 Struts 表单验证 第12章 标签库及其应用 12.1 Struts标签库基础知识 12.2 Struts标签库应用实例 12.3 JSTL基础知识 12.4 EL表达式基础...

    Java面试宝典2020修订版V1.0.1.doc

    分别在什么情况下使用。 26 9、java后台如何接收ajax数据? 26 10、为什么要有jquery? 26 11、jQuery选择器有多少种? 27 13、你是如何使用jquery中的ajax的? 27 14、jquery中的$.get和$.post请求区别? 27 15、...

Global site tag (gtag.js) - Google Analytics