用过滤器解决乱码问题

1、建立一个抽象类httpfilter实现filter接口

里面实现获取FilterConfig()和 getServletContext()方法;

建一个dofilter(HttpServletRequest request,HttpServletResponse response,FilterChain chainr)抽象方法让子类去实现;

提供一个init()初始化方法可以让子类去重写;

package webcard.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 

public abstract class HttpFilter implements Filter {
private FilterConfig fconfig;
@Override
public void destroy() {
// TODO Auto-generated method stub

}
public FilterConfig getFilterConfig(){
return this.fconfig;
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {

HttpServletRequest req=(HttpServletRequest) arg0;
HttpServletResponse res=(HttpServletResponse) arg1;
doFilter(req, res, arg2);
}
public abstract void doFilter(HttpServletRequest request,HttpServletResponse response,FilterChain chain)throws IOException, ServletException ;
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.fconfig=arg0;
init();
}
public void init()throws ServletException{}
public ServletContext getServletContext(){
return fconfig.getServletContext();
}
}

2、提供一个myrequest继承request,自带解决get编码方式

重写getparameter()方法

package webcard.filter;

import java.io.UnsupportedEncodingException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class MyRequest extends HttpServletRequestWrapper {
private String charset;

public MyRequest(HttpServletRequest request,String charset) {

super(request);
this.charset=charset;
// TODO Auto-generated constructor stub
}
@Override
public String getParameter(String name) {
HttpServletRequest request =(HttpServletRequest) getRequest();
String value=request.getParameter(name);
String method = this.getMethod();
if (method.equalsIgnoreCase("post")) {
try {
request.setCharacterEncoding(charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}else if (method.equalsIgnoreCase("get") && value!=null) {
try {
String param=new String(super.getParameter(name).getBytes("iso8859-1"),charset);
return param;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

return super.getParameter(name);

}

}

3、最后一步设置一个encoderfilter类继承httpfilter类;

package webcard.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 

public class EncodeFilter extends HttpFilter{

@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
//全站字符编码
String charset = getFilterConfig().getInitParameter("charset");
// System.out.println(charset);
if (charset==null) {
charset="utf-8";
}

response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset);
MyRequest myRequest = new MyRequest(request, charset);
myRequest.setCharacterEncoding(charset);
chain.doFilter(myRequest, response);
}

}

当然最后别忘了设置context.xml文件中的过滤器的作用范围:

<filter>
<filter-name>fil</filter-name>
<filter-class>webcard.filter.EncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>fil</filter-name>
<url-pattern>/*</url-pattern>//默认只过滤所有的重定向请求


<dispatcher>REQUEST</dispatcher>//过滤转发
<dispatcher>FORWARD</dispatcher>//过滤网页转发
<dispatcher>INCLUDE</dispatcher>//过滤include
</filter-mapping>

posted @ 2018-09-13 09:33  谁在逆水行舟  阅读(273)  评论(0编辑  收藏  举报