中文乱码

来源:互联网 发布:淘宝达人帖子教程 编辑:程序博客网 时间:2024/04/26 23:17

request.setCharacterEncoding("UTF-8")无效

2014-11-09 12:37射手slim | 浏览 6488 次
 互联网
我在servlet中写了request.setCharacterEncoding("UTF-8"),然后在网页地址后面加了?name=张三,测试了一下,结果发现request.setCharacterEncoding("UTF-8")并没有解决乱码问题,请问这是怎么回事?
举报| 2014-11-09 13:37网友采纳
你用是Tomcat 吗???如果是的话有下面几个地方需要你看看是否配置正确:(1)Tomcat 的 conf 下的server.xml文件中将<Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" />里添加上后面3个编码的设置<Connector port="8080" protocol="HTTP/1.1"                                   connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true"IEncoding="utf-8" URIEncoding="utf-8"/>(2)然后在servlet中配置如下:如果你用的是   post方式解决方案:     request.setCharacterEncoding("utf-8");     response.setCharacterEncoding( "utf-8" );get方式       String string = request。getParamers("");       String = new String(string.getBytes("ISO8859-1","utf-8"));(3)jsp页面中  pageEncoding="UTF-8"(4)如果你用的是Eclipse或者MyEclipse的话,那么你在window 下的preferences中的左上角的输入框中输入spelling 在spelling这个页面中看看Encoding 默认是否设置为utf-8
追问:
这是什么原因呢?我不是通过程序改过编码了吗?
追答:
是这样,链接的话会被默认进行相关的编码处理,所以如果你的链接后面有中文的话,编码处理之后就会出现乱码,如果你在servlet中接收的时候不做处理的话,中文就还是乱码啊,为了方便的话,你可以在我说的四个地方看看将编码统一起来
1 0
原创粉丝点击