HttpServlet
and
HttpServletRequest Interface
-
HTTP Protocol
-
Overview of HttpServlet
-
Overview of HttpRequest Interface
-
Some Insights and Thoughts
1. HTTP Protocol
What is HTTP?
The HTTP protocol, short for Hyper Text Transfer Protocol, is a protocol used for transferring hypertext from a server (WWW: World Wide Web) to a local browser. The HTTP protocol operates on a B/S architecture, where the browser acts as an HTTP client that sends requests (Request) to the HTTP server, which is the WEB server. The web server sends response information (Response) back to the client based on the received request data.
2. Characteristics of HTTP Protocol
Stateless: Each request is independent, and the server does not retain any session information. This means that each request needs to be re-validated.Flexibility: HTTP supports various data formats, including text, images, audio, and video, making it suitable for various application scenarios.Simplicity: The format of HTTP requests and responses is simple and easy to understand, facilitating development and debugging.
3. The lifecycle of an HTTP request from the client to the server:
1. The client sends an HTTP request ↓ 2. The web container (Tomcat) parses the raw message ↓ 3. Creates an HttpServletRequest object to encapsulate the data ↓ 4. Calls the corresponding method of HttpServlet (e.g., doGet()) ↓ 5. Processes business logic and generates a response ↓ 6. The container converts the response into an HTTP message and returns it
2. Overview of HttpServlet1. What is HttpServlet?HttpServlet is an abstract class in the Java Servlet API specifically designed to handle HTTP requests. It extends the GenericServlet class and provides a foundational framework for developing web applications based on the HTTP protocol.2. Main Features:
– Inherits from GenericServlet, optimized for the HTTP protocol – Provides a dispatch mechanism for the service() method– Includes methods like doGet() and doPost() to handle different types of HTTP requests– Serves as the foundation for most Java web applications3. Lifecycle of HttpServlet
1. Initialization phase (init()) – Executed once when the Servlet is first loaded – Can be configured with load-on-startup to load the Servlet with the container 2. Request handling phase (service()→doGet()/doPost()) – Can be executed multiple times, with each request being an independent process – Thread safety issue: Avoid using instance variables to store request-related data 3. Destruction phase (destroy()) – Called when the server shuts down or the Servlet is removed – Used to release resources (e.g., database connections, thread pools, etc.)
4. The process of Servlet handling HTTP requests
1. When the Servlet container receives an HTTP request from the client, it creates an HttpServletRequest object and an HttpServletResponse object for that request.2. The container passes the HttpServletRequest object and the HttpServletResponse object as parameters to the service() method and calls that method.3. In the service() method, the Servlet retrieves client information and request-related information through the HttpServletRequest object.4. Processes the HTTP request.5. After processing the request, encapsulates the response information into the HttpServletResponse object.6. The Servlet container returns the response information to the client.After the Servlet container returns the response information to the client, 7. The HttpServletRequest object and HttpServletResponse object are destroyed.
3. HttpServletRequest Interface
1. What is HttpServletRequest?
HttpServletRequest is an interface, and its parent interface is ServletRequest. It is an object that Tomcat converts and encapsulates the request message, passed in when Tomcat calls the service method. HttpServletRequest represents the request sent by the client, and all information in the request can be obtained through this object.
2. Main Functions:
– Retrieve basic information about the client request– Retrieve request parameters– Read request headers– Manage session state
3. Request Object
The “request scope” object is much smaller in scope than the “application scope” object and has a much shorter lifecycle. The request scope is only valid within a single request.
One request object corresponds to one request scope object. After a request ends, this request scope is destroyed.
Thoughts: How can two Servlets share data?
Data can be placed in the ServletContext application scope, but this scope is too large and consumes too many resources. It is not recommended.
Data can be placed in the request scope, and then AServlet can forward to BServlet, ensuring that AServlet and BServlet are in the same request, allowing two Servlets, or multiple Servlets, to share the same data.
4. Considerations
1. Thread safety: The HttpServletRequest object is only valid in the current request thread2. Character encoding: Set request.setCharacterEncoding(“UTF-8”) before processing POST requests3. Input streams: getInputStream() and getParameter() cannot be used interchangeably
4. Insights and Thoughts
HttpServlet is the core abstract class for handling HTTP requests in Java web development, providing a bridge for interaction with the HTTP protocol. By extending HttpServlet and overriding its doGet, doPost, and other methods, developers can write corresponding business logic handling code based on different HTTP request methods. This design pattern results in clear and maintainable code structure. During the learning process, deeply understanding the execution flow of these methods and how to retrieve request parameters, header information, and other data from HttpServletRequest is key to mastering the fundamentals of web development.