HTTP protocol
1. Request message: the data sent by the client to the server. Data
format:
(1) Request line
(2) Request header
(3) Request blank line
(4) Request body
2. Response message:
Data format sent by the server to the client :
(1) Response line
Composition: protocol/version response status code status code description
Response status code: the server tells the client browser that this request and a response
status code is 3 digits
1xx: the server receives the client message, But the reception has not been completed. After waiting for a period of time, the 1xx multiple status code
2xx: success is sent . Representative: 200
3xx: Redirect. Representative: 302 (resource redirection) 304 (access cache)
4xx: client error. Representative: 404 (the request path does not have a corresponding resource) 405 (the request method does not correspond to the doXxx method)
5xx: server-side error. Representative: 500 (an exception occurred inside the server)
(2) Response header
format: header name: value
Common response headers:
Content-Type: The server tells the client the data format and encoding format of the response body
Content-disposition: The server tells the client to What format to open the response body data
Value: in-line default value, open
attachment on the current page ; filename=xxx Open the response body as an attachment. Download Document
(3) Response blank line
(4) Response body: transmitted data
响应字符串格式:
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 101
Date: Wed, 06 Jun 2018 07:08:42 GMT
<html>
<head>
<title>$Title$</title>
</head>
<body>
hello , response
</body>
</html>
Response object
1. Set the response line
Format: HTTP/1.1 200 ok
Set the status code: setStatus(int sc)
2. Set the response header: setHeader(String name, String value)
3. Set the response body:
-
Get output stream
Character output stream:PrintWriter getWriter()
Byte output stream:ServletOutputStream getOutputStream()
-
Use the output stream to output data to the client browser