项目源码解读(1): web.xml <filter> 的配置

在最近接触的一个项目中,涉及到对系统初始化的检测。在web.xml中,配置过滤器,用于拦截请求信息。web.xml 中filter 元素 配置如下:
<filter>
<filter-name>PowerCheck</filter-name>
<filter-class>apabi.edoc.util.filter.PowerFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PowerCheck</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

filter 的配置和servlet 差不多,有一个filter-name 元素节点,定义了该filter的name ,filter-class 为该filter的具体实现的类,<init-param> 初始化参数 encoding,在powerFilter.java 文件中,可以获取。
对应的PowerFilter.java文件为:
public class PowerFilter implements Filter{
private FilterConfig filterConfig;
protected String encoding = null;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
{
try
{
//进行请求和响应的类型转换
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

String spath = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
spath = spath.toUpperCase();
if(spath.indexOf(".cebx") >= 0 || spath.indexOf("/downloadcebx.do") == 0 || spath.indexOf("/IMGS/") == 0 || spath.indexOf("/SCRIPTS/") == 0 || spath.indexOf("/CSS/") == 0 || spath.indexOf("/LAYOUTS/") == 0)
{
chain.doFilter(request, response);
return;
}

//设置编码,处理中文编码问题
if(encoding != null)
{
httpRequest.setCharacterEncoding(encoding);
httpResponse.setContentType("text/html; charset=" + encoding);
}
httpResponse.setHeader("Cache-Control", "no-cache");
/* String spath = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
spath = spath.toUpperCase();
if(spath.indexOf("/IMGS/") == 0 || spath.indexOf("/SCRIPTS/") == 0 || spath.indexOf("/CSS/") == 0 || spath.indexOf("/LAYOUTS/") == 0)
{
chain.doFilter(request, response);
return;
}
*/

//系统初始化
if(spath.indexOf("/SERVICES/ERRORPAGES/ERROR.HTM") == -1)
{
if (!SysEnv.init(httpRequest))
{
// TODO 添加系统初始化错误处理
PrintWriter wr = httpResponse.getWriter();
wr.write( "<script language='javascript'>alert('系统初始化失败,请检查。');window.top.location.replace( '" + httpRequest.getContextPath()+ "/services/errorpages/Error.htm' );</script>" );
wr.flush();
wr.close();
return;
}
}

//权限控制
// if(!PowerAdmin.PowerQuery(httpRequest, httpResponse))
// return;

chain.doFilter(request, response);
}
catch (ServletException sx)
{
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox)
{
filterConfig.getServletContext().log(iox.getMessage());
}
catch(Exception e)
{
Logger.getLogger().error(e.getMessage(), e);
}
catch(Error e)
{
Logger.getLogger().error(e.getMessage(), e);
}
}

public FilterConfig getFilterConfig()
{
return this.filterConfig;
}

public void setFilterConfig(FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}

public void init(FilterConfig filterConfig)throws ServletException
{
this.filterConfig = filterConfig;
this.encoding=filterConfig.getInitParameter("encoding");
}

public void destroy()
{
}
}

在该java文件中,需要实现三个方法,init(),doFilter(),以及destroy();其中,init()方法,在容器启动时便开始进行初始化。在web.xml 中,配置<url-pattern>/*</url-pattern> ,对所有的请求均通过该过滤器检查,如果 当前环境检测失败,提示系统初始化失败。
对servlet 中,filter 的配置,可以参考一下网页介绍,http://www.java2s.com/Tutorial/Java/0400__Servlet/0340__Filter.htm;
该网页提供了几个实例,可以小试一下。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值