filter过滤乱码的问题

filter过滤乱码的问题

我们的系统比较乱,为了兼容老的数据库的GBK编码,我们在整个web里面使用了GBK,但是在某些ajax的时候又不免使用了utf-8,使用的jquery,jmesa等等东东里面,时不时就会有乱码问题,最后这个ecodingfilter就越写越长了,不过还是最终满足了要求,这里将代码贴出来,以备以后使用,CharactorEncodingFilter.java如下:

public class CharactorEncodingFilter implements Filter {

private final static String DEFAULT_ENCODING = "GBK";

private final static String ajaxEncoding = "UTF-8";

protected String commonEncoding;

protected FilterConfig filterConfig;

protected boolean ignore = true;

public CharactorEncodingFilter() {
super();
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.commonEncoding = filterConfig.getInitParameter("encoding");

if (this.commonEncoding == null) {
this.commonEncoding = DEFAULT_ENCODING;
}

String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) {
try {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
// 接受第三方cookie
// response.addHeader("P3P","CP=CAO PSA OUR");
// response.setHeader("P3P", "CP=CAO PSA OUR");
if (ignore || (request.getCharacterEncoding() == null)) {
// post with the header named RequestType
if (request.getHeader("RequestType") != null
&& request.getHeader("RequestType").equalsIgnoreCase("ajax")) {
request.setCharacterEncoding(ajaxEncoding);
response.setCharacterEncoding(ajaxEncoding);
if (!ajaxEncoding.equalsIgnoreCase(request.getCharacterEncoding())) {
// 此处代码无法理解。哈哈。不这么写,weblogic10.3就是在ajax提交 的时候有乱码
request.setCharacterEncoding(ajaxEncoding);
response.setCharacterEncoding(ajaxEncoding);
}
response.setContentType("text/html;charset=" + ajaxEncoding);
} else if (request.getHeader("X-Requested-With") != null
&& request.getHeader("X-Requested-With").equalsIgnoreCase ("XMLHttpRequest")) {
// put this code to avoid jmesa messy code
if (request.getHeader("content-type") != null
&& request.getHeader("content-type") != "") {
request.setCharacterEncoding(ajaxEncoding);
}
// WARNING, the under line not allowed
// response.setCharacterEncoding(ajaxEncoding);
response.setContentType("text/html;charset=" + commonEncoding);
} else if (request.getHeader("user-agent") != null
&& request.getHeader("user-agent").contains("Flash")) {
// flash上传组件 带有头{user-agent:Shockwave Flash}信息
request.setCharacterEncoding(ajaxEncoding);
} else {
request.setCharacterEncoding(commonEncoding);
response.setCharacterEncoding(commonEncoding);
response.setContentType("text/html;charset=" + commonEncoding);
}
}
filterChain.doFilter(req, res);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}

public void destroy() {
this.commonEncoding = null;
this.filterConfig = null;
}

// for debug
@SuppressWarnings({ "unused", "unchecked" })
private void showHeaders(HttpServletRequest request) {
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()) {
Object k = e.nextElement();
DevLog.debug(k + ":" + request.getHeader(k.toString()));
}
}
}

其中比较有用的还是这个
private void showHeaders(HttpServletRequest request) {
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()) {
Object k = e.nextElement();
DevLog.debug(k + ":" + request.getHeader(k.toString()));
}
}
能打印出header的信息,这样我们才能分辨出不同的请求,比如,jquery的ajax的请求,打印出的信息如下:
jquery header
k=x-requested-with v=XMLHttpRequest
k=accept-language v=en-us,zh-cn;q=0.5
k=referer v=http://localhost:8080/easyjmesa/jsp/jquery.jsp
k=accept v=*/*
k=content-type v=application/x-www-form-urlencoded
k=accept-encoding v=gzip, deflate
k=user-agent v=Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
k=host v=localhost:8080
k=content-length v=29
k=connection v=Keep-Alive
k=cache-control v=no-cache
k=cookie v=JSESSIONID=38A23568899BD404F2357DAA1174F412

jmesa的ajax请求的打印信息如下:
jmesa header
k=x-requested-with v=XMLHttpRequest
k=accept-language v=en-us,zh-cn;q=0.5
k=referer v=http://localhost:8080/easyjmesa/jsp/jmesa.jsp
k=accept v=*/*
k=accept-encoding v=gzip, deflate
k=user-agent v=Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
k=host v=localhost:8080
k=connection v=Keep-Alive
k=cookie v=JSESSIONID=38A23568899BD404F2357DAA1174F412

我就根据里面的是否有content-type这个小小区别,来判断是否要设置utf-8来解决了,jquery乱码和jmesa乱码的冲突问题。
// put this code to avoid jmesa messy code
if (request.getHeader("content-type") != null
&& request.getHeader("content-type") != "") {
request.setCharacterEncoding(ajaxEncoding);
}

其实我自己的项目都比较喜欢采用UTF-8,应该不会遇到这样的问题,但是以后也说不定用得上。呵呵。记录一下。


你可能感兴趣的:(jquery,Ajax,jsp,.net,Flash)