JavaEE ------ GZIP full-site Compression
GZIP full-site Compression
Send the compressed text file to the browser to reduce traffic
GZIP Compression Conditions:
Set the header file protocol:
myresp.setHeader("Content-Encoding", "gzip");myresp.setContentLength(src.length);
Two main classes: ByteArrayOutputStream and GZIPOutputStream
Enhanced respone in packaging Mode
Main process:
There are two types of streams: byte stream outputStream and bytes stream PrintWriter.
We need to enhance the two streams in the respone object (namely, modify the write operations of the two streams by using the packaging design mode)
Filter:
After the response sub-assembly is completed, the data is uploaded to the background for reading, and all the data is put into our defined ByteArrayOutputStream, and then the data is extracted for compression. Use original to send to the front-end.
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletResponse resp = (response) response; Myresponse myresp = new Myresponse (resp ); // use the enhancement class to pass in and directly write it in the chain. doFilter (request, myresp); // compress the returned data to obtain the source data ByteArrayOutputStream bout = myresp. getBout (); // convert to byte data ---- byte [] src = bout is required for zip writing. toByteArray (); System. out. println ("before compression" + src. length); // count ByteArrayOutputStream baout = new ByteArrayOutputStream (); GZIPOutputStream zip = new GZIPOutputStream (baout); // the source data is compressed and written to the baout zip file in the memory stream. write (src); // close the stream zip. close (); // set the http protocol myresp. setHeader ("Content-Encoding", "gzip"); myresp. setContentLength (src. length); byte [] desc = baout. toByteArray (); System. out. println ("after compression" + desc. length); OutputStream out = resp. getOutputStream (); // After the bytes stream is compressed, it must be written out as the bytes stream. write (desc );}
Enhanced class Myresponse (Packaging Mode)
Separate writeGetOutputStream () of the byte stream function ()AndGetWriter () of the response stream ()
// This is an enhancement class, which must contain two response streams and a word throttling class Myresponse extends HttpServletResponseWrapper {private PrintWriter pw; ByteArrayOutputStream bout; public Myresponse (HttpServletResponse response) {super );} // writable stream, write a class in bytes, write the write function by yourself, and write the public ServletOutputStream getOutputStream () throws IOException {bout = new ByteArrayOutputStream (); // a new container is required each time. Return new Myout (bout);} // processing of the response stream. The response stream is different from the byte. The response stream can be directly integrated with IO. public PrintWriter getWriter () cannot be written separately () throws IOException {bout = new ByteArrayOutputStream (); // a new container is required each time. Pw = new PrintWriter (new OutputStreamWriter (bout, "UTF-8"), true); // directly writes data to the memory stream through streaming, simple return pw than word throttling ;} // finally, you need to return the container public ByteArrayOutputStream getBout () {if (pw! = Null) {pw. close (); // The RST stream must be refreshed; otherwise, no data is obtained.} return bout ;}}
Byte stream needs to write the function from
Construct parameter passing, and upload bout to this side for write operation
/This is the write function of the byte stream. Because the printwriter can be used for stream concatenation, you do not need to write another class Myout extends ServletOutputStream {ByteArrayOutputStream bout; public Myout (ByteArrayOutputStream bout) {this. bout = bout;} @ Overridepublic void write (int B) throws IOException {bout. write (B); // needs to be written to the memory stream }}
In the serlvet of the hyperlink, we used a test case.
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String str = "My pride is that happy and cool! Happy! Cool! come to asd salari. How long will it take to see Oscar three times if you have watched it "; // enter .... // Response. getWriter (). write (str);/* OutputStream out = response. getOutputStream (); byte [] B = str. getBytes ("UTF-8"); out. write (B); // bytes of all characters */PrintWriter pw = response. getWriter (); pw. write (str );}
When the server is started, compression will be blocked, index. jsp
When accessing the hyperlink to the servlet, it will also be compressed
The larger the compression efficiency, the higher the compression efficiency...
The above is the function to be implemented by the filter. You can use byte or character filtering to compress the entire site. You can configure the content to be compressed in web. xml. (I want to intercept all of them here)