jsp_filter 编码 过滤器
jsp_filter 编码 过滤器
李茂冉 发表于2年前
jsp_filter 编码 过滤器
  • 发表于 2年前
  • 阅读 3
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

##字符集过滤器

public class CharacterEncodingFilter implements Filter {   
    private String edcoding=null;   
    private FilterConfig filterConfig=null;   
    private boolean ignore=true;   

    public void destroy() {   
         edcoding=null;   
         filterConfig=null;   
    }   

    public void doFilter(ServletRequest request, ServletResponse response,   
            FilterChain filterChain) throws IOException, ServletException {   
        if(ignore==true||request.getCharacterEncoding()==null){   
            String encoding=setCharacterEncoding(request);   
              if(encoding!=null){   
                request.setCharacterEncoding(encoding);   

                }   
        }   
          filterChain.doFilter(request, response);   
    }   

    public void init(FilterConfig filterConfig) throws ServletException {   
        this.filterConfig=filterConfig;   
        this.edcoding=filterConfig.getInitParameter("encoding");   
        String value=filterConfig.getInitParameter("ignore");   
        if(value==null){   
            this.ignore=true;   
        }else if(value.equalsIgnoreCase("true")){   
            this.ignore=true;   

        }else{   
            this.ignore=false;   
        }   

    }   
    public String setCharacterEncoding(ServletRequest request){   
        return this.edcoding;   
    }   
}   

##web.xml中加以下代码

<filter>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 

###完整示例:

package dw05prj.util.filter; 
import javax.servlet.Filter; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.FilterChain; 
import java.io.IOException; 
/*页面字符集过滤流*/ 
/* 
在web.xml中配制的例子 
<filter> 
<filter-name>CharEncodingFilter</filter-name> 
<filter-class>dw05prj.util.filter.CharEncodingFilter</filter-class> 
<init-param> 
<param-name>charset</param-name> 
<param-value>GBK</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>CharEncodingFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
*/ 
public class CharEncodingFilter implements Filter { 
    private String charSet ; 
    public void init(FilterConfig parm1) throws ServletException { 
        charSet = parm1.getInitParameter("charset") ; 
        if (charSet == null && charSet.length() < 1)  { 
            charSet = "UTF-8" ; 
        } 
        System.out.println ("CharEncodingFilter--字符集: " + this.getCharSet()) ; 
    } 

    public void doFilter(ServletRequest parm1, ServletResponse parm2,  FilterChain parm3)  throws IOException, ServletException { 
        // 设定字符集 
        parm1.setCharacterEncoding(this.getCharSet()); 
        parm3.doFilter(parm1, parm2); 
        //页面中每个向服务器的请求都被过滤 -- 如 图片,js,HTML/JSP等等 
        //System.out.println ("---CharEncodingFilter: " + this.getCharSet()) ; 
    } 
    public void destroy() { 
        this.setCharSet(null) ; 
    } 
    public void setCharSet(String charSet) { 
        this.charSet = charSet; 
    } 
    public String getCharSet() { 
    return (this.charSet); 
    } 
}
共有 人打赏支持
粉丝 6
博文 103
码字总数 59256
×
李茂冉
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: