目录
1.过滤器(Filter)
1) Filter也属于Servlet规范
2) Filter开发步骤:
新建类实现Filter接口
--> 实现其中三个方法(init、doFilter、destory)
--> 配置Filter(可以使用注解@WebFilter,也可以使用xml文件<filter><filter-mapping>)
3) Filter在配置时,和Servlet一样,可以配置通配符,例如 @WebFilter("*.do"),表示拦截所有以.do结尾的请求
4)过滤器链
多个Filter拦截,若使用@WebFilter注解方式配置则自动识别先后顺序为a-z,0-9;
若在配置文件中配置,则按照配置顺序
2.事务管理
1) ThreadLocal
- get(),set(Obj)
- ThreadLocal:本地线程
通过set方法在当前线程上存储数据、通过get方法在当前线程上获取数据
public void set(T value) {
Thread t = Thread.currentThread(); //获取当前线程
ThreadLocalMap map = getMap(t); //每一个线程都维护各自的一个容器(ThreadLocalMap),一个http请求一般对应一个线程
if (map != null) {
map.set(this, value); //这里的key对应的是ThreadLocal实例,委会一个ThreadLocalMap,key为线程,根据不同线程获取不同的Map,这个Map的key为ThreadLocal实例
} else {
createMap(t, value);
}
}
(get()方法可通过ctrl+B跳转源码读得)
3.监听器
1) ServletContextListener -->监听ServletContext对象的创建(-Created)和销毁(-Destroyed)的过程
2) HttpSessionListener -->监听HttpSession对象的创建(-Created)和销毁(-Destroyed)
3) ServletRequestListener -->监听Request对象的创建(-Created)和销毁(-Destroyed)
4) ServletContextAttributeListener -->监听ServletContext中属性的创建、修改和销毁
a.attributeAdded(ServletContextAttributeEvent scab) 向ServletContext中添加属性时调用
b.attributeRemoved(ServletContextAttributeEvent scab) 从ServletContext中移除属性时调用
c.attributeReplaced(ServletContextAttributeEvent scab) 当ServletContext中的属性被修改时调用
ServletContextAttributeEvent对象代表属性变化事件,包含如下方法:
a.getName() 获取修改或添加的属性名
b.getValue() 获取被修改或添加的属性值
c.getServletContext() 获取ServletContext对象laodong
5) HttpSessionAttributeListener -->监听HttpSession中属性的创建、修改和销毁(方法同上)
6) ServletRequestAttributeListener -->监听ServletRequest中属性的创建、修改和销毁(方法同上)
7) HttpSessionBindingListener -->监听某个对象在Session域中的创建与移除
a.alueBound(HttpSessionBindingEvent event) 该类的实例被放到Session域中时调用
b.valueUnbound(HttpSessionBindingEvent event) 该类的实例从Session中移除时调用
HttpSessionBindingEvent对象代表属性变化事件,它包含的方法如下:
a. getName() 获取当前事件涉及的属性名
b. getValue() 获取当前事件涉及的属性值
c. getSession() 获取触发事件的HttpSession对象
8) HttpSessionActivationListener -->监听某个对象在Session中的序列化与反序列化。
a.sessionWillPassivate(HttpSessionEvent se) 该类实例和Session一起钝化到硬盘时调用
b.sessionDidActivate(HttpSessionEvent se) 该类实例和Session一起活化到内存时调用
HttpSessionEvent对象代表事件对象,通过getSession()方法获取事件涉及的HttpSession对象。