首页 > 代码库 > 在web.xml中设置全局编码

在web.xml中设置全局编码

在web.xml中配置

<filter>
<filter-name>characterFilter</filter-name>
<filter-class>com.cn.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

filter类:

public class CharacterFilter implements Filter{

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}

@Override
public void destroy() {

}
}

在web.xml中设置全局编码