Servlet Specification V2.4——SRV.3 Servlet Context

SRV.3 Servlet Context

SRV.3.1 Introduction to the ServletContext Interface

        ServletContext接口定义了一个web应用程序的servlet视图,这些servlet就运行在此web应用程序中。Container的提供者负责提供container中ServletContext接口的实现。使用ServletContext对象,servlet能够记录时间,取得资源的引用URL,设置并存储context中其它servlet也能够可访问的attribute(set and store attributes that other servlets in the context can access)。html

        ServletContext被root于web server中一个已知的路径。例如:一个servlet context能够被配置在http://www.google.com/catalog。做为context path,以request路径/catalog开始的全部请求,都会发送至与此ServletContext相关联的web application。java

 

 

SRV.3.2 Scope of a ServletContext Interface

        每一个发布到container中的web application都有一个ServletContext的实例与之相关联。若是container是分布式的运行在多台虚拟机上,那么web application将在每个JVM上有一个ServletContext的实例。
        在container中,没有做为web application的一部分被部署的servlet,是“默认”web application隐含的一部分,而且有一个默认的ServletContext。在分布式container中,默认的ServletContext并非分布式的,而是只存在于一个JVM中。web

 

 

SRV.3.3 Initialization Parameters

        下面的ServletContext接口的方法容许servlet访问存取与web application相关联的context初始化参数,这些参数由应用程序开发者在部署描述符中描述。数据库

  • getInitParameter服务器

  • getInitParameterNamessession

        初始化参数被开发者用来传达配置信息。典型的状况是站点管理员的的email地址,或者持有关键资源的系统的名称(the name of a system that holds critical data)。app

 

 

SRV.3.4 Context Attributes

        Servlet能够经过名称将一个对象做为属性绑定到context。全部与context绑定的属性都对于同一个web application中其余任何的servlet都是有效的。如下是ServletContext接口中支持这种功能的方法:jsp

  • setAttribute分布式

  • getAtrributeide

  • getAtrributeNames

  • removeAttribute

SRV.3.4.1 Context Attributes in a Distributed Container

        Context的attribute只在对于context所在的JVM中有效。这防止了ServletContext的attribute在分布式container中成为共享的内存数据。当有信息须要在运行在分布式环境下的不一样servlet之间共享时,这些信息应该被放到session中(参见SRV.7 Sessions),存在数据库中,或者set到一个EJB组件中。

 

 

SRV.3.5 Resourcess

        ServletContext接口只对web application中静态内容文档的层次结构提供了直接的存取的功能。,包括html,gif和jpeg文件,经过ServletContext接口的如下方法:

  • getResource

  • getResourceAsStream

        getResource和getResourceAsStream方法以一个以“/”开头的字符串做为参数,这个参数表明了资源相对于context的路径。这个文档的曾其结构可能会出如今server的文件系统里,web application存档文件里,或者一个远程服务器,甚至一些其余的位置。

        这些方法不是用来获取动态内容的。例如,在支持JSP规范的container中,一个形式为getResource("/index.jsp")的方法调用会返回这个 jsp的源代码,而不是jsp执行后的输出结果。参见SRV.8 Dispatching Request,以获取关于存取动态内容的更多信息。

        Web application中全部资源的列表能够经过使用getResourcePath(String path)方法得到。此方法的语法细则能够在servlet的API中找到。

 

 

SRV.3.6 Multiple hosts and Servlet Contexts

        Web server能够支持在一台服务器上多逻辑主机共享一个 IP地址,这种能力有时被称为“虚拟主机”。在这种状况下,每一个逻辑主机必须有它本身的servlet context或者servlet context 的集合。servlet context不能被多个虚拟主机共享。

 

 

SRV.3.7 Reloading Considerations

        虽然container提供者不是必须为了方便开发者而实现class reloading的方案,任何这样的实现都必须明确,全部它们可能使用的servlet和class(An exception is system classes that the servlet may use in a different class loader),都只装载在单个class loader中,这样才能保证应用程序呈现出开发者所指望的行为。为了方便开发者,container应该提供完整的session绑定listener的通知,以便用来监控在class reloading以前的session终止(session termination upon class reloading)。

        上一代的container建立新的class loader来装载servlet,不一样于被用来装载servlet context中其余servlet或者class的class loader(distinct from class loaders used to load other servlets or classes used in the servlet context)。

 

SRV.3.7.1 Temporary Working Directories

        每一个servlet context都须要一个临时存储目录。Servlet container必须为每一个servlet context提供一个私有的临时目录,而且经过javax.servlet.context.tempdir这个context attribute来使其可用。与这个attribute相关联的对象必须使java.io.File类型。         这一点被公认为是习惯作法,不少servlet引擎都提供了这样的实现。当container重启时,并不被要求去保持这个临时目录下的内容,可是必须保证临时目录中的一个servlet context的内容对与container中其余web application的servlet context使不可见的。

相关文章
相关标签/搜索