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.
- New Servlet and JSP features
- Talking about Servlet Web Server
- Implement Servlet applications in Weblogic
- Communication between Servlet and CGI
- Use MIDlet to activate Servlet