The Host header is a bound domain name, Host header is not required if no domain name exists
The so-called host header is called from the function of binding domain names in IIS. Generally, port 80 of an IP address on a Web server can only correspond to one website correctly and process access requests for one domain name. While the Web server does not use multiple IP addresses and ports, if you need to support multiple relatively independent websites, you need a mechanism to distinguish requests from different websites on the same IP address. This shows the Host Header binding method. Simply put, different website spaces correspond to different domain names, and the domain name fields in the connection request are used to distribute and respond to the correct file execution results of the corresponding space. For example, if the IP address of a server is 192.168.1.10, there are two domain names and the corresponding space on this server, and port 80 of 192.168.1.10 is used to provide services. If you simply resolve the Domain Name Records of two domain names A and B to this IP address, the web server will report the same website information when receiving any request, this clearly fails to meet the requirements. Next, we use the Host header to bind domain names a and B to their corresponding space folders C and D. When the Web request information containing domain name a reaches 192.168.1.10, the web server executes the homepage file in Space C corresponding to it and returns it to the client. The same is true for the Web request information containing domain name B, it only explains the space d file. Therefore, after using the Host Header binding function, you cannot use an IP address to access any website on it. Because the request information does not contain domain name information, an error occurs.
There is a concept called Host Header in IIS, which we have basically mastered. There is also a concept called HTTP header. Some people have asked me this question, in fact, the HTTP header involves more software stuff.
The most intuitive HTTP header is when we create an HTML page in frotpage or DreamweaverCodeThere will be such a line of code:
In fact, this is an HTTP header. Such a line of code is displayed at the beginning of an HTML page.
So is it an HTTP header? HTTP headers include HTTP request headers and HTTP response headers. The HTTP Request Header specifies the request type when sending a request to the server when the HTTP client browses the homepage of the server. A request type is a HTTP-GET or HTTP-POST, where each HTTP-GET and HTTP-POST are composed of a series of HTTP request headers that define what the client requests from the server, the response is composed of a series of HTTP Response Headers and response data. If the request succeeds, a response is returned.
My understanding: every time the client requests a page from the server through IE, it always uses the HTTP request header to send the get or post Type HTTP request headers. when the server sends a response, it sends an HTTP Response Request.
The specific identifiers of request headers and responses are not described in detail. Let's take a look at the following content:
HTTP Request Header Overview (httpservletrequest)
HTTP customerProgram(For example, a browser) must specify the request type (usually get or post) when sending a request to the server ). If necessary, the customer program can also choose to send other request headers. Most request headers are not required, except Content-Length. Content-Length must appear for post requests.
Below are some of the most common request headers
Accept: the MIME type acceptable to the browser.
Accept-charset: the acceptable character set of the browser.
Accept-encoding: The data encoding method that the browser can decode, such as gzip. Servlet can return gzip-encoded HTML pages to a browser that supports gzip. In many cases, this can reduce the download time by 5 to 10 times.
Accept-language: the type of language that the browser wants to use when the server can provide more than one language version.
Authorization: authorization information, usually displayed in the response to the www-Authenticate header sent by the server.
Connection: Indicates whether a persistent connection is required. If the servlet sees that the value here is "keep-alive", or the request uses HTTP 1.1 (HTTP 1.1 performs a persistent connection by default), it can take advantage of the advantages of persistent connections, when a page contains multiple elements (such as an applet or image), the download time is significantly reduced. To achieve this, the servlet needs to send a Content-Length header in the response. The simplest method is to write the content into bytearrayoutputstream first, then, calculate the size of the content before writing it.
Content-Length: the length of the Request Message Body.
COOKIE: This is one of the most important request header information.
From: the e-mail address of the Request sender, which is used by some special Web client programs and not used by the browser.
HOST: host and port in the initial URL.
If-modified-since: it is returned only when the requested content is modified after the specified date. Otherwise, the 304 "not modified" response is returned.
Pragma: specifying the "no-Cache" value indicates that the server must return a refreshed document, even if it is a proxy server and has a local copy of the page.
Referer: contains a URL from which you can access the current requested page.
User-Agent: browser type. This value is useful if the content returned by the servlet is related to the browser type.
UA-pixels, UA-color, UA-OS, UA-CPU: non-standard request headers sent by some versions of IE, indicating screen size, color depth, operating system, and cputype.
For more information about HTTP headers, see http://www.w3.org/protocols.
HTTP Response Header Overview (httpservletresponse)
The HTTP Response of a Web server generally consists of one status line, one or more response headers, one blank line, and content document. Setting the HTTP response header is often combined with the status code in the Set status line. For example, several status codes indicating "the document location has changed" are accompanied by a location header, while the 401 (unauthorized) status code must be accompanied by a WWW-Authenticate header.
However, it is useful to specify the Response Header even if the status code with special meanings is not set. The response header can be used to complete: Set the cookie, specify the modification date, instruct the browser to refresh the page at the specified interval, and declare the document length so that persistent HTTP connections can be used ,...... And many other tasks.
The most common method to set the response header is the setheader of httpservletresponse. This method has two parameters, indicating the name and value of the Response Header, respectively. Similar to the set status code, the set response header should be performed before any document content is sent.
The setdateheader and setintheadr methods are used to set the response headers containing dates and integers. The former avoids the trouble of converting Java time into GMT time strings, the latter avoids the trouble of converting integers into strings.
Httpservletresponse also provides many settings
Setcontenttype: Set the Content-Type header. Most servlets use this method.
Setcontentlength: Set the Content-Length header. This function is useful for browsers that support persistent HTTP connections.
Addcookie: Set a cookie (the servlet API does not have the setcookie method, because the response usually contains multiple set-Cookie headers ).
In addition, the location header is also set when the sendredirect method sets status code 302.
For more information about HTTP headers, see http://www.w3.org/protocols.
HTTP Response Header
Which request methods (such as get and post) are supported by the Allow server ).
The encoding (encode) method of the content-encoding document. The content type specified by the Content-Type header can be obtained only after decoding. Gzip compression can significantly reduce the download time of HTML documents. Java gzipoutputstream can be easily compressed by gzip, but it is supported only by Netscape on UNIX and IE 4 and IE 5 on Windows. Therefore, Servlet should view the accept-encoding header (request. getheader ("Accept-encoding") checks whether the browser supports gzip, returns the gzip-compressed HTML page for a browser that supports gzip, and returns a common page for other browsers.
Content-Length indicates the content length. This data is required only when the browser uses a persistent HTTP connection. If you want to take advantage of persistent connections, you can write the output document to bytearrayoutputstram, view its size, put the value in the Content-Length header, and finally use bytearraystream. writeto (response. content sent by getoutputstream.
Content-Type indicates the MIME type of the subsequent document. Servlet is text/plain by default, but it must be explicitly specified as text/html. Because Content-Type is often set, httpservletresponse provides a dedicated method setcontenttyep.
The current GMT time of date. You can use setdateheader to set this header to avoid the trouble of converting the time format.
When should expires consider that the document has expired and thus it is no longer cached?
The last modification time of the last-modified document. You can use the IF-modified-since request header to provide a date. This request is considered as a condition get. Only documents whose modification time is later than the specified time will be returned, otherwise, a 304 (not modified) status is returned. Last-modified can also be set using the setdateheader method.
Location indicates where the customer should extract the document. Location is usually not set directly, but through the sendredirect method of httpservletresponse. This method also sets the status code to 302.
Refresh indicates the time after which the browser should refresh the document, in seconds. In addition to refreshing the current document, you can also use setheader ("refresh", "5; url = http: // host/path") to allow the browser to read the specified page. Note that this function is usually implemented by setting the head area of the HTML page, because automatic refresh or redirection is very important for HTML writers who cannot use CGI or servlet. However, for servlet, it is more convenient to directly set the refresh header. Note that refresh indicates "refresh the page after n seconds or access the specified page", rather than "refresh the page or access the specified page every n seconds ". Therefore, continuous refreshing requires that a refresh header be sent each time, and sending the 204 status code can prevent the browser from refreshing continuously, whether it is using the refresh header or. Note that the refresh header is not part of the official HTTP 1.1 Specification but an extension, but is supported by both Netscape and IE.
Server name. Servlet generally does not set this value, but is set by the Web server.
Set-COOKIE: set the cookie associated with the page. Servlet should not use response. setheader ("Set-cookie",...), but should use the dedicated method addcookie provided by httpservletresponse. For more information about Cookie settings, see the following section.
What type of authorization information should the WWW-authenticate client provide in the Authorization header? This header is required in the Response containing the 401 (unauthorized) status row. For example, response. setheader ("www-authenticate", "Basic realm = \" executives \""). Note that servlet generally does not handle this problem, but allows the Web server to control access to password-protected pages (for example,. htaccess ).