| C H A P T E R 3 |
|
Servlet Context |
The ServletContext interface defines a servlet’s view of the web application within which the servlet is running. The container provider is responsible for providing an implementation of the ServletContext interface in the servlet container. Using the ServletContext object, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can access.
A ServletContext is rooted at a known path within a web server. For example, a servlet context could be located at http://<servername>/catalog. All requests that begin with the /catalog request path, known as the context path, are routed to the web application associated with the ServletContext.
One instance object of the ServletContext interface is associated with each web application deployed into a container.
Servlets in a container that were not deployed as part of a web application are implicitly part of a “default” web application and have a default ServletContext.
The following methods of the ServletContext interface allow the servlet access to context initialization parameters associated with a web application as specified by the application developer in the deployment descriptor:
Initialization parameters are used by an application developer to convey setup information. A typical example is the name of another peer system with which the application may interact.
A servlet can bind an object attribute into the context by name. Any attribute bound into a context is available to any other servlet that is part of the same web application. The following methods of the ServletContext interface allow access to this functionality:
The ServletContext interface provides direct access only to the hierarchy of static content documents that are part of the web application, including HTML, GIF, and JPEG files, via the getResourceAsStream method of the ServletContext interface.
The getResourceAsStream method takes a String with a leading “/” as an argument that gives the path of the resource relative to the root of the context. This hierarchy of documents may exist in the server’s file system, in a web application archive file, or at some other location.
This method is not used to obtain dynamic content. See Chapter 8 for more information about accessing dynamic content.
Web servers may support multiple logical hosts sharing one IP address on a server. This capability is sometimes referred to as “virtual hosting”. In this case, each logical host must have its own servlet context or set of servlet contexts. Servlet contexts cannot be shared across virtual hosts.
Copyright © 2009 Sun Microsystems, Inc. All rights reserved.