通过JSP页面向MySQL数据库插入中文数据时乱码问题
刚开始插入数据时,mysql数据库表内显示???,读取时同样也是???
对此查找了相关资料,首先注意保持服务器、数据库、项目、以及前端编码一致。
具体做法是:
一、项目编码,Eclipse设置。
Eclipse Java EE IDE for Web Developers ->右键项目 -> Properties -> Resource -> Text file encoding ->other utf-8 ->apply and close
二、tomcat服务器编码设置(conf\server.xml)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
三、数据库mysql的my.ini配置文件
[client] port = 3306 default-character-set=utf8 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash ; Remove the next comment character if you are not familiar with SQL ;safe-updates default-character-set=utf8 [isamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer_size = 2M write_buffer_size = 2M [myisamchk] key_buffer_size = 20M sort_buffer_size_size = 20M read_buffer_size = 2M write_buffer_size = 2M [mysqlhotcopy] interactive-timeout [mysqld] port = 3306 character-set-server = utf8
修改文件后需要使用net stop mysql/net start mysql关闭开启mysql服务重启mysql服务
四、项目
检查项目中有前端编码的地方
a. JSP页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
b. Servlet服务器端
response.setCharacterEncoding("UTF-8");
c. 其它有编码的地方。
把所有的编码设置一致。本例是为UTF-8.
通过以上步骤还款解决的话,可以在项目中加一个Filter:
package com.kh.utils; 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 SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } // Pass control on to the next filter chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }
在Web.xml中加入配置代码:
<filter> <filter-name>encodingFilter</filter-name> <filter-class>com.kh.utils.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
相关推荐
主要介绍了JSP MySQL插入数据时出现中文乱码问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文给大家介绍的是如何解决win平台下开发的项目移植到Linux平台后,向MySQL插入数据时中文出现乱码的问题,非常的简单实用,有需要的小伙伴可以参考下
在mysql中插入中文乱码,或mysql中中文正常显示,但jsp在前台显示mysql中的中文时乱码。 解决方法: 进入mysql控制台,执行下面的命令即可: SET character_set_client='utf8'; SET character_set_connection='utf8...
7.3.3 JDBC连接MySQL数据库 7.4 JDBC核心API 7.4.1 代表数据库连接的Connection接口 7.4.2 驱动程序管理器DriverManager类 7.4.3 执行SQL语句的Statement接口 7.4.4 预编译执行SQL语句的PreparedStatement接口 7.4.5...
常见问题 读取Properties文件出现中文乱码 182 第9章 Java异常处理与反射机制 183 实例73 运用throws、throw、try与catch 183 实例74 throws声明异常的实例 185 实例75 自定义异常类 187 实例76 使用finally...
8.4设 计 过 程 8.4.1数据库及数据表的设计 在MySQL中建一个数据库,命名为myDatabase,在该数据库中建一个表,命名为user,表结构为name(varchar)、password(varchar),向该表中插入若干条数据记
{1.11.2.3}插入排序}{40}{subsubsection.1.11.2.3} {1.11.3}递归调用}{41}{subsection.1.11.3} {1.12}Java API}{41}{section.1.12} {1.13}Linux命令}{41}{section.1.13} {1.13.1}基本查看、移动}{41}{...