Java 在servlet中显示阿拉伯语字符
我在servlet中使用下面两行来显示阿拉伯语URL参数Java 在servlet中显示阿拉伯语字符,java,servlets,unicode,utf-8,character-encoding,Java,Servlets,Unicode,Utf 8,Character Encoding,我在servlet中使用下面两行来显示阿拉伯语URL参数 response.setContentType(“text/html;charset=UTF-8”) String s=新字符串(request.getParameter(“p”).getBytes(“8859_1”),“UTF-8”) 请注意,在上述代码中,如果我将参数p作为阿拉伯字符传递,例如: http://localhost/sample/MyServlet?p=عربي 然后它将返回显示为??????人物 如果您有任何建议,我
response.setContentType(“text/html;charset=UTF-8”)代码>
String s=新字符串(request.getParameter(“p”).getBytes(“8859_1”),“UTF-8”)代码>
请注意,在上述代码中,如果我将参数p作为阿拉伯字符传递,例如:
http://localhost/sample/MyServlet?p=عربي
然后它将返回显示为??????人物
如果您有任何建议,我们将不胜感激,因为参数“p”是阿拉伯字符,您可能不应该将其解读为8859_1编码字符,因为它与阿拉伯字符不对应-
尝试使用UTF-8编码来获取字节。在Tomcatserver.xml在conf文件夹中,编辑和像这样放置编码
URIEncoding=“UTF-8”
如果您使用的是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" ...
请编辑您的问题,以显示由request.getParameter(“p”)
返回的内容示例。我尝试了几种组合:String s=newstring(request.getParameter(“p”).getBytes(“8859_1”),“cp1256”)代码>和字符串s=新字符串(request.getParameter(“p”).getBytes(“8859_1”),“UTF-8”)
但问题仍然存在@Stan在这两个示例中,您都使用8859_1作为getBytes()的参数。你在那里试过UTF-8吗?另外,您在哪里看到符号显示错误?是在jsp上还是在调试模式下检查?是的,我尝试使用UTF-8,结果相同,我使用以下代码PrintWriter pw=response.getWriter();setCharacterEncoding(“UTF-8”);setContentType(“text/html;charset=UTF-8”);字符串s=新字符串(request.getParameter(“p”).getBytes(“8859_1”),“UTF-8”);pw.println(s);关闭()
@StanBrowsers(通常)以UTF-8编码方式(按照IRI)发送URL中的Unicode字符。Servlet容器,除非另外配置,否则会错误地将其解码为ISO-8859-1,因为Servlet规范是过时的垃圾,并且说它们必须newstring(x.getBytes(“ISO-8859-1”),“UTF-8”)
是一种标准的习惯用法,用于在容器无法轻松重新配置的情况下,取消以这种方式错误解码的输入。我尝试过,最后,如果仅使用字符串操作,它对我有效。字符串s=request.getParameter(“p”);response.getOutputStream().print;
<!-- 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" ...