Interface introduction-Servlet Context

Source: Internet
Author: User

A set of Web components such as Servlets, JSP, and JavaBean installed on a server with a specific URL namespace such as/myapplication constitute a Web application, every Web application is the same JVM), the container will have a background object, while javax. servlet. the Servlet Context interface provides a way to access this background object. You can use the getServlet Context method of a Servlet instance to obtain the background object in which the Servlet runs. From this background object, you can access the following information or resources:

◆ Initialization parameters
◆ Objects stored in the background
◆ Resources associated with this background
◆ Log

You can call Servlet Context. getInitParameterNames () returns an enumerated object of the initialization parameter Java. util. enumeration), or directly specify a parameter name to obtain the specific parameter value: Servlet Context. getInitParameter (String name ).
In addition, objects of any type bound to a specific name can be stored in the Servlet background, for example:

Context. setAttribute (String name, Object object );
Binds the object to the name and stores it in the Servlet background, which can be shared by other servlets in the same background. Other servlets can use context. getAttribute (String name) to obtain an object in the background, or use context. removeAttribute (String name) to remove an object from the background.
The URL of a specific resource can be called by Servlet Context. getResource (String path) is obtained. The path parameter must start with "/" and be parsed as the relative path of the root of the current Servlet background. This method is different from the java. lang. Class. getResource method based on the Class loader. If you request a. JSP page through the Servlet Context. getResource method, you will get the JSP Source Code. To get the execution result, you can use the include method of the RequestDispatcher object. You can also directly obtain resources in the form of input streams,

Public InputStream getResourceAsStream (String path );

Servlet Context provides log support. You can simply call Servlet Context. log (String msg), or Servlet Context. the log (String msg, Throwable throwable) method writes log information to the underlying Servlet log records. The latter can also write exception information and throwable trace stack.

  1. New Servlet and JSP features
  2. Talking about Servlet Web Server
  3. Implement Servlet applications in Weblogic
  4. Communication between Servlet and CGI
  5. Use MIDlet to activate Servlet

Related Article

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.