About static resource compression technology

  HTTP compression can greatly improve the speed of browsing websites. Compared with ordinary browsing process HTML, CSS, Javascript, Text, it can save about 40% of the traffic. More importantly, it can compress dynamically generated web pages including CGI, PHP, JSP, ASP, Servlet, SHTML, etc., with amazing compression efficiency.
At present, there are two ways to achieve gzip compression:


method one, some functions provided by the container (server), but this is limited to a specific container. Such as apache+tomcat or resin-pro version . The second method is to manually gzip compression before deployment, and use it with servlet filter. This can realize the gzip function, but reduce the flexibility. Scheme 1 1. TOMCAT configure GZIP compression: tomcat5.5.x configuration Modify %TOMCAT_HOME%\conf \server.xml to enable support for gzip compression. Add the following attributes compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata"












compressableMimeType="text/html,text/xml"
TOMCAT configuration instructions
1) compression="on" to turn on the compression function
2) compressionMinSize="2048" to enable the compressed output content size, which is 2KB by default
3) noCompressionUserAgents="gozilla,
traviata " For the following browsers, do not enable

compression

a picture. Use Httpwatch to view
Accept-Encoding: gzip, deflate
Sent : 315
Received 43106

Start to configure Resin

I follow this configuration:
<web-app id="/" root-directory="webapps/MyProj">
<filter filter-name=" gzip" filter-class="com.caucho.filters.GzipFilter">
<init>


</filter>
<filter-mapping filter-name="gzip">
<url-pattern>
<exclude-pattern>*.jpg</exclude-pattern>
<include-pattern>/*</include-pattern>
</ url-pattern>
</filter-mapping>
</web-app>

Test again and find that it does not work! Not sure how to configure it!
I looked at the information again. It said that the professional version of resin is resin-pro
. Now look at my resin: Resin-3.2.1 (built Fri, 17 Oct 2008 04:11:01 PDT)

It is indeed mine. caused by version inconsistencies. Re-download the professional version of RESIN from the Internet and try again!
The following is the process of my re-testing: the
test found that it still does not work! I'm dizzy~~~ But I feel that the professional version and the normal version are still a little bit different in configuration.
To be continued !

Option 2:
Set Content-Encoding

Content-Encoding The encoding (Encode) method of the document. The content type specified by the Content-Type header can only be obtained after decoding. Using gzip to compress documents can significantly reduce the download time of HTML documents. Java's GZIPOutputStream can easily do gzip compression, but it is only supported by Netscape on Unix and IE 4 and 5 on Windows. Therefore, the servlet should check whether the browser supports gzip by looking at the Accept-Encoding header (i.e. request.getHeader("Accept-Encoding")), and return the gzip-compressed HTML page for browsers that support gzip, and return normal for other browsers page.
Compressed stream
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
OutputStream out = null;
String encoding = request.getHeader("Accept-Encoding");  
if (encoding != null && encoding.indexOf("gzip" ) != -1){
request.setHeader("Content-Encoding" , "gzip");  
out = new GZIPOutputStream(request.getOutputStream());
} 
else if (encoding != null && encoding.indexOf("comdivss") != -1){
request.setHeader("Content-Encoding" , "comdivss");  
out = new ZIPOutputStream(request.getOutputStream());
} else{  
out = request.getOutputStream();



Example:
Implemented with gzip servlet filter
Since HTTP/1.1, the client can add
Accept-Encoding: gzip,deflate to the request header (you can check it from HTTP WATCH and find that it is indeed supported)
Response to indicate to the requesting server that it supports Gzip compression. The web server adds
Content-Encoding: gzip
to the response header to indicate to the client that the response body is gzipped.

The program code is as follows:
(Thanks to http://tdcq.iteye.com/blog/453644 for providing the code)

The specific code is as follows:
package sh.blog.util.web.filter;
import java.io.IOException;
import java. util.zip.
import javax.servlet.ServletOutputStream;
public class CompressedStream extends ServletOutputStream {
private ServletOutputStream out;
private GZIPOutputStream     gzip;
/**
* 指定压缩缓冲流
* @param 输出流到压缩
* @throws IOException if an error occurs with the {@link GZIPOutputStream}.
*/
public CompressedStream(ServletOutputStream out) throws IOException {
this.out = out;
reset();
}

