Javaweb HTTP protocol Detailed _java

Source: Internet
Author: User
Tags http request locale

First, what is the HTTP protocol

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

Version of the HTTP protocol

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

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

In the HTTP1.0 protocol, a client can only obtain one web resource after it has established a connection to the Web server.
After the HTTP1.1 protocol, which allows a client to connect to a Web server, gets more than one web resource on a connection.

Iv. HTTP Requests

4.1. Content included in HTTP request

After the client connects to the server, a Web resource is requested from the server, which is called a client sending an HTTP request to the server.

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

4.2, the HTTP request details--Request Line

The get in the request line is called the request method: Post,, OPTIONS, deletes, TRACE, put, commonly used: Got, post
If users do not have settings, by default, the browser to send to the server is a GET request, such as in the browser directly address access, point hyperlink access, etc. are get, users want to change the way the request to post, can be implemented by changing the form of submission.
Whether post or get is used to request a Web resource from the server, the difference is primarily in data delivery: If the request is a GET method, it can be followed by the requested URL address. In the form of data that is given to the server, separated by &, For example: get/mail/1.html?name=abc&password=xyz http/1.1
The feature of Get mode: The parameters that come with the URL address are limited, and the data capacity is usually not more than 1K.
If the request is post, you can send data to the server in the requested entity content, which is characterized by a post that has no limit on the amount of data being transmitted.

4.3, the HTTP request details--Message header

Common message headers in HTTP requests

Accept: The browser uses this header to tell the server what type of data 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 that the supported compression formats
Accept-language: The browser tells the server through this header that its locale
Host: The browser uses this header to tell the server which host you want to access
If-modified-since: The browser tells the server through this header, the time that the data is cached
Referer: The browser through this head to tell the server, the client is which page to the anti-theft chain
Connection: The browser through this head to tell the server, after the request is disconnected or where the link

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
v. HTTP response

5.1. Content included in HTTP response

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

  

Example:

http/1.1 OK
server:apache-coyote/1.1
content-type:text/html;charset=iso-8859-1
content-length:
Date:tue, 2014 16:23:28 GMT

 
 

5.2, the HTTP response details--Status line

Status Line format: HTTP version number status code reason narration <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 categories, as follows:

  

5.3, HTTP response Details-Common response header

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

Set up response headers at the server side to control the behavior of the client browser

6.1, set the location response header to achieve 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 response status code of the server /** * Set response header, the server through Location this header, to tell the browser where to jump, this is called Request redirection/Response.setheader ("Location", "/javaweb_httpprotoc
  Ol_study_20140528/1.jsp "); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio
  n {this.doget (request, response); }
}

When using 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 following illustration:

  

The server returns a 302 status code to tell the browser that you want the resources I did not, but I passed the location response header to tell you where there is, and the browser resolves the response header location know to jump to/javaweb_httpprotocol_study_ 20140528/1.jsp page, you will automatically jump to 1.jsp, as shown in the following image:

6.2, set the content-encoding response header, tell the browser data compression format

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.HttpServletResponse; 
/** * @author GaCl * This applet is used to demonstrate the following two small knowledge points * 1, the use of gzipoutputstream flow to compress the data * 2, set response head content-encoding to tell the browser, the server sent back the data compression format * * public class ServletDemo02 extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Res Ponse) throws Servletexception, IOException {String data = ' Abcdabcdabcdabcdabcdabcdab ' + ' Cdabcdabcdab
        Cdabcdabcdabcdabcdabc "+" DABCDABCDABCDABCDABCDABCDABCDABC "+" Dabcdabcdabcdabcdabcdabcdabcdabcdab "+ "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "CDABCDABCDABCDABCDA
    Bcdabcdabcdabcdab "+" CDABCDABCDABCDABCDABCDABCDABCDABCDABCD "; System.out.pRINTLN ("Raw data size is:" + data.getbytes (). length);
    Bytearrayoutputstream bout = new Bytearrayoutputstream (); Gzipoutputstream gout = new Gzipoutputstream (bout);
    Buffer Gout.write (data.getbytes ());
    Gout.close ();
    Gets 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, Ioexcepti
  On {this.doget (request, response);

 }
}

The response information sent to the browser by the server is as follows:

The compression formats supported by the browser are:

6.3, set the Content-type response header, 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.HttpServletResponse; public class ServletDemo03 extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Respo NSE) throws Servletexception, IOException {/** * browser can receive (Accept) data types are: * application/x-ms-applicatio N, * image/jpeg, * application/xaml+xml, * image/gif, * image/pjpeg, * application/x-ms-xbap , * application/vnd.ms-excel, * application/vnd.ms-powerpoint, * application/msword, * * * respon Se.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 wp_20131005_002.jpg image in the IMG folder under the project root, returning 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 output stream while (len = in.read (buffer) > 0) {//Read input stream (in) contents stored in buffer (b Uffer) out.write (buffer, 0, Len);//Output the contents of the buffer to the browser} public void DoPost (HttpServletRequest request, Httpse
  Rvletresponse response) throws Servletexception, IOException {this.doget (request, response);
 }
}

The response information sent to the browser by the server is as follows:

The results of the ServletDemo03 operation are shown in the following illustration:

Display a picture in a browser

6.4, set the refresh response head, so that the browser to refresh periodically

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 allow the browser to refresh every 3
     seconds
    // Response.setheader ("Refresh", "3");
    /**
     * Set the Refresh response header to allow the browser to jump to http://www.baidu.com
     /Response.setheader in 3 seconds
    ("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 the Content-disposition response header, let the browser download files

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.HttpServletResponse; public class ServletDemo05 extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Respo NSE) throws Servletexception, IOException {/** * Set content-disposition response header, let the browser download the file * * * response.se
    Theader ("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, Ioexc eption {THIS.DOget (request, response);

 }

}

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

The above is the entire content of this article, I hope to help you learn.

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.