18

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.

5 Answers 5

25

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>
1
  • 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 :-) Mar 26, 2015 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

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

1
  • I have that in my jsp page and also encodingFilter is the first filter in my web.xml. Nov 22, 2012 at 6:35
4

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 */
    }
   }

}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.