Detailed explanation of Servlet, HTTP and Request

Request response understanding diagram

Servlet

  1. Concept (explained earlier)

  2. Steps (explained earlier)

  3. Execution principle (explained earlier)

  4. Life cycle (explained earlier)

  5. Servlet3.0 annotation configuration (explained earlier)

  6. Architecture of Servlet
    Servlet - Interface
    |
    GenericServlet – abstract class
    |
    HttpServlet - abstract class

    • GenericServlet: the default null implementation is made for other methods in the Servlet interface, and only the service() method is used as an abstraction

      • When defining Servlet classes in the future, you can inherit GenericServlet and implement the service() method
    • HttpServlet: an encapsulation of http protocol to simplify operation

      1. Define class inheritance HttpServlet
      2. Duplicate doGet/doPost methods
  7. Servlet related configuration

    1. urlpartten:Servlet access path
      1. A Servlet can define multiple access paths: @ WebServlet({"/d4","/dd4","/ddd4"})
      2. Path definition rules:
        1. /xxx: path matching
        2. /xxx/xxx: multi-layer path, directory structure
        3. *. do: extension matching

HTTP

concept

Hyper Text Transfer Protocol
*Transmission protocol: defines the format of sending data when the client and server communicate

characteristic:

	1. be based on TCP/IP Advanced protocol
	2. Default port number:80
	3. Request based/Response model:One request corresponds to one response
	4. Stateless: each request is independent of each other and cannot interact with data

* Historical version:
	* 1.0: Each request response establishes a new connection
	* 1.1: Multiplex connection

Request message data format

1. Request line

	Request mode request url Request protocol/edition
	GET /login.html	HTTP/1.1

	* Request method:
		* HTTP There are 7 request modes in the protocol, and 2 are commonly used
			* GET: 
				1. Request parameters are in the request line, in url After.
				2. Requested url Length limited
				3. Not very safe
			* POST: 
				1. The request parameter is in the request body
				2. Requested url Unlimited length
				3. Relative safety

2. Request header

The client browser tells the server some information
Request header name: request header value
*Common request headers:
1. User agent: the browser tells the server that I can access the version information of the browser you use
*The header information can be obtained on the server side to solve the compatibility problem of the browser
2. Referer: http://localhost/login.html
*Tell the server where I (current request) come from?
*Function:
1. Anti theft chain:
2. Statistical work:

3. Request blank line

	Blank lines are used for segmentation POST The request header of the request, and the of the request body.

4. Request body (text):

	* encapsulation POST Of the request parameters of the request message

* String format:
	POST /login.html	HTTP/1.1
	Host: localhost
	User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
	Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
	Accept-Encoding: gzip, deflate
	Referer: http://localhost/login.html
	Connection: keep-alive
	Upgrade-Insecure-Requests: 1
	
	username=zhangsan

Request

1. Principle of request object and response object

1. request and response The object is created by the server. Let's use them
2. request Object is to get the request message, response Object to set the response message

2. request object inheritance architecture

ServletRequest		--	Interface
	|	inherit
HttpServletRequest	-- Interface
	|	realization
org.apache.catalina.connector.RequestFacade class(tomcat)

3. request function

1. Get request message data

	1. Get request line data
		* GET /day14/demo1?name=zhangsan HTTP/1.1
		* method:
			1. Get request method: GET
				* String getMethod()  
			2. (*)Get virtual directory:/day14
				* String getContextPath()
			3. obtain Servlet route: /demo1
				* String getServletPath()
			4. obtain get Mode request parameters: name=zhangsan
				* String getQueryString()
			5. (*)Get request URI: /day14/demo1
				* String getRequestURI():		/day14/demo1
				* StringBuffer getRequestURL()  :http://localhost/day14/demo1

				* URL:Uniform resource locator: http://localhost/day14/demo1 	 The People's Republic of China
				* URI: Uniform resource identifier : /day14/demo1					republic
			
			6. Obtain agreement and version: HTTP/1.1
				* String getProtocol()

			7. Gets the name of the client IP Address:
				* String getRemoteAddr()

2. Get request header data

		* method:
			* (*)String getHeader(String name):Get the value of the request header by the name of the request header
			* Enumeration<String> getHeaderNames():Get all request header names

3. Obtain request body data

		* Request body: only POST The request method has a request body, which is encapsulated in the request body POST Request parameters of the request
		* Steps:
			1. Get stream object
				*  BufferedReader getReader(): Gets the character input stream. Only character data can be manipulated
				*  ServletInputStream getInputStream(): Get byte input stream, which can operate on all types of data
					* Explain after uploading knowledge points

			2. Then get the data from the stream object
2. Other functions:
	1.  Regardless get still post The following methods can be used to obtain request parameters in both request methods
		1. String getParameter(String name):Get parameter value according to parameter name    username=zs&password=123
		2. String[] getParameterValues(String name):Gets an array of parameter values based on parameter names  hobby=xx&hobby=game
		3. Enumeration<String> getParameterNames():Gets the parameter names of all requests
		4. Map<String,String[]> getParameterMap():Get all parameters map aggregate

Chinese garbled code problem

* get Method: tomcat 8 Already get The problem of garbled code has been solved
			* post Method: garbled code
				* Solution: before getting parameters, set request Coding of request.setCharacterEncoding("utf-8"); 

Request forwarding

concept

A resource jump mode inside the server

step

  1. Get the request forwarder object through the request object: RequestDispatcher getRequestDispatcher(String path)
  2. Use the RequestDispatcher object to forward: forward (ServletRequest, servletresponse, response)

characteristic

  1. The browser address bar path does not change
  2. It can only be forwarded to the internal resources of the current server.
  3. Forwarding is a request

shared data

* Domain object: an object with scope that can share data within the scope
		* request Domain: represents the scope of a request. It is generally used to share data among multiple resources requesting forwarding
		* method:
			1. void setAttribute(String name,Object obj):Store data
			2. Object getAttitude(String name):Get value by key
			3. void removeAttribute(String name):Remove key value pairs with key
		* Note: This is placed in forward Method, otherwise the response will be too fast to obtain information!

Keywords: server Network Protocol http

Added by donnierivera on Fri, 31 Dec 2021 22:49:22 +0200