请求大家帮助,我遇到的问题是:
jsp页面提交的数据存到数据库是乱码
直接在数据库中插入中文的话也是乱码
显示在jsp页面的更是乱码了
我查了很多方法,可就是解决不了这个问题,麻烦大家帮忙一下,在此谢了
------解决方案--------------------
jsp页面显示乱码是因为contenttype设置的问题
数据库中插入是乱码,是因为安装数据库时没有设置好,最好设置成UTF8
提交数据是乱码,需要过滤器,同时看看tomcat的server.xml配置文件 在<connector Port = 8080 的最 后加上URIEncoding="UTF-8"
------解决方案--------------------
- Java code
<filter> <filter-name>char</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>char</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
------解决方案--------------------
------解决方案--------------------
spring中有自带的字符过滤器
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
另外 数据库编码自己改一下
------解决方案--------------------
设置过滤器:
UnicodeFilter.java
package org.util;
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 UnicodeFilter implements Filter
{
String encoding="";
public void destroy()
{
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("encoding");
if (org.apache.commons.lang.StringUtils.isEmpty(encoding))
{
encoding="UTF-8";
}
config.getServletContext().setAttribute("encoding", encoding);
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse,
* javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain)
throws IOException, ServletException
{
req.setCharacterEncoding(this.encoding);
chain.doFilter(req,res);
}
}
web.xml加:
<filter>
<description>UnicodeFilter</description>
<display-name>UnicodeFilter</display-name>
<filter-name>UnicodeFilter</filter-name>
<filter-class>org.util.UnicodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UnicodeFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>UnicodeFilter</filter-name>