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.
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
@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
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
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;
}
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 */
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With