最近写项目要用到JSP,但是总是往数据库里存数据的时候老师乱码,没办法,只好找点资料,以下就是三种最常见的解决办法
- 修改Tomcat里server.xml文件
- 使用ChartFilter(过滤器编程)
- 向数据库中存入数据乱码问题
##(1)修改Tomcat里server.xml文件
打开你的tomcat安装的位置,找到conf,再找到server.xml文件,打开你会看到
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
类似于这种东西,Blablabla~~~就不复制了,但是咱们只要找到
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="utf-8"
/>
这一部分就行了,看看我的与你们的有区别没,哈哈哈,对就是
URIEncoding="utf-8"
这一行就行了,当然如果觉得这样没有技术含量的话还有第二种方法
##(2)使用ChartFilter(过滤器编程)
嘿嘿嘿,看完了这一点,连过滤器编程都会了
迎面而来就是一个.java
,来看一看这个应该写啥
public class ChartFilter implements Filter {
String encoding = "";
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
String en = config.getInitParameter("encoding");
if(en!=null){
this.encoding = en;
}
}
}
没错就是这么多了,当然老师讲过,过滤器要去web.xml
中配置
(⊙o⊙)…我怕小白找不到,虽然我也是
打开看看应该咋配置
<web-app
...
>
......
<filter>
<filter-name>ChartFilter</filter-name>
<filter-class>com.newcapec.filter.ChartFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ChartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
......
</web-app>
我不知道你们看懂了没反正我是看懂了。对,加张图
这下不能再清楚了!!!再见祝君好运
嗨呀我又来了,虽然你设置好了,但是数据从web端向数据库存的时候还是又一次转码,那么问题来了
##(3)向数据库中存入数据乱码问题
向你的数据库地址中加入
useUnicode=true&characterEncoding=UTF-8
接起来就是这样了
String URL="jdbc:mysql://localhost:3306/[你的数据库名]?useUnicode=true&characterEncoding=UTF-8";
如果使用的是Hibernate,那也好办,在配置文件中加入
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
就OK啦,如下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/smart_home_service
</property>
<property name="connection.username">XXX</property>
<property name="connection.password">XXX</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
MySQLconnection
</property>
<!--在这在这在这-->
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="c3p0.timeout">20000</property>
<mapping resource="..." />
</session-factory>
</hibernate-configuration>