|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java EE 5 Java Servlets: Advanced Features
Java Servlets: Advanced Features
By: Dr. Subrahmanyam
Feb. 1, 2000 12:00 AM
There's a lot of action going on with Java servlets. The recent public release of Java Servlet Specification v2.2 by Sun Microsystems enhances the functionality of the programming model and the deployment and runtime infrastructure of servlets, which provides for better packaging, security, distribution and management of servlet-based Web applications. The servlet technology is now a part of the Java 2 Enterprise Edition (J2EE) architecture and is expected to play an important role in the Web/enterprise application server market that has hitherto been dominated by proprietary programming models. When Java servlets were introduced by Sun in early 1997, the primary goal was to provide a Java language alternative to the CGI (Common Gateway Interface) model. Accordingly, the initial model was designed to serve dynamic content for incoming HTTP requests. Those who followed servlets from their inception will remember their early description as applets on the server side. Such descriptions were perhaps appropriate with the initial servlet model. Over the past two years, however, Sun has revised the specification significantly to let servlets cater to developing production-quality Web applications. Most of the development, deployment and runtime features of the servlet model have changed considerably over several revisions. Servlets have now passed their infancy, and are beginning to be used for developing mission-critical Web applications with commercial application servers such as WebSphere, Netscape Application Server, NetDynamics, WebLogic and Orion. So where do servlets stand today?
Servlet Programming Model - Overview
Servlet containers operate in tandem with Web servers, and invoke servlets based on requests from these servers. When deploying servlets onto servlet containers, you can also specify canonical names to them. The servlet container maintains the mapping between these canonical names and servlet classes. The servlet programming model is lightweight. Its core classes are HttpServlet, HttpServletRequest, HttpServletResponse, HttpSession, ServletContext and ServletConfig. Of these, the HttpServlet is the class that your application servlets extend from; the rest are interfaces. Container vendors provide implementations for all these classes/interfaces. Servlet programming consists of overriding the init(), service() (or one of its derivatives to process GET, POST, HEAD, DELETE, OPTIONS, PUT and TRACE requests) and destroy() methods of the HttpServlet class. Refer to Table 1 for a brief overview of the programming model. In this table the core classes/interfaces of the servlet API are categorized based on their responsibilities. At deployment time the site administrator configures the Web server to delegate all servlet requests to the servlet container. When a client user agent (typically a Web browser) sends an HTTP request whose request path maps to a servlet, the Web server delegates the request to the servlet container. In response to this, the servlet container creates HttpServletRequest and HttpServletResponse objects, sets up the environment, creates/locates a servlet instance that corresponds to the path specified in the incoming request, and invokes its service method with the HttpServletRequest and HttpServletResponse objects. The HttpServletRequest and HttpServletResponse objects are Java language objects that encapsulate the request and response streams from the client. The servlet's service method can process the request and write content dynamically to the response stream. It can also forward the request and response objects to a JSP to create dynamic content. The service method can make use of additional utility classes and back-end layers composed of databases, Enterprise JavaBeans, CORBA servers and more to process the request. This model is similar to the well-known CGI model for building Web sites with dynamic content. In addition to this basic facility, the servlet specification has facilities for session tracking and state management. (For an introductory discussion of these aspects, see references 3 and 4 in the Resources section at the end of this article.) However, the model and its level of abstraction aren't adequate for building enterprise-scale Web applications. These require higher-level abstractions for security, scalability, management and more. In the remaining sections of this article we'll look at some of the advanced facilities that the current servlet specification provides for building and managing large Web applications.
Web Applications, Servlets and Virtual Sandboxes
A Web application is a collection of Java servlets, JSP pages, HTML/XML documents and other resources organized in a structured hierarchy of directories. You can also package constituents of Web applications into Web archive (WAR) files. A WAR file is primarily an application deployment unit. Servlet containers provide the runtime environment for servlet-based Web applications. They also provide a host of other services, some of which we'll discuss here. At the time of deployment, you can associate a Web application with a specific path of the Web server. This path serves as a document root for serving servlets and other resources that are part of the Web application and therefore define a name tree under this path. For example, if you have a Web application called MyWebApp, you can associate it with the path /MyWebApp of your Web server. All the constituents of the public directory of your Web applications as well as servlets can be accessed relative to this path. As shown in Figure 1, the directory structure of a Web application consists of two parts. The first part is the public directory structure containing HTML/XML documents, JSP pages, images, applets, and so on. The container appropriately serves the directory's contents against incoming requests. The second part is a special WEB-INF directory that contains the following:
The contents of this directory are for use only by the containers, and the containers won't serve these directly to clients. What are the implications of the above organization? First, a Web application (whether archived into a WAR file or not) allows you to package all constituents of the application into one physical unit. Prior to the introduction of the notion of Web applications, you'd usually do the following to deploy an application:
The second important implication is the deployment descriptor. This is an attempt toward standardizing the deployment configuration of Web applications. The deployment descriptor, an XML document with a DTD (Document Type Definition - the current standard for specifying XML documents) specified in the Java servlet specification, allows you to postpone certain decisions from the build time to the deployment time. Examples of such decisions include initialization data for your servlets, canonical names for servlets, MIME type mappings, security (more about this later), database parameters and log file names. It's good practice to defer most of the hard-coding from your servlets to the deployment descriptor. The third implication is that each Web application is associated with a different context, so you can bundle your servlets in more than one application. Instances of such servlets remain independent in the same Java Virtual Machine of the container and don't share the same context (more about context in the next section). Thus this notion of a Web application introduces a virtual sandbox within a JVM. Note: Previously, you could deploy the same servlet more than once with different canonical names and initialization parameters. However, all such servlets share the same context since there was no notion of a Web application. Finally, different Web applications hosted on the same container can't share clients' session information. That is, if you have two Web applications deployed on the same container, servlets in both applications see two different sessions for the same client. This is a marked difference from the old servlet model in which a container (aka servlet engine) can establish only one client session. You now need to implement more ingenious ways to share data between your Web applications.
Containers - Not Just Hosts
Also, prior to version 2.2 of the specification, servlet engine was the term used to denote what is now called a container. While these terms represent the same in basic functionality, the change in language indicates a shift of emphasis from processing - that's what an engine does - to providing a runtime for hosting. Although a container essentially provides runtime and network services for hosting Web applications, it's worthwhile to interpret a container as a request interceptor between network services and the servlet instances. This allows us to discuss what goes on between the container receiving a request and a servlet instance being invoked to handle that request. It also lets us look at certain innovations that containers can implement by virtue of their request-interception capabilities. First, what goes on within a typical servlet container after it receives a request and before it invokes a servlet instance?
In addition, containers can perform certain advanced tasks while intercepting a request.
Request Serialization
Declarative Security
The servlet specification has now introduced another approach, declarative security, in which the container takes the responsibility of protecting Web applications based on the container's interception ability. Here's a scenario:
Session/Context Passivation
Distribution and Scalability
Instead, it expects the containers to make sure that a single node in a cluster will handle all requests pertaining to a user session. However, we can expect some application server vendors to provide instance-independent load balancing. For your Web application to avail itself of this facility, you should mark your Web applications as distributable in the deployment descriptor. Once marked, the container can activate the servlets contained in the Web application on multiple JVMs. The container can then employ any load-balancing strategy to distribute the incoming requests to servlets in one of the JVMs. Although thus far the servlet specification is silent about the failover capability of servlet containers, it's one of the features we can expect from some of the Web application server vendors. It requires that the container swap the HttpSession (and perhaps ServletContext too) objects of each application (in each JVM) to a persistent media whenever there's a change in the state of these objects. This way, active client sessions can be reactivated upon restarting the container (or the host nodes thereof). In addition, containers may implement session/context swapping between JVMs in the cluster to shift load from one JVM to another or to add/remove hosts in the cluster.
Summary
To summarize this discussion, the servlet programming model now addresses enterprise-level Web application development. The notion of Web applications and containers has profound implications on the way dynamic Web sites can be built and managed - a major leap from the earlier single-JVM, single-application model. In Part 2 of this article we'll look at certain precautions that need to be taken while building servlet-based Web applications in the changed scenario. Resources
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||