首页 > 学院 > 开发设计 > 正文

JAVA:Filter编码器

2019-11-18 14:43:25
字体:
来源:转载
供稿:网友

  〔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 {

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表