场景
中文windowsxp,区域设置为中国,
服务器Tomcat6.0,数据库MySql5.0,数据表charset设置为utf8
一个JSP网页提交请求,由Servlet负责把数据插入数据库,当中配有filter负责转码(采用对Request进行Wrap的方式,没有直接设置encoding)
JSP页面关于编码的书写如下
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
Filter配置正确也正常工作
JSP提交request请求采用Form的post方法提交问题如下
1、为什么request.getCharacterEncoding()返回值是null
2、为什么对中文字段参数的获取new String(super.getParameter(name).getBytes("gb2312"), "utf-8")执行结果是乱码,最终插入到数据库中也是乱码
3、httprequest当中的编码到底是什么小弟初学者,这个问题困扰了我几天了。恳请各位大虾赐教。
谢谢!
中文windowsxp,区域设置为中国,
服务器Tomcat6.0,数据库MySql5.0,数据表charset设置为utf8
一个JSP网页提交请求,由Servlet负责把数据插入数据库,当中配有filter负责转码(采用对Request进行Wrap的方式,没有直接设置encoding)
JSP页面关于编码的书写如下
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
Filter配置正确也正常工作
JSP提交request请求采用Form的post方法提交问题如下
1、为什么request.getCharacterEncoding()返回值是null
2、为什么对中文字段参数的获取new String(super.getParameter(name).getBytes("gb2312"), "utf-8")执行结果是乱码,最终插入到数据库中也是乱码
3、httprequest当中的编码到底是什么小弟初学者,这个问题困扰了我几天了。恳请各位大虾赐教。
谢谢!
解决方案 »
- jsp的cookie如何使用,在线等````````
- 什么开源缓存框架用的最多?
- jspsmartupload组件上传问题
- [求教]多选框checkbox提交后,getAttribute得到的是什么类型数据?
- tomcat下,用sqlserver使用连接池的问题,急用!!!
- 向各位大侠求救 关于fckeditor的问题
- 如何将jar做成.exe可执行文件
- jsp下如何用ip和端口连接远程sql server 7.0数据库
- 各位大神,小白求教!!!能帮帮忙看下这个错误吗?
- ireport5.6 line chart y轴刻度怎么去掉小数点
- 当文本框输入 123 456 的时候 就把当前的值给保存下来 我这个程序已经实现可以把当前输入的值保存下来 第二次提交有空格的地方就多了?号
- 网站首页如何做 !!!!!!!!!!!!!!!!!!!!!!!!!!!!! !
问题二,new String(super.getParameter(name).getBytes("gb2312"), "utf-8"),红色部分好像不是gb2312,具体是多少,忘了~呵。这是把取得的参数重构造成一个新的String.
问题三, contentType="text/html; charset=GB2312"这句是告之浏览器该用什么编码是处理它的,pageEncoding="UTF-8"是该页面的编码格式.httprequest的编码到底是什么,这个倒不清楚。
解决方案一,
如果用POST方式的话,用request.setCharacterEncoding()这样可以处理编码问题,要注意的是这里的编码应该和contentType="text/html; charset=GB2312",pageEncoding="UTF-8"都一致,这样不会出现冲突而导致编码问题。
解决方案二。
上面针对get方式无效,这样可以在tomcat下的conf/server.xml找到:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
在最后加上URIEncoding="UFT-8"这样就行了,
String name = request.getParameter("name"); 这样就可以了2 当中配有filter负责转码(采用对Request进行Wrap的方式,没有直接设置encoding)
你到底有没有设置啊? 增加这个
request.setCharacter("UTF-8"); // 建议你全部改成 UTF-8,和你数据库的相同,免去很多麻烦
<filter>
<filter-name>encoding</filter-name>
<filter-class>
filter.FilterEncoding
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>package filter;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 FilterEncoding implements Filter
{
protected String encoding; // 接收字符编码
protected boolean ignore; // 是否忽略大小写
protected FilterConfig filterConfig; // 初始化配置 public void init(FilterConfig filterConfig) throws ServletException
{
// 从web.xml文件中读取encoding的值
encoding = filterConfig.getInitParameter("encoding");
// 从web.xml文件中读取ignore的值
String value = filterConfig.getInitParameter("ignore");
// 以下三种情况均为忽略大小写
if(value == null)
{
ignore = true;
}
else if(value.equalsIgnoreCase("yes"))
{
ignore = true;
}
else if(value.equalsIgnoreCase("true"))
{
ignore = true;
}
}
// doFilter方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if(ignore || request.getCharacterEncoding() == null)
{
// 如果为空先从web.xml中得到
String encoding = selectEncoding(request);
if(encoding != null)
{
// 设置字符集编码
request.setCharacterEncoding(encoding);
}
}
// 继续执行
chain.doFilter(request, response);
}
// 得到字符编码
private String selectEncoding(ServletRequest request)
{
return encoding;
}
public void destroy()
{
}
}
如果是url的get提交参数带中文的
可以改下面这个文件,加入一个URIEncoding="GBK"
X:\Tomcat 5.5\conf\Server.xml
<Connector
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="30"
maxThreads="150"
maxSpareThreads="75"
URIEncoding="GBK"> jsp头文件<%@ page contentType="text/html;charset=gbk"%> //此处的charset的值要和web.xml里的 <param-value></param-value>值一样但个类的乱码可以这样来解决:tempStr = new String(str.getBytes("iso-8859-1"),"gb2312");
在接受时应为
new String(request.getParameter(name).getBytes("iso-8859-1"), "gbk") 还可配制过滤器
<filter>
<filter-name>encoding</filter-name>
<filter-class>
filter.FilterEncoding
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<filter-class>所写的java 文件的路径</filter-class>
</filter-mapping>
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]port=3306[mysql]default-character-set=gbk# 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=gbk
在filter当中使用request.setCharacterEncoding("gb2312");是可以解决编码问题的。
使用wap方式(我猜应该是Java动态代理),tempStr = new String(str.getBytes("iso-8859-1"),"gb2312");也可以解决
但是还有几个疑惑,为什么UTF-8就不行了?
从目前的得到的资料来看,UTF-8的字符集不是大于GB2312吗?而且UTF-8是三字节编码,应该说GB2312能够编码得出的字符,UTF-8就行的。
MySql的表设计中,字符集明明也是UTF-8,为什么UTF-8就是乱码?
另外,tempStr = new String(str.getBytes("iso-8859-1"),"gb2312");这句代码是否说明HttpRequest默认是iso-8859-1编码?
小弟菜鸟,请各位别嫌我啰嗦。