·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JAVA:Filter编码器

JAVA:Filter编码器

类别: JAVA教程  评论数:0 总得分:0
〔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 {
this.config = arg0;
this.RequestEncoding = config.getInitParameter("RequestEncoding");
this.ResponseEncoding = config.getInitParameter("ResponseEncoding");
}

/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
throws IOException, ServletException {
if(this.config == null){
return;
}
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;

CharacterEncodingHttpServletRequestWrapper mrequestw = new CharacterEncodingHttpServletRequestWrapper(request,ResponseEncoding);
CharacterEncodingHttpServletResponseWrapper wresponsew = new CharacterEncodingHttpServletResponseWrapper(response,ResponseEncoding);
fc.doFilter(mrequestw,wresponsew);
}

public void destroy() {
}

}


〔web.xml〕

<filter>
<filter-name>CharsetEncoding</filter-name>
<filter-class>javabean.com.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>RequestEncoding</param-name>
<param-value>iso-8859-1</param-value>
</init-param>
<init-param>
<param-name>ResponseEncoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>





-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1