Learning Monk

March 13, 2009

How many palces do you have to set Character encoding for a site

Filed under: spring, tomcat — Tags: — gnandiga @ 12:33 am

I was recently trying to provide character encoding “UTF-8” to our site. This is spring application running on tomcat. We are using velocity templating engine, with mysql backend.

To complete the loop, I had to do all of the following

  1. Update tomcat character encoding setting
  2. Add Filter to set Request objects character encoding
  3. Update Spring VelocityViewResolver to set content-type with correct character encoding
  4. Set character encoding for the database connection
  5. Set character encoding in the database

Update tomcat character encoding setting

In the tomcat server, edit the conf/server.xml to set URIEncoding

        <Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

Add Filter to set Request objects character encoding

Add the following filter to the web.xml in the tomcat application. If you dont use the spring framework, create a Filter extending the javax.servlet.Filter to set the character encoding on HttpServletRequest to UTF-8


<filter>
    <filter-name>Spring character encoding filter</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>
    <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
    </init-param>
</filter>

Update Spring VelocityLayoutViewResolver to set content-type with correct character encoding

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>

Set character encoding for the database connection

jdbc:mysql://${database.host}:3306/${database.name}?maxQuerySizeToLog=10000&dumpQueriesOnException=true&useUnicode=true&characterEncoding=UTF-8&includeInnodbStatusInDeadlockExceptions=true

Set character encoding in the database

ALTER TABLE tbl_name DEFAULT CHARACTER SET 'utf8';
Advertisements

Create a free website or blog at WordPress.com.