如何处理参数值中的特殊字符? [英] How to handle special chars in parameter values?

查看:208
本文介绍了如何处理参数值中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在GET参数中使用特殊字符(如Æ,ØogÅ)调用Java Servlet ,我有一些问题: http:// localhost:8080 / WebService / MyService?test =Øst。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