|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.servlet Interface ServletContext
public interface ServletContext
定义一组方法,servlet 使用这些方法与其 servlet 容器进行通信,例如,获取文件的 MIME 类型、分发请求或写入日志文件。
每个 Java 虚拟机的每个“Web 应用程序”都有一个上下文。(“Web 应用程序”是 servlet 和内容的 Collection,这些 servlet 和内容安装在服务器的 URL 名称空间(比如 /catalog
)的特定子集下,并且可能通过 .war
文件安装。)
如果 Web 应用程序在其部署描述符中标记为 "distributed",那么每个虚拟机都将有一个上下文实例。在这种情况下,不能将上下文用作共享全局信息的位置(因为该信息不会是真正全局共享的)。请使用外部资源(如数据库)替代。
ServletContext
对象包含在 ServletConfig
对象中,ServletConfig
对象在初始化 servlet 时由 Web 服务器提供给 servlet。
See also | getServletConfig, getServletContext |
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.
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.)
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.
The ServletContext
object is contained within
the ServletConfig
object, which the Web server provides the
servlet when the servlet is initialized.
- Author:
- Various
- See Also:
Servlet.getServletConfig()
,ServletConfig.getServletContext()
Method Summary | |
---|---|
Object |
getAttribute(String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name. |
Enumeration |
getAttributeNames()
Returns an Enumeration containing the
attribute names available
within this servlet context. |
ServletContext |
getContext(String uripath)
Returns a ServletContext object that
corresponds to a specified URL on the server. |
String |
getContextPath()
Returns the context path of the web application. |
String |
getInitParameter(String name)
Returns a String containing the value of the named
context-wide initialization parameter, or null if the
parameter does not exist. |
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. |
int |
getMajorVersion()
Returns the major version of the Java Servlet API that this servlet container supports. |
String |
getMimeType(String file)
Returns the MIME type of the specified file, or null if
the MIME type is not known. |
int |
getMinorVersion()
Returns the minor version of the Servlet API that this servlet container supports. |
RequestDispatcher |
getNamedDispatcher(String name)
Returns a RequestDispatcher object that acts
as a wrapper for the named servlet. |
String |
getRealPath(String path)
Returns a String containing the real path
for a given virtual path. |
RequestDispatcher |
getRequestDispatcher(String path)
Returns a RequestDispatcher object that acts
as a wrapper for the resource located at the given path. |
URL |
getResource(String path)
Returns a URL to the resource that is mapped to a specified path. |
InputStream |
getResourceAsStream(String path)
Returns the resource located at the named path as an InputStream object. |
Set |
getResourcePaths(String path)
Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. |
String |
getServerInfo()
Returns the name and version of the servlet container on which the servlet is running. |
Servlet |
getServlet(String name)
Deprecated. As of Java Servlet API 2.1, with no direct replacement. 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 corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element. |
Enumeration |
getServletNames()
Deprecated. As of Java Servlet API 2.1, with no replacement. This method was originally defined to return an
|
Enumeration |
getServlets()
Deprecated. As of Java Servlet API 2.0, with no replacement. This method was originally defined to return an |
void |
log(Exception exception,
String msg)
Deprecated. As of Java Servlet API 2.1, use log(String message, Throwable throwable)
instead.
This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file. |
void |
log(String msg)
Writes the specified message to a servlet log file, usually an event log. |
void |
log(String message,
Throwable throwable)
Writes an explanatory message and a stack trace for a given Throwable exception
to the servlet log file. |
void |
removeAttribute(String name)
Removes the attribute with the given name from the servlet context. |
void |
setAttribute(String name,
Object object)
Binds an object to a given attribute name in this servlet context. |
Method Detail |
---|
public String
getContextPath()
返回 Web 应用程序的上下文路径。
上下文路径是用来选择请求上下文的请求 URI 的一部分。请求 URI 中首先出现的总是上下文路径。路径以 "/" 字符开头但不以 "/" 字符结束。对于默认(根)上下文中的 servlet,此方法返回 ""。
servlet 容器可能通过多个上下文路径匹配一个上下文。在这种情况下,getContextPath()
将返回该请求使用的实际上下文路径,它可能不同于此方法返回的路径。此方法返回的上下文路径应该被认为是应用程序的主要或首选上下文路径。
return | Web 应用程序的上下文路径;对于默认(根)上下文,返回 "" | |
since |
| |
See also | getContextPath() |
getContextPath
String getContextPath()
- Returns the context path of the web application.
The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".
It is possible that a servlet container may match a context by more than one context path. In such cases the
HttpServletRequest.getContextPath()
will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application. - Returns:
- The context path of the web application, or "" for the default (root) context
- Since:
- Servlet 2.5
- See Also:
HttpServletRequest.getContextPath()
public ServletContext
getContext(String uripath)
返回与服务器上的指定 URL 相对应的 ServletContext
对象。
此方法允许 servlet 获得对服务器各个部分的上下文的访问,并根据需要从上下文中获得 RequestDispatcher
对象。给定路径必须以 "/" 开头,相对于服务器的文档根进行解释,并与宿主在此容器上的其他 Web 应用程序的上下文根匹配。
在安全意识较强的环境中,servlet 容器可能对给定 URL 返回 null
。
uripath |
指定容器中另一个 Web 应用程序的上下文路径的 String 。 |
return |
与指定 URL 相对应的 ServletContext 对象;如果该对象不存在,或者容器希望限制此访问,则返回 null。 |
See also | javax.servlet.RequestDispatcher |
getContext
ServletContext getContext(String uripath)
- Returns a
ServletContext
object that corresponds to a specified URL on the server.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.In a security conscious environment, the servlet container may return
null
for a given URL. - Parameters:
uripath
- aString
specifying the context path of another web application in the container.- Returns:
- the
ServletContext
object that corresponds to the named URL, or null if either none exists or the container wishes to restrict this access. - See Also:
RequestDispatcher
public int
getMajorVersion()
返回此 servlet 容器支持的 Java Servlet API 的主版本。所有遵守 Version 2.5 的实现必须让此方法返回整数 2。
return | 2 |
getMajorVersion
int getMajorVersion()
- Returns the major version of the Java Servlet API that this
servlet container supports. All implementations that comply
with Version 2.5 must have this method
return the integer 2.
- Returns:
- 2
public int
getMinorVersion()
返回此 servlet 容器支持的 Servlet API 的次版本。所有遵守 Version 2.5 的实现必须让此方法返回整数 5。
return | 5 |
getMinorVersion
int getMinorVersion()
- Returns the minor version of the Servlet API that this
servlet container supports. All implementations that comply
with Version 2.5 must have this method
return the integer 5.
- Returns:
- 5
public String
getMimeType(String file)
返回指定文件的 MIME 类型,如果 MIME 类型未知,则返回 null
。MIME 类型由 servlet 容器的配置确定,可以在 Web 应用程序部署描述符中指定。常见 MIME 类型是 "text/html"
和 "image/gif"
。
file |
指定文件名称的 String |
return |
指定文件的 MIME 类型的 String 。 |
getMimeType
String getMimeType(String file)
- Returns the MIME type of the specified file, or
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"
. - Parameters:
file
- aString
specifying the name of a file- Returns:
- a
String
specifying the file's MIME type
public java.util.Set<E>
getResourcePaths(String path)
返回指向 Web 应用程序中资源的所有路径的类似目录的清单,这些路径中最长的子路径与提供的 path 参数匹配。指示以 '/' 结尾的子目录路径的路径。返回的路径都是相对于 Web 应用程序根目录的并有一个前导 '/'。例如,对于包含以下内容的 Web 应用程序
/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderServlet.class,
getResourcePaths("/") 返回 {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/") 返回 {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}。
path | 用于匹配资源的部分路径,必须以 / 开头 | |
return | 包含目录清单的集合;如果 Web 应用程序中没有资源并且其路径以提供的路径开头,则返回 null。 | |
since |
|
getResourcePaths
Set getResourcePaths(String path)
- Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path
matches the supplied path argument. Paths indicating subdirectory paths end with a '/'. The returned paths are all
relative to the root of the web application and have a leading '/'. For example, for a web application
containing
/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderServlet.class,
getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.
- Parameters:
path
- the partial path used to match the resources, which must start with a /- Returns:
- a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.
- Since:
- Servlet 2.3
public java.net.URL
getResource(String path) throws java.net.MalformedURLException
返回指向映射到指定路径的资源的 URL。该路径必须以 "/" 开头,并相对于当前上下文根进行解释。
此方法允许 servlet 容器使某一资源可用于来自任意源的 servlet。资源可位于本地或远程文件系统上、数据库中或 .war
文件中。
servlet 容器必须实现访问资源所需的 URL 处理程序和 URLConnection
对象。
如果没有资源映射到路径名,则此方法返回 null
。
某些容器可能允许使用 URL 类的方法写入此方法返回的 URL。
资源内容是直接返回的,所以应该知道请求一个 .jsp
页面将返回 JSP 源代码。请改用 RequestDispatcher
以包含执行的结果。
此方法有一个与 java.lang.Class.getResource
不同的用途,后者基于类加载器查找资源。此方法不使用类加载器。
path |
指定指向资源的路径的 String |
return |
位于指定路径上的资源,如果该路径上没有资源,则返回 null |
Throws | java.net.MalformedURLException: 如果没有以正确格式给出路径名 |
getResource
URL getResource(String path) throws MalformedURLException
- Returns a URL to the resource that is mapped to a specified
path. The path must begin with a "/" and is interpreted
as relative to the current context root.
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.The servlet container must implement the URL handlers and
URLConnection
objects that are necessary to access the resource.This method returns
null
if no resource is mapped to the pathname.Some containers may allow writing to the URL returned by this method using the methods of the URL class.
The resource content is returned directly, so be aware that requesting a
.jsp
page returns the JSP source code. Use aRequestDispatcher
instead to include results of an execution.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. - Parameters:
path
- aString
specifying the path to the resource- Returns:
- the resource located at the named path,
or
null
if there is no resource at that path - Throws:
MalformedURLException
- if the pathname is not given in the correct form
public java.io.InputStream
getResourceAsStream(String path)
以 InputStream
对象的形式返回位于指定路径上的资源。
InputStream
中的数据可以是任意类型或长度。该路径必须根据 getResource
中给出的规则指定。如果指定路径上没有资源,则此方法返回 null
。
使用此方法时,可通过 getResource
方法获得的元信息(比如内容长度和内容类型)将丢失。
servlet 容器必须实现访问资源所需的 URL 处理程序和 URLConnection
对象。
此方法不同于 java.lang.Class.getResourceAsStream
,后者使用类加载器。此方法允许 servlet 容器使某一资源可用于来自任意位置的 servlet,而不必使用类加载器。
path |
指定指向资源的路径的 String |
return |
返回给 servlet 的 InputStream ,如果指定路径上没有资源,则返回 null 。 |
getResourceAsStream
InputStream getResourceAsStream(String path)
- Returns the resource located at the named path as
an
InputStream
object.The data in the
InputStream
can be of any type or length. The path must be specified according to the rules given ingetResource
. This method returnsnull
if no resource exists at the specified path.Meta-information such as content length and content type that is available via
getResource
method is lost when using this method.The servlet container must implement the URL handlers and
URLConnection
objects necessary to access the resource.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. - Parameters:
path
- aString
specifying the path to the resource- Returns:
- the
InputStream
returned to the servlet, ornull
if no resource exists at the specified path
public RequestDispatcher
getRequestDispatcher(String path)
返回一个 RequestDispatcher
对象,它充当位于给定路径上的资源的包装器。可以使用 RequestDispatcher
对象将请求转发到资源,或者在响应中包含资源。资源可以是动态的,也可以是静态的。
该路径名必须以 "/" 开头,并相对于当前上下文根进行解释。使用 getContext
获得用于外部上下文中资源的 RequestDispatcher
。如果 ServletContext
无法返回 RequestDispatcher
,则此方法返回 null
。
path |
指定指向资源的路径名的 String |
return |
充当指定路径上资源的包装器的 RequestDispatcher 对象,如果 ServletContext 无法返回 RequestDispatcher ,则返回 null 。 |
See also | javax.servlet.RequestDispatcher, getContext |
getRequestDispatcher
RequestDispatcher getRequestDispatcher(String path)
- Returns a
RequestDispatcher
object that acts as a wrapper for the resource located at the given path. ARequestDispatcher
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.The pathname must begin with a "/" and is interpreted as relative to the current context root. Use
getContext
to obtain aRequestDispatcher
for resources in foreign contexts. This method returnsnull
if theServletContext
cannot return aRequestDispatcher
. - Parameters:
path
- aString
specifying the pathname to the resource- Returns:
- a
RequestDispatcher
object that acts as a wrapper for the resource at the specified path, ornull
if theServletContext
cannot return aRequestDispatcher
- See Also:
RequestDispatcher
,getContext(java.lang.String)
public RequestDispatcher
getNamedDispatcher(String name)
返回充当指定 servlet 的包装器的 RequestDispatcher
对象。
servlet(以及 JSP 页面)可通过服务器管理或通过 Web 应用程序部署描述符给定名称。servlet 实例可使用 ServletConfig#getServletName
确定其名称。
如果 ServletContext
由于某种原因无法返回 RequestDispatcher
,则此方法返回 null
。
name |
指定要包装的 servlet 的名称的 String |
return |
充当指定 servlet 的包装器的 RequestDispatcher 对象,如果 ServletContext 无法返回 RequestDispatcher ,则返回 null 。 |
See also | javax.servlet.RequestDispatcher, getContext, getServletName |
getNamedDispatcher
RequestDispatcher getNamedDispatcher(String name)
- Returns a
RequestDispatcher
object that acts as a wrapper for the named 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()
.This method returns
null
if theServletContext
cannot return aRequestDispatcher
for any reason. - Parameters:
name
- aString
specifying the name of a servlet to wrap- Returns:
- a
RequestDispatcher
object that acts as a wrapper for the named servlet, ornull
if theServletContext
cannot return aRequestDispatcher
- See Also:
RequestDispatcher
,getContext(java.lang.String)
,ServletConfig.getServletName()
public Servlet
getServlet(String name) throws ServletException
deprecated |
从 Java Servlet API 2.1 起,不再直接替换。
定义此方法最初是为了从 作为此方法的替代,servlet 可以使用 |
getServlet
Servlet getServlet(String name) throws ServletException
- Deprecated. As of Java Servlet API 2.1, with no direct replacement.
This method was originally defined to retrieve a servlet from a
ServletContext
. In this version, this method always returnsnull
and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the 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. - Throws:
ServletException
public java.util.Enumeration<E>
getServlets()
deprecated |
从 Java Servlet API 2.0 起,不再提供替代方法。
定义此方法最初是为了返回此 servlet 上下文已知的所有 servlet 的 |
getServlets
Enumeration getServlets()
- Deprecated. As of Java Servlet API 2.0, with no replacement.
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.
public java.util.Enumeration<E>
getServletNames()
deprecated |
从 Java Servlet API 2.1 起,不再提供替代方法。
定义此方法最初是为了返回此上下文已知的所有 servlet 名称的 |
getServletNames
Enumeration getServletNames()
- Deprecated. As of Java Servlet API 2.1, with no replacement.
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 emptyEnumeration
and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
public void
log(String msg)
将指定消息写入 servlet 日志文件(通常是一个事件日志)。servlet 日志文件的名称和类型是特定于 servlet 容器的。
msg |
指定要写入日志文件的消息的 String |
log
void log(String msg)
- Writes the specified message to a servlet log file, usually
an event log. The name and type of the servlet log file is
specific to the servlet container.
- Parameters:
msg
- aString
specifying the message to be written to the log file
public void
log(Exception exception, String msg)
deprecated |
从 Java Servlet API 2.1 起,请改用 #log(String message, Throwable throwable) 。
定义此方法最初是为了将异常的堆栈跟踪和一条解释性错误消息写入 servlet 日志文件。 |
log
void log(Exception exception, String msg)
- Deprecated. As of Java Servlet API 2.1, use
log(String message, Throwable throwable)
instead.This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file.
public void
log(String message, Throwable throwable)
将给定 Throwable
异常的解释性消息和堆栈跟踪写入 servlet 日志文件。servlet 日志文件的名称和类型是特定于 servlet 容器的(通常是一个事件日志)。
message |
描述错误或异常的 String |
throwable |
Throwable 错误或异常 |
log
void log(String message, Throwable throwable)
- Writes an explanatory message and a stack trace
for a given
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. - Parameters:
message
- aString
that describes the error or exceptionthrowable
- theThrowable
error or exception
public String
getRealPath(String path)
为给定虚拟路径返回包含实际路径的 String
。例如,可以通过对 "http://host/contextPath/index.html" 的请求使路径 "/index.html" 返回服务器文件系统上的绝对文件路径,其中 contextPath 是此 ServletContext 的上下文路径。
将采用与正在其上运行 servlet 容器的计算机和操作系统相适应的形式返回实际路径(包括采用适当的路径分隔符)。如果 servlet 容器由于某种原因无法将虚拟路径转换为实际路径(例如正从 .war
归档文件中获得内容时),则此方法返回 null
。
path |
指定虚拟路径的 String |
return |
指定实际路径的 String ,如果无法执行转换,则返回 null。 |
getRealPath
String getRealPath(String path)
- Returns a
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..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). - Parameters:
path
- aString
specifying a virtual path- Returns:
- a
String
specifying the real path, or null if the translation cannot be performed
public String
getServerInfo()
返回正在其上运行 servlet 的 servlet 容器的名称和版本。
所返回字符串的形式是 servername/versionnumber。例如,JavaServer Web Development Kit 可能返回字符串 JavaServer Web Dev Kit/1.0
。
servlet 容器可以在主字符串之后在括号中返回其他可选信息,例如 JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)
。
return |
至少包含 servlet 容器名称和版本号的 String 。 |
getServerInfo
String getServerInfo()
- Returns the name and version of the servlet container on which
the servlet is running.
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
.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)
. - Returns:
- a
String
containing at least the servlet container name and version number
public String
getInitParameter(String name)
返回包含指定上下文范围初始化参数值的 String
,如果参数不存在,则返回 null
。
此方法可使可用的配置信息用于整个“Web 应用程序”。例如,它可以提供 webmaster 的电子邮件地址,或者提供包含关键数据的系统的名称。
name |
包含请求其值的参数的名称的 String |
return |
至少包含 servlet 容器名称和版本号的 String 。 |
See also | getInitParameter |
getInitParameter
String getInitParameter(String name)
- Returns a
String
containing the value of the named context-wide initialization parameter, ornull
if the parameter does not exist.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.
- Parameters:
name
- aString
containing the name of the parameter whose value is requested- Returns:
- a
String
containing at least the servlet container name and version number - See Also:
ServletConfig.getInitParameter(java.lang.String)
public java.util.Enumeration<E>
getInitParameterNames()
以 String
对象的 Enumeration
的形式返回上下文初始化参数的名称,如果该上下文没有初始化参数,则返回一个空的 Enumeration
。
return |
包含上下文初始化参数名称的 String 对象的 Enumeration 。 |
See also | getInitParameter |
getInitParameterNames
Enumeration getInitParameterNames()
- Returns the names of the context's initialization parameters as an
Enumeration
ofString
objects, or an emptyEnumeration
if the context has no initialization parameters. - Returns:
- an
Enumeration
ofString
objects containing the names of the context's initialization parameters - See Also:
ServletConfig.getInitParameter(java.lang.String)
public Object
getAttribute(String name)
返回具有给定名称的 servlet 容器属性,如果不具有该名称的属性,则返回 null
。属性允许 servlet 容器将此接口还没有提供的额外信息提供给 servlet。有关其属性的信息,请参见服务器文档。可使用 getAttributeNames
获取支持的属性的列表。
属性是以 java.lang.Object
或某个子类的形式返回的。属性名称应该与包名称遵守相同的约定。Java Servlet API 规范保留匹配 java.*
、javax.*
和 sun.*
的名称。
name |
指定属性名称的 String |
return |
包含属性值的 Object ,如果不存在匹配给定名称的属性,则返回 null |
See also | getAttributeNames |
getAttribute
Object getAttribute(String name)
- Returns the servlet container attribute with the given name,
or
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 usinggetAttributeNames
.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 matchingjava.*
,javax.*
, andsun.*
. - Parameters:
name
- aString
specifying the name of the attribute- Returns:
- an
Object
containing the value of the attribute, ornull
if no attribute exists matching the given name - See Also:
getAttributeNames()
public java.util.Enumeration<E>
getAttributeNames()
返回包含此 servlet 上下文中可用属性的名称的 Enumeration
。使用带有一个属性名称的 #getAttribute
方法获取属性值。
return |
属性名称的 Enumeration |
See also | getAttribute |
getAttributeNames
Enumeration getAttributeNames()
- Returns an
Enumeration
containing the attribute names available within this servlet context. Use thegetAttribute(java.lang.String)
method with an attribute name to get the value of an attribute. - Returns:
- an
Enumeration
of attribute names - See Also:
getAttribute(java.lang.String)
public void
setAttribute(String name, Object object)
将对象绑定到此 servlet 上下文中的给定属性名称。如果已将指定名称用于某个属性,则此方法将使用新属性替换具有该名称的属性。
如果在 ServletContext
上配置了侦听器,则容器将相应地通知它们。
如果传递了 null 值,则效果将与调用 removeAttribute()
相同。
属性名称应该与包名称遵守相同的约定。Java Servlet API 规范保留匹配 java.*
、javax.*
和 sun.*
的名称。
name |
指定属性名称的 String |
object |
表示要绑定的属性的 Object |
setAttribute
void setAttribute(String name, Object object)
- Binds an object to a given attribute name in this servlet context. If
the name specified is already used for an attribute, this
method will replace the attribute with the new to the new attribute.
If listeners are configured on the
ServletContext
the container notifies them accordingly.If a null value is passed, the effect is the same as calling
removeAttribute()
.Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching
java.*
,javax.*
, andsun.*
. - Parameters:
name
- aString
specifying the name of the attributeobject
- anObject
representing the attribute to be bound
public void
removeAttribute(String name)
从 servlet 上下文中移除具有给定名称的属性。完成移除操作后,为获取属性值而对 #getAttribute
进行的后续调用将返回 null
。
如果在 ServletContext
上配置了侦听器,则容器将相应地通知它们。
name |
指定要移除的属性的名称的 String |
removeAttribute
void removeAttribute(String name)
- Removes the attribute with the given name from
the servlet context. After removal, subsequent calls to
getAttribute(java.lang.String)
to retrieve the attribute's value will returnnull
.If listeners are configured on the
ServletContext
the container notifies them accordingly. - Parameters:
name
- aString
specifying the name of the attribute to be removed
public String
getServletContextName()
返回与此 ServletContext 相对应的 Web 应用程序的名称,该名称是通过 display-name 元素在部署描述符中为此 Web 应用程序指定的。
return | Web 应用程序的名称,如果尚未在部署描述符中声明该名称,则返回 null。 | |
since |
|
getServletContextName
String getServletContextName()
- Returns the name of this web application corresponding to this ServletContext as specified in the deployment
descriptor for this web application by the display-name element.
- Returns:
- The name of the web application or null if no name has been declared in the deployment descriptor.
- Since:
- Servlet 2.3
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!