It is important to set character encoding in Tomcat server while building your website. The default encoding in servlets is ASCII or ISO-8859-1 while the modern browsers use UTF-8 encoding. Due to the mismatch, Tomcat server may not be able to encode some of the UTF-8 (unicode) characters properly and you may get an error message. In this article, we will look at how to set UTF-8 encoding in Tomcat server.
How to Set UTF-8 Encoding in Tomcat Server
There are various places in your application where a character encoding can be a problem. For example, character encoding will affect how requested URLs are interpreted or misinterprested. It also affects the GET/POST data interpreted by the server. That is why, it is safe to set UTF-8 encoding throughout your application. We will look at the steps to set UTF-8 encoding in Tomcat server, for your entire application and not just a part of it.
1. Set URIEcoding
Open server.xml file and update Connector tag to include the following attribute
URIEncoding="UTF-8"
For example, here is a Connector tag with the above attribute
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
Also read : How to Use NGINX try_files
2. Update Content Type for JSPs
Update all your JSPs to include UTF-8 charset name in their contentType. For example, add the following line to all your JSPs.
<%@page contentType="text/html; charset=UTF-8" %>
and the following line for pages that use XML syntax (JSP documents.
<jsp:directive.page contentType="text/html; charset=UTF-8" />
Also read : Difference between ServerName and ServerAlias
3. Update all Servlets
Update all servlets to include charset name in response contentType. Add the following line to your servlets
response.setContentType("text/html; charset=UTF-8")
ORresponse.setCharacterEncoding("UTF-8")
Also read : How to Install NFS Server & Client in CentOS
4. Change Content Generation Libraries
Update any content generation libraries like Freemarker or Velocity to use UTF-8 and specify UTF-8 content type in their responses.
Also read : How to Sort Dictionary By Value in Python
5. Update filters
Use character encoding filter with default encoding set to UTF-8. Also disable any filter or valves that process the request the before the JSP or filter has had a chance to set the encoding to UTF-8.
Also read : How to Empty or Delete Contents of Large Files
The above steps to should change your default character encoding to UTF-8 throughout your application.