ID #78211

JAVA:Filter编码器


  〔CharacterEncodingHttpServletRequestWrapper.java〕

package creator.common.filter;

import javax.servlet.http.*;

public class CharacterEncodingHttpServletRequestWrapper extends HttpServletRequestWrapper {

PRivate String ecoding = null;
private String oldEncoding = null;

public CharacterEncodingHttpServletRequestWrapper(HttpServletRequest request,String encoding) {
super(request);
this.ecoding = encoding;
this.oldEncoding = request.getCharacterEncoding();
}

public String getParameter(String value) {
try{
if((oldEncoding == null isIOS88591(oldEncoding)) && super.getParameter(value)!=null){
return new String(super.getParameter(value).getBytes("iso-8859-1"),ecoding);
}
else{
return super.getParameter(value);
}
}
catch(Exception e){
e.printStackTrace();
return null;
}
}

private boolean isIOS88591(String endcoding){
endcoding = endcoding.toLowerCase();
return endcoding.startsWith("iso")&& (endcoding.indexOf("8859") != -1) && endcoding.endsWith("1");
}
}


〔CharacterEncodingHttpServletResponseWrapper.java〕

package creator.common.filter;

import javax.servlet.http.*;

public class CharacterEncodingHttpServletResponseWrapper extends HttpServletResponseWrapper {

private String ecoding = null;

public CharacterEncodingHttpServletResponseWrapper(HttpServletResponse response,String encoding) {
super(response);
this.ecoding = encoding;
}

/* (non-Javadoc)
* @see javax.servlet.ServletResponse#setContentType(java.lang.String)
*/
public void setContentType(String value) {
super.setContentType("text/Html;charset="+ecoding);
}

}


〔CharsetEncodingFilter.java〕

package creator.common.filter;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;


public class CharsetEncodingFilter implements Filter {
private FilterConfig config = null;
private String RequestEncoding = null;
private String ResponseEncoding = null;

public void init(FilterConfig arg0) throws ServletException {

2011-09-27 22:01
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

用Java程序编写记事本

深入浅出URL编码

不用spring,hibernate超傻瓜JAVA开发(javabean+数组)

用Java编写计算器的几种常见的做法

proxool.default (HouseKeeper.java:149)异常解决办法