如何告诉Tomcat在内部使用UTF-8?

时间:2014-04-13 16:05:36

标签: java jsp tomcat utf-8 character-encoding

所以我有这个非常简单的html文件:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<form method="post" action="formpost.do" accept-charset="utf-8">
    <label for="id">Your name please:</label>
    <input id="id" type="text" name="name"/>
    <input type="submit"/>
</form>
</body>
</html>

和output.jsp:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<%= request.getAttribute("name") %>
</body>
</html>

当我在表单中键入“ğğğ”时,我在output.jsp中看到了预期的“ğğğ”。处理此表单的Servlet如下所示:

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

    httpServletResponse.setCharacterEncoding("UTF-8");
    httpServletRequest.setCharacterEncoding("UTF-8");

    String name = httpServletRequest.getParameter("name");
    httpServletRequest.setAttribute("name",name);

    PrintWriter printWriter = new PrintWriter(new File("C:/text.txt"));
    printWriter.write(name);
    printWriter.flush();
    printWriter.close();

    httpServletRequest.getRequestDispatcher("/output.jsp")
            .forward(httpServletRequest, httpServletResponse);
}

问题是,在text.txt文件中我会看到“???”而不是“ğğğ”。我一直试图解决这个问题几个小时但没有运气..

我有

JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8

在Windows环境变量中,但就我所说的而言,它没有任何改变。

当我调试这个应用程序时,变量“name”在调试器窗口中被视为“ğğğ”,如果它有帮助的话。

那么如何让System.out打印正确的字符呢?

编辑1 当我创建一个单独的Java项目并运行时:

public static void main(String[] args)
        throws Exception {
    PrintWriter printWriter = new PrintWriter(new File("C:/text.txt"));
    printWriter.write("ğğğ");
    printWriter.flush();
    printWriter.close();
}

文本文件中的所有内容都符合预期。

编辑2 我已经在我的server.xml中了:

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

4 个答案:

答案 0 :(得分:4)

我不认为这是一个Tomcat问题。关于如何写入文件,这是一个Java问题。

写作时使用适当的字符集。

PrintWriter printWriter = new PrintWriter(new File("C:/text.txt"), "UTF-8");
显然,阅读和阅读。

答案 1 :(得分:1)

UTF-8问题可能很棘手。但是在Ubuntu的Tomcat 6中,我这样处理它。首先找到server.xml文件&amp;打开它进行编辑。我喜欢使用nano,但您应该使用您认为合适的编辑器:

sudo nano /etc/tomcat6/server.xml

现在查找<Connector…的片段并在其中放置URIEncoding="UTF-8"。在我的一个设置中,它看起来像这样:

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

答案 2 :(得分:0)

在这种情况下,您可能需要在server.xml中设置连接器,如下所示:

<Connector URIEncoding="UTF-8/>

当然,在连接器中,您应该设置您需要的任何其他属性

答案 3 :(得分:0)

不知怎的,我让它成功了。

这是我的JSP文件:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8"/>
</head>
<body>
<form accept-charset="UTF-8" action="postform.do" method="post">
    <label>
        <input type="text" name="name"/>
    </label>
    <input type="submit" name="submit" value="Post!">
</form>
</body>
</html>

和servlet:

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {
    httpServletResponse.setCharacterEncoding("utf-8");
    httpServletResponse.setContentType("text/html");
    httpServletRequest.setCharacterEncoding("utf-8");
    final String name = httpServletRequest.getParameter("name");
    System.out.println(name);
    final PrintWriter writer = httpServletResponse.getWriter();
    writer.print(name);
    writer.flush();
}