8

I'm attempting to post a simple form that includes unicode characters to a servlet action. On Jetty, everything works without a snag. On a Tomcat server, utf-8 characters get mangled.

The simplest case I've got:

Form:

<form action="action" method="post">
  <input type="text" name="data" value="It’s fine">`
</form>`

Action:

class MyAction extends ActionSupport {   
  public void setData(String data) {
    // data is already mangled here in Tomcat
  } 
}
  • I've got URIEncoding="UTF-8" on <Connector> in server.xml
  • The first filter on the action calls request.setCharacterEncoding("UTF-8");
  • The content type of the page that contains the form is "text/html; charset=UTF-8"
  • Adding "accept-charset" to the form makes no difference

The only two ways I can make it work are to use Jetty or to switch it to method="get". Both of those cause the characters to come through without a problem.

Parker
  • 7,949
  • 5
  • 26
  • 21
  • see this previous answer: http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps – Fredrik L Dec 05 '11 at 21:10

1 Answers1

17

I've got URIEncoding="UTF-8" on <Connector> in server.xml

That's only relevant for GET requests.


The first filter on the action calls request.setCharacterEncoding("UTF-8");

Fine, that should apply on POST requests. You only need to make sure that if you haven't called getParameter(), getReader(), getInputStream() or anything else which would trigger parsing the request body before calling setCharacterEncoding().


The content type of the page that contains the form is "text/html; charset=UTF-8"

How exactly are you setting it? If done in a <meta>, then you need to understand that this is ignored by the browser when the page is served over HTTP and the HTTP Content-Type response header is present. The average webserver namely already sets it by default. The <meta> content type will then only be used when the page is saved to local disk and viewed from there.

To set the response header charset properly, add the following to top of your JSP:

<%@page pageEncoding="UTF-8" %>

This will by the way also tell the server to send the response in the given charset.


Adding "accept-charset" to the form makes no difference

It only makes difference in MSIE, but even then it is using it wrongly. The whole attribute is worthless anyway. Forget it.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The response headers of the page containing the form include "Content-Type:text/html;charset=UTF-8" – Parker Dec 05 '11 at 21:06
  • Are you certain that the filter is invoked? The symptoms indicate it isn't or that the `setCharacterEncoding()` is overriden at later point. Note that it only works if you haven't called `getParameter()`, `getReader()`, `getInputStream()` and anything else beforehand which would trigger parsing the request body. – BalusC Dec 05 '11 at 21:21
  • 1
    Ah, ha! There was a single filter running before my setUtf8 one for profiling-- it has a hidden "getParameter()" I'd missed. Splitting it into two separate filters so my content type was called beforehand solved the issue. Thanks for your help. – Parker Dec 05 '11 at 21:58
  • setCharacterEncoding method has to be called in the servlet filter. It must be set before the any requests to the server is made. – zawhtut Jan 03 '13 at 04:48
  • @BalusC: How can we do the following while working on a HTML page? <%@page pageEncoding="UTF-8" %> – Ch Faizan Mustansar Jan 13 '14 at 09:51
  • This didn't work for me using tomcat7 with jdk1.7.0 setting the encoding as the first action for the request during a post did not cause the characters to render correctly. – Cheruvim Apr 05 '17 at 05:27