Two-pronged approach-accelerating webpage display with compression and transmission

Source: Internet
Author: User
Tags jboss server
Topic

This article mainly targets Tomcat and JBoss Series servers and uses JSP and Java-developed websites to compress and transmit the JavaScript scripts, CSS, and images contained in them during page loading, in order to speed up the display of the page, after the test comparison, the effect is more obvious.

Configuration 1: JBoss Server Configuration

In JBoss server. in the XML file, find connector Port = "80" or connector Port = "8080", which is used to configure the server port, add a parameter for enabling compression and the suffix of the file to be compressed.

After completing the above configuration, restart the server to see the effect ~~.

Configuration 2: Add a compression function to the Project

After the server is configured, the web page display effect has been improved to some extent. If you want to further improve, you need to work hard to configure it in the project, on the web. add a filter in XML to compress the specified file by using gzip. Because Gzip is very efficient, it often achieves good results.

The procedure is as follows:

Filter ImplementationCodeAs follows:

 
PackageCom. guet. Cab. Base. filter;
 
 
 
ImportJava. Io. ioexception;
 
 
ImportJava. util. enumeration;
 
 
 
ImportJavax. servlet. filter;
 
 
 
ImportJavax. servlet. filterchain;
 
 
 
ImportJavax. servlet. filterconfig;
 
 
 
ImportJavax. servlet. servletexception;
 
 
 
ImportJavax. servlet. servletrequest;
 
 
 
ImportJavax. servlet. servletresponse;
 
 
ImportJavax. servlet. http. httpservletrequest;
 
 
 
ImportJavax. servlet. http. httpservletresponse;
 
 
 
ImportOrg. Apache. commons. Logging. log;
 
 
 
ImportOrg. Apache. commons. Logging. logfactory;
 
 
 
PublicClassCompressionfilterImplementsFilter {
 
 
 
ProtectedLog = logfactory. getfactory (). getinstance (This. Getclass (). getname ());
 
 
@ Suppresswarnings ("Unchecked")
 
 
 
PublicVoidDofilter (servletrequest request, servletresponse response,
 
 
 
Filterchain chain)ThrowsIoexception, servletexception {
 
 
 
BooleanCompress =False;
 
 
 
If(RequestInstanceofHttpservletrequest ){
 
 
 
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 ){// Compress if the browser supports
 
 
 
Httpservletresponse httpresponse = (httpservletresponse) response;
 
 
 
Httpresponse. addheader ("Content-Encoding","Gzip");
 
 
 
Compressionresponse =NewCompressionresponse (httpresponse );
 
 
 
Chain. dofilter (request, compressionresponse );
 
 
 
Compressionresponse. Close ();
 
 
 
}
 
 
Else{// Do not compress if the browser does not support
 
 
 
Chain. dofilter (request, response );
 
 
 
}
 
 
 
}
 
 
 
PublicVoidInit (filterconfig config)ThrowsServletexception {
 
 
 
}
 
 
 
 
 
 
 
PublicVoidDestroy (){
 
 
 
}
 
 
}

 

The two called functions are:

 
PackageCom. guet. Cab. Base. filter;
 
 
 
ImportJava. Io. ioexception;
 
ImportJava.util.zip. gzipoutputstream;
 
 
 
ImportJavax. servlet. servletoutputstream;
 
 
 
Public ClassCompressedstreamExtendsServletoutputstream {
 
PrivateServletoutputstream out;
 
 
 
PrivateGzipoutputstream gzip;
 
 
 /**
 
 
 
* Specify the compressed buffer stream
 
 
 
* @ Param output stream to compression
 
 
 
* @ Throws ioexception if an error occurs with the {@ link gzipoutputstream }.
 
 
 
*/
 
 
 
PublicCompressedstream (servletoutputstream out)ThrowsIoexception {
 
 
 
This. Out = out;
 
 
 
Reset ();
 
 
 
}
 
 
/** @ See servletoutputstream **/
 
 
 
PublicVoidClose ()ThrowsIoexception {
 
 
 
Gzip. Close ();
 
 
 
}
 
 
 
/** @ See servletoutputstream **/
 
 
 
PublicVoidFlush ()ThrowsIoexception {
 
 
 
Gzip. Flush ();
 
 
 
}
 
 
/** @ See servletoutputstream **/
 
 
 
PublicVoidWrite (Byte[] B)ThrowsIoexception {
 
 
 
Write (B, 0, B. Length );
 
 
 
}
 
 
 
/** @ See servletoutputstream **/
 
 
 
PublicVoidWrite (Byte[] B,IntOff,IntLen)ThrowsIoexception {
 
 
Gzip. Write (B, off, Len );
 
 
 
}
 
 
 
/** @ See servletoutputstream **/
 
 
 
PublicVoidWrite (IntB)ThrowsIoexception {
 
 
 
Gzip. Write (B );
 
 
 
}
 
 
 
 
 
 
 
PublicVoidReset ()ThrowsIoexception {
 
 
Gzip =NewGzipoutputstream (out );
 
 
 
}
 
 
 
 
 
}

