Java Web request and response principle -2

Insert picture description here

Request and Response principles

First look at the code

@WebServlet("/demo")
public class ServletDemo implements Servlet{
	@Override 
	public void service(ServletRequest request,ServletResponse response){
		System.out.println("Servlet 3.0...问世");
	}
}

This is a simple Servlet code ,

When accessed through a browser, this sentence in the service method will be printed on the console http://localhost:8080/demo

Implementation principle

  1. The tomcat server will create a corresponding ServletDemo object according to the resource path of the url in the request
  2. tomcat server, willcreaterequest and response objects, the request object encapsulates the request message data
  3. tomcat will request and response two objectstransferTo the service method, and call the service method
  4. Within the method, you can get the request message data through the request object, and the response object sets the response message data.
  5. Before the server responds to the browser, it will take the response message data set by the programmer from the response object

note

  • The request and response objects are created by the server, we are here to use
  • The request object is to get the request message, the response object is to set the response message

Insert picture description here
The picture above is some parameters and attributes sent by the browser to the server ...

Inheritance architecture of Request and Response

  1. When we print the request object, what appears isorg.apache.catalina.RequestFacade@74cb396c
@WebServlet("/demo")
public class ServletDemo implements Servlet{
	@Override 
	public void service(ServletRequest request,ServletResponse response){
		System.out.println(request);
	}
}
  1. tomcat implements the RequestFacade class provided by apache and creates its object (this is the Tomcat source package)

Insert picture description here

  1. ServletRequest is an interface, which is inherited by the HttpServletRequest interface, and our core class RequestFacade implements HttpServletRequest
Published 24 original articles · praised 33 · visits 2391

Guess you like

Origin blog.csdn.net/weixin_41241629/article/details/104609611