简体   繁体   中英

How can I tell Tomcat to use UTF-8 internally?

So I have this very simple html file:

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

And the output.jsp:

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

When I type "ğğğ" in the form, I see "ğğğ" as expected in output.jsp. The Servlet that handles this form looks like this:

@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);
}

The problem is, in text.txt file I will see "???" instead of "ğğğ". I have been trying to solve this for hours but no luck..

I have

JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8

in Windows environmental variables, but it changes nothing as far as I can say.

When I debug this application, the variable "name" is seen as "ğğğ" in the debugger window if it helps at all.

So how can I make the System.out print the correct characters?

EDIT 1 When I create a separate Java project and simply run:

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

everything is as expected in the text file.

EDIT 2 I already have in my server.xml:

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

I don't think this is a Tomcat issue. It's a Java issue with how you are writing to the file.

Use the appropriate character set when writing.

PrintWriter printWriter = new PrintWriter(new File("C:/text.txt"), "UTF-8");

and reading, obviously.

UTF-8 issues can be tricky. But in Tomcat 6 on Ubuntu I handle it this way. First find the server.xml file & open it up for editing. I like to use nano but you should use whatever editor you feel comfortable with:

sudo nano /etc/tomcat6/server.xml

Now look for the fragment for <Connector… and place URIEncoding="UTF-8" in there. In one of my setups it looks like this:

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

What you probably need in this case is to set the connector in server.xml like the following:

<Connector URIEncoding="UTF-8/>

Of course in the connector you should set whatever other properties you need

Somehow I made it work.

This is the JSP file I have:

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

and the 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM