How to Set Request Encoding in Tomcat

How to set request encoding in Tomcat?

The request.setCharacterEncoding("UTF-8"); only sets the encoding of the request body (which is been used by POST requests), not the encoding of the request URI (which is been used by GET requests).

You need to set the URIEncoding attribute to UTF-8 in the <Connector> element of Tomcat's /conf/server.xml to get Tomcat to parse the request URI (and the query string) as UTF-8. This indeed defaults to ISO-8859-1. See also the Tomcat HTTP Connector Documentation.

<Connector ... URIEncoding="UTF-8">

or to ensure that the URI is parsed using the same encoding as the body1:

<Connector ... useBodyEncodingForURI="true">

See also:

  • Unicode - How to get the characters right? - JSP/Servlet request

1 From Tomcat's documentation (emphasis mine):

This setting is present for compatibility with Tomcat 4.1.x, where the
encoding specified in the contentType, or explicitly set using
Request.setCharacterEncoding method was also used for the parameters
from the URL. The default value is false.


Please get rid of those scriptlets in your JSP. The request.setCharacterEncoding("UTF-8"); is called at the wrong moment. It would be too late whenever you've properly used a Servlet to process the request. You'd rather like to use a filter for this. The response.setCharacterEncoding("UTF-8"); part is already implicitly done by pageEncoding="UTF-8" in top of JSP.

I also strongly recommend to replace the old fashioned <%= request.getParameter("q") %> scriptlet by EL ${param.q}, or with JSTL XML escaping ${fn:escapeXml(param.q)} to prevent XSS attacks.

HttpRequest's parameters default encoding in Java (Tomcat)

I think, if I recall correctly, you can set the URIEncoding property on the connector to default to UTF-8.

According to this link, the default under Tomcat 8 (when strict servlet compliance is off) is UTF-8. Under Tomcat 7, at my company we set this explicitly.

In server.xml for the connector element(s):

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

According to the configuration documentation for the Tomcat 7 HTTP connector, this is by default ISO-8859-1.

The configuration documentation for the Tomcat 8 HTTP connector does explicitly state that if org.apache.catalina. STRICT_SERVLET_COMPLIANCE is false, UTF-8 will be used.

I do have some inklings that this dates back to Servlet 2.4 specification which states that if no character encoding is specified, ISO-8859-1 is used.

how to retain utf-8 character encoding in tomcat with GET request

I found a way somehow... I imported org.springframework.web.bind.ServletRequestUtils class from org.springframework.web-3.0.0.RELEASE.jar and used the following for parsing the parameter 'jobDetails' from 'request' object:

String jobDetails = new String((ServletRequestUtils.getStringParameter(request, "jobDetails")).getBytes("ISO-8859-1"), "UTF-8");


Related Topics



Leave a reply



Submit