FilterConfig的用法是什么
发布日期:2021-11-04 11:54:16
浏览次数:17
分类:技术文章
本文共 1533 字,大约阅读时间需要 5 分钟。
FilterConfig的用法是什么?
1. FilterConfig的用法:
FilterConfig可以从web.xml当中取得一些有关Filter参数,当Web应用启动时就可以获得了
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; } }`转载地址:https://blog.csdn.net/wxsyj/article/details/109637743 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月17日 08时49分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【redis】来吧,展示一下redis 发布-订阅模式
2019-04-27
当下热点词再学:redis缓存预热、更新、降级,限流
2019-04-27
【redis6.0.6】redis源码慢慢学,慢慢看 -- 第五天:adlist
2019-04-27
别抖,OK? 操作系统抖动现象、网络抖动与延迟、函数抖动之防抖与节流,串讲
2019-04-27
通过域名获取主机IP -- struct addrinfo
2019-04-27
【C++】算法集锦(8):从两数和问题拓展到一百数和问题
2019-04-27
【C++】算法集锦(9):背包问题
2019-04-27
【C++】算法集锦(10)通俗讲kmp算法
2019-04-27
【C++】算法集锦(12):高楼扔鸡蛋
2019-04-27
【图解】拥塞控制
2019-04-27
线程上下文切换
2019-04-27
什么是服务熔断?
2019-04-27
服务器压力过大?CPU打满?我来帮你快速检查Linux服务器性能
2019-04-27
C++面经总结之《Effective C++》(一)
2019-04-27
C++面经总结之《Effective C++》(二)
2019-04-27
这是什么“虎狼之词”啊!!!程序员的健康问题,看一线老中医怎么说!!!
2019-04-27
打开我的收藏夹 -- Python数据分析杂谈
2019-04-27