If I need to set the same encoding for all applications deployed in a tomcat instance, I can edit server.xml
and add a section like this:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
Is there a way to specify encoding for a certain application? (maybe in its web.xml
or somewhere else)?
If you use Spring MVC, you can use CharacterEncodingFilter in web.xml like this:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>my-spring-dispatcher-servlet</servlet-name>
</filter-mapping>
If not, you need to write a filter that does something like:
httpRequest.setCharacterEncoding("UTF-8")
EDIT:
You do need to specify useBodyEncodingForURI="true"
in Tomcat 5+ for this filter to be effective:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
useBodyEncodingForURI="true" />
As far as I know web.xml does not allow what you want, so I'd suggest the following ways.
<%@page pageEncoding="UTF-8" %>
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With