Java web Learning Summary (4), java web Learning Summary

Source: Internet
Author: User

Java web Learning Summary (4), java web Learning Summary
1. What is HTTP?

HTTP is short for hypertext transfer protocol (hypertext transfer protocol). It is an application layer protocol of TCP/IP protocol, used to define the process of data exchange between WEB browsers and WEB servers. After the client connects to the web server, if you want to obtain a web Resource in the web server, you must comply with a certain communication format. the HTTP protocol is used to define the communication format between the client and the web server.

Ii. HTTP Version

HTTP versions: HTTP/1.0 and HTTP/1.1

Iii. Differences between HTTP1.0 and HTTP1.1

In HTTP1.0, after the client establishes a connection with the web server, only one web resource can be obtained.
In HTTP1.1, a client can establish a connection with the web server to obtain multiple web resources in one connection.

Iv. HTTP request 4.1 and content contained in HTTP requests

  After the client connects to the server, it requests a web resource from the server, which is called an HTTP request sent by the client to the server..

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

  

4.2. HTTP request details-Request Line

GET in the request line is called the request method. The request methods include POST, GET, HEAD, OPTIONS, DELETE, TRACE, and PUT. commonly used methods include GET and POST.
If the user does not set it, by default, the browser sends get requests to the server. For example, if the browser directly enters the address access, and the URL access requests are all get requests, if the user wants to change the Request Method to post, you can change the form submission method.
Whether POST or GET is used to request a WEB resource from the server, the difference between the two methods is mainly manifested in data transmission: if the request method is GET, you can use? Take the data that is handed over to the server. Multiple Data are separated by &, for example, GET/mail/1.html? Name = abc & password = xyzhttp/1.1
GET Mode features: the parameters attached to the URL address are limited, and the data capacity cannot exceed 1 K.
If the request method is POST, data can be sent to the server in the request's entity content. The Post method has the following features: the amount of data transmitted is unlimited.

4.3. HTTP request details-Message Header

Common Message Headers in HTTP requests

Accept: the browser uses this header to tell the server the data types it supports.
Accept-Charset: the browser uses this header to tell the server which character set it supports
Accept-Encoding: the browser uses this header to tell the server the supported compression format.
Accept-Language: the browser uses this header to tell the server its Language environment
Host: the browser uses this header to tell the server which Host to access.
If-Modified-Since: the browser uses this header to tell the server the time when data is cached.
Referer: the browser uses this header to tell the server which page the client uses for anti-leech protection.
Connection: the browser uses this header to tell the server whether the Connection is closed or what link is held after the request is complete.

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. Content included in HTTP Response 5.1 and HTTP Response

  An HTTP response indicates the data that the server sends back to the client.It includes a status line, several message headers, and entity content.

  
Example:

 1 HTTP/1.1 200 OK 2 Server: Apache-Coyote/1.1 3 Content-Type: text/html;charset=ISO-8859-1 4 Content-Length: 105 5 Date: Tue, 27 May 2014 16:23:28 GMT 6  7 5.2. HTTP Response Details-status line

Status row format:Cause description of HTTP Version Status Code <CRLF>
Example: HTTP/1.1 200 OK
The status code is used to indicate the server's request processing result. It is a three-digit decimal number. The response status codes are classified into five categories, as shown below:
  

5.3 HTTP Response Details-Common Response Headers

Common Response Headers (message headers) in HTTP responses)
Location: the server uses this header to tell the browser where to jump
Server: the Server uses this header to tell the browser the Server model.
Content-Encoding: the server uses this header to tell the browser about the data compression format.
Content-Length: the server uses this header to tell the browser the Length of the data to be sent back.
Content-Language: the server uses this header to tell the browser Language environment
Content-Type: the server uses this header to tell the browser the Type of data to be sent back.
Refresh: the server uses this header to tell the browser to regularly Refresh
Content-Disposition: the server uses this header to tell the browser to download data.
Transfer-Encoding: the server uses this header to tell the browser that the data is sent back in blocks.
Expires:-1 control browser not to cache
Cache-Control: no-cache
Pragma: no-cache

