Middle
How is HTTP request processing implemented in the HttpServlet class?
sobes.tech AI
Answer from AI
Processing an HTTP request in the HttpServlet class is done by overriding one of the methods doGet, doPost, doPut, doDelete, doHead, doOptions, doTrace, or service.
Request handling lifecycle:
- The servlet container (e.g., Tomcat) receives an HTTP request.
- The container determines the appropriate servlet based on the request URL.
- If the servlet is not yet loaded, the container creates an instance, calls the
init()method (once during the servlet's lifetime), and places it in the pool. - The container creates
HttpServletRequestandHttpServletResponseobjects, encapsulating request data and providing methods for forming a response. - The container calls the servlet's
service(HttpServletRequest req, HttpServletResponse resp)method. - By default,
servicedetermines the HTTP method type (GET, POST, etc.) and delegates processing to the correspondingdo<Method>method (doGet,doPost, etc.), passing the request and response objects. - The developer overrides the needed
do<Method>method to implement specific request processing logic. - The
do<Method>method forms the response by writing data toHttpServletResponse. - After the
servicemethod completes, the container sends the formed response to the client. - When the container decides to unload the servlet (e.g., on application shutdown), it calls the
destroy()method to release resources.
Example of overriding doGet:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set content type and encoding
response.setContentType("text/html;charset=UTF-8");
// Get PrintWriter to write response
try (PrintWriter out = response.getWriter()) {
// Write HTML markup
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Greeting</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello from my servlet!</h1>");
out.println("</body>");
out.println("</html>");
}
}
}
Main do<Method> methods and their purpose:
| Method | Purpose |
|---|---|
doGet |
Handles GET requests (retrieving data, idempotent operations). |
doPost |
Handles POST requests (sending data for processing, creating resources). |
doPut |
Handles PUT requests (updating resources). |
doDelete |
Handles DELETE requests (deleting resources). |
doHead |
Handles HEAD requests (retrieving only response headers). |
doOptions |
Handles OPTIONS requests (getting information about available methods). |
doTrace |
Handles TRACE requests (debugging, request echo). |
Overriding the service method allows complete control over request dispatching logic, but typically specific do<Method> methods are overridden.