|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
A class created by the server to give servlets access to certain
environment related objects and methods.
It contains standard information like the names of all the servlets,
two kinds of log methods, the server name, etc.
The server can also store extra information here in the form
of {String, Object} pairs.
Different servlets can live in different ServletContexts,
but a servlet engine can group related Servlets in the same
ServletContext.
Servlet specific information can be transferred to a servlet using
a class implementing the ServletConfig interface.
Method Summary | |
java.lang.Object |
getAttribute(java.lang.String name)
Gets the value of a named attribute |
java.util.Enumeration |
getAttributeNames()
Gets an enumeration containing all the attribute names |
ServletContext |
getContext(java.lang.String UriPath)
Gives the ServletContext of another servlet indicated by
the UriPath on the same server. |
int |
getMajorVersion()
Major version number of the Servlet API the servlet engine supports. |
java.lang.String |
getMimeType(java.lang.String filename)
Gives the mimetype of the requested file |
int |
getMinorVersion()
Minor version number of the Servlet API the servlet engine supports. |
java.lang.String |
getRealPath(java.lang.String virtualPath)
Translates the requested virtual path to the real filesystem path using the servers knowledge of the document root. |
RequestDispatcher |
getRequestDispatcher(java.lang.String UriPath)
Returns a RequestDispatcher to forward requests or
include responses from another (active) resource. |
java.net.URL |
getResource(java.lang.String virtualPath)
Translates the requested virtual path to an URL object that can be accesed by the servlet. |
java.io.InputStream |
getResourceAsStream(java.lang.String virtualPath)
A convenience method for getResource(virtualPath).openStream() . |
java.lang.String |
getServerInfo()
A server supplied string containing the server name, version number, etc |
Servlet |
getServlet(java.lang.String name)
Deprecated. Always returns null. Since the servlet engine cannot know if a servlet ever gives up the reference to another servlet it could never destroy the servlet after this call. Only the servlet engine should have references to Servlets. |
java.util.Enumeration |
getServletNames()
Deprecated. Always returns an empty Enumeration. Only the servlet engine should have references to Servlets. |
java.util.Enumeration |
getServlets()
Deprecated. Always returns an empty Enumeration. Only the servlet engine should have references to Servlets. |
void |
log(java.lang.Exception exception,
java.lang.String message)
Deprecated. Use log(String, Throwable) which is more
general. |
void |
log(java.lang.String message)
Writes a message to the log |
void |
log(java.lang.String message,
java.lang.Throwable t)
Writes an exception + message to the log |
void |
removeAttribute(java.lang.String name)
Removes the named object from the ServletContext |
void |
setAttribute(java.lang.String name,
java.lang.Object o)
Puts a named object into the ServletContext . |
Method Detail |
public java.lang.Object getAttribute(java.lang.String name)
name
- the name of the attributepublic java.util.Enumeration getAttributeNames()
public ServletContext getContext(java.lang.String UriPath)
ServletContext
of another servlet indicated by
the UriPath
on the same server. For security reasons this
can return null even if there is an active servlet at that location.
This can then be used to set attributes in the context of another
servlet.UriPath
- The path to the servlet,
such as /servlet/ShowBook
UriPath
or when it is unavailable
due to security restrictionssetAttribute(java.lang.String, java.lang.Object)
public int getMajorVersion()
public int getMinorVersion()
public java.lang.String getMimeType(java.lang.String filename)
filename
- the filepublic java.lang.String getRealPath(java.lang.String virtualPath)
getResource
and getResourceAsStream
methods to access the original object in a more abstract way not tied to
the filesystem.virtualPath
- the path to be translated
(e.g. /graphics/baby-gnu.png
)getResource(java.lang.String)
,
getResourceAsStream(java.lang.String)
public java.net.URL getResource(java.lang.String virtualPath)
getRealPath
method since it is not tied to the local
filesystem. This means that a servlet can access the resource even when
loaded in a servlet engine on a different machine. The servlet engine
should make sure that the appropriate URLStreamHandlers
and
URLConnection
classes are implemented to acces to resource.
This can also be used to write to a resource if the resource
(URLConnection
) supports it. The following example gives
you an OutputStream
:
URLConnection con = getResource("/logs/mylog.txt").openConnection();
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
Note that a ServerContext
does not have to have access to
the complete servers document space and is allowed to return null even
for valid virtual paths.
Note that according to the 2.1 API documentation this method can throw a MalformedURLException. But according to the official spec it does not throw any exceptions.
virtualPath
- the path to the requested resource
(e.g. /philosophy/words-to-avoid.html
)URL.openConnection()
,
URLConnection.setDoOutput(boolean)
,
URLConnection.getOutputStream()
public java.io.InputStream getResourceAsStream(java.lang.String virtualPath)
getResource(virtualPath).openStream()
.
But the servlet engine is allowed to implement is in a more efficient
way.virtualPath
- the path to the requested resource
(e.g. /philosophy/words-to-avoid.html
)getResource(java.lang.String)
,
URL.openStream()
public RequestDispatcher getRequestDispatcher(java.lang.String UriPath)
RequestDispatcher
to forward requests or
include responses from another (active) resource. Some resources can also
be accessed by the getResource
method.UriPath
- the path to another (active) resource
(e.g. /servlet/OtherServlet
)RequestDispatcher
for the (active) resource
found at UriPath
getResource(java.lang.String)
public java.lang.String getServerInfo()
public void log(java.lang.String message)
message
- the message to writepublic void log(java.lang.String message, java.lang.Throwable t)
t
- the exceptionmessage
- the messagepublic void log(java.lang.Exception exception, java.lang.String message)
log(String, Throwable)
which is more
general.exception
- the exceptionmessage
- the messagelog(java.lang.String, java.lang.Throwable)
public void setAttribute(java.lang.String name, java.lang.Object o)
ServletContext
.
Can be used to communicate with other servlets in this
ServletContext
. The names used must follow the conventions
used for naming java packages.name
- - which is used to refer to this objectobject
- - which should be returned when somebody calls
getAttribute(name)
getAttribute(java.lang.String)
public void removeAttribute(java.lang.String name)
ServletContext
name
- The name which was used to set the object with
setObject
public Servlet getServlet(java.lang.String name) throws ServletException
name
- the name of the wanted servletpublic java.util.Enumeration getServlets()
public java.util.Enumeration getServletNames()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |