Javaweb Study Summary (iv)--HTTP agreement

Source: Internet
Author: User

Javaweb Study Summary (iv)--HTTP Protocol I. What is the HTTP protocol

HTTP is shorthand for the Hypertext Transfer Protocol (Hypertext Transfer Protocol), an application-layer protocol to the TCP/IP protocol that defines the process of exchanging data between a Web browser and a Web server. After the client connects to the Web server, if you want to obtain a Web resource in the Web server, you need to follow a certain communication format, the HTTP protocol is used to define the format of the client and Web server communication.

Ii. version of the HTTP protocol

Version of the HTTP protocol: http/1.0, http/1.1

Iii. the difference between HTTP1.0 and HTTP1.1

In the HTTP1.0 protocol, after a client has established a connection with a Web server, only one Web resource can be obtained.
In the HTTP1.1 protocol, when a client connects to a Web server, it obtains multiple Web resources on one connection.

Iv. HTTP Request 4.1, HTTP request includes content

  after the client connects to the server, it requests a Web resource from the server, which is called the client sends an HTTP request to the server .

A complete HTTP request includes the following: a request line, several message headers, and entity content
Example:

  

4.2. Details of HTTP request--Request Line

The get in the request line is called the request method: POST,, OPTIONS, DELETE, TRACE, PUT, commonly used are: GET, POST
If the user is not set, by default the browser sends a GET request to the server, for example, in the browser direct output address access, point hyperlink access, etc. are get, the user wants to change the request mode to post, can be implemented by changing the form's submission method.
Regardless of whether post or get is used to request a Web resource to the server, the difference between the two approaches is mainly in the data delivery: If the request is a Get mode, you can bring the data to the server in the form of the requested URL address, separating multiple data with &. Example: Get/mail/1.html?name=abc&password=xyz http/1.1
The feature of Get mode is that the parameters accompanying the URL address are limited and the data capacity is usually not more than 1K.
If the request is in the form of post, you can send data to the server in the requested entity content, with the characteristics of post: Unlimited amount of data transferred.

4.3. Details of HTTP request--Message header

Common message headers in HTTP requests

Accept: The browser tells the server through this header which data types it supports
Accept-charset: The browser tells the server through this header which character set it supports
Accept-encoding: Browser Through this header to tell the server, the supported compression format
Accept-language: The browser tells the server through this header that its language environment
Host: The browser tells the server through this header which host to access
If-modified-since: The browser tells the server through this header that the time to cache the data
Referer: The browser through this header to tell the server, the client is which page to the anti-theft chain
Connection: Browser Through this header to tell the server, after the request is broken link or how to hold the link

For example:

1 accept:application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, Image/pjpeg, 2     application/ X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*3 referer:http:// LOCALHOST:8080/JAVAWEBDEMOPROJECT/WEB/2.JSP4 Accept-language:zh-cn5 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) 6 Accept-encoding:gzip, Deflate7 host:localhost:80808 connection:keep-alive
V. HTTP response 5.1, content included in HTTP response

  an HTTP response represents the data that the server sends back to the client , including a status line, several message headers, and entity content.

  
Example:

1 http/1.1 OK 2 server:apache-coyote/1.1 3 content-type:text/html;charset=iso-8859-1 4 content-length:105 5 date:t UE, 16:23:28 GMT 6  7 
5.2. HTTP response details--Status line

Status line format: http version number status code reason description <CRLF>
Example: http/1.1 OK
The status code is used to represent the result of the server's processing of the request, which is a three-bit decimal number. The response status code is divided into 5 classes, as follows:
  

5.3, HTTP response details--Common response header

