利用Filter解决全站乱码的两种方案
1、实现HttpServletRequestWrapper类,增强HttpServletRequest
2、动态代理技术,解决全站乱码
代码如下
1、使用HttpServletRequestWrapper方式
CharSetFilter.java 过滤器
//servlet过滤器技术解决全站乱码问题
public class CharSetFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//强制类型转换,将ServletResponse转化为HttpServletResponse
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
//下面三行代码只针对post请求的乱码处理
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
//MyRequest,继承HttpServletRequestWrapper类,增强HttpServletRequest,可以处理get乱码问题
chain.doFilter(new MyRequest(request), response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
//HttpServletRequestWrapper是系统默认的HttpServletRequest增强类
class MyRequest extends HttpServletRequestWrapper{
private HttpServletRequest request; //基础传递进来的request
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
//HttpServletRequest的所有的方法都被HttpServletRequestWrapper写好了,自定义的类只需覆写需要增强的方法
@Override
public String getParameter(String name) {
String value = this.request.getParameter(name);
if(!this.request.getMethod().equalsIgnoreCase("get")){ //如果不是以get方法提交的数据
return value;
}
if(value == null){
return null;
}
//处理get乱码
try {
return new String(value.getBytes("iso8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// return super.getParameter(name);
}
}
web.xml配置过滤器
<filter>
<filter-name>CharSetFilter</filter-name>
<filter-class>cn.itcast.charset.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、动态代理技术解决全站乱码
//动态代理设计模式 解决全站乱码问题
public class CharSetFileterProxy implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//由于动态代理使用到内部类,所以将request,response声明为final类型
final HttpServletRequest request = (HttpServletRequest) req;
final HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("UTF-8"); //解决POST乱码
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
//传入request的代理类
chain.doFilter((ServletRequest) Proxy.newProxyInstance(CharSetFileterProxy.class.getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//如果不是getParameter,返回原来的方法的执行结果
if(!method.getName().equals("getParameter")){
return method.invoke(request, args);
}
//如果不是get请求,返回原来方法的执行结果
if(!request.getMethod().equalsIgnoreCase("get")){
return method.invoke(request, args);
}
String value = (String) method.invoke(request, args);
if(value == null){
return null;
}
value = new String(value.getBytes("iso8859-1"),"utf-8");
return value;
}
}), response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}