TomCat Configuration

 

Notes (Note)

 

Public Description - TomCat Configuration

TomCat and International Characters

Simply put: Java natively works with Unicode characters (UTF-16 to be precise). When generating a web page the ONLY way to be sure that all such characters are correctly sent to the browser is to use UTF-8. There are no characters that you can represent in Java that can not be represented in UTF-8. And every browser in the world can receive UTF-8 correctly, displaying what it can display. (There is no guarantee that the actual glyphs for the characters will be installed on the receiving machine -- that is a problem with the browser hosting machine -- but at least you got the right code values there without corruption.)

You must configure TomCat to send all data as UTF-8 and receive all data as UTF-8. You send UTF-8 because all your data is in Java string, right. You receive UTF-8 so that in case someone types any character (or copies/pastes them) that is not ASCII, you will receive it correctly.

Unfortunately, the people who first created TomCat were somewhat ignorant about Unicode and international characters, so TomCat does not do this by default. And TomCat is fiendishly difficult to configure given that you can configure everything in mind boggling different ways, it is almost impossible to tell which configuration setting is actually being used for a particular request/response.

Some of the fixes are mentioned in UTF-8 Encoding fix article by Cagan Senturk. I summarise these points below:

Place '<%@ page language="java" pageEncoding="ISO-8859-1" contentType="text/html;charset=utf-8"%>' as the first line in 'ALL' the JSP files. The page encoding is the character set that the files on your disk use, and if you are using a normal text editor it is probably not saving files in UTF-8. This parameter is dependent upon your system and text editor configuration.

Place '<%req.setCharacterEncoding("UTF-8");%>' as the first executable line in every JSP files.

Place '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' in the HTML inside the <head> tag. You shouldn't need this because it should already be in the HTTP header, but this will make double sure.

in Server.xml, you connector tag needs to look like: <Connector port="7000" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" URIEncoding="UTF-8" connectionTimeout="20000" disableUploadTimeout="true" />