Web Container Architecture:
Java servlets are components that must exist in a web container. The web container is built on top of the Java SE platform and implements the servlet API and all of the services required to process HTTP (and other Transmission Control Protocol/ Internet Protocol [TCP/IP]) requests.
The figure given below does show the architecture for a web server that uses a web container.
- The web container activates the servlet that matches the request URL by calling the service method on an instance of the servlet class.
- Specifically, the activation of the service method for a given HTTP request is handled in a separate thread within the web container process.
Request and Response Process:
In this, it does illustrate the web container’s request and response process using component diagrams.
Browser Connects to the Web Container:
- The first step in this process is a web browser sending an HTTP request to the web container.
To process a request, the browser makes a TCP socket connection to the web container. The figure given below shows this step:
Web Container Objectifies the Input/output Streams:
- Next, the web container creates an object that encapsulates the data in the request and response streams.
These two objects represent all of the information available in the HTTP request and response streams. The figure given below shows this step:
- The web container then executes the service method on the requested servlet. The request and response objects are passed as arguments to this method.
The execution of the service method occurs in a separate thread. The figure given below shows this step:
Servlet Uses the Output Stream to Generate the Response:
Finally, the text of the response generated by the servlet is packaged into an HTTP response stream, which is returned to the web browser. The figure given below does show this step:
Developing a Simple HTTP Servlet:
- As an HTTP servlet developer, what do you need to do? When developing an HTTP servlet, you provide a Java class which is used to respond to requests from the client browser.
List Leagues Architecture Model:
- The architecture model for the List All Available Leagues use case describes the components involved. From a welcome page (index.html) the user selects the List League link.
- The browser does make a GET request for the URL. This request is processed by a List Leagues view servlet.
The figure given below does show the architecture model diagram for the Soccer League application:
The HttpServlet API:
- To create a servlet that responds to an HTTP request, you should create the class that extends the HttpServlet abstract class (provided in the javax.servlet.http package).
The figure given below provides a UML class diagram that shows the relationships between your servlet class and the interfaces to access data in the request and response streams:
The HttpServlet API
- The HttpServlet service method does look at the HTTP method in the request stream and dispatches to a doXYZ instance method based on the HTTP method.
- For example, if the servlet needs to respond to an HTTP GET request, you should override the doGet method.
The table given below does illustrate the HTTP request and their corresponding HttpServlet methods
Corresponding HttpServlet Methods:
|HTTP Method||Corresponding HttpServlet Method|
The HttpServletRequest API:
- The HTTP request information is encapsulated by the HttpServletRequest interface.
- The getHeaderNames method does return an enumeration of strings composed of the names of each header in the request stream.
- To retrieve the value of specific header, you can use the getHeader method. This method does return a String.
- Some header values are strings that represent either integer or a date. There are two convenience methods, getIntHeader and getDateHeader, thatperforms this conversion for you.
- The following code does highlight demonstrate how headerinformation can be used to select the content that gets generated for the client.
- boolean displayXHTML = false;
- String userAgent = request.getHeader(“User-Agent”);
- // browser can handle XHTML content
- displayXHTML = true;
- // XHTML content output here
- // regular HTML content output here
Discussion: what are some ways that you could use request headers to augment a web application?
The HttpServletResponse API:
- The HTTP response information is encapsulated by the HttpServletResponse interface. You can set a response header using the setHeader method.
- If the header value you want to set is either an integer or a date, then you can use the convenience methods setIntHeader or setDateHeader.
- Also,the HttpServletResponse interface gives you access to the body of the response stream. The response body is a data that is sent to the browser to be displayed.
- The response body is encapsulated in a Java technology stream object. The response body is interpreted by the web container, and is embedded in the HTTP response stream similar to a letter in an envelope.
- The web container is responsible for packaging the response text with the header information.
- You need to generate the response text. You must retrieve the response body stream using either the getWriter or getOutputStream method.
- If you are generating an HTML response, you should use the getWriter method, which returns a character stream, a PrintWriter object.
- If you generate a binary response, such as a graphic image, you should use the getOutputStream method, which returns a binary stream, a ServletOutputStream object.
- You must also set the content type of the response text. The content type defines the MIME type of the response text.
- It is the content type header that tells the web browser how to render the body of the HTTP response. Examples of a MIME type include text/plain, text/html, image/jpeg, image/png, audio/au, and so on.
- The default MIME type for servlets is text/plain.To declare a response of any other type, you must use the setContentType method.
- The following code highlights demonstrate how you can use header information to disable browser and proxy caching.
- When the Cache-Control header is set to the no-cache, this is an instruction for the browser to not use the response for a subsequent request without revalidation with the origin server.
The private setting for Cache-Control indicates that the response is intended for a single user and should not be stored in a shared cache (for example, the cache in a proxy server).