从Tomcat Servlet中读取韩文字符

时间:2014-11-12 03:30:15

标签: java tomcat character-encoding

我的服务器无法通过POST请求接收韩语字符,也无法将其写入控制台。

这是我的代码:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    request.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();

    StringBuffer buffer = new StringBuffer();
    String line = null;
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null)
        buffer.append(line);
    String requestMessage = buffer.toString();

    //working
    System.out.println("teniszütő");
    //not working (???????)
    System.out.println("\uc548\ub155\ud558\uc138\uc694\uc138\uacc4");
    //not working (??)
    System.out.println("여니");
    //accents are working, Korean characters aren't
    System.out.println(requestMessage);
    //writing 3F (the code of the ? instead of the code of the character)
    System.out.println(bytesToHexString(requestMessage.getBytes()));

    out.println("Answer");

}

我使用Eclipse,如果我检查项目的属性,我可以看到"文本文件编码"是"继承自容器(UTF-8)"。如果我制作一个普通的HelloWorld应用程序(使用相同的设置),它会打印韩文字符。

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我必须设置URIEncoding =&#34; UTF-8&#34;在我的<Connector>在server.xml中,如下所示:

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