0% found this document useful (0 votes)
2 views23 pages

Chapter 8 Web Programming

The document outlines the life cycle of an HTTP server, detailing the processes from initialization to shutdown, including handling requests and responses. It also explains how servlets facilitate client/server communication, providing examples of servlet code for handling HTTP requests and generating responses. Additionally, it includes HTML form examples that interact with servlets to create dynamic web applications.

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Chapter 8 Web Programming

The document outlines the life cycle of an HTTP server, detailing the processes from initialization to shutdown, including handling requests and responses. It also explains how servlets facilitate client/server communication, providing examples of servlet code for handling HTTP requests and generating responses. Additionally, it includes HTML form examples that interact with servlets to create dynamic web applications.

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Outline

8.1. The Life Cycle of a HTTP server


8.2. Handling HTTP Requests & Responses
8.3. HTML Choices
8.4. Client/Server Communication
1. Initialization: During the initialization stage, the HTTP server
software is loaded into memory and configured to listen for
incoming HTTP requests.
 This may involve reading configuration files.
 setting up network connections, and starting any necessary background
processes or threads.
2. Accepting connections: Once the server is initialized, it begins
listening for incoming connections from clients.
 This involves waiting for clients to establish a TCP connection to the
server's port.
 When a client establishes a connection, the server accepts it and creates a
new socket to handle communication with that client.
3. Handling requests: When a client sends an HTTP request to the
server, the server reads the request data from the socket and
parses it to determine the appropriate action to take.
 This may involve reading data from files or databases, executing scripts or
other programs, or generating dynamic content on the fly.
 The server then generates an appropriate response to the request and sends it
back to the client.
4. Sending responses: Once the server has generated a response to
the client's request, it sends the response back over the established
TCP connection.
 This involves writing data to the network socket,
 may also involve compressing the response, manipulating the response data, or
performing other actions to optimize network performance.
5. Connection termination: When the server has finished sending the
response to the client, it closes the TCP connection, freeing up
any resources used during the request-handling process.
 Depending on the server configuration, the server may keep the connection
open for a certain amount of time in case the client sends additional requests.
5. Logging: Throughout the entire life cycle, the server may log
information about incoming requests, responses, and errors to
help with debugging and analysis.
 This may include recording information such as the client's IP address, the
requested URL, the response status code, and any error messages or stack
traces generated during request processing.
 The server may write these logs to a file or database for later analysis.
6. Shutdown: Finally, when the server is ready to be shut down
(either due to a graceful shutdown or a system reboot), it
performs cleanup tasks such as closing any open files or network
sockets
Handling HTTP Requests with Servlets:
1. Receiving the Request: The servlet container receives the
HTTP request from the client and determines which servlet
should handle it.
2. Parsing the Request: The servlet container parses the request
and creates a HttpServletRequest object that contains
information about the request, such as the requested URL,
query parameters, headers, and message body.
3. Routing the Request: Once the servlet has received the
HttpServletRequest object, it uses the information contained
within it to determine which part of the application code should
handle the request.
4. Handling the Request: The servlet then passes the
HttpServletRequest object to the appropriate part of the
application code to handle the request.
Handling HTTP Responses with Servlets:
1. Generating the Response: Once the application code has processed
the request and generated a response, the servlet creates a
HttpServletResponse object that represents the response to be sent
back to the client.
2. Sending the Response: The servlet then sends the
HttpServletResponse object back to the client over the established
TCP connection.
3. Logging and Monitoring: Throughout the entire process of handling
the request and generating the response, the servlet may log
information about incoming requests, responses, and errors to help
with debugging and analysis.
 Here's an example code for the HelloWWW and ThreeParams
servlets
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWWW extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>HelloWWW</title></head>");
out.println("<body><h1>Hello WWW</h1></body>");
out.println("</html>");
out.close();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ThreeParams extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>ThreeParams</title></head>");
out.println("<body>"); out.println("<h1>Parameter values:</h1>");
out.println("<ul>"); out.println("<li>param1: " +
request.getParameter("param1") + "</li>");
out.println("<li>param2: " + request.getParameter("param2")
+ "</li>"); out.println("<li>param3: " +
request.getParameter("param3") + "</li>");
out.println("</ul>"); out.println("</body>");
out.println("</html>"); out.close();
}
}
And here's an example HTML form that submits a POST request
to the ThreeParams servlet:

<form action="/servlet/ThreeParams" method="post">


First Parameter: <input type="text" name="param1"><br>
Second Parameter: <input type="text" name="param2"><br>
Third Parameter: <input type="text" name="param3"><br>
<input type="submit" value="Submit">
</form>

Note that the ACTION attribute of the FORM tag should match
the URL pattern used to map the ThreeParams servlet
HTML choices can be used in conjunction with servlets to create
dynamic web applications that respond to user input. Here's an
example of how you can use HTML choices with a servlet:
<form action="colorServlet" method="post">
<label for="color">Select a color:</label>
<select name="color" id="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
<button type="submit">Submit</button>
</form>
On the server-side, you can create a servlet that handles the HTTP
POST request and retrieves the selected color value from the request
parameters. Here's an example of how you can do this in Java:

@WebServlet("/colorServlet")
public class ColorServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
String color = request.getParameter("color");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("You selected the color: " + color);
out.println("</body></html>");
}
}
If the user selects "Red" from the dropdown list and submits the
form, the servlet will send the following HTML response:

<html><body>
You selected the color: red
</body></html>
Servlets can be used to facilitate communication between clients
and servers in a web application. Here's an example of how this can
be done:
1. The client sends a request to the server using an HTTP protocol.

2. The server receives the request and uses a servlet to process it.
The servlet can retrieve data from a database, perform some
computation, or generate a dynamic web page based on the
request.
3. The servlet sends a response back to the client using an HTTP
protocol.
4. The client receives the response and processes it. For example,
if the response is an HTML page, the client can render the page
in a web browser.
1. Here's an example of how you can use servlets to implement
client/server communication in a web application:
Create an HTML form that allows the user to input a number:

<form action="calculateServlet" method="post">


<label for="number">Enter a number:</label>
<input type="text" name="number" id="number">
<button type="submit">Calculate</button>
</form>
2. Create a servlet that retrieves the number from the request
parameters, performs some computation, and sends the result back
to the client:
@WebServlet("/calculateServlet")
public class CalculateServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
String numberString = request.getParameter("number");
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
String numberString = request.getParameter("number");
int number = Integer.parseInt(numberString);
int result = number * 2;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("The result is: " + result);
out.println("</body></html>");
}
}
3. Deploy the HTML form and the servlet on a web server such
as Apache Tomcat. When the user submits the form, the
browser sends an HTTP POST request to the server with the
input number as a parameter.
4. The servlet retrieves the number from the request
parameters, performs the computation (in this case,
multiplying the number by 2), and sends an HTML response
back to the client that displays the result.
5. The client receives the HTML response and renders it in the
web browser, displaying the result of the computation.
If the user enters "5" in the input field and submits the
form, the servlet will send the following HTML response:

<html><body>
The result is: 10
</body></html>

This is because the servlet multiplies the input number by


2 and displays the result in the HTML response. So if the
input number is 5, the result will be 10.

You might also like