SERVLET API - ServletContext interface brief description

zhaozj2021-02-16  50

A brief description of the servletContext interface: public interface servletContext

A range of methods are defined for communicating with the corresponding servlet container, such as the MIME type, assignment request, or write logs to the log file. Each web-app can only have a servletContext, and the web-app can be a folder placed with a web application file or a .war file. The ServletContext object is included in the ServletConfig object, and the servletconfig object provides a servlet object when servlet initialization.

getContext () Method Overview: Public ServletContext getContext (java.lang.string uripath) Returns a servletContext object that specifies the URL address. The method allows servlets to obtain access to the context of each part of the server, and obtain the RequestDispatcher object from the context as needed. This specified URL path must be "/", interpreted as a relative path under the server document root directory and matches the context root directory of other web-app hosts. In a safe environment, the servlet container returns NULL.

Getmajorversion () Method Overview: Public INT getmajorversion () Returns the version number of the servlet API supported by the servlet container, all implementations must return to the total number 2.

The getminorversion () method is slightly.

GetMimeType () method Overview: Public Java.lang.String getMimetype (java.lang.string file) Returns the file type of the specified file, and returns NULL if the file type is unknown. The file type is determined by the configuration of the servlet container and is specified in a web-app. The file type in general is: "text / html" and "image / gif".

GetResourcePaths () Method Overview: Public Java.util.Set getResourcePaths (java.lang.string path) Returns a set of SETs for all resource paths in the web-app. The path represents a subdirectory at the end of "/" and indicates a relative path to the web-app in "/".

Example: /welcome.html/catalog/index.html/catalog/Products.html/catalog/offers/books.html/catalog/offers/music.html/customer/login.jsp/web-inf/web.xml/web -Inf / classes / com.acme.orderservlet.class,

getResourcePaths ("/") will return set {"/welcome.html", "/ catalog /", "/ customer /", "/ web-inf /"}; getResourcePaths ("/ catalog /") will return set { "/catalog/index.html", "/catalog/products.html", "/ catalog / offers /"}.

Returns NULL if the subdirectory is empty.

GetResource () Method Overview: Public Java.Net.URL getResource (java.lang.string path) THROWS JAVA.NET.MALFORMEDURLEXCEPTION Returns a URL object corresponding to the resource path specified by Path, the PATH must start with "/" and act as The relative position of the current directory. This method allows the servlet container to make one of the resources in Serlets is available. This resource allows you to be a local resource or a remote file system that can be in a database or in one .war file. The servlet container must implement the URL handle and URLCONNECTION objects, which are required for resource access. This method returns NULL if there is no match. Some containers can even write data to the resource returned by the method. This resource can be returned directly, so ask an A.jsp file to return JSP source files. Using a RequestDispatcher object, you can include the result of the execution. This method function is different from java.lang.class.getResource, a method based on Class Loader retrieval resource. But the former does not rely on Class Loaders. GetResourceAsStream () Method Overview: Public Java.io.InputStream GetResourceSstream (Java.lang.String Path) Returns an InputStream that specifies a location resource by String Path. The returned InputStream can be any type and length.

GetRequestDispatcher () Method: Public RequestDispatcher getRequestDispatcher (java.lang.string path) Returns a RequestDispatcher object that plays a role of a given resource packager. A RequestDispatcher object can be used to transmit a request to a specific resource or contain a particular resource to a response. This particular resource can be dynamic or static. The RequestDispatcher object can be obtained for the external Contexts resource through the getContext () method. Returns NULL if servletContext cannot get the RequestDispatcher object.

GetNamedDispatcher () Method Overview: Public RequestDispatcher getNamedDispatcher (java.lang.string name) Returns a RequestDispatcher object for the servlet object for the specified name. The Servlet and JSP pages can be named by server management or Web Application Deployment Descriptor. The name of a servlet instance can be determined by servletconfig.getServletName ().

GetServlet () method, GetServlets () method, GetServletNames () method, is not recommended.

Log (java.lang.string msg) method Overview: Public void log (java.lang.string MSG) Write the specified information into the servlet log file, usually a log log. The name and type of the log file are related to the servlet container. Public void log (java.lang.exception Exception, java.lang.string msg) public void log (java.lang.string message, java.lang.throwable throwable)

GetRealPath () Method Overview: Public Java.lang.String GetRealPath (Java.lang.String Path) Returns a string of the true path (full path) of the specified virtual path. Example: Virtual Path "/Index.html" will return the absolute path "http://host/contextPath/index.html" in the server file system, which is the context path of servletContext. The returned path will apply to the operating system running by the servlet container. If the virtual path is not mapped to a real path, the method will return NULL. (For example, when the content specified by the path is from the .war file) GetServerInfo () method overview: public java.lang.string getServerInfo () Returns the name and version number of the servlet container. The returned string format is ServerName / VersionNumber (server name / version number), for example: The JavaServer Web Development Kit will return "JavaServer Web dev Kit / 1.0". The servlet container will also return other optional information, such as "JavaServer Web dev Kit / 1.0 (JDK 1.1.6; Windows NT 4.0 x86)"

GetInitParameter () method Overview: Public Java.lang.String GetInitParameter (java.lang.string name) Returns the value of the variable defined by the context, if the variable does not exist, return NULL. See servletconfig.getinitparameter (java.lang.string).

GetInitParameterNames () Method Overview: Public Java.util.Enumeration GetInitParameterNames () Returns the enumeration function of the above-definition, if the empty returns an empty enumeration function.

GetAttribute () Method Overview: Public Java.Lang.Object GetAttribute (Java.lang.String Name) Returns the servlet container variable value of the specified name, and returns NULL if there is no variable.

GetAttributeNames () Method Overview: Public Java.util.EnuMeration GetAttributeNames () Returns the enumeration function of all variables of the Servlet container, if air returns an empty enumeration function.

SetAttribute () Method Overview: Public Void SetAttribute (Java.lang.String Name, Java.Lang.Object Object) Binds a specified object to a specified name in the servlet container. If the name has been bound to an object, it is covered with this object. If Listeners Arext The Container Notifies THEM Accordingly. (SOLVED) If you pass a NULL value, it is equivalent to call RemoveAttribute ().

Removettribute () Method Overview: Public Void Removettribute (Java.lang.String Name) Remove the servlet container variable of the specified name.

GetServletContextName () Method Overview: Public Java.lang.String GetServletContextName () Returns the name of the Web Application. Finally, see servlet.getServletConfig (), servletconfig.getServletContext ()

转载请注明原文地址:https://www.9cbs.com/read-20225.html

New Post(0)