JAVA:Filter编码器

原创 2005年03月04日 08:31:00

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

接上一篇改篇:Java EE7,Tomcat8,HTML5,WebSocket1编码器与解码器的例子

import java.io.IOException; import javax.websocket.CloseReason; import javax.websocket.EncodeExcepti...

Java EE HTML5 WebSocket 编码器与解码器的例子

至少原文要保留,原味没有了.也就没意思了, 原文链接:http://www.byteslounge.com/tutorials/java-ee-html5-websockets-encoder-an...

Java Web基础知识之Filter:过滤一切你不想看到的事情

不要相信客户端, 所以做后端的人都应该铭记的事情。因为前端传过来的数据并不总是合法和有效的,所以后端是对访问资源的最后一道保护伞。之前我们在Spring中说到过AOP编程,AOP基础知识,它就可以在执...
  • lmy86263
  • lmy86263
  • 2016年04月26日 23:03
  • 23012

Java Filter相关资料

  • 2010年04月19日 23:00
  • 39KB
  • 下载

java中filter的用法

  • 2012年03月07日 09:50
  • 25KB
  • 下载

Java三大器之过滤器(Filter)的工作原理和代码演示

一、Filter简介   Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servl...

jsp-Java-servlet-jdbcMysql-filter-CRUD

  • 2017年04月24日 12:17
  • 2.05MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA:Filter编码器
举报原因:
原因补充:

(最多只允许输入30个字)