ServletContext- It is a global space for storing information, the server starts, it exists, and the server shuts down before it is released. Request, one user can have multiple; session, one user; ServletContext, all users share one. Therefore, in order to save space and improve efficiency, in servletcontext, it is necessary to put the required, important, all users need to share the thread is a security of some information.
To put it another way, each Web application running in a Java Virtual machine has a servlet context associated with it. The ServletContext object is the root of a known path in the Web server, and the servlet context is set to the HTTP://LOCALHOST:8080/project name. All requests starting with the/Project name request path (called the context Path) are sent to the Web application associated with this ServletContext. A ServletContext object represents the context of a Web application.
servlet Context: The servlet context provides access to the various resources and functions common to all servlets in the application. The Servlet context API is used to set information common to all servlets in the application. Servlets may need to share common information between them. Servlets running on the same server sometimes share resources, such as JSP pages, files, and other servlets.
Example:
For example, to do a shopping site, to extract information from the database, if the session to save these items information, each user access to a database, the efficiency is too low; So to use the servlet context to save, at the beginning of the server, access to the database, Store the item information in the servlet context so that each user can only read the item information from the context.
3.1 ServletContext Interface Introduction
The ServletContext interface defines the servlet view of the Web app that runs the servlet. The container vendor is responsible for providing the implementation of the ServletContext interface within the servlet container. Using the ServletContext object, the servlet can record the event log, get the URL address of the resource, and set and save the properties of other servlets that can be accessed within the context.
ServletContext a known path to the Web as the root path. For example, suppose a servlet context is located in Http://www.mycorp.com/catalog. All requests beginning with the/CATALOG request path, known as the context path, are routed to the Web app associated with the ServletContext.
3.2 ServletContext Interface Scope
Each Web application deployed in a container has an instance object associated with the ServletContext interface. If the container is distributed across multiple virtual machines, a web app will have one ServletContext instance in each VM.
Servlets in containers that are not deployed as part of a web app are default as part of the "default" Web app and have a default ServletContext. In a distributed container. The default ServletContext is non-distributed and must exist only in one VM.
3.3 initialization parameters
The initialization parameters of the ServletContext interface allow the servlet to access context initialization parameters related to Web applications, which are specified by the application developer in the deployment descriptor:
java.lang.String Getinitparameter (java.lang.String name)
Java.util.enumeration<java.lang.string>getinitparameternames ()
application developers take advantage of initialization parameters to transfer configuration information. A typical example is a Web administrator's e-mail address or a system name that holds critical data.
3.4 Context Properties
A servlet can bind an object property to a context by name. Any property bound to the context can be used by other servlets of the same Web application. The following methods of the ServletContext interface allow access to this functionality:
void SetAttribute (java.lang.String name, Java.lang.Object Object)
Java.lang.Object getattribute (java.lang.String name)
Java.util.enumeration<java.lang.string> Getattributenames ()
void RemoveAttribute (java.lang.String name)
3.4.1 Context properties in a distributed container
Context properties are local to the VM that created them. This prevents the ServletContext property from being stored in the shared memory of the distributed container. When information needs to be shared between Servlets running in a distributed environment, the information is placed in a session (see Chapter 7th, "Sessions"), stored in a database, or stored in an EJB component.
3.5 Resources
The ServletContext interface provides direct access to a static content document hierarchy consisting of Web apps, including html,gif and JPEG files, in the following ways:
Java.net.URL getresource (java.lang.String path)
Java.io.InputStreamgetResourceAsStream (java.lang.String path) The
The complete list of web App resources can be accessed using the Getresourcepaths (String path) method. Complete information on the semantics of this method can be found in the API documentation for this specification.
More than 3.6 hosts and ServletContext
The Web server may support multiple logical hosts on one server to share an IP address. This feature is sometimes referred to as a "virtual host." In this case, each logical host must have its own servlet context or a servlet context group. The servlet context cannot be shared by multiple virtual hosts.
3.7 Overloading considerations
Although container vendors do not require class loading for ease of development, any implementation must ensure that all of the servlets and classes that they might use 2 are loaded within a single classloader scope. The application should be guaranteed to work as expected by the developer. As a development aid, the full semantics of the session notification of the binding listener should be supported by the container and used on the listener for session termination on class loading.
The previous generation of containers creates a new class loader to load the servlet, which is different from the ClassLoader used to load other servlets or classes used in the servlet context. This can cause object references within the servlet context to point to an unexpected class or object, causing unexpected behavior. Problems raised by the next-generation class loader need to be blocked.
3.7.1 Temporary working directory
A temporary storage directory is required for each servlet context. The servlet container must provide a private temporary directory for each servlet context and make it available through the Javax.servlet.context.tempdir context property. The objects associated with these properties must be of type Java.io.File.
This requirement recognizes the common conveniences offered by many servlet engine implementations. The container does not need to maintain the contents of the Temp directory when the servlet restarts, but it needs to ensure that the contents of the temporary directory for a servlet context are not visible to the servlet context of other web apps running on that servlet container.
Web Backend Learning--java ServletContext detailed