1. What is HTTP protocol
HTTP is the abbreviation of hypertext transfer protocol. It is an application-layer protocol of the TCP/IP protocol, which is used to define the process of exchanging data between the WEB browser and the WEB server. After the client connects to the web server, if you want to obtain a certain web resource in the web server, you must abide by a certain communication format. The HTTP protocol is used to define the format for the client to communicate with the web server.
2. HTTP protocol version
HTTP protocol version: HTTP/1.0, HTTP/1.1
3. The difference between HTTP1.0 and HTTP1.1
In the HTTP 1.0 protocol, after the client establishes a connection with the web server, only one web resource can be obtained.
In the HTTP 1.1 protocol, after the client establishes a connection with the web server, multiple web resources are obtained on one connection.
4. HTTP request
4.1. Contents included in HTTP requests
After the client connects to the server, it requests a certain web resource from the server, which is called the client sending an HTTP request to the server.
A complete HTTP request includes the following content: a request line, several message headers, and an example of entity content:
4.2. Details of HTTP requests - request line
The GET in the request line is called the request method. The request methods include: POST, GET, HEAD, OPTIONS, DELETE, TRACE, PUT, and commonly used ones include: GET, POST
If the user does not have settings, by default, the browser sends get requests to the server. For example, access is directly entered in the browser, access is clicked on hyperlink, etc., etc. are all gets. If the user wants to change the request method to post, he can change the form submission method.
Whether POST or GET is used to request a WEB resource from the server. The difference between these two methods is mainly reflected in data transmission: if the request method is GET, the data handed to the server can be brought in the form of a ? after the requested URL address, and multiple data are separated by &, for example: GET /mail/1.html?name=abc&password=xyz HTTP/1.1
Features of the GET method: The parameters attached to the URL address are limited, and the data capacity usually cannot exceed 1K.
If the request method is POST, data can be sent to the server in the requested entity content. The characteristics of the Post method are: the amount of data transmitted is unlimited.
4.3. Details of HTTP requests - message header
Common message headers in HTTP requests
accept: The browser tells the server through this header, and the data type it supports. Accept-Charset: The browser tells the server through this header, which character set it supports. Accept-Encoding: The browser tells the server through this header, and the supported compression format Accept-Language: The browser tells the server through this header, and its locale Host: The browser tells the server through this header, and which host to access. If-Modified-Since: The browser tells the server through this header, and the time to cache data. Referer: The browser tells the server through this header, and which page the client is from. Connection: The browser tells the server through this header, and whether to disconnect the link or the link after the request is completed.
For example:
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg,
application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost:8080/JavaWebDemoProject/Web/2.jsp
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)
Accept-Encoding: gzip, deflate
Host: localhost:8080
Connection: Keep-Alive
5. HTTP response
5.1. Contents included in HTTP response
An HTTP response represents data sent back by the server to the client, which includes: a status line, several message headers, and entity content.
example:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: text/html;charset=ISO-8859-1Content-Length: 105Date: Tue, 27 May 2014 16:23:28 GMT<html> <head> <title>Hello World JSP</title> </head> <body> Hello World! </body></html>
5.2. Details of HTTP response - Status line
Status line format: HTTP version number status code reason statement <CRLF>
Example: HTTP/1.1 200 OK
The status code is used to represent the server's processing result of the request. It is a three-digit decimal number. Response status codes are divided into 5 categories, as shown below:
5.3. HTTP response details - commonly used response headers
Common response headers (message headers) in HTTP responses
Location: The server uses this header to tell the browser where to jump to Server: The server uses this header to tell the browser the model of the server Content-Encoding: The server uses this header to tell the browser the compression format of the data Content-Length: The server uses this header to tell the browser the length of the data to be returned Content-Language: The server uses this header to tell the browser the locale to be returned Content-Type: The server uses this header to tell the browser the type of data to be returned Refresh: The server uses this header to tell the browser to refresh Content-Disposition: The server uses this header to tell the browser to write data in a downloaded manner Transfer-Encoding: The server uses this header to tell the browser to tell the browser the data to be returned in blocks Expires: -1 Control the browser not to cache Cache-Control: no-cache
Pragma: no-cache
6. Set response headers on the server to control the behavior of the client browser
6.1. Set the Location response header to realize request redirection
package gacl.http.study;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author gacl * */public class ServletDemo01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(302);//Set the server's response status code/** *Set the response header, and the server uses the Location header to tell the browser where to jump. This is the so-called request redirection*/ response.setHeader("Location", "/JavaWeb_HttpProtocol_Study_20140528/1.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}When you use the URL address "http://localhost:8080/JavaWeb_HttpProtocol_Study_20140528/servlet/ServletDemo01" to access ServletDemo01, you can see the status code and response header information sent to the browser after the server responds, as shown in the figure below:
The server returns a 302 status code to tell the browser that I don’t have the resources you want, but I tell you where it is through the Location response header. After the browser parses the response header, it knows that it wants to jump to the /JavaWeb_HttpProtocol_Study_20140528/1.jsp page, so it will automatically jump to 1.jsp, as shown in the figure below:
6.2. Set the Content-Encoding response header to tell the browser the compression format of the data
package gacl.http.study;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.zip.GZIPOutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author gacl *This applet is used to demonstrate the following two little knowledge points*1. Use GZIPOutputStream stream to compress data*2. Set the response header Content-Encoding to tell the browser that the compressed format of the data sent back by the server*/public class ServletDemo02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd "cdabcdabcdabcdabcdabcdabcdabcdabc" + "dabcdabcdabcdabcdabcdabcdabcdabc" + "dabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdabcd"; System.out.println("原始数据的大小为:" + data.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); //buffer gout.write(data.getBytes()); gout.close(); //get the compressed data byte g[] = bout.toByteArray(); response.setHeader("Content-Encoding", "gzip"); response.setHeader("Content-Length", g.length +""); response.getOutputStream().write(g); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}The response information sent by the server to the browser is as follows:
The compression formats supported by the browser are:
6.3. Set content-type response header and specify the loopback data type
package gacl.http.study;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletDemo03 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * The data types that the browser can receive (Accept) are: * application/x-ms-application, * image/jpeg, * application/xaml+xml, * image/gif, * image/pjpeg, * application/x-ms-xbap, * application/vnd.ms-excel, * application/vnd.ms-powerpoint, * application/msword, */ response.setHeader("content-type", "image/jpeg");//Use the content-type response header to specify the data type sent to the browser as "image/jpeg" //Read the image WP_20131005_002.jpg in the img folder located in the root directory of the project, and return an input stream InputStream in = this.getServletContext().getResourceAsStream("/img/WP_20131005_002.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream();//Get the output stream while ((len = in.read(buffer)) > 0) {//Read the contents in the input stream (in) and store them in the buffer (buffer) out.write(buffer, 0, len);//Output the contents in the buffer to the browser} } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}The response information sent by the server to the browser is as follows:
The running results of ServletDemo03 are shown in the figure below:
Images are displayed in the browser
6.4. Set the refresh response header to refresh the browser regularly
package gacl.http.study;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletDemo04 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * Set the refresh response header to let the browser refresh regularly every 3 seconds */ // response.setHeader("refresh", "3"); /** * Set the refresh response header to let the browser jump to http://www.baidu.com in 3 seconds */ response.setHeader("refresh", "3;url='http://www.baidu.com'"); response.getWriter().write("gacl"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }} 6.5. Set content-disposition response header to let the browser download the file
package gacl.http.study;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletDemo05 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * Set the content-disposition response header to let the browser download the file */ response.setHeader("content-disposition", "attachment;filename=xxx.jpg"); InputStream in = this.getServletContext().getResourceAsStream("/img/1.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}When accessing ServletDemo05 in the browser, the file download box will pop up, as shown in the following figure:
The above is all about this article, I hope it will be helpful to everyone's learning.