在web.xml中设置全局编码
Posted 江山一族
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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中设置全局编码的主要内容,如果未能解决你的问题,请参考以下文章
Web.xml中设置Servlet和Filter时的url-pattern匹配规则
DWR中使用时,web.xml中设置 <url-pattern>/dwr/*</url-pattern>,但无法生效,请问是为啥?