Chapter 8 Web Programming
Chapter 8 Web Programming
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:
@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>