乱码问题 有兴趣的看看 估计大部分都不知道为什么

cfd406635982 2011-01-17 04:22:59

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 EncodingFilter implements Filter {
protected String encoding = null;

protected FilterConfig filterConfig = null;

public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encoding = selectEncoding(request);
// request.getParameter("");放到这儿也是乱码
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
request.getParameter("");// 如果没有的话,则jsp页面提交到后台bean会是乱码
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}

protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}


虽然现在不乱码了 但是不知道原因是什么
前台的编码一致。
系统是win7
只要request.getParameter("");把这句删了就会中文乱码
留着就没事 从来没有遇到过。
web.xml配置放到最前面了
希望高手能不能给出原因
...全文
265 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2011-01-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cfd406635982 的回复:]

引用 7 楼 dracularking 的回复:
直接查调用前后变化了什么
通过getCharacterEncoding

执行前后都是:GBK
[/Quote]
肯定有什么变化了,仔细检查下request对象吧
cfd406635982 2011-01-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dracularking 的回复:]
直接查调用前后变化了什么
通过getCharacterEncoding
[/Quote]
执行前后都是:GBK
dracularking 2011-01-18
  • 打赏
  • 举报
回复
直接查调用前后变化了什么
通过getCharacterEncoding
cfd406635982 2011-01-18
  • 打赏
  • 举报
回复
是post方式
其实就算其他方式是可行的
但是这个问题太纠结了
只要调用getParameter("")就不会乱码了
里面的参数是什么都无所谓 只要调用getParameter方法就不乱码
这是掉方法就乱码了
试了几天了 都这样
licip 2011-01-18
  • 打赏
  • 举报
回复
你用的服务器如是tomcat,看你提交的方式了,如果是post那用过滤器来实现,如果是get那要改配置文件,把tomcat的配置文件:server.xml的<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="gbk"/>加上一个:URIEncoding="GBK"来解决
jsut_yi 2011-01-18
  • 打赏
  • 举报
回复
你是用的post方式吗?
cfd406635982 2011-01-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liubowin 的回复:]
要把ServletRequeset转成HttpServletRequeset对象的request对象

HttpSServletRequeset requeset = (HttpServletRequeset)requeset;
[/Quote]
不是这个问题 转化了也是一样的
shrimp0576 2011-01-18
  • 打赏
  • 举报
回复
if (!parametersParsed)
parseParameters();//这个方法就是执行参数编码

return coyoteRequest.getParameters().getParameter(name);

这是getParameter 全部源码,判断条件parametersParsed默认是flase,也就是说默认是要先编码的,编码逻辑以及顺序在parseParameters方法里,request里不止一处执行parseParameters()这个方法,只要执行之后parametersParsed就是true了,所以就不会再编码了。

这些在tomcat的java\org\apache\catalina\connector\Request里有的
天空win 2011-01-17
  • 打赏
  • 举报
回复
要把ServletRequeset转成HttpServletRequeset对象的request对象

HttpSServletRequeset requeset = (HttpServletRequeset)requeset;
falconkabu 2011-01-17
  • 打赏
  • 举报
回复
你重新 设置了request 编码 再次使用getParameter("")是获取 你重新设置的编码吧 不是很清楚 随便理解的

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