---
title: "Servlet Specification V2.4——SRV.3 Servlet Context"
author: "sailor90"
date: "2010-03-25T17:30:07Z"
tags: ["JSP", "JVM", "Servlet", "Web", "应用服务器"]
categories: ["企业架构"]
slug: "servlet-specification-v2-4-srv-3-servlet-context"
draft: false
lastmod: "2010-03-25T17:30:07Z"
id: 624921
url: "https://sailor90.iteye.com/blog/624921"
---

# 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）。

        ServletContext被root于web server中一个已知的路径。例如：一个servlet context可以被配置在http://www.google.com/catalog。作为context path，以request路径/catalog开始的所有请求，都会发送至与此ServletContext相关联的web application。

 

 

## 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中。

 

 

## SRV.3.3 Initialization Parameters

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

- getInitParameter
- getInitParameterNames

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

 

 

## SRV.3.4 Context Attributes

        Servlet可以通过名称将一个对象作为属性绑定到context。所有与context绑定的属性都对于同一个web application中其他任何的servlet都是有效的。以下是ServletContext接口中支持这种功能的方法：

- setAttribute
- getAtrribute
- 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使不可见的。