Getting Started with the Web (2)-http protocol

Source: Internet
Author: User
Tags character set http request
Getting Started with the Web (2)-http protocol first, 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 , HTTP1.0 and HTTP1.1 differences

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 Requests 4.1. What the HTTP request includes

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.jsp
4 ACCEPT-LANGUAGE:ZH-CN
5 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, deflate
7 host:localhost:8080
8 connection:keep-alive

v. HTTP response 5.1. What the HTTP response includes

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:






6

8
9 <title>hello World jsp</title>10 <body>
Hello world!

</body>

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 for 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 */
public class ServletDemo01 extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
14
Response.setstatus (302);//Set the response status code for the server
16/**
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 redirection
18 */
Response.setheader ("Location", "/javaweb_httpprotocol_study_20140528/1.jsp");
20}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.doget (request, response);
24}
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 following figure:

  

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 the following image:

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

 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;
/**
One * @author gacl
12 * This applet is used to demonstrate the following two small knowledge points
13 * 1, using Gzipoutputstream stream to compress data
+

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.