You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Character Encoding Issues

Questions

  1. [#Q1 What is the default character encoding?]

  2. [#Q2 How do I change how GET parameters are interpreted?]

  3. [#Q3 How do I change how POST parameters are interpreted?]

  4. [#Q4 How can I test if my configuration will work correctly?]

  5. [#Q5 I'm having a problem with character encoding in Tomcat 5]

Answers

What is the default character encoding?

If a character encoding is not specified, the Servlet specification requires that an encoding of ISO 8859-1 is used.

How do I change how GET parameters are interpreted?

Set the URIEncoding parameter on the Connector element in server.xml.

How do I change how POST parameters are interpreted?

POST requests should specify the encoding of the parameters and values they send. Since many clients fail to set an explicit encoding, the default is used (ISO 8859-1). In many cases this is not the preferred interpretation so one can employ a javax.servlet.Filter to set request encodings. Writing such a filter is trivial. Furthermore Tomcat already comes with such an example filter. Please take a look at:
4.x::

webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

5.x::

webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
webapps/jsp-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

6.x::

webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

How can I test if my configuration will work correctly?

The following sample JSP should work on a clean Tomcat install for any input. If you set the URIEncoding="UTF-8" on the connector, it will also work with method="GET".

<%@ 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>

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

In Tomcat 5 - there have been issues reported with respect to character encoding (usually of the the form "request.setCharacterEncoding(String) doesn't work"). Odds are, its not a bug. Before filing a bug report, see these bug reports as well as any bug reports linked to these bug reports:

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=23929 23929]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=25360 25360]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=25231 25231]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=25235 25235]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=22666 22666]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=24557 24557]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=24345 24345]

  • [http://issues.apache.org/bugzilla/show_bug.cgi?id=25848 25848]

  • No labels