您的位置:首页 > Web前端 > CSS

双管齐下--采用压缩传输加快网页显示速度

2012-04-15 23:40 405 查看

主题

      本文主要针对Tomcat、JBoss系列服务器,采用JSP、Java开发的网站在页面加载过程中对其中包含的Javascript脚本和CSS和图片等进行压缩传输,以加快页面的显示速度,经过试验对比,效果比较明显。

配置一:JBOSS服务器的配置

     在JBOSS的server.xml文件中找到Connector port="80"或者Connector port="8080"这一项,这是配置服务器端口的,在其中添加一个开启压缩传输的参数和要压缩传输的文件的后缀名。     按照上面的配置完毕后,重新启动服务器,看看效果~~。

配置二:在项目中添加压缩函数

    配置完服务器后,网页的显示效果有了一定的提升,如果还想要进一步的提升,那么就需要自己辛苦一下,在项目中做一下配置了,在Web.xml中添加一个过滤器,对指定的文件进行gzip压缩,由于gzip的压缩效率很高,所以往往会取得不错的效果。    具体的做法如下:过滤器的实现代码如下:
package com.guet.cab.base.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(){
}
  }
 其中调用的两个函数分别为:
package com.guet.cab.base.Filter;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
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 );
   }
}
函数2:
package com.guet.cab.base.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();
    }
}
 编写完毕后重新启动服务器,测试没有问题后进行对比一下吧,性能肯定会有比较大的提高。

其他的一些加快网页加载速度的方法

      1.对js脚本和css文件进行压缩,压缩的目的是去掉其中的注释和空格等无用的东西,节省空间,在传输的过程中也会加快,如果想用程序实现自动压缩,建议使用yuicompressor这个开源的包,在sourcefrog上面都有的。      2.如果不想麻烦的话可以使用网上热心的人提供的在线的压缩网页,如jsmin在线js压缩工具对其中的例子进行压缩的截图:    3.还有就是别人做好的批处理程序,在Windows下安装JDK就可以使用,这个程序叫jsMinifier,有兴趣的话可以到网上查一下。

总结

      主要是为了解决网页内脚本和CSS等越来越多所造成的网页加载速度越来越慢的问题,本文采用的方法是先在服务器内部开启服务器压缩功能,然后在程序中进行自动二次压缩,并对脚本和CSS等进行手动压缩,经过三步压缩过程,网页的加载速度有了明显的提高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息