/** @see ServletOutputStream * */
public void close() throws IOException {
gzip.close();
}

/** @see ServletOutputStream * */
public void flush() throws IOException {
gzip.flush();
}

/** @see ServletOutputStream * */
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}

/** @see ServletOutputStream * */
public void write(byte[] b, int off, int len) throws IOException {
gzip.write(b, off, len);
}

/** @see ServletOutputStream * */
public void write(int b) throws IOException {
gzip.write(b);
}      

public void reset() throws IOException {
gzip = new GZIPOutputStream(out);
}

}



package sh.blog.util.web.filter;
import java.io.IOException;
import java.io.PrintWriter;   
import javax.servlet.ServletOutputStream;   
import javax.servlet.http.HttpServletResponse;   
import javax.servlet.http.HttpServletResponseWrapper;     

public class CompressionResponse extends HttpServletResponseWrapper{
protected HttpServletResponse response;   
private ServletOutputStream out;   
private CompressedStream compressedOut;  
private PrintWriter writer;   
protected int contentLength;   

public CompressionResponse(HttpServletResponse response) throws IOException {   
super(response);
this.response = response;   
compressedOut = new CompressedStream(response.getOutputStream());
}

public void setContentLength(int len) {
contentLength = len;   
}

public ServletOutputStream getOutputStream() throws IOException {   
if (null == out) {   
if (null != writer) {  
throw new IllegalStateException("getWriter() has already been called on this response.");   
}
out = compressedOut;   
}
return out;
}

public PrintWriter getWriter() throws IOException {   
if (null == writer) {   
if (null != out) {   
throw new IllegalStateException("getOutputStream() has already been called on this response.");
}
writer = new PrintWriter(compressedOut);  
}
return writer;   

}

public void flushBuffer() {   
try {   
if (writer != null) {
writer.flush();
}else if (out != null) {  
out.flush();   
}

}catch (IOException e) {  
e.printStackTrace();   
}
}

public void reset() {
super.reset();   
try {   
compressedOut.reset();   
}catch (IOException e) {  
throw new RuntimeException(e);   
}
}

public void resetBuffer() {   
super.resetBuffer();   
try {   
compressedOut.reset();   
}catch (IOException e) {  
throw new RuntimeException(e);
}
}

public void close() throws IOException {   
compressedOut.close();   
}



}



package sh.blog.util.web.filter;

import java.io.IOException;   
import java.util.Enumeration;   
import javax.servlet.Filter;   
import javax.servlet.FilterChain;   
import javax.servlet.FilterConfig;   
import javax.servlet.ServletException;   
import javax.servlet.ServletRequest;   
import javax.servlet.ServletResponse;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
import org.apache.commons.logging.Log;   
import org.apache.commons.logging.LogFactory;   

public class CompressionFilter implements Filter {   
protected Log log = LogFactory.getFactory().getInstance(this.getClass().getName());   

@SuppressWarnings("unchecked")   
public void doFilter(ServletRequest request, ServletResponse response,   
FilterChain chain) throws IOException, ServletException {   
boolean compress = false;   
if (request instanceof HttpServletRequest){   
HttpServletRequest httpRequest = (HttpServletRequest) request;  
Enumeration headers = httpRequest.getHeaders("Accept-Encoding");   
while (headers.hasMoreElements()){   
String value = (String) headers.nextElement();   
if (value.indexOf("gzip") != -1){   
compress = true;   
}   
}   
}   

if (compress){//如果浏览器支持则压缩   
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("Content-Encoding", "gzip");   
CompressionResponse compressionResponse= new CompressionResponse(httpResponse);   
chain.doFilter(request, compressionResponse);   
compressionResponse.close();   
}   
else{//如果浏览器不支持则不压缩   
chain.doFilter(request, response);   
}   

}   

public void init(FilterConfig config) throws ServletException {   

}   

public void destroy(){   
}   

}   
There are three CLASS files in total! Realize GZIP compression output response