Common response headers in HTTP responses (message headers)
Location: The server goes through this header to tell the browser where to jump
Server: Servers through this header, tell the browser server model
Content-encoding: The server through this header, tells the browser, the data compression format
Content-length: The server passes this header and tells the browser the length of the loopback data
Content-language: The server uses this header to tell the browser the locale
Content-type: The server passes this header and tells the browser the type of loopback data
Refresh: The server passes this header and tells the browser to refresh periodically
Content-disposition: The server through this header, tell the browser to download the way to hit the data
Transfer-encoding: The server uses this header to tell the browser that the data is being echoed in chunks
Expires:-1 Control browser do not cache
Cache-control:no-cache
Pragma:no-cache

Six, set the response header on the server to control the behavior of the client browser 6.1, set the location response header, to achieve request redirection
1 package gacl.http.study; 2 Import java.io.IOException; 3 Import javax.servlet.ServletException; 4 Import Javax.servlet.http.HttpServlet; 5 Import Javax.servlet.http.HttpServletRequest; 6 Import Javax.servlet.http.HttpServletResponse; 7/** 8  * @author gacl 9  *10  */11 public class ServletDemo01 extends HttpServlet {All public     void Doget (HTT Pservletrequest request, HttpServletResponse response)             throws Servletexception, IOException         Response.setstatus (302);//Set the response status code for the server         /**17          * Set the response header, the server through the location of this header, to tell the browser where to jump, this is called the request redirect         Response.setheader ("Location", "/javaweb_httpprotocol_study_20140528/1.jsp"),}21 public     void DoPost (HttpServletRequest request, httpservletresponse response)             throws Servletexception, IOException {23         This.doget (request, response);     25}

When you use the URL address "http://localhost:8080/JavaWeb_HttpProtocol_Study_20140528/servlet/ServletDemo01" in the browser 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 server returns a 302 status code to tell the browser that you want the resources I have not, but I pass the location response header to tell you where there is, and the browser parses the response header location after knowing to jump to/javaweb_httpprotocol_study_ 20140528/1.jsp page, so it will automatically jump to 1.jsp, as shown in:

  

6.2, set the content-encoding response header, tell the browser data compression format
 1 package gacl.http.study; 2 3 Import Java.io.ByteArrayOutputStream; 4 Import java.io.IOException; 5 Import Java.util.zip.GZIPOutputStream; 6 Import javax.servlet.ServletException; 7 Import Javax.servlet.http.HttpServlet; 8 Import Javax.servlet.http.HttpServletRequest; 9 Import javax.servlet.http.httpservletresponse;10/**11 * @author gacl12 * This applet is used to demonstrate the following two small knowledge points 13 * 1, using the Gzipoutputstream stream     To compress the data 14 * 2, set the response header content-encoding to tell the browser, the server sends back the data compressed after the format */16 public class ServletDemo02 extends HttpServlet {17 18 public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexce                 ption {String data = "Abcdabcdabcdabcdabcdabcdab" +21 "CDABCDABCDABCDABCDABCDABCDABCDABC" +22                 "Dabcdabcdabcdabcdabcdabcdabcdabc" +23 "Dabcdabcdabcdabcdabcdabcdabcdabcdab" +24            "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" +25 "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" +26     "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" +27 "CDABCDABCDABCDABCDABCDABCDABCDABCDABCD"; . OUT.PRINTLN ("The size of the original data is:" + data.getbytes (). length); Bytearrayoutputstream bout = new bytearrayoutputs Tream (); Gzipoutputstream gout = new Gzipoutputstream (bout); Buffer32 Gout.write (Data.getbytes ()); Gout.close (); 34//Get Compressed data (bytes g[] = Bout.tob Ytearray (); Response.setheader ("Content-encoding", "gzip"), PNS Response.setheader ("Content-length", G.leng th + ""); Response.getoutputstream (). write (g);}40-public void DoPost (HttpServletRequest request, Ht Tpservletresponse response) throws Servletexception, IOException {this.doget (request, response); 4 4}45}

The server sends a response message to the browser as follows:

The compression formats supported by the browser are:

6.3. Set the Content-type response header to specify the loopback data type
 1 package gacl.http.study; 2 Import java.io.IOException; 3 Import Java.io.InputStream; 4 Import Java.io.OutputStream; 5 Import javax.servlet.ServletException; 6 Import Javax.servlet.http.HttpServlet; 7 Import Javax.servlet.http.HttpServletRequest; 8 Import Javax.servlet.http.HttpServletResponse;  9 public class ServletDemo03 extends HttpServlet {ten public void doget (HttpServletRequest request, HttpServletResponse          Response) throws Servletexception, IOException {12/**13 * The data types that the browser can receive (accept) are: 14 * application/x-ms-application, * image/jpeg, * application/xaml+xml, * image/gif, * Image/pjpeg, * application/x-ms-xbap, * application/vnd.ms-excel, * APPL Ication/vnd.ms-powerpoint, Application/msword, */24 response.setheader ("Content-type", " Image/jpeg ")///Use Content-type response header to specify that the data type sent to the browser is" Image/jpeg "25//Read W in the IMG folder located under the project root directoryP_20131005_002.jpg This picture, returns an input stream of inputstream in = This.getservletcontext (). getResourceAsStream ("/img/wp_20131005_0 02.jpg "); byte buffer[] = new byte[1024];28 int len = 0;29 OutputStream out = Response.getoutput Stream ();//Gets the output stream (len = in.read (buffer)) > 0) {//reads the contents of the input stream (in) stored in buffer (buffer) to Out.write ( Buffer, 0, Len);//The contents of the buffer are output to the browser}33}34 public void DoPost (HttpServletRequest request, HTTPSERVLETRESP Onse response) throws Servletexception, IOException {this.doget (request, response); 37}38}

The server sends a response message to the browser as follows:

The result of the ServletDemo03 operation is as follows:

A picture is displayed in the browser

6.4, set the refresh response header, let the browser refresh periodically
 1 package gacl.http.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax. Servlet.http.HttpServlet; 6 Import Javax.servlet.http.HttpServletRequest; 7 Import Javax.servlet.http.HttpServletResponse; 8 9 public class ServletDemo04 extends HttpServlet {ten public void doget (HttpServletRequest request, HTTPSERVLETRESPO          NSE response) throws Servletexception, IOException {12/**13 * Set Refresh response header to allow the browser to refresh periodically every 3 seconds 14 */15//Response.setheader ("Refresh", "3"); 16/**17 * Set Refresh response header to allow the browser to jump to http://www after 3 seconds. Baidu.com18 */19 response.setheader ("Refresh", "3;url= ' http://www.baidu.com '"); Response.getwri             ter (). Write ("GaCl");}22 doPost (httpservletrequest request, httpservletresponse response) 24 Throws Servletexception, IOException {this.doget (request, response);}27) 
6.5, set content-disposition response header, let the browser download files
 1 package gacl.http.study; 2 3 Import java.io.IOException; 4 Import Java.io.InputStream; 5 Import Java.io.OutputStream; 6 7 Import Javax.servlet.ServletException; 8 Import Javax.servlet.http.HttpServlet; 9 Import javax.servlet.http.httpservletrequest;10 Import javax.servlet.http.httpservletresponse;11 public class             SERVLETDEMO05 extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) 14 Throws Servletexception, IOException {15/**16 * Set Content-disposition response header, let browser download file 17 * /18 Response.setheader ("Content-disposition", "attachment;filename=xxx.jpg"); InputStream in = This.getS         Ervletcontext (). getResourceAsStream ("/img/1.jpg"); byte buffer[] = new byte[1024];21 int len = 0;22 OutputStream out = Response.getoutputstream (); (len = in.read (buffer)) > 0) {OUT.WR ITE (buffer, 0, Len);}26}27-public void DoPost(HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {30 This.doget (request, response); 31}32 33}

Accessing ServletDemo05 in the browser pops up the file download box, as shown in:

  

Javaweb Study Summary (iv)--HTTP agreement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.