如何处理参数值中的特殊字符? [英] How to handle special chars in parameter values?
问题描述
I
protected void doGet(HttpServletRequest请求,HttpServletResponse响应)throws ServletException,IOException {
System.out.println(request.getParameterValues(test)[0]);
}
控制台中打印的消息是:Ã?st 。
Web服务应该能够处理这样的调用。如何以正确的方式编码参数值?
这需要在服务器级配置。不清楚你正在使用哪一个,所以我只给Tomcat和Glassfish的例子。
Tomcat :添加 URIEncoding
属性为< Connector>
元素 /conf/server.xml
:
< Connector ... URIEncoding =UTF-8>
Glassfish :添加< parameter-encoding> ;
到 /WEB-INF/glassfish-web.xml
(或 sun-web.xml
对于旧版本):
< parameter-encoding default-charset =UTF- 8/>
另请参见:
I've some issues with my Java Servlet if it's called with special chars (like Æ, Ø og Å) in the GET-parameters: http://localhost:8080/WebService/MyService?test=Øst.
I than have this code in my doGet
:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameterValues("test")[0]);
}
The messages printed in the console is: Ã?st.
The Web Service should be able to handle calls like this. How can I encode the parameter values in a proper way?
This needs to be configured at servet level. It's not clear which one you're using, so I'll give examples for Tomcat and Glassfish only.
Tomcat: add URIEncoding
attribute to <Connector>
element in /conf/server.xml
:
<Connector ... URIEncoding="UTF-8">
Glassfish: add <parameter-encoding>
to /WEB-INF/glassfish-web.xml
(or sun-web.xml
for older versions):
<parameter-encoding default-charset="UTF-8" />
See also:
这篇关于如何处理参数值中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!