HTTP Protocol (A Brief Talk)

what is HTTP

HyperText Transfer Protocol: It is an application layer protocol specified by w3c (World Wide Web Consortium) to define how the browser and the web server communicate and the data format of the communication.

How HTTP communicates

1. Establish a connection;

2. Pack and send the request;

3. Pack and send the response;

4. Close the connection

Note: One connection at a time is requested, and the web server can serve as many clients as possible with limited connections.

HTTP data format

Request data:

1. Request line: request method + request resource path + version protocol

2. Message header (several): The message header is some key-value pairs, generally defined by w3c. The two sides of the communication convey some specific meanings through the message header. For example, the browser can send the User-Agent message header to tell the Web server the type and version of the browser. Most of them are automatically generated, and sometimes you need to add message headers yourself.

3. Entity content: Only when the request method is post, the entity content will have data. (i.e. request parameters)


Response data:

1. Status line: protocol type + version + status code + status description

2. Message line (several): The web server returns some message headers to the browser. For example, the Context-Type header is returned, which tells the browser the data type and character set returned by the server.

3. Entity content: the result of program processing

Note: In the development of HTTP, the steps (step 4) of the communication between the browser and the server have been implemented. The request data packaging is implemented by the browser, and the response data packaging is implemented by the server; only the content of the request data and the content of the corresponding data need to be implemented by the developer. supply.

How Servlet Handles the Http Protocol

How to control communication data

When the web container receives an HTTP request, the communication data is encapsulated and provided by the web container, and this information is interpreted as two objects

· Corresponding to the request data is an object of type HttpServletRequest

Corresponding to the corresponding data is an object of type HttpServletResponse

protected void service(HttpServletRequest req,HttpServletResponse res){}
HttpServletRequest object

Represents the client's request. When the client accesses the server through the HTTP protocol, all the messages in the request are encapsulated in this object, and the request data can be obtained through the related methods of this object;

Read and write HTTP request data (request lines, message headers, etc.)

   // message header
   //The data in the message header is stored according to the key-value pair, which is equivalent to the map can be traversed
    Enumeration<String>e = req.getHeaderNames();
    while(e.hasMoreElements()){
	    String key = e.nextElement();
	    String value = req.getHeader(key);
	    System.out.println(key+":"+value);
    }

· Get ​​and set cookies

//The first parameter is the name of the cookie, the second parameter is the value of the cookie
//If the value is Chinese, use URLEncoder.encode("","") to encode: its first parameter is value, and the second parameter is: encoding method
Cookie cookie = new Cookie(“”,“”);

· Get ​​route information

System.out.println("Servlet path (that is, servlet screen name)"+req.getServletPath());

Indicates an HTTP session

System.out.println("Protocol type: "+req.getProtocol());

· Request type

System.out.println("Request type: "+req.getMethod());

· and other APIs

protected void service(HttpServletRequest req,HttpServletResponse res){
		//title
		System.out.println(req.getContextPath());
		//Servlet screen name
		System.out.println(req.getServletPath());
		//absolute path
		System.out.println(req.getRequestURI());
		//Complete route
		System.out.println(req.getRequestURL());
	}

· Implement request forwarding

HttpServletResponse对象

Represents the response provided to the client and encapsulates the HTTP response data. Through this object, the status line, message header, and entity content can be set.

· Set the output content to the client

// set entity content
	PrintWriter out = res.getWriter();
	out.println("<p>Hello</p>");
	out.close();

· Set the status code of the response (to solve the problem of garbled characters)

res.setContentType("text/html");

· Set the decoding method of the browser (to solve the problem of garbled characters)

res.setContentType("text/html;charset=utf-8");

· Setting cookies

· Implement redirection

res.sendRedirect(String url)

Request method:

Why distinguish the request method

The request method is the intention statement of the client side when it talks to the server, and it is the key to distinguish the types of requests; different request methods are not only different in data transmission, but also in form submission and server processing. Different types of request methods will also cause browsers to use different caching methods to process subsequent requests, thereby improving response speed.

Type of request

GET: request the specified resource

POST: Submit the data to be processed to the specified resource

HEAD: The request response is the same as the corresponding GET, but without a response body

PUT: upload the specified resource

DELETE: delete the specified resource

GET request method

The method used when it is necessary to request a specific resource from the server; it should not be used in operations that cause side effects (a common misuse in web applications when using it to submit requests)

· When the browser sends a GET request

    - Enter an address in the address bar

    - click the link

    - Form submit by default

· Features

    - The request data will be added to the back of the request resource path (that is, using the path to pass parameters), so only a small amount of data can be submitted to the web server;

    - The request parameters are displayed on the browser address bar, which is not safe;

POST request method

· Submit the data to be processed to the server. These data are written in the content of the request, which can lead to the generation of new resources and the update of existing resources.

· When the browser sends a POST request

    - Set the form method attribute to POST

· Features

    - The request parameters are added to the entity content (that is, using the entity to pass parameters), which can submit a large amount of data;

    - The request parameters will not be displayed in the browser address bar, which is relatively safe;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325944331&siteId=291194637
Recommended