在Servlet中显示阿拉伯字符

时间:2015-04-02 22:42:40

标签: java servlets unicode utf-8 character-encoding

我在servlet中使用以下两行来显示阿拉伯语URL参数

response.setContentType("text/html; charset=UTF-8");

String s = new String(request.getParameter("p").getBytes("8859_1"), "UTF-8");

请注意,在上面的代码中,如果我将参数p作为阿拉伯字符传递,例如:

http://localhost/sample/MyServlet?p=عربي

然后它显示返回,如??????字符

任何建议都将受到赞赏

2 个答案:

答案 0 :(得分:0)

由于参数“p”是阿拉伯字符,因此您不应将其视为8859_1编码字符,因为它与阿拉伯字符不对应 - http://en.wikipedia.org/wiki/ISO/IEC_8859-1 尝试使用UTF-8编码来获取字节。

答案 1 :(得分:0)

conf 文件夹中的 Tomcat server.xml 修改投放

像这样的编码

<强>的URIEncoding = “UTF-8”

<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->

<Connector URIEncoding="UTF-8" connectionTimeout="20000" maxPostSize="67589953" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>

如果您使用 https

做同样的事情

<!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
<Connector URIEncoding="UTF-8" SSLEnabled="true" clientAuth="false"  ...