8.A] Explain how to handle HTTP request and response with an example
Answer:
In Java servlets, handling HTTP requests and responses involves overriding methods from the HttpServlet
class. The most common methods are doGet()
and doPost()
, which correspond to HTTP GET and POST requests, respectively. Here’s a breakdown of how to handle these requests and responses with examples.
Common HTTP Methods
- GET Method
- Purpose: Retrieves data from the server.
- Usage: Often used for requesting data or resources.
- Characteristics: Parameters are sent in the URL.
- Example URL:
http://example.com/servlet?param1=value1¶m2=value2
2. POST Method
- Purpose: Submits data to be processed by the server.
- Usage: Often used for form submissions and data modifications.
- Characteristics: Parameters are sent in the body of the request, not in the URL.
Example: Handling HTTP Requests and Responses
1. HTML Form for GET and POST Requests
<!DOCTYPE html> <html> <head> <title>Form Example</title> </head> <body> <!-- Form to handle GET requests --> <h2>GET Request</h2> <form method="get" action="getServlet"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> <!-- Form to handle POST requests --> <h2>POST Request</h2> <form method="post" action="postServlet"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> </body> </html>
2. Handling GET Requests in a Servlet
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set content type response.setContentType("text/html"); // Get the PrintWriter to write the response PrintWriter out = response.getWriter(); // Retrieve parameters from the request String name = request.getParameter("name"); // Generate response out.println("<html><body>"); out.println("<h2>GET Request</h2>"); out.println("<p>Hello, " + name + "!</p>"); out.println("</body></html>"); // Close the PrintWriter out.close(); } }
3. Handling POST Requests in a Servlet
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; public class PostServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set content type response.setContentType("text/html"); // Get the PrintWriter to write the response PrintWriter out = response.getWriter(); // Retrieve parameters from the request String name = request.getParameter("name"); // Generate response out.println("<html><body>"); out.println("<h2>POST Request</h2>"); out.println("<p>Hello, " + name + "!</p>"); out.println("</body></html>"); // Close the PrintWriter out.close(); } }
Explanation
- HTML Form:
- GET Form: Submits data using the GET method, appending parameters to the URL.
- POST Form: Submits data using the POST method, including parameters in the request body.
2. Servlets:
doGet()
Method: Handles GET requests. Retrieves parameters from the URL and generates a response.doPost()
Method: Handles POST requests. Retrieves parameters from the request body and generates a response.
Differences Between GET and POST Requests:
Feature | GET Request | POST Request |
---|---|---|
Data Visibility | Data is visible in the URL query string. Suitable for non-sensitive data. | Data is included in the request body. Suitable for sensitive data and larger amounts of data. |
Data Length | Limited by URL length restrictions (typically around 2048 characters). | No practical limits on data size. |
Idempotency | Idempotent; multiple identical GET requests should have the same effect as a single request. | Not necessarily idempotent; each request can have a different effect. |
Caching | Requests can be cached by browsers and servers. | Requests are generally not cached. |