|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface ServletContext
ServletContexts encapsulate applications. Applications are generalized virtual hosts; a URL prefix defines a distinct application. So /myapp and /yourapp could define different applications. As a degenerate case, each virtual host has its own ServletContext.
Each application is entirely distinct. Each has its own:
URIs are relative to the application root (e.g. /myapp) for most ServletContext methods. So you can define user workspaces with identical JSP files and servlets in different applications.
Forwarding and including files, the Servlet equivalent of SSI are handled by the RequestDispatcher methods.
There is no direct equivalent of a global.jsa. To initialize and cleanup shared classes on start and stop, use a load-on-startup servlet. The init() method will be called when the application starts and the destroy() method will be called when the application finishes.
<servlet servlet-name='global'
servlet-class='test.InitServlet'
load-on-startup/>
<web-app id='/myapp' app-dir='/www/myweb'/>
So the application object is best used as a cache rather than as a way for servlets to communicate.
| Method Summary | |
|---|---|
void |
addFilter(java.lang.String filterName,
java.lang.String description,
java.lang.String className,
java.util.Map<java.lang.String,java.lang.String> initParam)
Adds a filter. |
void |
addFilterMapping(java.lang.String filterName,
java.lang.String[] urlPatterns,
java.lang.String[] servletNames,
java.util.EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter)
Adds a filter mapping. |
void |
addServlet(java.lang.String servletName,
java.lang.String description,
java.lang.String className,
java.util.Map<java.lang.String,java.lang.String> initParam,
int loadOnStartup)
Adds a runtime servlet. |
void |
addServletMaping(java.lang.String servletName,
java.lang.String[] urlPatterns)
Adds a runtime servlet mapping |
java.lang.Object |
getAttribute(java.lang.String name)
Returns an attribute value. |
java.util.Enumeration |
getAttributeNames()
Returns an enumeration of all the attribute names. |
ServletContext |
getContext(java.lang.String uri)
Returns the ServletContext for the uri. |
java.lang.String |
getContextPath()
Returns the context-path for the web-application. |
java.lang.String |
getInitParameter(java.lang.String name)
Returns the value of an initialization parameter from the configuration file. |
java.util.Enumeration |
getInitParameterNames()
Returns an enumeration of all init parameter names. |
int |
getMajorVersion()
Returns the major version of the servlet API. |
java.lang.String |
getMimeType(java.lang.String uri)
Returns the mime type for the given uri. |
int |
getMinorVersion()
Returns the minor version of the servlet API. |
RequestDispatcher |
getNamedDispatcher(java.lang.String servletName)
Returns a request dispatcher based on a servlet name. |
java.lang.String |
getRealPath(java.lang.String uri)
Returns the real file path for the given uri. |
RequestDispatcher |
getRequestDispatcher(java.lang.String uri)
Returns a request dispatcher for later inclusion or forwarding. |
java.net.URL |
getResource(java.lang.String uri)
Returns the resource for the given uri. |
java.io.InputStream |
getResourceAsStream(java.lang.String path)
Returns the resource as a stream. |
java.util.Set |
getResourcePaths(java.lang.String prefix)
Returns the set all resources held by the application. |
java.lang.String |
getServerInfo()
Returns a server-specific string identifying the servlet engine. |
Servlet |
getServlet(java.lang.String name)
Deprecated. |
java.lang.String |
getServletContextName()
Returns the URL prefix for the ServletContext. |
java.util.Enumeration |
getServletNames()
Deprecated. |
java.util.Enumeration |
getServlets()
Deprecated. |
SessionCookieConfig |
getSessionCookieConfig()
Sets the session cookie configuration |
java.util.EnumSet<SessionTrackingMode> |
getSessionTrackingModes()
The session tracking mode |
void |
log(java.lang.Exception exception,
java.lang.String msg)
Deprecated. |
void |
log(java.lang.String msg)
Logs a message. |
void |
log(java.lang.String message,
java.lang.Throwable throwable)
Logs a message and a stack trace. |
void |
removeAttribute(java.lang.String name)
Removes an attribute. |
void |
setAttribute(java.lang.String name,
java.lang.Object value)
Sets an attribute value. |
void |
setSessionCookieConfig(SessionCookieConfig cookieConfig)
The session cookie configuration |
void |
setSessionTrackingModes(java.util.EnumSet<SessionTrackingMode> modes)
The session tracking mode |
| Method Detail |
|---|
java.lang.String getServletContextName()
java.lang.String getServerInfo()
int getMajorVersion()
int getMinorVersion()
java.lang.String getInitParameter(java.lang.String name)
<web-app id='/myapp' app-dir='/www/myapp'>
<context-param name1='value1'/>
<context-param name2='value2'/>
</web-app>
name - init parameter name
java.util.Enumeration getInitParameterNames()
ServletContext getContext(java.lang.String uri)
uri - path relative to the root
java.lang.String getContextPath()
java.lang.String getRealPath(java.lang.String uri)
See ServletRequest to return the real path relative to the request uri.
uri - path relative to the application root to be translated.
RequestDispatcher getRequestDispatcher(java.lang.String uri)
The following example includes the result of executing inc.jsp
into the output stream. If the context path is /myapp, the equivalent
uri is /myapp/inc.jsp
RequestDispatcher disp;
disp = getRequestDispatcher("/inc.jsp?a=b");
disp.include(request, response);
See ServletRequest to return a request dispatcher relative to the request uri.
uri - path relative to the app root (including query string)
for the included file.
RequestDispatcher getNamedDispatcher(java.lang.String servletName)
servletName - the servlet name to include or forward to.
java.lang.String getMimeType(java.lang.String uri)
uri - path relative to the application root.java.lang.Object getAttribute(java.lang.String name)
name - of the attribute.
java.util.Enumeration getAttributeNames()
void setAttribute(java.lang.String name,
java.lang.Object value)
A typical initialization of an application attribute will look like:
ServletContext app = getServletContext();
Object value;
synchronized (app) {
value = app.getAttribute("cache");
if (value == null) {
value = new Cache();
app.setAttribute("cache", value);
}
}
name - of the attribute.value - value to storevoid removeAttribute(java.lang.String name)
name - of the attribute.void log(java.lang.String msg)
void log(java.lang.String message,
java.lang.Throwable throwable)
java.net.URL getResource(java.lang.String uri)
throws java.net.MalformedURLException
uri - path relative to the application root.
java.net.MalformedURLExceptionjava.util.Set getResourcePaths(java.lang.String prefix)
java.io.InputStream getResourceAsStream(java.lang.String path)
uri - path relative to the application root.
Servlet getServlet(java.lang.String name)
throws ServletException
ServletExceptionjava.util.Enumeration getServlets()
java.util.Enumeration getServletNames()
void log(java.lang.Exception exception,
java.lang.String msg)
void addServlet(java.lang.String servletName,
java.lang.String description,
java.lang.String className,
java.util.Map<java.lang.String,java.lang.String> initParam,
int loadOnStartup)
void addServletMaping(java.lang.String servletName,
java.lang.String[] urlPatterns)
void addFilter(java.lang.String filterName,
java.lang.String description,
java.lang.String className,
java.util.Map<java.lang.String,java.lang.String> initParam)
void addFilterMapping(java.lang.String filterName,
java.lang.String[] urlPatterns,
java.lang.String[] servletNames,
java.util.EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter)
void setSessionCookieConfig(SessionCookieConfig cookieConfig)
SessionCookieConfig getSessionCookieConfig()
void setSessionTrackingModes(java.util.EnumSet<SessionTrackingMode> modes)
java.util.EnumSet<SessionTrackingMode> getSessionTrackingModes()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||