对象作用域与Servlet事件监听器



                                                                                               第六章     对象作用域与Servlet事件监听器

   

 作用域对象         属性操作方法 (通用)         作用域范围
ServletContext(上下文)       SetAttribute(String, Object)整个web应用程序
HttpSessoin(会话)getAttribute(String)一个会话过程
ServletRequest请求()removeAttribute(String)

一次请求过程

     (一)ServletContext应用上下文

               ServletContext context=ServletConfig.getServletContext(String);

                 (1)WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
                 (2) ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通  过                                ServletConfig.getServletContext方法获得ServletContext对象。
                  (3)由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。

               (4)ServletContext对象通常也被称之为context域对象。

    (二)会话作用域Sessoin

           HttpSession session=request.getSession();

             session在下列情况下被删除:

                    A.程序调用HttpSession.invalidate()

                    B.距离上一次收到客户端发送的session id时间间隔超过了session的最大有效时间

                    C.服务器进程被停止

                    D.服务器重置

              会话作用域Sessoin的使用是建立在Cookie 的基础之上

   (三)ServletRequest请求作用域  

             List list=(List)request.getAttribute(String);

                request.SetAttribute(String,object)

Servlet事件监听器

     servlet事件监听器
按监听的对象划分:servlet2.4规范定义的事件有三种:
1.用于监听应用程序环境对象(ServletContext)的事件监听器
2.用于监听用户会话对象(HttpSession)的事件监听器
3.用于监听请求消息对象(ServletRequest)的事件监听器

按监听的事件类项划分
1.用于监听域对象自身的创建和销毁的事件监听器
2.用于监听域对象中的属性的增加和删除的事件监听器
3.用于监听绑定到HttpSession域中的某个对象的状态的事件监听器


监听域对象的创建和销毁
在一个web应用程序的整个运行周期内,web容器会创建和销毁三个重要的对象,ServletContext,HttpSession,ServletRequest。
servlet2.4中定义了三个接口:ServletContextListener,HttpSessionListener,ServletRequestListener。
1.在ServletContextListener接口中定义了两个事件处理方法,分别是contextInitialized()和contextDestroyed()
   public void contextInitialized(ServletcontextEvent sce)
 这个方法接受一个ServletContextEvent类型参数,在contextInitialized可以通过这个参数获得当前被创建的ServletContext对象。
   public void contextDestroyed(ServletContextEvent sce)

  使用ServletContextAttributeListener


ServletContextAttributeListener用于监听ServletContext(application)范围内属性的变化,实现该接口的监听器需要实现如下三个方法。


attributeAdded(ServletContextAttributeEvent event):当程序把一个属性存入application范围时触发该方法。


attributeRemoved(ServletContextAttributeEvent event):当程序把一个属性从application范围删除时触发该方法。


attributeReplaced(ServletContextAttributeEvent event):当程序替换application范围内的属性时将触发该方法。
 
2.在HttpSessionListneter接口中共定义了两个事件处理方法,分别是sessionCreated()和sessionDestroyed()
   public void sessionCreated(HttpSessionEvent se)
 这个方法接受一个(HttpSessionEvent 类型参数,在sessionCreated可以通过这个参数获得当前被创建的HttpSession对象。
   public void sessionDestroyed(HttpSessionEvent se)


在Servlet中,传入Session的对象如果是一个实现HttpSessionBindingListener接口的对象(方便起见,此对象称为监听器),则在传入的时候(即调用HttpSession对象的setAttribute方法的时候)和移去的时候(即调用HttpSession对象的removeAttribute方法的时候或Session Time out的时候)Session对象会自动调用监听器的valueBound和valueUnbound方法(这是HttpSessionBindingListener接口中的方法

HttpBindingListener是不用在web.xml中设定Listener的。


HttpSessionAttributeListener有3个接口需要实现

attributeAdded//在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的

attributeRemoved//修改、删除session中添加对象时触发此操作  笼统的说就是调用 removeAttribute这个方法时候会触发的

attributeReplaced//在Session属性被重新设置时



3.在ServletRequestListener接口中定义了两个事件处理方法,分别是requestInitialized()和requestDestroyed()
   public void requestInitialized(ServletRequestEvent sre)
 这个方法接受一个(ServletRequestEvent 类型参数,在requestInitialized可以通过这个参数获得当前被创建的ServletRequest对象。
   public void requestDestroyed(ServletRequestEvent sre)

(8)ServletRequestAttributeListener                 该接口提供了一下三种方法

                    1.attributeAdded(ServletRequestAttributeEvent event);        当有对象加入request的范围时,通知正在收听的对象


                    2.attributeReplaced(ServletRequestAttributeEvent event);         当在request的范围内有对象取代两一个对象时,通知正在收听的对象


                   3.attributeRemoved(ServletRequestAttributeEvent event);      当有对象从request的范围移除时,通知正在收听的对象








                  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值