2.1 Compress image output to test
Create a directory to store images in pdf
Step 1: Use HTTP WATCHE to discover
image/jpeg : 42891 bytes, 670 x 446 pixels


Step 2: Configure Web.xml configuration filter
<filter>
<filter-name>gzip</filter-name>
<filter-class>sh.blog.util.web.filter.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter -name>gzip</filter-name>
<url-pattern>/pdf/*</url-pattern>
</filter-mapping> and

then use HTTP WATCH to find
image/jpeg : 42891 bytes, gzip compressed to 42712 bytes ( 0.417 % saving ),
670 x 446 pixels for one compressed output!
PS: I did another test with png format pictures and found that GZIP compression output can be achieved once
Conclusion: The above filter can compress the image and improve the response speed!


2.2 The music compression process takes the output of MP3 as the test object

, and creates a directory music to store the music.
Step 1: Find
audio/mpeg : 9001 bytes of binary data without configuring the filter.

Step 2: Configure the filter

<filter>
<filter- name>gzip</filter-name>
<filter-class>sh.blog.util.web.filter.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter- name>
<url-pattern>/music/*</url-pattern>   
</filter-mapping>

Check again and find:
audio/mpeg : , gzip compressed to 0 bytes ( 0 % saving )
Conclusion: The above algorithm works for music files Does not compress. I feel that the algorithm of this GZIP should be different in different formats. 2.3


Compress the output of

JS files .





<filter-class>sh.blog.util.web.filter.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter-name>
<url-pattern>/scripts /*.js</url-pattern>   
</filter-mapping>

output:
application/x-javascript : 4636 bytes, gzip compressed to 69 bytes ( 98.5 % saving )

Check that the compression of JS is quite high!
Conclusion: Store JS in the specified directory and then do GZIP compression output directly to this directory. You can see that the effect is remarkable!
By doing GZIP compression output, you can reduce the network bandwidth traffic and speed up the download speed!



2.4 Compressing and outputting CSS files

Step 1: output text/css without compression
: 413 bytes

Step 2: Compression

Configuration:
<filter>
<filter-name>gzip</filter-name>
<filter-class>sh.blog. util.web.filter.CompressionFilter</filter-class>
<

<filter-name>gzip</filter-name>
<url-pattern>/scripts/*.js</url-pattern>   
<url-pattern>/style/*.css</url-pattern>
</filter- mapping>
result:
text/css : 413 bytes, gzip compressed to 101 bytes ( 75.5 % saving )
Conclusion: The compression effect of CSS is also very obvious!

2.5 Compress HTML page output

Step 1: Uncompress
text/html : 2272 bytes

Step 2; Compress
<filter>
<filter-name>gzip</filter-name>
<filter-class>sh.blog.util.web .filter.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter-name>
<url-pattern>/scripts/*.js</url-pattern>   
<url -pattern>/style/*.css<

</filter-mapping>
Result:
text/html : 2272 bytes, gzip compressed to 240 bytes ( 89.4 % saving )

Conclusion: The compression effect on HTML is also very obvious!

2.6 Compression of JSP pages
Step 1: Uncompressed
text/html; charset=iso-8859-1 : 1008 bytes

Step 2: Compressed output
<filter>
<filter-name>gzip</filter-name>
<filter -class>sh.blog.util.web.filter.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter-name>
<url-pattern>/scripts/* .js</url-pattern>   
<url-pattern>/style/*.css</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.jsp</url -pattern>
</filter-mapping>

Result: the page has no output!

in conclusion:
The above algorithm can be applied to GZIP compressed output of pictures, HTML, CSS, and JS. Not valid for JSP pages!

application:

In the future, such filters can be written in the site to GZIP the page content as much as possible to improve the download speed




This is a good post I saw elsewhere, I hope you can read it

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324140537&siteId=291194637
Recommended