The character encoding specified in the page (or in the web.xml) is applied to the following phases of an HTTP communication:
- Preparing / sending the Request from the Client to the Server
- Receiving / reading the Request in the Server
- Preparing / sending the Response from the Server to the Client
- Receiving / reading the Response in the Client
The Application Server is the only responsible for the phase 2.
You need to look for your specific application server settings, to alter the default character encoding (that could easily be ISO-8859-1
), and alter it to work in UTF-8
.
For example, in Tomcat you would need to edit the conf/server.xml
file, by adding the URIEncoding="UTF-8"
parameter to the <Connector>
, for example from
<Connector port="8090" />
to
<Connector port="8090" URIEncoding="UTF-8"/>
In the Apache Wiki there is a nice list of things to check to make sure all your components are running in UTF-8:
What can you recommend to just make everything work? (How to use UTF-8
everywhere).
Using UTF-8 as your character encoding for everything is a safe bet.
This should work for pretty much every situation.
In order to completely switch to using UTF-8, you need to make the
following changes:
- Set URIEncoding="UTF-8" on your in server.xml. References: HTTP
Connector,
AJP
Connector.
- Use a character encoding filter with
the default encoding set to UTF-8
- Change all your JSPs to include charset name in their contentType. For example, use
<%@page contentType="text/html; charset=UTF-8" %>
for the usual JSP pages and <jsp:directive.page
contentType="text/html; charset=UTF-8" />
for the pages in XML syntax
(aka JSP Documents).
- Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8. Use
response.setContentType("text/html; charset=UTF-8")
or
response.setCharacterEncoding("UTF-8")
.
- Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content
type of the responses that they generate.
- Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set
the encoding to UTF-8. For more information see
http://www.mail-archive.com/[email protected]/msg21117.html.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…