Servlet过滤器
来源:互联网 发布:java软件开发发展前景 编辑:程序博客网 时间:2024/06/04 18:06
Servlet过滤器
过滤器是什么
过滤器(Filter)是对(浏览器向jsp,servlet,html等这些web资源发出请求)和
(服务器回应给浏览器的内容),他可以进行过滤,对内容进行提取,或者进行修改。
过滤器的作用
- 在HttpServletRequest到达Servlet 之前,拦截客户的HttpServletRequest。
- 根据需要检查HttpServletRequest ,也可以修改HttpServletRequest 头和数据。
- 在HttpServletResponse 到达客户端之前,拦截HttpServletResponse 。
- 根据需要检查HttpServletResponse ,可以修改HttpServletResponse 头和数据。
过滤器的执行过程
浏览器向服务端发起http请求(request),经由web容器处理后,由过滤器进行过滤,之后再由Servlet,jsp,html等资源进行处理。处理完形成响应(response)由过滤器过滤,经由web容器返回给浏览器。
过滤器HelloWorld
1.配置web.xml
<filter> <filter-name>HelloFilter</filter-name> <filter-class>com.etc.filter.HelloFilter</filter-class> </filter> <filter-mapping> <filter-name>HelloFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.开发HelloFilter类
package com.etc.filter;public class HelloFilter implements Filter { // ### 过滤器初始化 public void init(FilterConfig config) throws ServletException { } // ### 过滤器销毁 public void destroy() { } // ### 过滤器处理函数 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("hello filter"); chain.doFilter(req,res); } }
Filter接口
包含以下三个方法:
- init() 在Filter生命周期中仅执行一次,web容器在调用init方法时
- destory() 在Web容器卸载Filter对象之前被调用。该方法在Filter的生命周期中仅执行一次,可以释放过滤器使用的资源。
- doFilter() Filter链的执行
FilterChain接口
代表过滤器链
* doFilter() 调用过滤器链中的下一个过滤器
0 0
- servlet 过滤器
- Servlet 过滤器
- servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet过滤器
- Servlet 过滤器
- Java的hashcode()和equals()
- ajax请求之后 用新窗口打开 防止浏览器阻止和拦截
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- iOS 隐藏/去掉 导航栏返回按钮中的文字
- java标准--校验参数
- Servlet过滤器
- OOM(3)
- 在Apex里面操作数据的时候的异常处理
- Redis和Memcache对比及选择
- Linux中的工作队列
- Docker容器修改hosts文件重启不变
- php不用数组获取checkbox的值
- Android中跨进程通讯的4种方式
- C# Windows - 菜单栏和工具栏