Servlet context servletcontext Interface

Source: Internet
Author: User

Description of servletcontext Interface

Description of the servletcontext interface: public interface servletcontext defines a series of methods for communication with the corresponding servlet container, such as obtaining the MIME type of the file, dispatching requests, or writing logs to the log file. Each web-app can have only one servletcontext. A web-app can be a folder containing web application files or a. War file. The servletcontext object is included in the servletconfig object. The servletconfig object provides the servlet object during servlet initialization.

Getcontext () method Overview: Public servletcontext getcontext (Java. Lang. String uripath) returns a servletcontext object with the specified URL address. This method allows Servlets to gain access to the context of each part of the server and obtain the requestdispatcher object from the context as needed. The specified URL path must contain "/", which is interpreted as the relative path under the root directory of the server document and matches with the context root directory of other web-app hosts. In a secure environment, the servlet container returns NULL.

Getmajorversion () method Overview: Public int getmajorversion () returns the servlet API version supported by the servlet container. All implementations must return the integer number 2.

The getminorversion () method is omitted.

Getmimetype () method Overview: Public java. Lang. String getmimetype (Java. Lang. String file) returns the file type of the specified file. If the file type is unknown, null is returned. The file type is determined by the servlet container configuration and specified in a Web-app. Generally, the file types are text/html and image/GIF ".

Getresourcepaths () method Overview: Public java. util. Set getresourcepaths (Java. Lang. String path) returns a set (SET) that stores all resource paths in the Web-app ). The path ends with a slash (/) to indicate a sub-directory and starts with a slash (/) to indicate a relative path to the web-app.

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/") returns set {"/CATALOG/index.html", "/CATALOG/products.html", "/CATALOG/offers /"}.

If the subdirectory is empty, null is returned.

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 serve as the relative location of the current directory. This method allows the servlet container to make a resource in serlets available. This resource can be a local resource or a remote file system. These resources can be in a database or a. War file. Servlet containers must implement URL handles and urlconnection objects, which are required for resource access. If no matched resource exists, this method returns NULL. Some containers can even write data to the resources returned by this method. This resource can be directly returned, so the JSP source file will be returned when a. jsp file is requested. Replace it with a requestdispatcher object to include the execution result. This method is different from Java. Lang. Class. getresource. It is a class loader-based resource retrieval method. However, the former does not depend on class loaders.

Getresourceasstream () method Overview: Public java. Io. inputstream getresourceasstream (Java. Lang. String path) returns an inputstream at the resource specified by the string path. The returned inputstream can be of any type and length.

Getrequestdispatcher () method Overview: Public requestdispatcher getrequestdispatcher (Java. Lang. String path) returns a requestdispatcher object, which plays the role of a given resource wrapper. A requestdispatcher object can be used to send a request to a specific resource or include a specific resource in a response. The specified resource can be dynamic or static. The getcontext () method can be used to obtain the requestdispatcher object for external contexts resources. If servletcontext cannot obtain the requestdispatcher object, null is returned.

Getnameddispatcher () method Overview: Public requestdispatcher getnameddispatcher (Java. Lang. string name) returns a requestdispatcher object for the servlet object with the specified name. 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.

The getservlet (), getservlets (), and getservletnames () methods are not recommended.

Log (Java. Lang. String MSG) method Overview: Public void log (Java. Lang. String MSG) writes the specified information into the servlet log file, usually the transaction 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) slightly public void log (Java. Lang. String message, java. Lang. throwable) slightly

Getrealpath () method Overview: Public java. Lang. String getrealpath (Java. Lang. String path) returns a string of the real path (complete path) of the specified virtual path. For example, the virtual path "/index.html" will return the absolute path "http: // host/contextpath/index.html" in the Server File System. The contextpath in this path is the context path of servletcontext. The returned path applies to the operating system running the servlet container. If you cannot map a virtual path to a real path, this method returns 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 assumerver Web Development Kit returns the string "assumerver 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.0x86 )"

Getinitparameter () method Overview: Public java. Lang. String getinitparameter (Java. Lang. string name) returns the value of the context-defined variable. If the variable does not exist, null is returned. See servletconfig. getinitparameter (Java. Lang. String ).

Getinitparameternames () method Overview: Public java. util. Enumeration getinitparameternames () returns the enumerated function of the variable defined in the context. If it is null, an empty enumerated function is returned.

Getattribute () method Overview: Public java. Lang. Object getattribute (Java. Lang. string name) returns the servlet container variable value of the specified name. If no such variable exists, null is returned.

Getattributenames () method Overview: Public java. util. Enumeration getattributenames () returns the enumerated functions of all variables of the servlet container. If it is null, null enumeration functions are returned.

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 will be overwritten with this object. If listeners are configured on the servletcontext the container notifies them accordingly. (no translation, for help) If a null value is passed, removeattribute () is called ().

Removeattribute () method Overview: Public void removeattribute (Java. Lang. string name) removes the servlet container variable with the specified name.

Getservletcontextname () method Overview: Public java. Lang. String getservletcontextname () returns the name of the Web application.

Finally, see servlet. getservletconfig (), servletconfig. getservletcontext ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.