A servlet context is an interface provided by the servlet engine to serve Web applications. The Servlet context has a unique directory mapped to the file system by name (which belongs to the Web application name.
A servlet can reference the servlet context through the getServletContext () method of the ServletConfig object. If the servlet directly or indirectly calls the subclass GenericServlet, The getServletContext () method can be used.
In a Web application, servlet can be obtained using the servlet context:
1. Save and retrieve attributes during the call, and share these attributes with other servlets.
2. Read the file content and other static Resources in the Web application.
3. Sending Requests to each other.
4. Record errors and information messages.
Methods In the ServletContext Interface
Object getAttribute (String name) is returned to an Object with a specified name in the servlet context, or an Object is bound with a specified name. From the standard perspective of Web applications, such objects are global objects because they can be accessed by the same servlet at another time. Or any other servlet access in the context.
Void setAttribute (String name, Object obj) sets the Object with the specified name in the servlet context.
Enumeration getAttributeNames () returns an Enumeration of all attribute names stored in the servlet context.
ServletContext getContext (String uripath) returns the servlet context mapped to another URL. In the same server, the URL must start.
String getInitParameter (String name) returns the initialization parameter value of the specified context range. This method is different from the name of the ServletConfig method. The latter is only applicable to the encoded specified servlet. This method is applied to all parameters in the context.
Enumeration getInitParameterNames () returns (may be blank) the enumerated value of the name of the initialization parameter value for the specified context range.
Int getMajorVersion () returns the maximum and minimum versions supported by servlet APIs in this context.
Int getMinorVersion ()
String getMimeType (String fileName) returns the MIME type of the specified file name. Typically, it is based on the file extension rather than the content of the file itself (it does not have to exist ). If the MIME type is unknown, null is returned.
RequestDispatcher getNameDispatcher (String name) returns the RequestDispatcher of the servlet or JSP with the specified name or path. If RequestDispatch cannot be created, null is returned. If a path is specified, it must start with "/" and be relative to the top of the servlet context.
RequestDispatcher getNameDispatcher (String path)
String getRealPath (String path) specifies a URI and returns the absolute path of the URI in the file system. If the ing is not allowed, null is returned.
URL getResource (String path) returns the URL corresponding to the specified absolute path of the servlet context or the input stream that reads the URL. If the resource does not exist, null is returned.
InputStream getResourceAsStream (String path)
String getServerInfo () returns the name and version number of the servlet engine.
Void log (String message)
Void log (String message, Throwable t) writes a message to the servlet registration. If the Throwable parameter is provided, the stack track is included.
Void removeAttribute (String name) deletes the specified attribute from the servlet context.
GetServletContext () and getServletConfig ()
GetServletConfig () during servlet initialization, the container passes in a ServletConfig object and stores it in the servlet instance. This object allows access to two items: the initialization parameter and the ServletContext object, the former is usually specified by the container in the file, allowing the sevrlet to pass the relevant scheduling information at runtime, such as getServletConfig (). the latter of getInitParameter (& quot; debug & quot;) provides the servlet with container information.
GetServletContext () and getServletConfig ()
GetServletContext () and getServletConfig () mean. getServletContext () a servlet can use the getServletContext () method to obtain the servletContext of the web application, and use the getServletContext method to obtain some values, such as getServletContext (). getRealPath (& quot;/& quot;) to obtain the absolute System Path getServletContext (). getResource (& quot; WEB-INF/config. xml & quot;) to obtain the content of the xml file.