package com.platform.common.web.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class SetCharEncodingFilter implements Filter { protected FilterConfig filterConfig = null; protected String encoding = "UTF-8"; public void destroy() { filterConfig = null; encoding = null; } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { /* if(encoding != null && !"".equals(encoding)) servletRequest.setCharacterEncoding(encoding);*/ servletRequest.setCharacterEncoding("utf-8"); //servletResponse.setContentType("text/html;charset=utf-8"); filterChain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); } }
web.xml:
<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.platform.common.web.servlet.SetCharEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>