<!-- -->
|
JavaTM 2 Platform Ent. Ed. v1.4 |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES <!-- if(window==top) { document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>'); } //--> All Classes <noscript></noscript> | |||||||
SUMMARY:NESTED|FIELD|CONSTR|METHOD | DETAIL:FIELD|CONSTR|METHOD |
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. 定义了一组方法,servlet用来和它的servlet容器通信,比如,获得文件的MIME类型,分配请求,或者输出日志文件。
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog
and possibly installed via a .war
file.) 每个Java虚拟机的每个web应用都会有一个context。(一个web应用是指安装在服务器的URL命名空间的子集下(比如/catalog)也有可能通过.war文件安装的一组 servlet和内容的组合)。
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead. 如果web应用在它的部署描述符中标记为“distributed”,那么每一个虚拟机将只有一个context实例。在这种情况下,context不会用来作为全球共享信息的地址(因为信息不可能真的全球共享),而是使用外部的资源,比如数据库来替代。
The ServletContext
object is contained within the ServletConfig
object, which the Web server provides the servlet when the servlet is initialized. ServletContext对象被包含于ServletConfig对象内,该对象由servlet初始化时web服务器提供给servlet。
Servlet.getServletConfig()
,
ServletConfig.getServletContext()
<!-- ======== NESTED CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><!-- ========== METHOD SUMMARY =========== --><!-- -->
Method Summary | |
Object |
getAttribute(Stringname) Returns the servlet container attribute with the given name, or null if there is no attribute by that name. 返回给定名称的的servlet容器属性,如果没有该名称的属性,返回null。 |
Enumeration |
getAttributeNames() Returns an Enumeration containing the attribute names available within this servlet context. 返回servlet上下文中可用的属性枚举。 |
ServletContext |
getContext(Stringuripath) Returns a ServletContext object that corresponds to a specified URL on the server. 根据服务器上的指定URL返回ServletContext对象。 |
String |
getInitParameter(Stringname) Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. 返回等于指定名称的context初始化参数值的String,如果该参数不存在,返回null。 |
Enumeration |
getInitParameterNames() Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters. 以String对象枚举的形式返回context初始化参数的名称,如果context没有初始化参数,返回一个空枚举。 |
int |
getMajorVersion() Returns the major version of the Java Servlet API that this servlet container supports. 返回servlet容器支持的Java Servlet API的主版本。 |
String |
getMimeType(Stringfile) Returns the MIME type of the specified file, or null if the MIME type is not known. 返回指定文件的MIME类型,如果MIME类型未知,返回null。 |
int |
getMinorVersion() Returns the minor version of the Servlet API that this servlet container supports. 返回servlet容器支持的Java Servlet API的次版本。 |
RequestDispatcher |
getNamedDispatcher(Stringname) Returns a RequestDispatcher object that acts as a wrapper for the named servlet. 返回一个RequestDispatcher对象,作为给定名称的servlet的包装器。 |
String |
getRealPath(Stringpath) Returns a String containing the real path for a given virtual path. 由给出的虚拟路径返回真实路径的String。 |
RequestDispatcher |
getRequestDispatcher(Stringpath) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. 返回一个RequestDispatcher对象,作为位于给定路径的资源包装器。 |
URL |
getResource(Stringpath) Returns a URL to the resource that is mapped to a specified path. 返回映射到指定路径的资源URL。 |
InputStream |
getResourceAsStream(Stringpath) Returns the resource located at the named path as an InputStream object. 以InputStream对象形式返回位于指定路径的资源。 |
Set |
getResourcePaths(Stringpath) Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. 返回web应用中所有资源路径的列表(类似于目录),它们的完整子路经应匹配提供的路径参数。 |
String |
getServerInfo() Returns the name and version of the servlet container on which the servlet is running. 返回正在运行servlet的servlet容器名称和版本。 |
Servlet |
getServlet(Stringname) Deprecated.As of Java Servlet API 2.1, with no direct replacement. 从Java Servlet API 2.1以来,不再推荐使用。没有替代方法。 This method was originally defined to retrieve a servlet from a In lieu of this method, servlets can share information using the |
String |
getServletContextName() Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element. 根据web应用中指定部署描述符中display-name元素的ServletContext,返回web应用的名称。 |
Enumeration |
getServletNames() Deprecated.As of Java Servlet API 2.1, with no replacement. 从Java Servlet API 2.1以来,不再推荐使用。没有替代方法。 This method was originally defined to return an |
Enumeration |
getServlets() Deprecated.As of Java Servlet API 2.0, with no replacement. 从Java Servlet API 2.0以来,不再推荐使用。没有替代方法。 This method was originally defined to return an |
void |
log(Exceptionexception, Stringmsg) Deprecated.As of Java Servlet API 2.1, use log(String message, Throwable throwable) instead. 从Java Servlet API 2.1以来,使用log(String message, Throwable throwable)代替。 This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file. 该方法原本定义为向servlet日志文件写入异常堆栈跟踪和错误说明信息。 |
void |
log(Stringmsg) Writes the specified message to a servlet log file, usually an event log. 向servlet日志文件写入指定信息,通常为事件日志。 |
void |
log(Stringmessage, Throwablethrowable) Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file. 向servlet日志文件写入错误说明信息和给定Throwable异常堆栈跟踪。 |
void |
removeAttribute(Stringname) Removes the attribute with the given name from the servlet context. 从servlet上下文中删除给定名称的属性。 |
void |
setAttribute(Stringname, Objectobject) Binds an object to a given attribute name in this servlet context. 在servlet上下文中绑定对象到给定的属性名。 |
<!-- ============ FIELD DETAIL =========== --><!-- ========= CONSTRUCTOR DETAIL ======== --><!-- ============ METHOD DETAIL ========== --><!-- -->
Method Detail |
public ServletContext getContext(Stringuripath)
ServletContext
object that corresponds to a specified URL on the server. 根据服务器上的指定URL返回ServletContext对象。
This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher
objects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container. 该方法允许servlet访问服务器上各种组成的上下文,也是从上下文中获取RequestDispatcher的需要。 路径必须以“/”开头,解释为相对于server文档根路径,对于驻留在容器的其它web应用从上下文根路径匹配。
In a security conscious environment, the servlet container may return null
for a given URL. 在一个有安全意识的环境中,servlet容器对于给出的URL可能返回null。
uripath
- a
String
specifying the context path of another web application in the container. String,指定容器中另一个web应用的上下文路径。
ServletContext
object that corresponds to the named URL, or null if either none exists or the container wishes to restrict this access. 符合指定URL的ServletContext对象,如果一个都不存在或者容器希望限制访问,返回null。
RequestDispatcher
public int getMajorVersion()
public int getMinorVersion()
public String getMimeType(Stringfile)
null
if the MIME type is not known. The MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor. Common MIME types are
"text/html"
and
"image/gif"
. 返回指定文件的MIME类型,如果MIME类型未知,返回null。MIME类型 可以由servlet容器的配置决定,也可以由web应用的部署描述符指定。 通常MIME类型为“text/html”和“image/gif”。
file
- a
String
specifying the name of a file String,指定文件名
String
specifying the file's MIME type String,表示文件的MIME类型
public Set getResourcePaths(Stringpath)
path
- the partial path used to match the resources, which must start with a / 用来匹配资源的部分路径,必须以/开头
public URL getResource(Stringpath) throws MalformedURLException
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war
file. 该方法允许servlet容器使得任何来源的资源对servlet可用。资源可以位于本地或远程文件系统,数据库,或者.war文件。
The servlet container must implement the URL handlers and URLConnection
objects that are necessary to access the resource. servlet容器必须实现URL处理器和URLConnection对象,这些对于访问资源是必需的。
This method returns null
if no resource is mapped to the pathname. 如果没有资源映射该路径名,该方法返回null。
Some containers may allow writing to the URL returned by this method using the methods of the URL class. 某些容器允许使用URL类的方法向该方法返回的URL输出。
The resource content is returned directly, so be aware that requesting a .jsp
page returns the JSP source code. Use a RequestDispatcher
instead to include results of an execution. 资源内容会直接返回,因此要意识到请求一个.jsp页面会返回JSP源代码。要包含执行的结果时,使用RequestDispatcher代替。
This method has a different purpose than java.lang.Class.getResource
, which looks up resources based on a class loader. This method does not use class loaders. 该方法和java.lang.Class.getResource目的有一点不同,它基于类加载器 查找资源。本方法不使用类加载器。
path
- a
String
specifying the path to the resource String,指定资源路径
null
if there is no resource at that path 位于指定路径的资源,如果该路径没有资源,返回null
MalformedURLException
- if the pathname is not given in the correct form 如果给出的路径名格式不正确抛出
public InputStream getResourceAsStream(Stringpath)
InputStream
object. 以InputStream对象形式返回位于指定路径的资源。
The data in the InputStream
can be of any type or length. The path must be specified according to the rules given in getResource
. This method returns null
if no resource exists at the specified path. InputStream中的数据类型和长度任意。路径必须根据getResource给定的规则指定。 如果指定路径不存在资源,方法返回null。
Meta-information such as content length and content type that is available via getResource
method is lost when using this method. 通过getResource方法获得的元信息,比如内容长度和内容类型,会在使用该方法时丢失。
The servlet container must implement the URL handlers and URLConnection
objects necessary to access the resource. servlet容器必须实现URL处理器和URLConnection对象,这些对于访问资源是必需的。
This method is different from java.lang.Class.getResourceAsStream
, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. 该方法和java.lang.Class.getResourceAsStream有一点不同,它使用类加载器。 本方法允许servlet容器使得对于任何位置的servlet资源可用,而不使用类加载器。
path
- a
String
specifying the path to the resource String,指定资源路径
InputStream
returned to the servlet, or
null
if no resource exists at the specified path 返回给servlet的InputStream,如果指定路径不存在资源,返回null
public RequestDispatcher getRequestDispatcher(Stringpath)
RequestDispatcher
object that acts as a wrapper for the resource located at the given path. A
RequestDispatcher
object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static. 返回一个RequestDispatcher对象,作为位于给定路径的资源包装器。RequestDispatcher 对象可以用来转发对资源的请求或者包含响应中的资源。资源可以是动态或静态。
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext
to obtain a RequestDispatcher
for resources in foreign contexts. This method returns null
if the ServletContext
cannot return a RequestDispatcher
. 路径必须以“/”开头,解释为相对于当前上下文根路径。使用getContext来获得外部上下文 资源的RequestDispatcher。如果ServletContext不能返回一个RequestDispatcher,方法返回null。
path
- a
String
specifying the pathname to the resource String,指定资源的路径名
RequestDispatcher
object that acts as a wrapper for the resource at the specified path, or
null
if the
ServletContext
cannot return a
RequestDispatcher
RequestDispatcher对象,作为指定路径资源的包装器,如果ServletContext 不能返回一个RequestDispatcher,返回null
RequestDispatcher
,
getContext(java.lang.String)
public RequestDispatcher getNamedDispatcher(Stringname)
RequestDispatcher
object that acts as a wrapper for the named servlet. 返回一个RequestDispatcher对象,作为给定名称的servlet的包装器。
Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor. A servlet instance can determine its name using ServletConfig.getServletName()
. Servlet或者JSP页面可以通过server管理器或web应用部署描述符指定名称。 servlet实例可以使用ServletConfig.getServletName()来确定它的名称。
This method returns null
if the ServletContext
cannot return a RequestDispatcher
for any reason. 如果ServletContext因为某个原因不能返回一个RequestDispatcher,该方法返回null。
name
- a
String
specifying the name of a servlet to wrap String,指定要包装的servlet名称
RequestDispatcher
object that acts as a wrapper for the named servlet, or
null
if the
ServletContext
cannot return a
RequestDispatcher
RequestDispatcher对象,作为指定servlet的包装器,如果ServletContext 不能返回一个RequestDispatcher,返回null
RequestDispatcher
,
getContext(java.lang.String)
,
ServletConfig.getServletName()
public Servlet getServlet(Stringname) throws ServletException
This method was originally defined to retrieve a servlet from a ServletContext
. In this version, this method always returns null
and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API. 该方法原本定义为从ServletContext获取servlet。在目前版本中,该方法始终返回null,保留下来仅出于兼容性考虑。在Java Servlet API的以后版本中将会被永久删除。
In lieu of this method, servlets can share information using the ServletContext
class and can perform shared business logic by invoking methods on common non-servlet classes. 代之以,servlet使用ServletContext类共享信息,普通的非servlet类可以调用方法执行共享的业务逻辑。
ServletException
public Enumeration getServlets()
This method was originally defined to return an Enumeration
of all the servlets known to this servlet context. In this version, this method always returns an empty enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API. 该方法原本定义为返回servlet上下文可知的所有servlet的枚举。在目前版本中, 该方法始终返回一个空枚举,保留该方法仅出于兼容性考虑。在Java Servlet API的以后版本中将会被永久删除。
public Enumeration getServletNames()
This method was originally defined to return an Enumeration
of all the servlet names known to this context. In this version, this method always returns an empty Enumeration
and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API. 该方法原本定义为返回该上下文可知的所有servlet名称的枚举。在目前版本中, 该方法始终返回一个空枚举,保留该方法仅出于兼容性考虑。在Java Servlet API的以后版本中将会被永久删除。
public void log(Stringmsg)
msg
- a
String
specifying the message to be written to the log file String,指定向日志文件输出的消息
public void log(Exceptionexception, Stringmsg)
log(String message, Throwable throwable)
instead. 从Java Servlet API 2.1以来,使用log(String message, Throwable throwable)代替。 This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file. 该方法原本定义为向servlet日志文件写入异常堆栈跟踪和错误说明信息。
public void log(Stringmessage, Throwablethrowable)
Throwable
exception to the servlet log file. The name and type of the servlet log file is specific to the servlet container, usually an event log. 向servlet日志文件写入错误说明信息和给定Throwable异常堆栈跟踪。 servlet日志文件的名称和类型向servlet容器指定,通常为事件日志。
message
- a
String
that describes the error or exception String,描述错误或异常
throwable
- the
Throwable
error or exception Throwable错误或异常
public String getRealPath(Stringpath)
String
containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext. 由给出的虚拟路径返回真实路径的String。比如,路径“/index.html”返回“http://host/contextPath/index.html” 请求服务的server的文件系统的绝对文件路径,这里contextPath是ServletContext的上下文路径。
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null
if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war
archive). 真实路径将以合适的格式返回给运行servlet容器的计算机和操作系统。 如果servlet容器因为某种原因(比如内容正在被.war文件使用)不能将虚拟路径转换成真实路径,方法返回null。
path
- a
String
specifying a virtual path String,指定虚拟路径
String
specifying the real path, or null if the translation cannot be performed String,表示真实路径,如果转换不能执行,返回null
public String getServerInfo()
The form of the returned string is servername/versionnumber. For example, the JavaServer Web Development Kit may return the string JavaServer Web Dev Kit/1.0
. 返回字符串的格式为“服务器名/版本号”。比如,JavaServer Web开发套件可能返回字符串JavaServer Web Dev Kit/1.0。
The servlet container may return other optional information after the primary string in parentheses, for example, JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)
. servlet容器可能会在基本的字符串之后以括号形式返回其他的可选信息,比如,JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)。
String
containing at least the servlet container name and version number String,至少包括servlet容器名和版本号
public String getInitParameter(Stringname)
String
containing the value of the named context-wide initialization parameter, or
null
if the parameter does not exist. 返回等于指定名称的context初始化参数值的String,如果该参数不存在,返回null。
This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster's email address or the name of a system that holds critical data. 该方法使得配置信息对于整个web应用可用。比如,它可以提供一个web管理员的 email地址或者持有关键数据的系统名称。
name
- a
String
containing the name of the parameter whose value is requested String,表示请求值的参数
String
containing at least the servlet container name and version number String,至少包括servlet容器名和版本号
ServletConfig.getInitParameter(java.lang.String)
public Enumeration getInitParameterNames()
Enumeration
of
String
objects, or an empty
Enumeration
if the context has no initialization parameters. 以String对象枚举的形式返回context初始化参数的名称,如果context没有初始化参数,返回一个空枚举。
Enumeration
of
String
objects containing the names of the context's initialization parameters String对象的枚举,表示上下文初始化参数名
ServletConfig.getInitParameter(java.lang.String)
public Object getAttribute(Stringname)
null
if there is no attribute by that name. An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. A list of supported attributes can be retrieved using
getAttributeNames
. 返回给定名称的的servlet容器属性,如果没有该名称的属性,返回null。 属性使得servlet容器可以提供给servlet该接口还没有提供的额外信息。关于属性的信息查看server文档。使用getAttributeNames方法可以获得支持的属性列表。
The attribute is returned as a java.lang.Object
or some subclass. Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*
, javax.*
, and sun.*
. 属性以java.lang.Object或某些子类形式返回。属性命名应该遵循包命名的相同约定。Java Servlet API规范保留匹配java.*, javax.*和sun.*的名称。
name
- a
String
specifying the name of the attribute String,指定属性名
Object
containing the value of the attribute, or
null
if no attribute exists matching the given name Object,表示属性值,如果存在的属性不匹配给定的名称,返回null
getAttributeNames()
public Enumeration getAttributeNames()
Enumeration
containing the attribute names available within this servlet context. Use the
getAttribute(java.lang.String)
method with an attribute name to get the value of an attribute. 返回servlet上下文中可用的属性枚举。使用带属性名称的getAttribute(java.lang.String)方法可以获得属性值。
Enumeration
of attribute names 属性名的枚举
getAttribute(java.lang.String)
public void setAttribute(Stringname, Objectobject)
If listeners are configured on the ServletContext
the container notifies them accordingly. 如果ServletContext配置有监听器,容器将因此对其通知。
If a null value is passed, the effect is the same as calling removeAttribute()
. 如果传递的值为null,和调用removeAttribute()效果相同。
Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*
, javax.*
, and sun.*
. 属性命名应该遵循包命名的相同约定。Java Servlet API规范