6. Set a response header on the server side to control the browser behavior of the client. 6.1. Set a Location response header to redirect requests.
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 {12 public void doGet (HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException {14 15 response. setStatus (302); // set the server's response status code 16/** 17 * to set the response header. The server uses the Location header to tell the browser where to jump, this is the so-called request redirection 18 */19 response. setHeader ("Location", "/JavaWeb_HttpProtocol_Study_20140528/1.jsp"); 20} 21 public void doPost (HttpServletRequest request, HttpServletResponse response) 22 throws ServletException, IOException {23 this. doGet (request, response); 24} 25}

When the URL "http: // localhost: 8080/JavaWeb_HttpProtocol_Study_20140528/servlet/ServletDemo01" is used 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 I don't have the resources you want, but I use the Location response header to tell you where there is, after the browser parses the Response Header Location, it knows 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 to tell the browser the 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 tips 13*1. Use GZIPOutputStream stream to compress data 14*2. Set the response header Content-Encoding to tell the browser, the format of the compressed data sent from the server is 15 */16 public class ServletDemo02 extends HttpServlet {17 18 public void doGet (HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException {20 String data = "abcdabcdabcdabcdabcdabcdab" + 21 "cdabcdabcdabcdabcdabcdabcdabc" + 22 "highlight" + 23 "yellow" + 24 "yellow" + 25 "yellow" + 26 "yellow" + 27 "cdabcdabcdabcdabcdabcdabcdabcdabcdabcd "; 28 System. out. println ("Raw data size:" + data. getBytes (). length); 29 30 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 31 GZIPOutputStream gout = new GZIPOutputStream (bout); // buffer32 gout. write (data. getBytes (); 33 gout. close (); 34 // obtain the compressed data 35 byte g [] = bout. toByteArray (); 36 response. setHeader ("Content-Encoding", "gzip"); 37 response. setHeader ("Content-Length", g. length + ""); 38 response. getOutputStream (). write (g); 39} 40 41 public void doPost (HttpServletRequest request, HttpServletResponse response) 42 throws ServletException, IOException {43 this. doGet (request, response); 44} 45}

The response message sent from the server to the browser is as follows:

The following compression formats are supported by the browser:

6.3 set the content-type response header and specify the type of the return data
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 {10 public void doGet (HttpServletRequest request, HttpServletResponse response) 11 throws ServletException, IOException {12/** 13 * the browser can receive (Accept) data types include: 14 * application/x-ms-application, 15 * image/jpeg, 16 * application/xaml + xml, 17 * image/gif, 18 * image/pjpeg, 19 * application/x-ms-xbap, 20 * application/vnd. ms-excel, 21 * application/vnd. ms-powerpoint, 22 * application/msword, 23 */24 response. setHeader ("content-type", "image/jpeg "); // use the content-type response header to specify the Data type sent to the browser as "image/jpeg" 25 // read the image wp_20131005_002.jpg in the IMG folder of the Project root directory, and return an input stream 26 InputStream in = this. getServletContext (). getResourceAsStream ("/img/WP_20131005_002.jpg"); 27 byte buffer [] = new byte [1024]; 28 int len = 0; 29 OutputStream out = response. getOutputStream (); // get the output stream 30 while (len = in. read (buffer)> 0) {// read the content in the input stream (in) and store it in the buffer (buffer) 31 out. write (buffer, 0, len); // output the content in the buffer to the browser 32} 33} 34 public void doPost (HttpServletRequest request, HttpServletResponse response) 35 throws ServletException, IOException {36 this. doGet (request, response); 37} 38}

The response message sent from the server to the browser is as follows:

Shows the running result of ServletDemo03:

An image is displayed in the browser.

6.4 set the refresh response header to regularly refresh the browser
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 {10 public void doGet (HttpServletRequest request, HttpServletResponse response) 11 throws ServletException, IOException {12/** 13 * set the refresh response header, refresh the browser every 3 seconds at 14 */15 // response. setHeader ("refresh", "3"); 16/*** 17 * set the refresh Response Header so that the browser jumps to the http://www.baidu 3 seconds later. com18 */19 response. setHeader ("refresh", "3; url = 'HTTP: // www.baidu.com '"); 20 response. getWriter (). write ("gacl"); 21} 22 23 public void doPost (HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException {25 this. doGet (request, response); 26} 27 28}
6.5 set the content-disposition response header to allow the browser to 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 12 public class ServletDemo05 extends HttpServlet {13 public void doGet, let the browser download the file 17 */18 response. setHeader ("content-disposition", "attachment=filename=xxx.jpg"); 19 InputStream in = this. getServletContext (). getResourceAsStream ("/img/1.jpg"); 20 byte buffer [] = new byte [1024]; 21 int len = 0; 22 OutputStream out = response. getOutputStream (); 23 while (len = in. read (buffer)> 0) {24 out. write (buffer, 0, len); 25} 26} 27 28 public void doPost (HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException {30 this. doGet (request, response); 31} 32 33}

When you access ServletDemo05 in a browser, the file download box is displayed, as shown in:

  

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.