http://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/ServletContext.html
1.可以由ServletConfig获取
2.该对象代表当前WEB 应用:可以认为SevletContext是当前web应用的大管家,可以获取各个方面的信息
1)获取当前WEB应用的参数
设置初始化参数
<!-- 配置当前web应用的初始化参数>
<context-param>
<param-name>driver</parma-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
ServletContext servletContext == servletConfig.getServletContext();
String driver = servletContext.getInitParameter("driver");
Enumeration<String> names = servletContext.getInitParameterNames()
while(names.hasMoreElements()){
String name = names.nextElement();
System.out.println("name: " + name);
}
2)获取当前WEB应用的某一个文件的绝对路径
getRealPath(String path);
-
-
Method Summary
Modifier and Type Method and Description FilterRegistration.Dynamic
addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
Add filter to context.FilterRegistration.Dynamic
addFilter(java.lang.String filterName, Filter filter)
Add filter to context.FilterRegistration.Dynamic
addFilter(java.lang.String filterName, java.lang.String className)
Add filter to context.void
addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
TODO SERVLET3 - Add commentsvoid
addListener(java.lang.String className)
TODO SERVLET3 - Add comments<T extends java.util.EventListener>
voidaddListener(T t)
TODO SERVLET3 - Add commentsServletRegistration.Dynamic
addServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> servletClass)
Add servlet to context.ServletRegistration.Dynamic
addServlet(java.lang.String servletName, Servlet servlet)
Register a servlet instance for use in this ServletContext.ServletRegistration.Dynamic
addServlet(java.lang.String servletName, java.lang.String className)
Register a servlet implementation for use in this ServletContext.<T extends Filter>
TcreateFilter(java.lang.Class<T> c)
TODO SERVLET3 - Add comments<T extends java.util.EventListener>
TcreateListener(java.lang.Class<T> c)
TODO SERVLET3 - Add comments<T extends Servlet>
TcreateServlet(java.lang.Class<T> c)
TODO SERVLET3 - Add commentsvoid
declareRoles(java.lang.String... roleNames)
Add to the declared roles for this ServletContext.java.lang.Object
getAttribute(java.lang.String name)
Returns the servlet container attribute with the given name, ornull
if there is no attribute by that name.java.util.Enumeration<java.lang.String>
getAttributeNames()
Returns anEnumeration
containing the attribute names available within this servlet context.java.lang.ClassLoader
getClassLoader()
Get the web application class loader associated with this ServletContext.ServletContext
getContext(java.lang.String uripath)
Returns aServletContext
object that corresponds to a specified URL on the server.java.lang.String
getContextPath()
Return the main path associated with this context.java.util.Set<SessionTrackingMode>
getDefaultSessionTrackingModes()
Obtains the default session tracking modes for this web application.int
getEffectiveMajorVersion()
int
getEffectiveMinorVersion()
java.util.Set<SessionTrackingMode>
getEffectiveSessionTrackingModes()
Obtains the currently enabled session tracking modes for this web application.FilterRegistration
getFilterRegistration(java.lang.String filterName)
TODO SERVLET3 - Add commentsjava.util.Map<java.lang.String,? extends FilterRegistration>
getFilterRegistrations()
java.lang.String
getInitParameter(java.lang.String name)
Returns aString
containing the value of the named context-wide initialization parameter, ornull
if the parameter does not exist.java.util.Enumeration<java.lang.String>
getInitParameterNames()
Returns the names of the context's initialization parameters as anEnumeration
ofString
objects, or an emptyEnumeration
if the context has no initialization parameters.JspConfigDescriptor
getJspConfigDescriptor()
int
getMajorVersion()
Returns the major version of the Java Servlet API that this servlet container supports.java.lang.String
getMimeType(java.lang.String file)
Returns the MIME type of the specified file, ornull
if the MIME type is not known.int
getMinorVersion()
Returns the minor version of the Servlet API that this servlet container supports.RequestDispatcher
getNamedDispatcher(java.lang.String name)
Returns aRequestDispatcher
object that acts as a wrapper for the named servlet.java.lang.String
getRealPath(java.lang.String path)
Returns aString
containing the real path for a given virtual path.RequestDispatcher
getRequestDispatcher(java.lang.String path)
Returns aRequestDispatcher
object that acts as a wrapper for the resource located at the given path.java.net.URL
getResource(java.lang.String path)
Returns a URL to the resource that is mapped to a specified path.java.io.InputStream
getResourceAsStream(java.lang.String path)
Returns the resource located at the named path as anInputStream
object.java.util.Set<java.lang.String>
getResourcePaths(java.lang.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.java.lang.String
getServerInfo()
Returns the name and version of the servlet container on which the servlet is running.Servlet
getServlet(java.lang.String name)
Deprecated.As of Java Servlet API 2.1, with no direct replacement.java.lang.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.java.util.Enumeration<java.lang.String>
getServletNames()
Deprecated.As of Java Servlet API 2.1, with no replacement.ServletRegistration
getServletRegistration(java.lang.String servletName)
Obtain the details of the named servlet.java.util.Map<java.lang.String,? extends ServletRegistration>
getServletRegistrations()
TODO SERVLET3 - Add commentsjava.util.Enumeration<Servlet>
getServlets()
Deprecated.As of Java Servlet API 2.0, with no replacement.SessionCookieConfig
getSessionCookieConfig()
java.lang.String
getVirtualServerName()
Get the primary name of the virtual host on which this context is deployed.void
log(java.lang.Exception exception, java.lang.String msg)
Deprecated.As of Java Servlet API 2.1, uselog(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(java.lang.String msg)
Writes the specified message to a servlet log file, usually an event log.void
log(java.lang.String message, java.lang.Throwable throwable)
Writes an explanatory message and a stack trace for a givenThrowable
exception to the servlet log file.void
removeAttribute(java.lang.String name)
Removes the attribute with the given name from the servlet context.void
setAttribute(java.lang.String name, java.lang.Object object)
Binds an object to a given attribute name in this servlet context.boolean
setInitParameter(java.lang.String name, java.lang.String value)
Set the given initialisation parameter to the given value.void
setSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes)
Configures the available session tracking modes for this web application.
-