The client browser and server tomcat are handling basic requests and responses.
In fact, the browser and server communication data format has been fixed with the protocol, as long as the two sides send a fixed format of data, then you can complete the basic communication.
If developers need to look at such data formats, then they need to install a browser-related plugin for the HTTP protocol.
If you want to learn more about the data that the browser and server communicate with, you need to have a detailed understanding of what data is sent between them.
1 Request Information
get/http/1.1 */* 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 line uname=jack&upsw= JNB ? Post data submitted by the request body
2. Request Line
Request mode Get and post
Request resources typically begin with/start with the name of the site and page you need to access
Protocol Information Protocol name/version
2. Request Header
The request header is the browser sent to the server, then the browser wants to notify the server to do something.
The MIME type specifies what kind of file format to use to open the application.
Accept:text/html,image/* ? Notifies the server that the browser receives the data type Mimeaccept-charset:iso-8859-1 ? How does the notification server submit data encoded Accept-encoding:gzip,compress ? Notifies the server browser that data compression ACCEPT-LANGUAGE:EN-US,ZH-CN is supported ? Notifies the server browser of the current language information host:www.it315.org:80 ? Notifies the server to request the hostname if-modified-since:tue, 18:23:51? http://www.it315.org/index.jsp ? Notifies the server which urluser-agent:mozilla/4.0 this request is from? Notifies the server client of the browser kernel cookie used ? Notification server request with cookie data connection:close/keep-alive ? The notification server can maintain a well established connection Date:tue, 18:23:51 GMT ? Notifies the server when the request was sent
The request was sent by the browser. Then we programmers are generally not able to modify these request header data. However, in a specific situation where the requested header information needs to be modified, it is necessary to use some of the basics of Javase's network programming knowledge.
URLs primarily use this class to describe address information in the browser's address bar.
HttpURLConnection mainly describes the connection objects established by the client and the server
Setrequestproperty () can set the request header information
3. The request body
It mainly contains the data submitted by the post.
4. Learn one trick: What is the difference between HTTP1.0 and 1.1?
HTTP1.0 the protocol does not have a host field at the time of sending the request, and the browser establishes a separate connection for each request.
Think: If you need to browse for a page that has 3 images. So how many requests have you sent?
4 = 1 times text + 3 pictures
HTTP1.1 provides a mandatory host field and can be reused once a connection is established. Improve the user's Internet experience.
5 Response Information
http/1.1 $OK? Response Line Server:apache-coyote/1.1Set-cookie:jsessionid=a8fb52 ...; path=/day04? response header Content-type:text/html;charset=iso-8859-1Content-length:620Date:fri, OneJan - -: the: theGMT? Blank line this isMy JSP page. ? Response body
1. Response lines
Protocols and versions
Response status Code 200 processing succeeds 302 and 304 requires refinement request 404 resource does not exist 500 server failed
Status Code description Information OK
2. Response header
Location:http://www.it315.org/index.jsp? Notifies the browser of the need to further refine the request's pathServer:apache Tomcat? Notifies the browser server of the name content-encoding:gzip? Notification browser response data type is compressed format content-length: the? Notifies the browser of the length of the data content-language:zh-CN? Notification Browser Language content-type:text/html; charset=GB2312? notification browser content type last-modified:tue, OneJul - -: at:WuyiGMT? Notifies the browser that the resource last modified at the time of refresh:1; Url=http://www.it315.org? notification Browser auto-timed refreshContent-disposition:attachment; Filename=A.zip? Notifies the browser how data is handled transfer-encoding:chunked? Notifies the browser that the data is in a block set-COOKIE:SS=Q0=5LB_NQ; path=/search? Notifies the browser to use cookies to store data expires:-1? Notifies the browser not to cache page caches-control:no-Cache Pragma:no-Cache Connection:close/keep-Alive? Notifies the browser server that the connection Date:tue has been maintained. OneJul - -: at:WuyiGmt? Notifies the browser server of the processing time
3. Response body
Resource data that the server sends to the browser
Case one: Using Dynamic Web development technology to achieve image download
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//notify the browser to download the way to open the beautyResponse.setheader ("content-disposition", "attachment;filename=mv.jpg"); //Get Picture ResourcesInputStreaminch= This. Getservletcontext (). getResourceAsStream ("/0001.jpg"); //gets the output stream object that is output to the pageOutputStream out=Response.getoutputstream (); byte[] bs =New byte[1024x768]; intLen =0; while(len =inch. Read (BS))! =-1){ out. Write (BS,0, Len); } //Freeing Resources inch. Close (); out. Close (); }
Summary: The HTTP protocol itself transmits data at the time the data is transmitted in a plaintext. If a user needs to transfer some privacy data, it needs to be encrypted.
Method 1: Use encryption methods supported by various languages.
Mode 2: Establish a communication connection between the client and server-side encryption.
HTTPS protocol
Encrypted HTTP protocol, an encrypted channel is established between the client browser and the server, and the data passed under that channel is secure.
such as: Online trading, Online banking.
Then we need to build https in Tomcat.
1. Generate a Certificate
Keytool-genkey-alias Tomcat-keyalg Rsa-keystore D:/tomcat.keystore
2. Introduction of Certificates in Tomcat
Configure the Server.xml file as follows
<connector port= "8443" protocol= "Org.apache.coyote.http11.Http11NioProtocol"
Sslenabled= "true"
maxthreads= "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:
- When Tomcat starts, it flashes past.
The JAVA_HOME environment variable is not configured. java_home=d:\jdk6.0
- If you start a direct error.
If you immediately close the window when you find the error message on startup, look at the log information file. If the address conflict is found, then modify the 8080 port in the Server.xml file.
Javase Study Notes-http Protocol (10)