Java ServletContext Detailed

Source: Internet
Author: User

ServletContext, is a global storage space for information, the server starts, it exists, the server shuts down, and 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 Initialize 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:

Getinitparameter

Getinitparameternames

The application developer takes advantage of the 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:

SetAttribute

GetAttribute

Getattributenames

RemoveAttribute

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 applications, including html,gif and JPEG files, in the following ways:

GetResource

getResourceAsStream

The GetResource and getResourceAsStream methods begin with a string of "/" as a parameter that specifies the resource-relative path of the context root path. The level of the document may exist in the server's file system, the war file, the remote server, or in some other location.

These methods do not need to get dynamic content. For example, in a container that supports JSP Specification 1, a method call of GetResource ("/index.jsp") will return the JSP source code instead of the processed output. For more information on accessing dynamic content, see Chapter 8th, "Forwarding requests".

A 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

Java ServletContext Detailed

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.