在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() {

}
}

上一篇:UIImage常用封装


下一篇:【C++探索之旅】第一部分第三课:第一个C++程序