servlet中的编码问题 [英] encoding problem in servlet
问题描述
我有一个servlet从客户端接收一些参数,然后做一些工作。
来自客户端的参数是中文,所以我经常在servet中得到一些无效字符。
例如:
如果我输入
http:// localhost:8080 / Servlet?q =中文& type = test
然后在servlet中,'type'的参数是正确的),但是'q'的参数不正确编码,它们变成无法解析的无效字符。
但是,如果我再次输入加法器栏,更改为:
http:// localhost:8080 / Servlet?q =%D6%D0%CE%C4& type = test
现在我的servlet将获得'q'的正确参数。
有什么问题?
UPDATE
/ p>
BTW,当我发送带有帖子的表单时,它很好。
当我在ajax中发送它们时,例如:
url =http://..q= '中文',
xmlhttp.open(POST,url,true);
然后服务器端也得到无效字符。
似乎只是当中文字符编码为%xx,服务器端可以得到正确的结果。 p>
这就是说 http://.../q=中文
不起作用,
http://.../q=%D6%D0%CE%C4
工作。
但是为什么 http://www.google.cn.hk/search?hl=zh-CN&newwindow=1&safe=strict&q=%E4%B8%AD%E6%96%87&btnG= Google +%E6%90%9C%E7%B4%A2& aq = f& aqi =& aql =& oq =& gs_rfai =
work?
确认使用表单本身的页面的编码也是UTF-8,并且确保浏览器被指示将页面读取为UTF-8。假设它是JSP,只是把它放在页面的最顶部,以实现: / p>
<%@ page pageEncoding =UTF-8%>然后,要将GET查询字符串作为UTF-8处理,请确保所讨论的servletcontainer配置为:这样做。目前还不清楚你使用的是哪一个,所以这里有一个Tomcat示例:设置 URIEncoding
属性-6.0-doc / config / http.html>< Connector>
元素。/conf/server.xml
到UTF-8
。< Connector URIEncoding =UTF-8>
对于要使用POST的情况,您需要确保
HttpServletRequest
被指示使用UTF-8解析POST请求主体。请求.setCharacterEncoding(UTF-8);
在访问第一个参数之前调用此
另请参阅:
I have a servlet which receive some parameter from the client ,then do some job. And the parameter from the client is Chinese,so I often got some invalid characters in the servet. For exmaple: If I enter
http://localhost:8080/Servlet?q=中文&type=test
Then in the servlet,the parameter of 'type' is correct(test),however the parameter of 'q' is not correctly encoding,they become invalid characters that can not parsed.
However if I enter the adderss bar again,the url will changed to :
http://localhost:8080/Servlet?q=%D6%D0%CE%C4&type=test
Now my servlet will get the right parameter of 'q'.
What is the problem?
UPDATE
BTW,it words well when I send the form with post. WHen I send them in the ajax,for example:
url="http://..q='中文',
xmlhttp.open("POST",url,true);
Then the server side also get the invalid characters.
It seems that just when the Chinese character are encoded like %xx,the server side can get the right result.
That's to say http://.../q=中文
does not work,
http://.../q=%D6%D0%CE%C4
work.
But why "http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&q=%E4%B8%AD%E6%96%87&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&aqi=&aql=&oq=&gs_rfai=
" work?
Ensure that the encoding of the page with the form itself is also UTF-8 and ensure that the browser is instructed to read the page as UTF-8. Assuming that it's JSP, just put this in very top of the page to achieve that:
<%@ page pageEncoding="UTF-8" %>
Then, to process GET query string as UTF-8, ensure that the servletcontainer in question is configured to do so. It's unclear which one you're using, so here's a Tomcat example: set the URIEncoding
attribute of the <Connector>
element in /conf/server.xml
to UTF-8
.
<Connector URIEncoding="UTF-8">
For the case that you'd like to use POST, then you need to ensure that the HttpServletRequest
is instructed to parse the POST request body using UTF-8.
request.setCharacterEncoding("UTF-8");
Call this before you access the first parameter. A Filter
is the best place for this.
See also:
这篇关于servlet中的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!