The specification for the Java 2, Enterprise Edition ( J2EE) defines a platform for developing web-enabled applications that includes Enterprise JavaBeans, servlets, and JavaServer 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 help you understand J2EE, we must introduce servlets and JSP and explain the synergy between these technologies and Enterprise JavaBeans.
At the risk of spoiling the story, J2EE provides two kinds of “glue” to make it easier for components to interact. We have already seen both types of glue. First, the JNDI enterprise naming context (ENC) is used to standardize the way components look up resources they need. We have already seen the ENC in the context of enterprise beans; in this chapter, we will look briefly at how servlets, JSPs, and even some clients can use the ENC to find resources. Second, the use 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 JSP pages can be packaged with deployment descriptors that define their relationships to their environment. Deployment descriptors are also used to define entire assemblies of many components into applications.
The servlet 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 to 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.[63] The servlet is
provided with 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 17-1 illustrates how a request
is sent by a browser and serviced by a servlet running in a web
server.
Servlets are similar to session beans because they both perform a service and can directly access backend resources (e.g., databases) through JDBC, but they do not represent persistent data. Servlets do not, however, have support for container-managed transactions and are not composed of business methods. Servlets deal with very specific (usually HTTP) requests and respond by writing to an output stream.
The servlet specification is extensive and robust but also simple and elegant. It is 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).