17

I need some help.

I placed the code snippet below in my 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>
</filter>

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

and in my server.xml:

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

My jsp pages are encoded as UTF-8 and my mysql table is encoded as utf8_general_ci.

My problem is that whenever I save a ñ it becomes ?.

When I tried to manually save ñ in the mysql terminal its saving properly. I suspect the problem lies within my server or my program. Please help.

TheOnlyIdiot
  • 1,072
  • 7
  • 16
  • 34

5 Answers5

24

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>
Andrew
  • 13,367
  • 12
  • 62
  • 79
Hanske1967
  • 341
  • 2
  • 4
  • Thanks again. I somehow lost the * in the url-mapping and the filter stopped working. Took me a while to spot the missing star. This markup you posted above works perfectly, just make sure you don't remove the star from the url-pattern :-) – Peter Perháč Mar 26 '15 at 22:40
8

To work in spring boot you can use

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(filter);
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}

another response

Community
  • 1
  • 1
palhares
  • 1,166
  • 12
  • 13
5

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

zb226
  • 7,475
  • 4
  • 37
  • 64
DDK
  • 940
  • 16
  • 33
3

My solution, using Spring (3.2.x) AnnotationConfigWebApplicationContext, based on Spring Framework Reference:

public class StudentApplicationConfig extends AbstractDispatcherServletInitializer {

@Override
protected WebApplicationContext createServletApplicationContext() {

    AnnotationConfigWebApplicationContext dispatcherContext
            = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherConfig.class);
    return dispatcherContext;
}

@Override
protected String[] getServletMappings() {

    return new String[] { "/" };
}

@Override
protected WebApplicationContext createRootApplicationContext() {

    AnnotationConfigWebApplicationContext appContext
            = new AnnotationConfigWebApplicationContext();
    return appContext;
}

@Override
protected Filter[] getServletFilters() {
    Filter[] filters;

    CharacterEncodingFilter encFilter;
    HiddenHttpMethodFilter httpMethodFilter = new HiddenHttpMethodFilter();

    encFilter = new CharacterEncodingFilter();

    encFilter.setEncoding("UTF-8");
    encFilter.setForceEncoding(true);

    filters = new Filter[] {httpMethodFilter, encFilter};
    return filters;
}
3

it might be little be late, but it´s better to configure it in tomcat conf/web.xml (or for project in web.xml)

<filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

See http://wiki.apache.org/tomcat/FAQ/CharacterEncoding or tomcat´s web.xml

Or for jetty like this:

public class StartEmbeddedJetty{

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler sch = new ServletContextHandler();
    sch.addFilter(CharacterEncodingFilter .class, "/*", EnumSet.of(DispatcherType.REQUEST));
      ...
    server.start();
    server.join();
}

public static class CharacterEncodingFilter implements Filter {
     @Override
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // set encoding to utf-8
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
     }

     @Override
     public void init(FilterConfig arg0) throws ServletException {
        /* empty */
     }

     @Override
    public void destroy() {
        /* empty */
    }
   }

}

Vitali Heinrich
  • 151
  • 1
  • 6