JavaWeb总结(八)

对象作用域 

  在Servlet里可以用一个名字绑定一个对象,并且在应用中传递和使用这个对象

作用域对象

属性操作方法

作用域范围说明

ServletContext(上下文)

void setAttribute(String,Object)

Object getAttribute(Sting)

void removeAttribute(String)

Enumeration getAttributeNames()

整个Web应用程序

HttpSession(会话)

一个会话交互过程

ServletRequest(请求)

一次请求过程

ServletContext应用上下文

  对于整个Web应用来说只有一个ServletContext对象,而且在Web应用中所有的部分都能访问它。

 

ServletContext对象

  设置在ServletContext对象中的属性可以被Web应用中的任何一部分来访问 

提示:对ServletContext对象的初始化参数通过web.xml文件中<context-param>标签设置,在其他Servlet中可以通过getInitParameter(String)方法来获取。

  获取ServletContext的方法

- this.getServletContext();

- this.getServletConfig().getServletContext();

- request.getSession().getServletContext();

 

Web应用中每个线程都能访问上下文属性

提示:上下文作用域中设置的属性在整个Web中应用中被共享,紫瑶服务器不被关闭,Web应用中任何一部分都能访问到该属性。所以线程并不安全!

会话作用域

  Session用于维护与一个客户的会话状态。对于同一个客户的多个请求,Session会跨这些请求持久存储 

 

session跨请求存储属性

  在会话作用域中设置的属性不能被Web应用中所有的部分访问,只能由同一个客户或服务器的一个持续会话交互过程中被存储维护,会话被销毁设置在其中的属性也会被销毁,会话作用域范围比上下文作用域方法小。

请求作用域 

- 系统的资源消耗(长久保存的会大量消耗系统的资源)

- 属性可以保存在请求作用域范围中 

- 请求作用范围仅仅作用在与一个请求相关的两个资源之间

 

请求作用域范围

存储时间比上下文作用域和会话作用域短,在请求结束后,对象就会被垃圾回收

监听器概述

- 监听session,request,application这三个对象里存取数据的变化

- 监听器对象可以在事情发生前、发生后可以做一些必要的处理

- Servlet监听器主要目的是给Web应用增加事件处理机制,以便更好地监视和控制Web应用的状态变化 

监听器分类

  应用程序事件监听器是实现一到多个Servlet事件监听器借口的类。它们在Web应用程序部署时,被Web容器初始化和注册。开发者通常以WAR格式提供应用程序事件监听器。

事件类型

描述

Listener接口

ServletContext事件

生命周期

Servlet上下文刚被创建并可以开始为第一次请求服务,或者Servlet上下文将要被关闭发生的事件

ServletContextListener

属性改变

Servlet上下文内的属性被增加、删除或者替换时发生的事件

ServletContextAttributeListener

HttpSession事件

生命周期

HttpSession被创建、无效或超时时发生

HttpSessionListenerHttpSessionActivationListener

会话迁移

HttpSession被激活或钝化时发生

属性改变

在HttpSession中的属性被增加、删除、替换时发生

HttpSessionAttributeListenerHttpSessionBindingListener

对象绑定

对象被绑定到或者移出HttpSession时发生

ServletRequest事件

生命周期

在Servletr请求开始被Web组件处理时发生

ServletRequestListener

属性改变

在ServletRequest对象中的属性被增加、删除、替换时发生

ServletRequestAttributeListener

Web应用程序范围内的事件

- Web应用启动和销毁事件

- Web应用程序的属性发生改变的事件(包括增加、删除、修改)。

- 定义了ServletContextListener和ServletContextAttributeListener两个接口 - ServletContextListenner

  用于监听Web应用程序启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListenner接口

  ServletContextListenner接口方法

- void contextInitalized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化

- void contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁

public class MyServletContextListener implements ServletContextListener {
   public void contextInitialized(ServletContextEvent sce) {
      //Initialized code
   }
   public void contextDestroyed(ServletContextEvent sce)   { 
      //Destroyed code
   }
}

ServletContextEvent 中的方法

  ServletContext getServletContext():取得servletContext对象

生命周期事件的一个实际应用由context监听器管理共享数据库连接。在web.xml中定义监听器

<listener>
   <listener-class>
      com.lovobook.MyServletContextListener
   </listener-class>
</listener> 

  ServletContextAttributeListener接口

  监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性 

- attributeAdded(ServletContextAttributeEvent scab):若有属性对象加入Application的范围,通知正在收听的对象

- attributeRemoved(ServletContextAttributeEvent scab):若有属性对象从Application的范围移除,通知正在收听的对象

- attributeReplaced(ServletContextAttributeEvent scab):若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象

 

public class MyServletContextAttributeListener implements ServletContextAttributeListener { 
   public void attributeAdded(ServletContextAttributeEvent scae){}
   public void attributeRemoved(ServletContextAttributeEvent scae) {}
   public void attributeReplaced(ServletContextAttributeEvent scae) {} 
}

 

 

监听会话范围内事件 

  管理从同一个客户端或用户向一个Web应用程序发出的一系列请求相关的状态或资源

  - HttpSessionBindingListener接口 

注意:唯一不需要在web.xml中设定的Listener

- 监听对象加入Session范围时

- 监听从Session范围中移出对象时

  接口有两个方法

- void valueBound(HttpSessionBindingEvent event):当对象正在绑定到Session中,Servlet容器调用该方法来通知该对象

- void valueUnbound(HttpSessionBindingEvent event):当从Session中删除对象时,Servlet容器调用该方法来通知该对象

  HttpSessionBindingEvent类提供如下方法:

- public String getName():返回绑定到Session中或从Session中删除的属性名字。

- public Object getValue():返回被添加、删除、替换的属性值

- public HttpSession getSession():返回HttpSession对象

  - HttpSessionAttributeListener接口

    监听HttpSession中的属性的操作

- 当在Session中增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;

- 当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;

- 当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。 

  - HttpSessionListener接口

    监听HttpSession对象的创建和销毁操作

- 当创建一个Session时,激发session Created(HttpSessionEvent se)方法 

- 当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法 

  - HttpSessionActivationListener接口

监听HttpSession对象的激活和迁移

- public void sessionDidActivate():会话被激活

- public void sessionWillPassivate():会话被迁移

监听请求生命周期内事件 

  - ServletRequestListener接口

    - public void requestDestroyed(ServletRequestEvent sre):当请求被销毁时被处理。

    - public void requestInitialized(ServletRequestEvent sre):当请求被创建时被处理 

  - ServletRequestAttributeListener接口

- public void attributeAdded(ServletRequestAttributeEvent arg0) :当在请求作用域中添加一个属性的时候调用该方法。

- public void attributeRemoved(ServletRequestAttributeEvent arg0) :当在请求作用域中删除一个属性时调用

- public void attributeReplaced(ServletRequestAttributeEvent arg0) :当在请求作用域中替换一个属性值的时候调用 

 

我不作恶

但有权拒绝为善

我不赞同

但是我捍卫你不为善的权力

原文地址:https://www.cnblogs.com/HackerBlog/p/5966319.html