- 字符编码的格式有时候不是UTF_8的时候,我们可以采用统一的过滤器进行处理,这样能得到一个很好的效果对于我们的实验来说的话!
- 这种思想其实和设计模式中的设计模式之Chain-of-Responsiblility模式,非常的类似,以及我们的异常的处理也是很类似的,这样的通过接口,形成一线。比较的值得欣赏!http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/29/2662077.html
- 我们通过这样的方式比较适合web这种不确定的来源
- 代码片如下
package com.hdu.filter;
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 CharsetFilter implements Filter {
private String encoding;
@Override
public void destroy() {
// TODO Auto-generated method stub
this.encoding=null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(this.encoding);
response.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.encoding=filterConfig.getInitParameter("encoding");
}
}
web.xml 中的配置
ShopingChat
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
encoding
com.hdu.filter.CharsetFilter
encoding
utf-8
encoding
/*