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.DynamicaddFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)Add filter to context.FilterRegistration.DynamicaddFilter(java.lang.String filterName, Filter filter)Add filter to context.FilterRegistration.DynamicaddFilter(java.lang.String filterName, java.lang.String className)Add filter to context.voidaddListener(java.lang.Class<? extends java.util.EventListener> listenerClass)TODO SERVLET3 - Add commentsvoidaddListener(java.lang.String className)TODO SERVLET3 - Add comments<T extends java.util.EventListener>
voidaddListener(T t)TODO SERVLET3 - Add commentsServletRegistration.DynamicaddServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> servletClass)Add servlet to context.ServletRegistration.DynamicaddServlet(java.lang.String servletName, Servlet servlet)Register a servlet instance for use in this ServletContext.ServletRegistration.DynamicaddServlet(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 commentsvoiddeclareRoles(java.lang.String... roleNames)Add to the declared roles for this ServletContext.java.lang.ObjectgetAttribute(java.lang.String name)Returns the servlet container attribute with the given name, ornullif there is no attribute by that name.java.util.Enumeration<java.lang.String>getAttributeNames()Returns anEnumerationcontaining the attribute names available within this servlet context.java.lang.ClassLoadergetClassLoader()Get the web application class loader associated with this ServletContext.ServletContextgetContext(java.lang.String uripath)Returns aServletContextobject that corresponds to a specified URL on the server.java.lang.StringgetContextPath()Return the main path associated with this context.java.util.Set<SessionTrackingMode>getDefaultSessionTrackingModes()Obtains the default session tracking modes for this web application.intgetEffectiveMajorVersion()intgetEffectiveMinorVersion()java.util.Set<SessionTrackingMode>getEffectiveSessionTrackingModes()Obtains the currently enabled session tracking modes for this web application.FilterRegistrationgetFilterRegistration(java.lang.String filterName)TODO SERVLET3 - Add commentsjava.util.Map<java.lang.String,? extends FilterRegistration>getFilterRegistrations()java.lang.StringgetInitParameter(java.lang.String name)Returns aStringcontaining the value of the named context-wide initialization parameter, ornullif the parameter does not exist.java.util.Enumeration<java.lang.String>getInitParameterNames()Returns the names of the context's initialization parameters as anEnumerationofStringobjects, or an emptyEnumerationif the context has no initialization parameters.JspConfigDescriptorgetJspConfigDescriptor()intgetMajorVersion()Returns the major version of the Java Servlet API that this servlet container supports.java.lang.StringgetMimeType(java.lang.String file)Returns the MIME type of the specified file, ornullif the MIME type is not known.intgetMinorVersion()Returns the minor version of the Servlet API that this servlet container supports.RequestDispatchergetNamedDispatcher(java.lang.String name)Returns aRequestDispatcherobject that acts as a wrapper for the named servlet.java.lang.StringgetRealPath(java.lang.String path)Returns aStringcontaining the real path for a given virtual path.RequestDispatchergetRequestDispatcher(java.lang.String path)Returns aRequestDispatcherobject that acts as a wrapper for the resource located at the given path.java.net.URLgetResource(java.lang.String path)Returns a URL to the resource that is mapped to a specified path.java.io.InputStreamgetResourceAsStream(java.lang.String path)Returns the resource located at the named path as anInputStreamobject.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.StringgetServerInfo()Returns the name and version of the servlet container on which the servlet is running.ServletgetServlet(java.lang.String name)Deprecated.As of Java Servlet API 2.1, with no direct replacement.java.lang.StringgetServletContextName()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.ServletRegistrationgetServletRegistration(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.SessionCookieConfiggetSessionCookieConfig()java.lang.StringgetVirtualServerName()Get the primary name of the virtual host on which this context is deployed.voidlog(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.
voidlog(java.lang.String msg)Writes the specified message to a servlet log file, usually an event log.voidlog(java.lang.String message, java.lang.Throwable throwable)Writes an explanatory message and a stack trace for a givenThrowableexception to the servlet log file.voidremoveAttribute(java.lang.String name)Removes the attribute with the given name from the servlet context.voidsetAttribute(java.lang.String name, java.lang.Object object)Binds an object to a given attribute name in this servlet context.booleansetInitParameter(java.lang.String name, java.lang.String value)Set the given initialisation parameter to the given value.voidsetSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes)Configures the available session tracking modes for this web application.
-
本文详细介绍了Servlet Context接口的功能和使用方法,包括如何通过Servlet Config获取ServletContext对象、如何利用它获取初始化参数、文件路径等信息,以及提供了多种管理和操作Web应用程序资源的方法。
9657

被折叠的 条评论
为什么被折叠?



