Young Harry
Young Harry

Reputation: 97

How to set Tomcat's Encoding type?

Actucally I am not sure that it is the problem caused by Tomcat.I had to deal with some Chinese words(encode in UTF-8).When I debug the program on eclipse with "Run on server",it will return the expected result.But when I export it to war package and run on tomcat, then all the Chinese words will showed in GBK, and can not be read.I do not know where the problem is.Could anyone tell me how to solve it?

Upvotes: 0

Views: 2347

Answers (2)

John Ding
John Ding

Reputation: 1350

besides the answer by elkorchi, you also need the filter added in web.xml

<filter>
    <filter-name>charsetFilter</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>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

above works if you use spring framework, otherwise you need write your own filter like this and change the class in the xml config accordingly,

see the blog for details: enter link description here

Upvotes: 1

Anas EL KORCHI
Anas EL KORCHI

Reputation: 2048

You need to do several changes to make the UTF-8 as your default encoding character :

  • Set URIEncoding="UTF-8" on your Connector in server.xml.
  • 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 info see this

Upvotes: 1

Related Questions