WEB开发网
开发学院WEB开发Jsp JAVA:Filter编码器 阅读

JAVA:Filter编码器

 2008-01-05 09:00:21 来源:WEB开发网   
核心提示:〔CharacterEncodingHttpServletRequestWrapper.java〕package creator.common.filter;import javax.servlet.http.*;public class CharacterEncodingHttpServletRequestWrapp

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

Tags:JAVA Filter 编码器

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接