首页 > 学院 > 开发设计 > 正文

ServletContext

2019-11-06 07:32:09
字体:
来源:转载
供稿:网友

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

Methods 
Modifier and TypeMethod 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 comments
voidaddListener(java.lang.String className)TODO SERVLET3 - Add comments
<T extends java.util.EventListener> voidaddListener(T t)TODO SERVLET3 - Add comments
ServletRegistration.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 comments
voiddeclareRoles(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, or null if there is no attribute by that name.
java.util.Enumeration<java.lang.String>getAttributeNames()Returns an Enumeration containing 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 a ServletContext object 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 comments
java.util.Map<java.lang.String,? extends FilterRegistration>getFilterRegistrations() 
java.lang.StringgetInitParameter(java.lang.String name)Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
java.util.Enumeration<java.lang.String>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.
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, or null if 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 a RequestDispatcher object that acts as a wrapper for the named servlet.
java.lang.StringgetRealPath(java.lang.String path)Returns a String containing the real path for a given virtual path.
RequestDispatchergetRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object 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 an InputStream 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.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 comments
java.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, 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.

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 given Throwable exception 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.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表