我有一些UTF-8的问题。我的客户(在GWT实现)提出请求,以我的servlet,在URL中的一些参数应用,如下:request.getQueryString()似乎需要一些编码
http://localhost:8080/servlet?param=value
当我取回URL servlet的,我有一些问题,UTF-8字符。 我用这个代码:
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String reqUrl = request.getRequestURL().toString();
String queryString = request.getQueryString();
System.out.println("Request: "+reqUrl + "?" + queryString);
...
所以,如果我把这个网址:
http://localhost:8080/servlet?param=così
的结果是这样的:
Request: http://localhost:8080/servlet?param=cos%C3%AC
我能做些什么来设置正确的字符编码?
#1中的URIEncoding设置位于Tomcat的server.xml中。其他servlet容器应合理地具有相同的设置。 – schematic 2010-06-12 17:10:48
对于#2,您不能再使用request.getParameter()方法,因为该方法将检索错误解码的参数。您必须将解码后的查询字符串(由getQueryString()生成)手动解析(例如,按照&符号分割字符'&',然后将结果字符串拆分第一个等号'=')。 – schematic 2010-06-12 17:13:52
我遇到了server.xml设置的问题。在Windows机器上它工作正常,但在我们生产的基于Red Hat的机器上,Tomcat似乎忽略了server.xml设置。我们最终不得不实现我们自己的查询参数解析器,它使用UTF-8明确解码它。 – Herms 2010-06-12 17:29:01