how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding? how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding? spring spring

how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding?


I tried successfully with this in web.xml !

<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>


To work in spring boot you can use

@Beanpublic FilterRegistrationBean filterRegistrationBean() {    CharacterEncodingFilter filter = new CharacterEncodingFilter();    filter.setEncoding("UTF-8");    FilterRegistrationBean registrationBean = new FilterRegistrationBean();    registrationBean.setFilter(filter);    registrationBean.addUrlPatterns("/*");    return registrationBean;}

another response


Make sure you have the following snippet in your jsp

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

and also make sure that the encodingFilter is the first filter in web.xml file