[关闭]
@Ablechen 2019-07-09T12:41:31.000000Z 字数 5450 阅读 370

监听器和过滤器技术


过滤器

Created with Raphaël 2.1.2客户端浏览器客户端浏览器Filter1Filter1Filter2Filter2......服务器服务器requestHttpRequsetHttpResponseresponse

过滤机制包含的接口

主要使用的接口Filter接口

接口中需要实现三种方法

在doFilter中需要调用chain的doFilter方法,否则就会造成请求的拦截

关于过滤器的文件配置

  1. 使用xml进行配置

    filterfilter-namefilter-classinit-paramparam-nameparam-valuefilter-mappingfilter-nameurl-pattern

    其中的init-param的作用为flter的初始化值
    url-pattern的作用为指定filter对那些请求有效

  2. 使用标签来陪配置过滤器

    1. @WebFilter(fileName, value, urlPatterns, servletNames, initParams)

    在使用initParams属性的使用需要注意的是,initParams本身就是一个标签属性

    1. inintParams = {@WebInitParams(name="", value="")}
    2. <!-- urlPatternsxml中配置的作用是一样的, fileName的作用和fileter-name效果是一样的-->

监听器

监听器主要是用来监听web应用的启动和关闭的,通过监听web应用状态的改变,来引起相应的serlvet容器产生事件,做出相应的处理

监听器接口

8个监听器接口, 6个Event类

Event类ServletContextEventServletContextAttributeEventHttpSessionEventHttpSessionBindingEventServletRequestEventServletRequestAttributeEventHttpSessionListenerHttpSessionActiveListenerHttpSessionBindingListenerHttpSessionAttributeListenerServletContextListenerServletContextAttributeListenerServletRequestListenerServletRequestAttributeListener

监听对象的创建与销毁

application, session, request对象创建和销毁所对应的接口分别是ServletContextListener, HttpSessionListener, ServletRequestListener
这三个接口中都只包含两种方法Created(), Destoryed()

监听对象的属性的状态变化

application, session, request对象对应的接口分别是ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener
这三个接口中都只包含三种方法attributeAdded(), attributeRemoved(),attributeReplace(*)

监听session中的对象

使用的接口为HttpSessionBindingListener和HttpSessionActivationListener

  1. HttpSessionBindingListener用于监听session中的绑定信息
    valueBound():当加入session范围的对象的时候调用
    valueUnbound():当移除session范围的对象的时候调用
  2. HttpSessionBindingListener用于监听session中的绑定信息

关于统计网站登陆人数的小web程序

Created with Raphaël 2.1.2login每个客户端产生一个seessionsession设置name属性name属性是否为nullwelcomeexit?结束nono
WebjspServletbeanlistenlogin.jspindex.jspServletLoginServletExitUsers只包含静态方法OnlineListen
**其中login.jsp负责进行用于登陆的界面**
**index.jsp负责进行登陆成功之后的页面显示**
**ServletLogin负责登陆之后对进行设置属性**
**ServletExit负责在点击对出登陆之后进行的删除的操作**
**onlindeListener负责响应当session中对象的属性变化**

对于用户队列的修改是发生在监听器的事件中的,在servlet中进行的只是session属性的修改,可以将这些看作是一个标志位的修改

关于HttpSessionBindingEvent类的使用

  1. getName(): 获得删除,或者添加,覆盖的属性的名字
  2. getValue(): 获得被删除,添加,覆盖的属性的值
  3. getSession(): 获得当前对象的session
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注