Spring UTF-8 conversion using CharacterEncodingFilter

June 10, 2008

Context:

Having to deal with non english characters can often lead to frustation, even after specifying…

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

… within the jsp page. This is because “current browsers typically do not set a character encoding even if specified in the HTML page or form”. So this is when using CharacterEncodingFilter comes into play.

Solution:

Simply copy and paste the following configuration into your web.xml file…

 <!--
    - This is useful because current browsers typically do not
    - set a character encoding even if specified in the HTML page or form
 -->
 <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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
 </filter>

 <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

… that’s all there is to it.

Reference:

- CharacterEncodingFilter

Wa Allahu A’lam

Entry Filed under: Java, Spring, XHTML. Tags: , , , , , .

1 Comment Add your own

  • 1. Ben  |  June 5, 2009 at 5:22 pm

    Please note that that filter must be the first one defined in your web.xml

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Recent Comments

Categories

Top Posts

Links

Blog Stats

Meta