start page | rating of books | rating of authors | reviews | copyrights

Book Home Enterprise JavaBeans Search this book

Chapter 11. Java 2, Enterprise Edition

Contents:

Servlets
Java Server Pages
Web Components and EJB
J2EE Fills in the Gaps
Fitting the Pieces Together
Future Enhancements

The specification for the Java 2, Enterprise Edition ( J2EE) defines a platform for developing web-enabled applications that includes Enterprise JavaBeans, Servlets, and Java Server Pages ( JSP). J2EE products are application servers that provide a complete implementation of the EJB, Servlet, and JSP technologies. In addition, the J2EE outlines how these technologies work together to provide a complete solution. To understand what J2EE is, it's important that we introduce Servlets and JSP and explain the synergy between these technologies and Enterprise JavaBeans.

At risk of spoiling the story, J2EE provides two kinds of "glue" to make it easier for components to interact. We've already seen both types of glue. The JNDI Enterprise Naming Context (ENC) is used to standardize the way components look up resources that they need. We've seen the ENC in the context of enterprise beans; in this chapter, we'll look briefly at how servlets, JSPs, and even some clients can use the ENC to find resources. Second, the idea of deployment descriptors--in particular, the use of XML to define a language for deployment descriptors--has been extended to servlets and JSP. Java servlets and server pages can be packaged with deployment descriptors that define their relationship to their environment. Deployment descriptors are also used to define entire assemblies of many components into applications.

11.1. Servlets

The Servlet 2.2 specification defines a server-side component model that can be implemented by web server vendors. Servlets provide a simple but powerful API for generating web pages dynamically. (Although servlets can be used for many different request-response protocols, they are predominantly used to process HTTP requests for web pages.)

Servlets are developed in the same fashion as enterprise beans; they are Java classes that extend a base component class and have a deployment descriptor. Once a servlet is developed and packaged in a JAR file, it can be deployed in a web server. When a servlet is deployed, it is assigned to handle requests for a specific web page or assist other servlets in handling page requests. The following servlet, for example, might be assigned to handle any request for the helloworld.html page on a web server:

import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse response)
    throws ServletException,java.io.IOException {

    try {
     ServletOutputStream writer = response.getWriter(  );
     writer.println("<HTML><BODY>");
     writer.println("<h1>Hello World!!</h1>");
     writer.println("</BODY></HTML>");    
  } catch(Exception e) {
  // handle exception
  }
  ...
}

When a browser sends a request for the page to the web server, the server delegates the request to the appropriate servlet instance by invoking the servlet's doGet( ) method.[1] The servlet is provided information about the request in the HttpServletRequest object, and can use the HttpServletResponse object to reply to the request. This simple servlet sends a short HTML document including the text "Hello World" back to the browser, which displays it. Figure 11-1 illustrates how a request is sent by a browser and serviced by a servlet running in a web server.

[1]HttpServlets also have a doPost( ) method which handles requests for forms.

figure

Figure 11-1. Servlet servicing an HTTP request

Servlets are similar to session beans because they both perform a service and can directly access backend resources like a database through JDBC, but they do not represent persistent data. Servlets do not, however, have support for transactions and are not composed of business methods. Servlets respond to very specific requests, usually HTTP requests, and respond by writing to an output stream.

The Servlet specification is extensive and robust but also simple and elegant. It's a powerful server-side component model. You can learn more about servlets by reading Java™ Servlet Programming, by Jason Hunter and William Crawford (O'Reilly).



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.