August 17, 2010

Spring MVC and UTF-8 Encoding with CharacterEncodingFilter

« How to get all window events at GNOME environment | Main | DBus Example »

JSP web applications often have to use the following page instruction for setting charset encoding. The pageEncoding attribute defines the charset encoding of the response, which will be send to the browser client. The contentType attribute includes the content type and the encoding of the content writer (see Servlet specification).

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

But this solution isn't very favoured. Because you have to set in every JSP page this instruction. A more central solution would be have more advantages.

This problems can be solved by using the CharacterEncodingFilter, which enables an automatic way of setting the character encoding in HttpServletRequests. You have only to add the filter configuration in your web.xml file.

    
     encodingFilter  
     org.springframework.web.filter.CharacterEncodingFilter  
       
        encoding  
        UTF-8  
       
       
        forceEncoding  
        true  
       
    
    
     encodingFilter  
     /*  
 

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 8:04 PM in Java

 

[Trackback URL for this entry]

Comment: XXX at Mo, 23 Aug 1:31 PM

Hi

This filter is for incoming request only, not for outgoing response (the jsp page is a response).

Regards

Comment: Rafael Sobek at Mo, 23 Aug 5:19 PM

Hi,

sorry. But you're wrong. Please have a look at this Java doc link:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/filter/CharacterEncodingFilter.html#setForceEncoding%28boolean%29

Regards
Rafael

Comment: Txumari at Fr, 28 Jan 6:08 PM

Thank for your post, it solve my problem.

Comment: andriy at Mo, 7 Feb 8:28 PM

thank you veru much. You' ve solved my problem, and save a lot of my time.

Comment: Javier at Mo, 10 Okt 3:25 AM

Hi my problem was resolved with this solution; I was add filter before context-param

Regards

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« August »
SunMonTueWedThuFriSat
1234567
891011121314
15161718192021
22232425262728
293031