Learn Java for Web Development

(Tina Meador) #1
CHAPTER 2: Building Web Applications Using Servlets and JSP 57

In the following section, you will learn how the request flows through the application and how
the container finds the servlet in the helloworld application you developed. Then you will use the
HelloWorld servlet to understand the life-cycle methods of the servlet.


Request Flow for the HelloWorld Servlet

The request originating from the web browser flows through the web server and the servlet container
before the HelloWorld servlet can generate the response, as explained in following sections.


HTTP Request Message


The URL used to access the helloworld web application is http://localhost:8080/helloworld/hello.
When the user accesses the web application through this URL, the web browser creates the HTTP
request, as illustrated in Listing 2-3.


Listing 2-3. HttpRequest Message


1.GET /helloworld/hello HTTP/1.1
2.Host: localhost:8080
3.User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko)
Chrome/26.0.1410.43 Safari/537.31


   Line 3: This line describes the specific user agent (web browser) that initiated
communication by making a request for a specific resource using HTTP,
described in line 1.
 Line 1: The request made by the user agent is of the form shown in Figure 2-14.

Figure 2-14. Anatomy of HTTP request message

   Line 2: This machine (your machine) that is running a web server is a server
machine. Localhost is specified where you would otherwise use the host name
of a computer. For example, directing a browser installed on a system running
an HTTP server to http://localhost will display the home page of the web site
installed on that system, as illustrated in Figure 2-15.
Free download pdf