Java Servlet Technology Primer
- Java Servlet Technology
- The Tomcat Servlet Container
- Six Steps to Running Your First Servlet
- About This Article
In the past, ASP and servlet/JSP have been the main technologies used in web application development. With the release of ASP.NET, it is not hard to predict that this technology will become the servlet/JSP's main competitor. Both ASP (and ASP.NET) and servlet/JSP have their own fans, however, which makes it hard to know which one will come out the winner. The most likely outcome is that there won't be an absolute winner that captures the market. Instead, both technologies will probably compete head-to-head in the near future. Like other Java technologies, however, servlet (and JSP) offers the benefits that are not necessarily available in other technologies. This article shows you the benefits of servlets and explains how they work, their architecture, and how to run your own servlet.
The Benefits of Java Servlet
Java servlet (and JSP) offers web application developers a number of benefits they won't find in other technologies. These include the following:
PerformanceThe performance of servlets is superior to CGI because there is no process creation for each client request. Instead, each request is handled by the servlet container process. After a servlet is finished processing a request, it stays resident in memory, waiting for another request.
PortabilityLike other Java technologies, servlet applications are portable. You can move them to other operating systems without serious hassles.
Rapid development cycleAs a Java technology, servlets have access to the rich Java library that will help speed up the development process.
RobustnessServlets are managed by the Java Virtual Machine. As such, you don't need to worry about memory leak or garbage collection, which helps you write robust applications.
Widespread acceptanceJava is a widely accepted technology. This means that there are numerous vendors that work on Java-based technologies. One of the advantages of this widespread acceptance is that you can easily find and purchase components that suit your needs, which can save precious development time.
Servlet Application Architecture
A servlet is a Java class that can be loaded dynamically into and run by a special web server. This servlet-aware web server, known as a servlet engine in the early days of the servlet technology, is now called a servlet container.
Servlets interact with clients via a request-response model based on HTTP. Because servlet technology works on top of HTTP, a servlet container must support HTTP as the protocol for client requests and server responses. However, a servlet container also can support similar protocols such as HTTPS (HTTP over SSL) for secure transactions.
Figure 1.1 provides the architecture of a servlet application.
Figure 1.1 The servlet application architecture.
For reasons that will become clear after you read Chapter 8, "JSP Basics," in a JSP application, the servlet container is replaced by a JSP container. Both the servlet container and the JSP container often are referred to as the web container or servlet/JSP container, especially if a web application consists of both servlets and JSP pages.
As you can see, a servlet application also can include static contents, such as HTML pages and image files. Allowing the servlet container to serve these static contents is not preferable, however, because static contents are faster if served by a more robust HTTP server such as the Apache Web server or Microsoft Internet Information Server. As such, developers commonly put a web server at the front to handle all client requests. The web server serves static contents and passes to the servlet containers all client requests for servlets.
As such, a more common architecture for a servlet application is shown in Figure 1.2.
Figure 1.2 The servlet application architecture employing an HTTP server.
A Java web application architecture employing a J2EE server is different from the diagrams shown in Figure 1.1 and 1.2. This architecture is discussed in Chapter 28, "The Enterprise JavaBeans (EJB) Technology."
How a Servlet Works
A servlet is loaded by the servlet container the first time the servlet is requested. The servlet receives the forwarded user request, processes it, and returns the response to the servlet container, which in turn sends the response back to the user. Afterward, the servlet stays in memory waiting for other requests. It won't be unloaded from the memory unless the servlet container sees a shortage of memory. Each time the servlet is requested, however, the servlet container will compare the timestamp of the loaded servlet with the servlet class file. If the class file timestamp is more recent, the servlet is reloaded into memory. This way, you don't need to restart the servlet container every time you update your servlet.
The way a servlet works inside the servlet container is depicted in the diagram in Figure 1.3.
Figure 1.3 How a servlet works.