The core Internet protocols substantiate and sustain the Web, so understanding these protocols is fundamental to understanding how web applications are developed.
The Internet is a colossal network of networks, and in general, all of the machines on the Internet can be classified into two types: the server and the client. The client is the machine requesting some information, and the server is the machine that provides that information. The information data that flows from the information provider (that is, the server) to the information requester (that is, the client) is bound by a definite rule that governs the marshaling of the information to be transmitted by the server and the unmarshaling of the information to be translated or read by the client.
This rule is called the protocol. The web browser (that is, the client), the webserver (that is, the server), and the web application all converse with each other through the Hypertext Transfer Protocol (HTTP). The clients send HTTP requests to the web servers, and the web servers return the requested data in the form of HTTP responses. The HTTP clients and the HTTP servers are the bricks and mortar that lay the foundation of the World Wide Web, and HTTP is the lingua franca of the Web.
Servlets are the central processing unit of a Java web application and are responsible for most of the processing required by a web application. Specifically, a servlet is a Java class that implements the java. servlet.Servlet interface. The Servlet interface defines the methods that all servlets must implement. “
Request Flow for the HelloWorld Servlet
The request originating from the web browser flows through the webserver and the servlet container before the HelloWorld servlet can generate the response, as explained in the following sections.
Examining the Request
When the client (web browser) makes a request (a GET request in this case), the webserver (Tomcat) sees the resource path /helloworld/hello in the request in line 1 and determines that the resource requested by the user is not a static page (for example a .html file) and so forwards the request to the web container (Tomcat). Astute readers will notice that Tomcat serves the role of the webserver and the web container.
Locating the Servlet
The resource path in the request is mapped to the HelloWorld servlet through the web.xml file written. This web.xml file is called a deployment descriptor because it describes the deployed servlet to the web container. Through the deployment descriptor, the web container determines the servlet that needs to be called to serve the original HTTP request that the web browser initiated.
A Java EE web.xml file can contain many additional XML tags. Besides mapping URLs to actual servlets, you can use the deployment descriptor to customize other aspects of your web application such as security roles, error pages, tag libraries, and initial configuration information. However, these additional tags are not needed for this HelloWorld application. The web container loads the HelloWorld servlet class and instantiates it. Only a single instance of the HelloWorld servlet is created, and concurrent requests to the HelloWorld servlet are executed on that same instance. Every client request generates a new pair of request and response objects. The container runs multiple threads to process multiple requests to a single instance of the HelloWorld servlet
Most servlets provide similar basic functionality through an abstract javax.servlet.GenericServlet class provided by the Servlet API. The GenericServlet class implementation is protocol-independent, so it does not matter if it has to respond to HTTP or FTP requests. The GenericServlet abstract class defines an init() method that is called by the default init(ServletConfig) method to execute any application-specific servlet initialization.
This chapter introduced servlets and JSP and showed you how to make your first web application using these web components. Then the chapter implemented the real-world MVC-based Java web application, a bookstore using servlets and JSP. In the next chapter, we will augment this application to use the best practice of separating the business-logic concerns from the presentation using JSTL and Expression Language.