我是一个新手,最近在tomcat6.0+MyEclipse6.5+MSSQL2000下开发一个网站,可是不知道为什么form表单提交的中文数据出现乱码(我是在MyEclipse控制台下看到提交的数据是乱码的)。当然保存到数据库中也变成了乱码。项目统一采用UTF-8编码。各位大侠帮我看一下到底出了什么问题:
这是我的JSP文件头:
<%@ page contentType="text/html; charset=UTF-8" language="java"
import="java.util.*" pageEncoding="UTF-8" errorPage=""%>
均设置了UTF-8编码。
同时我还写了一个过滤器:SetCharacterEncodingFilter
package d8city.jskr.EncodingFilter;
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 {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
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下面也进行了相关配置:
Set Character Encoding
d8city.jskr.EncodingFilter.SetCharacterEncodingFilter
encoding
UTF-8
ignore
true
Set Character Encoding
/*
是不是我还有什么没有进行配置,提交的中文数据总是出现乱码。谢谢各位了,这问题已经缠了我几天了,
同时,我吧页面的编码全部改成gb2312后提交的数据便是中文。这是为什么。
[b]那要看你用什么方式提交的,如果是get方式,就算写Filter也不行,因为Filter针对的是post方式提交的数据,而get方式就不行了,如果用get方式必须要转码.建议用post方式。[/b]
[quote] <%@ page contentType="text/html; charset=UTF-8" language="java"
import="java.util.*" pageEncoding="UTF-8" errorPage=""%> [/quote]
你的页面编码格式是UTF-8的,你要在提交后台看到中文,那你在后台就要对其进行编码的格式转换了,如: String ss = new String(ss1.getBytes("UTF-8"),"GBK")
你肯定用了get请求,如果用post就不会有乱码,如果想用get,就请进入tomcat目录下的conf目录,找到server.xml,打开,找到里面你tomcat默认的端口配置的位置,一般默认是8080。类似于这样的一个标签:
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" [color=red]URIEnocking="utf-8"[/color]/>
在其中加入红色部分属性,搞定get请求中文乱码
一楼二楼都可行。
你的配置没问题的。
编码处理就是这样处理的,看不出配置有什么问题。难道真的是用GET?你form的method注意一下看是不是忘了写了,默认是GET的,改成POST.
另外数据库也要配置成UTF-8编码格式的。
前两天我也是get提交有问题,post没问题
hiphunter921 说的很对 呵呵
建议用get请求提交数据的时候使用encode转一下