Function 2:

 
PackageCom. guet. Cab. Base. filter;
 
 
 
ImportJava. Io. ioexception;
 
ImportJava. Io. printwriter;
 
 
 
ImportJavax. servlet. servletoutputstream;
 
ImportJavax. servlet. http. httpservletresponse;
 
ImportJavax. servlet. http. httpservletresponsewrapper;
 
 
PublicClassCompressionresponseExtendsHttpservletresponsewrapper {
 
 
 
ProtectedHttpservletresponse response;
 
 
 
PrivateServletoutputstream out;
 
 
 
PrivateCompressedstream compressedout;
 
 
 
PrivatePrintwriter writer;
 
 
 
ProtectedIntContentlength;
 
 
PublicCompressionresponse (httpservletresponse response)ThrowsIoexception {
 
 
 
Super(Response );
 
 
 
This. Response = response;
 
 
 
Compressedout =NewCompressedstream (response. getoutputstream ());
 
 
 
}
 
 
 
PublicVoidSetcontentlength (IntLen ){
 
 
 
Contentlength = Len;
 
 
 
}
 
 
PublicServletoutputstream getoutputstream ()ThrowsIoexception {
 
 
 
If(Null= Out ){
 
 
 
If(Null! = Writer ){
 
 
 
ThrowNewIllegalstateexception ("Getwriter () has already been called on this response.");
 
 
 
}
 
 
 
Out = compressedout;
 
 
}
 
 
 
ReturnOut;
 
 
 
}
 
 
 
PublicPrintwriter getwriter ()ThrowsIoexception {
 
 
 
If(Null= Writer ){
 
 
 
If(Null! = Out ){
 
 
 
ThrowNewIllegalstateexception ("Getoutputstream () has already been called on this response.");
 
 
}
 
 
 
Writer =NewPrintwriter (compressedout );
 
 
 
}
 
 
 
ReturnWriter;
 
 
 
}
 
 
 
PublicVoidFlushbuffer (){
 
 
 
Try{
 
 
 
If(Writer! =Null){
 
 
 
Writer. Flush ();
 
 
}ElseIf(Out! =Null){
 
 
 
Out. Flush ();
 
 
 
}
 
 
 
}Catch(Ioexception e ){
 
 
 
E. printstacktrace ();
 
 
 
}
 
 
 
}
 
 
 
PublicVoidReset (){
 
 
 
Super. Reset ();
 
 
Try{
 
 
 
Compressedout. Reset ();
 
 
 
}Catch(Ioexception e ){
 
 
 
ThrowNewRuntimeexception (E );
 
 
 
}
 
 
 
}
 
 
 
PublicVoidResetbuffer (){
 
 
 
Super. Resetbuffer ();
 
 
 
Try{
 
 
Compressedout. Reset ();
 
 
 
}Catch(Ioexception e ){
 
 
 
ThrowNewRuntimeexception (E );
 
 
 
}
 
 

 

 
}
 
 
 
PublicVoidClose ()ThrowsIoexception {
 
 
 
Compressedout. Close ();
 
 
 
}
 
 
 
 
 
 
 
}
 
 
 

 

After writing, restart the server. After the test is complete, make a comparison. The performance will certainly be greatly improved.

Other methods to accelerate webpage Loading

1. Compress JS scripts and CSS files. The purpose of compression is to remove comments, spaces, and other useless things to save space and speed up the transmission process. If you want to useProgramWe recommend that you use the yuicompressor open-source package, which is available on sourcefrog.

2. If you don't want to bother, you can use the online compressed web pages provided by enthusiastic people on the Internet, such

Jsmin online JS compression Tool

Compress the example:

3. There is also a batch processing program prepared by others, which can be used after JDK is installed in windows. This program is called jsminifier. If you are interested, check it online.

Summary

This article mainly aims to solve the problem of slow loading speed caused by more and more web pages such as scripts and CSS. The method used in this article is to first enable the server compression function inside the server, then perform automatic secondary compression in the program, and manually compress scripts and CSS. After three steps of compression, the webpage loading speed is significantly improved.

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.