It is not as good as the 16-http protocol (2) of bad pen headers and the transfer of page data compression.
When we use tools such as HttpServletRequest and HttpServletReonse, these tools use the http protocol, which is also common for our daily WEB development. Because the http protocol is well encapsulated, we often ignore it. However, in the ultimate pursuit of performance, these basic protocols have become the basis for our continued efforts.
Response plays an important role in http.
You can set a Location response header to redirect requests (you can view the Content related to HttpServletRequest), or set a Content-Encoding response header to indicate the compression format of browser data; you can also set the content-type response header to specify the type of the response data. You can also set the content-disposition response header to allow the browser to download the file (you can view the content related to HttpServletReonse ))...
You can also use it to do many things.
1. Compression in WEB data transmission (Content-Encoding) Source code
When the bandwidth is not enough, compression is a good way to solve the problem. Now the bandwidth is getting bigger and bigger, but the file size is getting bigger and bigger. compression is still a hero. Baidu and other websites also use compressed data transmission methods.
Package com. servlet;
Import java. io. ByteArrayOutputStream;
Import java. io. FileInputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java.util.zip. GZIPOutputStream;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Importjavax. servlet. http. HttpServletRequest;
Importjavax. servlet. http. HttpServletResponse;
/**
* Compression of transmitted files through HttpServletResponse
*
* @ Author fan fangming
*/
Public class ResponseEncode extendsHttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponseresponse)
Throws ServletException, IOException {
String data = "123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890" +
"123456789012345678901234567890 ";
System. out. println ("Raw data size:" + data. getBytes (). length );
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
// Objects that can compress files
GZIPOutputStream gout = new GZIPOutputStream (bout); // buffer
Gout. write (data. getBytes ());
Gout. close ();
// Obtain the compressed data
Byte g [] = bout. toByteArray ();
// Tell the browser to decompress the package using gzip
Response. setHeader ("Content-Encoding", "gzip ");
Response. setHeader ("Content-Length", String. valueOf (g. length ));
System. out. println ("the size of the compressed data is:" + g. length );
Response. getOutputStream (). write (g );
}
Publicvoid doPost (HttpServletRequest request, HttpServletResponse response)
ThrowsServletException, IOException {
DoGet (request, response );
}
}
Modify web. xml as follows:
<Servlet>
<Servlet-name> encode </servlet-name>
<Servlet-class> com. servlet. ResponseEncode </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> encode </servlet-name>
<Url-pattern>/encode </url-pattern>
</Servlet-mapping>
2. Running result
Browser: 123456789012345678901234...
It seems that there is no effect.
3. Test the compression method.
The access URL on the browser is: http: // localhost: 8080/webStudy/encode
All the data is normal, but it seems that nothing is done, and it is the same as the common method.
Use httpClient of apache commons to write a simple test case. (For details about the Apache commons blog Series)
HttpClient test source code:
Package test. ffm83.commons. httpClient;
Import org. apache. commons. lang. StringUtils;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. methods. HttpGet;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. util. EntityUtils;
/**
* Simple httpClient Application
* Based on version 4.x
* @ Author fan fangming
*/
Public class EasyHttpGetA {
Public finalstatic void main (String [] args) throws Exception {
GetHttpGetOld ("http://www.baidu.com /");
GetHttpGetOld ("http: // localhost: 8080/webStudy/encode ");
}
Private staticvoid getHttpGetOld (String url) throws Exception {
HttpClient httpclient = newDefaultHttpClient (); // the old method is not recommended.
Try {
HttpGet httpget = newHttpGet (url );
System. out. println (StringUtils. center (url, 50, "= "));
HttpResponse response httpclient.exe cute (httpget); // the old method is not recommended.
HttpEntity entity = response. getEntity ();
System. out. println (response. getStatusLine ());
String webContent = "";
If (entity! = Null ){
WebContent = EntityUtils. toString (entity );
System. out. println ("ResponsegetContentLength:" + entity. getContentLength ());
System. out. println ("ResponsetoString () length:" + webContent. length ());
}
System. out. println (response. toString (); // display the HTTP Request header
System. out. println ("----------------------------------------");
Httpget. abort ();
}
Finally {
Httpclient. getConnectionManager (). shutdown ();
}
}
}
4. Final running result
Here I tested two URL addresses: Baidu and http: // localhost: 8080/webStudy/encode.
The running result is as follows:
=================== Http://www.baidu.com/===============
HTTP/1.1 200 OK
Response getContentLength:-1
Response toString () length: 88174
HTTP/1.1 200 OK [Date: Mon, 02 Feb2015 07:35:35 GMT, Content-Type: text/html; charset = UTF-8, Transfer-Encoding: chunked, Connection: Keep-Alive, vary: Accept-Encoding, Set-Cookie: BAIDUID = Accept: FG = 1; expires = Thu, 31-Dec-37 23: 55: 55GMT; max-age = 2147483647; path = /; domain = .baidu.com, Set-Cookie: BAIDUPSID = Taobao; expires = Thu, 31-Dec-37 23: 55: 55GMT; max-age = 2147483647; path =/; domain = .baidu.com, set-Cookie: BDSVRTM = 0; path =/, Set-Cookie: BD_HOME = 0; path =/, Set-Cookie: H_PS_PSSID = Hangzhou; path =/; domain = .baidu.com, p3P: CP = "oti dsp cor iva our ind com", Cache-Control: private, Cxy_all: baidu + fefa91c7f05681df2e287f12aec4d30b, Expires: Mon, 02 Feb 2015 07:35:27 GMT, x-Powered-By: HPHP, Server: BWS/1.1, BDPAGETYPE: 1, BDQID: 0xec3eeae200002e8a, BDUSERID: 0] org. apache. http. conn. basicManagedEntity @ fc9944
----------------------------------------
===== Http: // localhost: 8080/webStudy/encode ======
HTTP/1.1 200 OK
Response getContentLength: 35
Response toString () length: 35
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Content-Encoding: gzip, Content-Length: 35, Date: Mon, 02Feb 2015 07:37:50 GMT] org. apache. http. conn. basicManagedEntity @ 8b819f
----------------------------------------
You can see from the tool:
Response getContentLength: 35
Response toString () length: 35
The content printed from the console:
Raw data size: 300
The size of the compressed data is 35.
The browser is capable of self-extracting, data is compressed during transmission, reducing bandwidth usage.