Http response (response)

Http response (response)

1. HTTP response message
(1) The http protocol is divided into two parts
1. Request message: data sent by the client to the server
2. Response message: data sent by the server to the client.
After setting the response message, the browser will automatically Analyze response message content
3, data format
response line, response header, response blank line, response body
4, response string format:
①, response line
composition: protocol + version + response status code
such as: HTTP/1.1 (version) 404, 200 (status code)
②, response header
For example:
Content-Type: text/html;charset=utf-8 response content type (html type) and encoding format (utf-8)
Content-Language: en
Content-Length: 1085 The length of the response content
Date: Thu, 30 Apr 2020 05:28:53 GMT Greenwich time
③, the response is
blank
④, the response body: the data sent in the response
For example: the
Insert picture description here
response body is the html content displayed on the page The
response header is how the page is Displayed, displayed format
(2) The response status code of the response line
Open the homepage of Firefox, right-click to check, click Network, view the original header
1 of the response header , the response line style:
HTTP/1.1 200 OK
Composition: protocol/version number status code status code description
2, response status code: the
server tells the browser a status of the response to this request. A coded number is used to indicate that the status codes are all three-dimensional numbers.
Classification:
1**: The server receives the client's message, but the reception is not completed. The server waits for a period of time and sends a status code
1 to confirm whether it needs to continue to send messages. 2 : On behalf of success, the response to this request is successful. For example: 200
3**: Represents redirection (302).
Redirection: a. Ask Zhang San to borrow money. Zhang San said that I have no money. You go to Li Si, Li Si has money. a Very obedient, go to Li Si to borrow money. This process is called redirection.
Representing cache (304): When the client accesses the browser, if the server resource does not change frequently, the resource is directly stored in the browser after the response, so when the client visits again, The server responded with a 304, telling the client not to take the data from me, but from my own home. If the resource changes later, it will be refreshed automatically and no longer allow access to the cache.
4**: Client error
Representative: 404: The request path does not have a corresponding resource.
Representative: 405: The request method does not find the corresponding request method. For
example, we build a servlet, delete the doGet method, and then use the get request.
5**: Server-side error
Represents: 500: An exception
occurred inside the server For example: we write an int i=3/0 in the doget method; the
server code is wrong
(3) Response header
1. Format:
header name: value
such as
Insert picture description here
2. The meaning of common response headers.
Content-Type: The server tells the client the data format and encoding format of the response body. The browser will automatically analyze the data according to the response value.
For example: view the encoding format of the jsp page contentType="text/html; charset=utf-8"
after modification, view the different encoding effects in the IE browser
Content-Length: the number of bytes in the response body
Date: Date
Connection: close: The connection is interrupted, the server is lost, and it has nothing to do with the website.
Second, Response object
Function: Set the response message
(1) Set the response line
Format: protocol/version number status code
Set status code: setStatus(int sc);
Insert picture description here
(two ) Set the response header
setHeader(String name, String value)
(3) Set the response body Set
by stream.
Steps:
1. Obtain the output stream.
Character output stream PrintWriter getWriter()
Insert picture description here
Byte output stream ServletOutputStream getOutputStream0
Insert picture description here
2. Use the output stream. Output the data to the client browser.

Guess you like

Origin blog.csdn.net/tan1024/article/details/111643394
Recommended