Servlet Filter(过虑器)
来源:互联网 发布:淘宝店多少销量能靠前 编辑:程序博客网 时间:2024/05/22 05:48
Servlet API中提供了一个Filter接口,开发web应用时,如果编写的Java类实现了这个接口,则把这个java类称之为过滤器Filter。通过Filter技术,开发人员可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截
如上图:也就是说每次访问网站时,都会先经过虑器,再到调用的资源,所以我们可以在过虑器里面做相应的处理
如,自动登录,全站的编码问题,控制缓存==;
Filter的生命周期
1、init(FilterConfig filterConfig)throws ServletException:和我们编写的Servlet程序一样,Filter的创建和销毁由WEB服务器负责。 web 应用程序启动时,web 服务器将创建Filter 的实例对象,并调用其init方法进行初始化开发人员通过init方法的参数,可获得代表当前filter配置信息的FilterConfig对象。
2、 doFilter(ServletRequest,ServletResponse,FilterChain)每次filter进行拦截都会执行在实际开发中方法中参数requestresponse通常转换为HttpServletRequest和HttpServletResponse类型进行操作
3、destroy():在Web容器卸载 Filter 对象之前被调用。
public class Filter1 implements Filter { //在filter初始化的时候执行@Overridepublic void init(FilterConfig filterConfig) throws ServletException {} // 在过滤器每次执行过滤的时候调用@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {System.out.println("filter1 ...");// 如果 要想去 访问 拦截的 的资源 的话 就 要 放行chain.doFilter(request, response);} // 在过滤器销毁的时候调用。@Overridepublic void destroy() {}}
<span style="font-family: 'Source Code Pro', monospace; font-size: 16px; white-space: pre;"></span><span style="font-family:Source Code Pro, monospace;font-size:18px;"><span style="white-space: pre-wrap;"><filter> <filter-name></span></span><span style="font-family: 'Source Code Pro', monospace; font-size: 16px; white-space: pre-wrap;">Filter1 </span><span style="font-family:Source Code Pro, monospace;font-size:18px;"><span style="white-space: pre-wrap;"></filter-name> <filter-class>org.test.</span></span><span style="font-family: 'Source Code Pro', monospace; font-size: 16px; white-space: pre-wrap;">Filter1</span><span style="font-family: 'Source Code Pro', monospace; font-size: 16px; white-space: pre-wrap;"></filter-class></span><span style="font-family:Source Code Pro, monospace;font-size:18px;"><span style="white-space: pre-wrap;"></span></span><span style="font-family: 'Source Code Pro', monospace; font-size: 16px; white-space: pre;"></span><span style="font-family:Source Code Pro, monospace;font-size:18px;"><span style="white-space: pre-wrap;"></filter></span></span><span style="font-size:18px;"></span>
<span style="white-space: pre;"></span><div><div><span style="font-size:18px;"><span style="white-space:pre"></span><filter-mapping> <span style="white-space:pre"></span> <filter-name>Filter1</filter-name> <span style="white-space:pre"></span> <url-pattern>/test.jsp</url-pattern><span style="white-space:pre"></span></filter-mapping></span></div></div>
0 0
- Servlet Filter(过虑器)
- Filter过虑器链
- 四个有用的过虑器 Filter
- Filter-过虑器应用之1-设置request编码
- Filter-过虑器应用之2-设置页面缓存
- Filter-过虑器应用之3-验证用户是否登录
- Filter-过虑器应用之4-用户自动登录
- Filter-过虑器应用之-数据的压缩
- 使用(Filter)过虑器实现对Session是否过时的判断
- filter对没有登录的用户过虑
- 如何加入过虑器
- shiro的过虑器
- servlet.Filter
- Servlet Filter
- Servlet Filter
- Servlet Filter
- servlet filter
- servlet-filter
- litepal数据库开源框架简单用法和我的感悟
- java对redis的基本操作
- Find Peak Element - LeetCode 162
- FZU 1890 竞技游戏 水
- 2015年5月16日
- Servlet Filter(过虑器)
- Python中List(列表)和tuple(元组)的差别
- 《算法的乐趣》作者王晓华:“玩”过就是收获
- poj1021
- Deep Learning in NLP (一)词向量和语言模型
- 修改和删除XML DOM节点以及simpleXML快速解析
- Android API 的一些问题
- 心态
- 在iOS开发中使用FMDB