一:  小试 EhCache web 用来缓存JSP页面

0) 涉及到的jar包

     ehcache-core-2.5.2.jar,

     ehcache-web-2.0.4.jar

1) web.xml 

    这里使用了个简单的过滤器来拦截所有的jsp请求 

 

   

         PageCacheFilternet.sf.ehcache.constructs.web.filter.SimplePageCachingFilter  

   

 

    PageCacheFilter/*.jsp  

 

2) ehcache.xml

 

 

   

           maxElementsInMemory="10000"  

           maxElementsOnDisk="1000"  

           eternal="false"  

           overflowToDisk="true"  

           timeToIdleSeconds="5"  

           timeToLiveSeconds="10"  

           memoryStoreEvictionPolicy="LFU"  

            />  

   

            maxElementsInMemory="10000"  

            eternal="false"  

            timeToIdleSeconds="120"  

            timeToLiveSeconds="120"  

            overflowToDisk="true"  

            maxElementsOnDisk="10000000"  

            diskPersistent="false"  

            diskExpiryThreadIntervalSeconds="120"  

            memoryStoreEvictionPolicy="LRU"  

            />  

 

3)一个简单的index.jsp页面来打印出日志

 <%@page import="java.sql.ResultSet"%>  

<%@page import="com.db.DB"%>  

<%@ page language="java" contentType="text/html; charset=utf-8"  

    pageEncoding="utf-8"%>  

 

 

 

 

测试  

 

 

<%  

    System.out.println(System.currentTimeMillis());  

%> 

 

 

4)测试方法 

  1.启动了项目去访问index.jsp页面 

  2.刷新index.jsp页面,看后台是否打印出日志

  3.其实使用页面缓存已对文件进行了gzip压缩了。无需在使用下面的GzipFilter进行过滤处理。

 

二、使用gzip优化web应用(filter实现)

    测试时没有发现有用,似乎tomcat已经启用了gzip功能 

   gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,

   返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果开启Gzip ,那么服务器端响应后,会将页面,

   JS,CSS等文本文件或者其他文件通过高压缩算法将其压缩,然后传输到客户端,由客户端的浏览器负责解压缩与呈现。

   通常能节省40%以上的流量   

 1) web.xml中添加过滤器

   

       gzipFilter  

       

           net.sf.ehcache.constructs.web.filter.GzipFilter

       

   

   

        gzipFilter

        *.js

        *.tpl

        *.prd

        *.ftl

        *.html

        *.css

   


http://happysoul.iteye.com/blog/1151692