2008-03-29
将应用从weblogic移植到tomcat时学习了一个servlet规范
关键字: servlet规范, form-accept-charset
一个遗留web网页(gbk编码)需要POST方式请求一个servlet应用(utf-8), 得写一个filter进行转码。关于form表单提交的编码问题,可以参考这个:Java Web应用的form文字编码问题,或者在IE下面指定表单编码方式。
参照上面的第一个文档,这个filter很好写:
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws
IOException, ServletException {
if(servletRequest.getCharacterEncoding()==null){
String encoding = servletRequest.getParameter("_charset_");
if(encoding == null){
encoding = _defaultEncoding;
}
if (encoding != null) {
servletRequest.setCharacterEncoding(encoding);
if(LOG.isDebugEnabled()){
LOG.debug("Set request encoding to:"+encoding);
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
这段代码在weblogic8下面应用良好,gbk编码的网页很好的提交到一个utf-8编码的servlet应用,没有其他的额外转码工作。
当移植到tomcat6下面时,乱码问题出现了。即使通过一个utf-8编码的网页来提交依然乱码,request中的数据是iso-8859-1编码。很久没有碰到过乱码问题了,于是我又不自信起来,找了每个环节的问题。最后终于发现是这个filter没有遵循一个servlet规范,ServletRequest的文档中说:
setCharacterEncoding
public void setCharacterEncoding(java.lang.String env)
throws java.io.UnsupportedEncodingException
- Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
- Parameters:
- a - String containing the name of the chararacter encoding.
- Throws:
- java.io.UnsupportedEncodingException - if this is not a valid encoding
我错了,光看方法名字就开练,api文档看得不够仔细。在这次迁移工作中,学了这么一个servlet规范。
发表评论
- 浏览: 1865 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
SNA方案之session store ...
我的解决方案: 1、MemcachedServer 使用类似 RIAD的 0+1 ...
-- by tangchao -
SNA方案之session store ...
robbin 写道sorphi 写道 使用mysql memory table, ...
-- by sorphi -
SNA方案之session store ...
neptune 写道我也有个方案,不知行不行(程序已实现,就是还没有实际用过) ...
-- by robbin -
SNA方案之session store ...
sorphi 写道 使用mysql memory table,我需要在存放ite ...
-- by robbin -
SNA方案之session store ...
我也有个方案,不知行不行(程序已实现,就是还没有实际用过)memcached部分 ...
-- by neptune






评论排行榜