Filter
1.什么是过滤器?
Filter过滤器它是JavaWEB三大组件之一(Servlet应用程序、Listener监听器、Filter过滤器)
Filter过滤器它是JavaEE的规范,也就是接口。
Filter过滤器它的作用:拦截请求,过滤响应
它会在一组资源(jsp,servlet,.css,.html等)的前面执行
概述
过滤器实际上就是对web资源(HTML、CSS、Servlet、JSP)进行拦截,做一些处理后再交给下一个过滤器或servlet处理
通常都是用来拦截request进行处理的,也可以对返回的response进行拦截处理
过滤器使用场景
自动登录 eg:防止未登录就进入界面
统一设置编码格式
访问权限控制
敏感字符过滤等
Filter快速入门
(1)编写一个Filter,定义类实现接口Filter,实现接口中的抽象方法
(2)在web.xml文件中配置访问过滤路径(和Servlet的配置有些相似)
3.如何编写过滤器?
a.自定义类实现Filter接口,重写接口中的抽象方法(3个)
b.在web.xml中进行配置
<filter>
<filter-name>DemoFilter</filter-name>
<filter-class>com.zking.filter.DemoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DemoFilter</filter-name>
<url-pattern>/as.do</url-pattern>
</filter-mapping>
常用配置项
urlPatterns
以指定资源匹配。例如"/index.jsp"
以目录匹配。例如"/servlet/*"
以后缀名匹配,例如"*.jsp"
通配符,拦截所有web资源。"/*"
:你要拦截的内容是什么?eg:
<url-pattern>/a.jsp</url-pattern>
<url-pattern>/*.jsp</url-pattern>
<url-pattern>/*.do</url-pattern>
<url-pattern>/*</url-pattern>
Filter生命周期
(1)构造器:Filter的构造器在服务器启动时调用。 构造器只会调用一次,说明Filter也是单例多线程的。
(2)init():在构造器被调用后,紧接着被调用。作用:用来初始化Filter。
(3)doFilter():每一次拦截请求时都会调用。
(4)destroy方法在项目停止时调用,用来在对象被销毁前做一些收尾工作。
多个Filter的执行顺序
(1)在web.xml中,filter执行顺序跟<filter-mapping>的顺序有关,先声明的先执行
(2)使用注解配置的话,filter的执行顺序跟名称的字母顺序有关,例如AFilter会比BFilter先执行
(3)如果既有在web.xml中声明的Filter,也有通过注解配置的Filter,那么会优先执行web.xml中配置的Filter
4.Filter接口的生命周期
void init(FilterConfig)
* 创建之后,马上执行:Filter会在服务器启动时就创建
void destory()
* 销毁之前执行!在服务器关闭时销毁
void doFilter(ServletRequest,ServletResponse,FilterChain)
* 每次过滤器时都会执行5.Filter接口中的相关类介绍
FilterConfig---->与ServletConfig相似
* 获取初始化参数:getInitParameter
* 获取过滤器名称:getFilterName
* 获取application:getServletContextFilterChain
* doFilter(ServletRequest,ServletResponse):放行请求6.多个过滤器的执行顺序
过滤器的执行顺序和url-pattern标签匹配的精确程度无关,只和他们的filter-mapping标签在web.xml文件中的顺序有关,靠上的配置的先执行。7.过滤器的四种拦截方式(了解)
REQUEST、FORWARD、INCLUDE、ERROR。REQUEST:直接访问目标资源时执行过滤器。包括:在地址栏中直接访问、表单提交、超链接、重定向,只要在地址栏中可以看到目标资源的路径,就是REQUEST;
FORWARD:转发访问执行过滤器。包括RequestDispatcher#forward()方法、<jsp:forward>标签都是转发访问;INCLUDE:包含访问执行过滤器。包括RequestDispatcher#include()方法、<jsp:include>标签都是包含访问;
ERROR:当目标资源在web.xml中配置为<error-page>中时,并且真的出现了异常,转发到目标资源时,会执行过滤器。可以在<filter-mapping>中添加0~n个<dispatcher>子元素,来说明当前访问的拦截方式。 例子:
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/b.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher</filter-mapping>
这个配置中,b.jsp为目标资源。
当直接请求b.jsp时,会执行过滤器。
当转发到b.jsp页面时,会执行过滤器。
Listener
第三种Servlet程序: 监听Servlet Listener
主要功能是负责监听Web的各种操作,当相关的事件触发后将产生事件,并对事件进行处理。监听器分为3种:
application监听器
session监听器
request监听器
事件:点击、移动、键盘、触摸
1.application监听器 servlet---ServletContext
实现:ServletContextListener (常用)
重写:
//容器启动时调用
public void contextInitialized(ServletContextEvent event){
}
//容器消毁时调用
public void contextDestroyed(ServletContextEvent event){
}
ServletContextEvent事件方法:
.getServletContext() //取得ServletContext对象,即上下文
例示:
event.getServletContext().getContextPath();
2.Session监听器
实现:HttpSessionListener (偶尔用)
重写:
//session创建时调用
public void sessionCreated(HttpSessionEvent event){}
//session销毁时调用
public void sessionDestroyed(HttpSessionEvent event){}
HttpSessionEvent事件方法:
.getSession() //取得当前的session例示:
event.getSession().getId(); //得到session的ID
实现:HttpSessionAttributeListener (不用,性能差)
重写:
//增加属性时触发
public void attributeAdded(HttpSessionBindingEvent event){
}//删除属性时触发
public void attributeRemoved(HttpSessionBindingEvent event){
}//替换属性时触发
public void attributeReplaced(HttpSessionBindingEvent event){
}
HttpSessionBindingEvent事件方法:
.getSession() //取得session
.getName() //取得属性的名称
.getValue() //取得属性的内容例示:
event.getSession() //取得session
event.getName() //取得属性的名称
event.getValue() //取得属性的内容
3.request监听器
实现:ServletRequestListener (不用,性能差)
重写:
//请求开始时调用
public requestInitialized(ServletRequestEvent event){}
//请求结束时调用
public requestDestroyed(ServletRequestEvent event){}
ServletRequestEvent事件方法:
.getServletRequest() //取得ServletRequest对象
.getServletContext() //取得ServletContext对象例示:
event.getServletRequest().getRemoteAddr(); //得到IP地址
event.getServletContext().getContextPath(); //得到当前路径
为什么说session监听器和request监听器一般都不用?
答:以request监听器为例,如果采用request监听,那就意味着每次请求都要触发一次监听,这大大降低了程率的效率,因此很少用。