📄 encodingfilter.java
字号:
package com.beiktech.util;
import java.io.IOException ;
import javax.servlet.Filter ;
import javax.servlet.FilterChain ;
import javax.servlet.FilterConfig ;
import javax.servlet.ServletException ;
import javax.servlet.ServletRequest ;
import javax.servlet.ServletResponse ;
public class EncodingFilter
implements Filter
{
/**
* 默认的字符集
*/
protected String encoding = null ;
/**
×FilterServlet指定的配置,如果这个为空则标记着这个Filter还没有配置
*/
protected FilterConfig filterConfig = null ;
/**
* 客户端指定的字符集是否要求被忽略
*/
protected boolean ignore = true ;
/**
* 回收垃圾,同时本Filter不再起作用
*/
public void destroy ()
{
this.encoding = null ;
this.filterConfig = null ;
}
/**
* 改变Reqeust中的字符集
*
*
* @param request 正在处理的servlet Request
* @param response 处理后的response
* @param chain 正在处理的Filter连
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter (
ServletRequest request ,
ServletResponse response ,
FilterChain chain )
throws
IOException ,
ServletException
{
if ( ignore || ( request.getCharacterEncoding () == null ) )
{
//request.setCharacterEncoding ( selectEncoding ( request ) ) ;
request.setCharacterEncoding("utf-8");
//System.out.println("request has been set to GBK123");
}
chain.doFilter ( request , response ) ;
}
/*******************************************************
* 初始化过滤器服务.
* @param filterConfig The filter configuration object
* @throws ServletException 如果遇到异常
* @author chenbingc
* @version 1.0
* @since 1.0
********************************************************/
public void init ( FilterConfig filterConfig )
throws ServletException
{
this.filterConfig = filterConfig ;
this.encoding = filterConfig.getInitParameter ( "encoding" ) ;
String value = filterConfig.getInitParameter ( "ignore" ) ;
if ( value == null )
this.ignore = true ;
else if ( value.equalsIgnoreCase ( "true" )
|| value.equalsIgnoreCase ( "yes" ) )
this.ignore = true ;
else
this.ignore = false ;
}
/***************************************************
* selectEncoding
* 从web.xml中取出配置字符集的信息
* @param request 正在处理的Request
* @return 返回配置文件中设定的字符集
***************************************************/
protected String selectEncoding ( ServletRequest request )
{
return ( this.encoding ) ;
}
/**
* Returns the filterConfig.
* @return FilterConfig
*/
public FilterConfig getFilterConfig ()
{
return filterConfig ;
}
/**
* Sets the filterConfig.
* @param filterConfig The filterConfig to set
*/
public void setFilterConfig ( FilterConfig filterConfig )
{
this.filterConfig = filterConfig ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -