Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Last active December 28, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenr/7582907 to your computer and use it in GitHub Desktop.
Save jeroenr/7582907 to your computer and use it in GitHub Desktop.
package nl.ibanx.web.filter;
import java.io.IOException;
import javax.servlet.*;
/**
* Set the character encoding of the request to UTF-8.
*/
public class SetCharacterEncodingFilter implements Filter {
public static final String UTF8_ENCODING = "UTF-8";
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
if(encoding == null) {
encoding = UTF8_ENCODING;
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(request.getCharacterEncoding() == null) {
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
// nothing to destroy here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment