How the Web Works
The client and the Web server communicate over the HTTP protocol. The Web server has to be also called an HTTP server or a Web container. The HTTP protocol uses the request/response pattern. That is, the client initiates an HTTP request, the Web server receives and resolves to process the HTTP request, and sends the HTTP response to the client.
Web server
A Web server refers to a software program, such as Apache Tomcat, Jboos, and so on. Their role is to manage the Web application, when the client makes an HTTP request, the corresponding Web server receives the HTTP request, invokes the appropriate Web application to process the request, and the Web server returns the response back to the client.
Additional instructions: The server is a hardware concept that refers to a 24-hour uninterrupted running host. In a nutshell, a computer is configured with Web servers and Web applications. The client can make an HTTP request to the server through the URL address to access the Web app's resource file.
URL (Uniform Resource location Uniform Resource Locator)
When the client (browser) enters a URL address, it can receive the data sent by the Web server. The process is to communicate using the HTTP protocol.
URL format: protocol://[host.] domain[:p Ort][/context][/resource][?query String]
Description of each parameter of the URL:
Protocol: the means by which communication is made. The IP and domain name are the same, and the domain name maps an IP address. The function is to identify a computer address, which is used to determine the server address. Port: After the computer is found, the program on a computer is determined by the port. For example, the default port for Tomcat is the path to the specified resource in the 8080[/context][/resource]:web app. [? query string]: The data submitted by the form as a request parameter is the query string here
Take the example of a tomcat-configured Web application. Browser input URL address: http://localhost:8080/MyWebProgram/index.jsp?admin=abc&info=123456 "URL address," followed by parameters, multiple parameters separated by & 】
The browser issues an HTTP request to access the/MYWEBPROGRAM/INDEX.JSP resource, and a 8080-port program, the Web server (TOMCAT), sends the index.jsp as the response body to the browser after receiving and resolving the HTTP request. In this way, a request/response communication process is over. Of course, the index.jsp here will be compiled into a servlet before being sent to an HTML document for the past. About servlet/jsp is not detailed here.
HTTP protocol
HTTP communication is not restricted to specific system platforms and programming languages because the HTTP protocol strictly specifies the data format for HTTP requests and HTTP responses. Therefore, the HTTP client (browser) and the Web server adhere to the HTTP protocol, then you can read the data of both sides, so as to communicate smoothly.
Request format and Response format for HTTP protocol
The HTTP request format consists of three parts:
Request method, URI, and HTTP protocol version request header (requests header): Contains useful information for many client environments and request bodies. Request Body (Body): Contains request parameters. It is separated from the request header by a space to indicate the end of the request header.
In the case of a servlet, initiate a request to the servlet to obtain the HTTP request information:
Get/testservlet/testrequestheaders http/1.1-------Request Header infomation--------Accept---application/ X-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, Image/pjpeg, APPLICATION/X-MS-XBAP, */* Accept-language---ZH-CNUA-CPU---amd64accept-encoding---gzip, deflateuser-agent---mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; trident/4.0;. NET CLR 2.0.50727; SLCC2;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c;. NET4.0E) Host---localhost:8089connection---keep-alive
This example describes the HTTP request that contains the information. The GET Request method is used here. The difference between the various methods of request, will be said later. The servlet code for this example is as follows:
@WebServlet (urlpatterns= {"/testrequestheaders"}, Name= "Requestheadertest") Public classRequestformatextendshttpservlet{@Overrideprotected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {printwriter pw=Resp.getwriter (); Resp.setcontenttype ("Text/html"); Resp.setcharacterencoding ("UTF-8");p W.write (");//HTTP request First line sectionPw.write (Req.getmethod () + Req.getrequesturi () + req.getprotocol () + "<br>");//HTTP request HeaderPw.println ("-------Request Header infomation--------<br>"); Enumeration<String> headers = Req.getheadernames ();//gets the name of all request headers while(Headers.hasmoreelements ()) {String header=(String) headers.nextelement ();p w.write (header+ "---" + req.getheader (header) + "<br>");} Pw.write ("</body>);}}
HTTP request method
There are seven HTTP request methods, the most common is the Get and post method, about the other 5 is not introduced here, because not commonly used.
Get mode:
Post mode:
The HTTP response format consists of three parts:
HTTP protocol version, status code and Description response header (Response header) Response body/content (Response content)
The response body here is the HTML document. In most cases, the HTML document responds, in the preceding Servlet example, the generated HTML document is the response body, which is sent by the Web server to the client (browser). However, sometimes the response body is not an HTML document, such as a doc document, and the browser may be opened with a native Word program. If it is a RAR archive, the document will be downloaded. So depending on the response content type, the browser will take a different approach. To set the response content type, call: Response.setcontexttype () in the servlet. This method is very important, memorize!
The basic workings of the Web, HTTP protocols, and URL descriptions