📄 characterfilter.java
字号:
/**
* 中文编码处理
*/
package fuguo.yy1.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;
/**
* @author cankongyun
*
*/
public class CharacterFilter implements Filter {
protected String encoding=null;
protected FilterConfig filterConfig=null;
protected boolean ignore=true;
/* (非 Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
* 初始化方法,通过配置的参数设置标识变量
*/
public void init(FilterConfig _filterConfig) throws ServletException {
// TODO 自动生成方法存根
this.filterConfig=_filterConfig;
this.encoding=_filterConfig.getInitParameter("encoding");
String value=filterConfig.getInitParameter("ignore");
if(value==null)
this.ignore=false;
else if(value.equalsIgnoreCase("false"))
this.ignore=true;
else if(value.equalsIgnoreCase("no"))
this.ignore=true;
else
this.ignore=false;
}
/**
* 得到配置文件的编码类型
* @param request
* @return
*/
protected String selectEncoding(ServletRequest _request){
return this.encoding;
}
/* (非 Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
* Filter的主体操作
*/
public void doFilter(ServletRequest _request, ServletResponse _response,
FilterChain _filterChain) throws IOException, ServletException {
// 判断是否需要进行编码的设置
if(!ignore||(_request.getCharacterEncoding()==null)){
//得到配置文件的编码类型
String encoding=selectEncoding(_request);
if(encoding!=null){
_request.setCharacterEncoding(encoding);
}
}
//Pass control on to the next filter
_filterChain.doFilter(_request,_response);
}
/* (非 Javadoc)
* @see javax.servlet.Filter#destroy()
* 设置申请对象的值为空,有利于内存资源的快速释放
*/
public void destroy() {
// TODO 自动生成方法存根
this.encoding=null;
this.filterConfig=null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -