Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Wiki Markup
    \[#Q1 What is the default character encoding of the request or response body?\]
  2. Wiki Markup
    \[#Q2 How do I change how GET parameters are interpreted?\]
  3. Wiki Markup
    \[#Q3 How do I change how POST parameters are interpreted?\]
  4. Wiki Markup
    \[#Q4 How can I test if my configuration will work correctly?\]
  5. Wiki Markup
    \[#Q5#Q6 I'mHow havingcan aI problemsend withhigher character encodingcharacters in TomcatHTTP 5headers?\]
  6. Wiki Markup
    \[#Q6#Q8 What can you recommend to just make everything work?\]
  7. Wiki Markup
    \[#Q9 Why does everything have to be this way?\]
    \\
  8. Wiki Markup
    \[#Q5 I'm having a problem with character encoding in Tomcat 5\]
    \\

Answers

What is the default character encoding of the request or response body?

...

No Format
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <title>Character encoding test page</title>
   </head>
   <body>
     <p>Data posted to this form was:
     <%
       request.setCharacterEncoding("UTF-8");
       out.print(request.getParameter("mydata"));
     %>

     </p>
     <form method="POST" action="index.jsp">
       <input type="text" name="mydata">
       <input type="submit" value="Submit" />
       <input type="reset" value="Reset" />
     </form>
   </body>
</html>

How can I send higher characters in my HTTP headers?

You have to encode them in some way before you insert them into a header. Using url-encoding (% + high byte number + low byte number) would be a good idea.

What can you recommend to just make everything work?

Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation. In order to completely switch to using UTF-8, you need to make the following changes:

  1. Set URIEncoding="UTF-8" on your <Connector> in server.xml
  2. Wiki Markup
    Use a \[#Q3 character encoding filter\] with the default encoding set to UTF-8
  3. Change all your JSPs to set the correct Content-Type (use <%@page cotnentType="mime/type; charset=UTF-8" %>)
  4. Change all your servlets to set the content type for responses to UTF-8
  5. Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 as the content type

Why does everything have to be this way?

Everything covered in this page comes down to practical interpretation of a number of specifications. When working with Java servlets, the Java Servlet Specification is the primary reference, but the servlet spec itself relies on older specifications such as HTTP for its foundation. Here are a couple of references before we cover exactly where these items are located in them.

  1. Wiki Markup
    \[http://jcp.org/aboutJava/communityprocess/mrel/jsr154/index2.html Java Servlet Specification 2.5\]
  2. Wiki Markup
    \[http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html Java Servlet Specification 2.4\]
  3. Wiki Markup
    \[http://www.w3.org/Protocols/rfc2616/rfc2616.txt HTTP 1.1 Protocol]] (\[http://www.w3.org/Protocols/rfc2616/rfc2616.html hyperlinked version\])
  4. Wiki Markup
    \[http://www.ietf.org/rfc/rfc2396.txt URI Syntax\]
  5. Wiki Markup
    \[http://www.w3.org/Protocols/rfc822/ ARPA Internet Text Messages\]
    \\

Default Encoding for POST

Wiki Markup
\[http://en.wikipedia.org/wiki/Iso-8859-1 ISO-8859-1\] is defined as the default character set for HTTP request and response bodies in the servlet specification (request encoding: section 4.9 for spec version 2.4, section 3.9 for spec version 2.5; response encoding: section 5.4 for both spec versions 2.4 and 2.5). This default is historical: it comes from sections 3.4.1 and 3.7.1 of the HTTP/1.1 specification.

Some notes about the character encoding of a POST request:

  1. Section 3.4.1 of HTTP/1.1 states that recipients of an HTTP message must respect the character encoding specified by the sender in the Content-Type header if the encoding is supported. A missing character allows the recipient to "guess" what encoding is appropriate.
  2. Most web browsers today do not specify the character set of a request, even when it is something other than ISO-8859-1. This seems to be in violation of the HTTP specification. Most web browsers appear to send a request body using the encoding of the page used to generate the POST (for instance, the <form> element came from a page with a specific encoding... it is that encoding which is used to submit the POST data for that form).

Default encoding for GET

Wiki Markup
The character set for HTTP query strings (that's the technical term for 'GET parameters') can be found in sections 2 and 2.1 the "URI Syntax" specification. The character set is defined to be \[http://en.wikipedia.org/wiki/ASCII US-ASCII\]. Any character that does not map to US-ASCII must be encoded in some way. Section 2.1 of the URI Syntax specification says that characters outside of US-ASCII must be encoded using {{%}} escape sequences: each character is encoded as a literal {{%}} followed by the two hexadecimal codes which indicate its character code. Thus, {{a}} (US-ASCII character code 0x97) is equivalent to {{%97}}.

Some notes about the character encoding of URIs:

  1. ISO-8859-1 and ASCII are compatible for character codes 0x20 to 0x7E, so they are often used interchangeably. Most of the web uses ISO-8859-1 as the default for query strings.
  2. Many browsers are starting to offer (default) options of encoding URIs using UTF-8 instead of ISO-8859-1. Some browsers appear to use the encoding of the current page to encode URIs for links (see the note above regarding browser behavior for POST encoding).
  3. When in doubt, use POST for any data you think might have problems surviving a trip through the query string.

HTTP Headers

Section 3.1 of the ARPA Internet Text Messages spec states that headers are always in US-ASCII encoding. Anything outside of that needs to be encoded. See the section above regarding query strings in URIs.

I'm having a problem with character encoding in Tomcat 5

...

  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=23929 23929\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=25360 25360\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=25231 25231\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=25235 25235\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=22666 22666\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=24557 24557\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=24345 24345\]
  • Wiki Markup
    \[http://issues.apache.org/bugzilla/show_bug.cgi?id=25848 25848\]
    \\

What can you recommend to just make everything work?

Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation. In order to completely switch to using UTF-8, you need to make the following changes:

...

  • \

...

  • ]

...