Java Study Notes-HTTP protocol (10), java Study Notes

Source: Internet
Author: User

Java Study Notes-HTTP protocol (10), java Study Notes

The client browser and Server Tomcat need to process basic requests and responses.

In fact, the format of the data that the browser communicates with the server has been fixed using the Protocol. As long as both parties send the data in a fixed format, the basic communication can be completed.

If developers need to view such data formats, they need to install an HTTP-related plug-in for the browser.

If you want to learn more about the data communication between browsers and servers, you need to know the data content sent between them in detail.

1. Request Information

GET/HTTP/1.1 request line Accept: */* Accept-Language: zh-cn, en-US; q = 0.5 Request Header User-Agent: Mozilla/4.0Accept-Encoding: gzip, deflateHost: www.sina.com. cnConnection: Keep-Alive blank row uname = jack & upsw = POST data submitted by the jnb Request body

2. Request Line

Request Methods: GET and POST

The requested resource generally starts with a forward slash (/), indicating the website name to be accessed and the page name.

Protocol Information Protocol name/version

2. Request Header

The request header is sent by the browser to the server, so the browser wants to notify the server to do something.

The MIME type specifies the file format that needs to be opened by the application.

Accept: text/html, image/* indicates the type of data received by the Server Browser MIMEAccept-Charset: the Encoding method that the ISO-8859-1 notifies the server to submit data Accept-Encoding: gzip, compress Notification Server Browser supports data compression Accept-Language: en-us, zh-cn Notification Server Browser's current Language information Host: www.it315.org: 80 notifies the server of the host name to be requested If-Modified-Since: Tue, 11 Jul 2000 18:23:51 notifies the server of the latest modification time Referer: http://www.it315.org/index.jsp notifies the server of which URLUser-Agent the request comes from: mozilla/4.0 notifies the Server client of the browser kernel Cookie used to notify the server request with Cookie data Connection: close/Keep-Alive to Keep the established Connection Date: Tue, 11 Jul 2000 18:23:51 GMT notifies the server of the time when a request is sent

Requests are sent by the browser. Therefore, our programmers generally cannot modify the request header data. However, if you need to modify the request header information in a specific situation, you need to use the network programming knowledge of JavaSE.

The URL class is used to describe the address information in the address bar of the browser.

HttpURLConnection mainly describes the connection Objects established between the client and the server.

SetRequestProperty () can be used to set request header information

3. Request body

It mainly contains the data submitted by POST.

4.Learn more: What is the difference between HTTP1.0 and 1.1.

HTTP1.0 This Protocol does not have the Host field when sending a request. The browser establishes an independent connection for each request.

Think: If you want to browse the page, there are 3 images. How many requests have been sent?

4 = 1 text + 3 images

HTTP1.1 provides a required Host field, which can be reused after a connection is established. Improve the user's Internet experience.

5. Response Information

HTTP/1.1 200 OK response line Server: Apache-Coyote/1.1Set-Cookie: JSESSIONID = A8FB52 ...; Path =/day04 Response Header Content-Type: text/html; charset = ISO-8859-1Content-Length: 620 Date: Fri, 11 Jan 2013 07:42:39 GMT blank line This is my JSP page. response body

1. Response line

Protocol and version

Response Status Code 200 processed successfully 302 and 304 need to be refined request 404 resource does not exist 500 Server failure

Status Code Description OK

2. Response Header

Location: http://www.it315.org/index.jsp notification Browser needs to further refine the Request Path Server: apache tomcat notification Browser Server Name Content-Encoding: gzip notification browser response data type is compressed format Content-Length: 80 notification browser Data Length Content-Language: zh-cn notification browser Language Content-Type: text/html; charset = GB2312 notification browser Content Type Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT notifies the browser of the last modification time Refresh: 1; url = http://www.it315.org notifies the browser to automatically Refresh Content-Disposition: attachment; filename=a.zip: the processing method of the browser data to be notified. Transfer-Encoding: chunked: whether to block the browser data: Set-Cookie: SS = Q0 = 5Lb_nQ; path =/search: -1 notify the browser not to Cache the page cache-Control: no-cache Pragma: no-Cache Connection: close/Keep-Alive to notify the browser that the server has maintained the Connection Date: Tue, 11 Jul 2000 18:23:51 GMT: time when the Browser Server is notified to process

3. response body

Resources required by the server to send data to the browser

Case 1: Use Dynamic Web development technology to download images
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// notify the browser to open the beauty response by downloading. setHeader ("content-disposition", "attachment?filename=mv.jpg"); // obtain the image resource InputStream in = this. getServletContext (). getResourceAsStream ("/0001.jpg"); // gets the output stream object OutputStream out = response. getOutputStream (); byte [] bs = new byte [1024]; int len = 0; whi Le (len = in. read (bs ))! =-1) {out. write (bs, 0, len) ;}// release the resource in. close (); out. close ();}

Conclusion: When the HTTP protocol transmits data, the data is clearly transmitted. If you need to transmit some private data, you need to encrypt it.

Method 1: Use the encryption methods supported by various languages.

Method 2: Establish an encrypted communication connection between the client and the server.

HTTPS protocol

Encrypted HTTP protocol. An encrypted channel is established between the client browser and the server. The data transmitted through this channel is secure.

Such as online transactions and online banking.

So we need to build HTTPS in Tomcat.

1. Generate a certificate

Keytool-genkey-alias tomcat-keyalg RSA-keystore d:/tomcat. keystore

2. Introduce the Certificate in tomcat

Configure the server. xml file as follows:

<Connector port = "8443" protocol = "org. apache. coyote. http11.Http11NioProtocol"

SSLEnabled = "true"

MaxThreads = "150" scheme = "https" secure = "true"

ClientAuth = "false" sslProtocol = "TLS"

KeystoreFile = "d: \ tomcat. keystore"

Keystorepass= "123456"

/>

3. Use the browser to establish a secure connection https: // localhost: 8443

Problem:

The java_home environment variable is not configured. Java_home = D: \ jdk6.0

If the window is closed after an error is found during startup, view the log information file. If the address conflict is found, modify port 8080 in the server. xml file.

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.