java filterconfig_java关于ServletConfig FilterConfig什么用

具体的使用方法你可以在google上搜索 “filter 过滤器”,FilterConfig可以获取部署描述符文件(web.xml)中分配的过滤器初始化参数。

针对你的问题回答,结果就是说FilterConfig可以获得web.xml中,以 filter 作为描述标签内的参数。

定义:

FilterConfig对象提供对servlet环境及web.xml文件中指派的过滤器名的访问。

FilterConfig对象具有一个getInitParameter方法,它能够访问部署描述符文件(web.xml)中分配的过滤器初始化参数。

实例:

将下面的代码加入到web.xml中,试用FilterConfig就可以获得以 filter 作为描述标签内的参数。

CacheFilter

com.jspbook.CacheFilter

/TimeMonger.jsp

nocache

/TestCache.jsp

nocache

cacheTimeout

600

locale-sensitive

true

CacheFilter

*.jsp

用法:

filterConfig.getInitParameter("locale-sensitive"); 得到的就是 ture

filterConfig.getInitParameter("cacheTimeout"); 得到的就是 600

filterConfig.getInitParameter(request.getRequestURI()); 得到的就是param-name 对应的 param-value 值

过滤处理类:

public class CacheFilter implements Filter {

ServletContext sc;

FilterConfig fc;

long cacheTimeout = Long.MAX_VALUE;

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

// check if was a resource that shouldn't be cached.

String r = sc.getRealPath("");

String path = fc.getInitParameter(request.getRequestURI());

if (path != null && path.equals("nocache")) {

chain.doFilter(request, response);

return;

}

path = r + path;

}

public void init(FilterConfig filterConfig) {

this.fc = filterConfig;

String ct = fc.getInitParameter("cacheTimeout");

if (ct != null) {

cacheTimeout = 60 * 1000 * Long.parseLong(ct);

}

this.sc = filterConfig.getServletContext();

}

public void destroy() {

this.sc = null;

this.fc = null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值