gembin

OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

HBase, Hadoop, ZooKeeper, Cassandra

Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

About Me

 

用CashFilter实现页面级缓存实践

用CashFilter实现页面级缓存实践
 

1.mysql在本地localhost的test数据库 建person表,暂以字段id,name,password ,表中可输入一行值.

2.建类 DAOUtil 

import  java.sql.Connection;
import  java.sql.DriverManager;

public class DAOUtil {
    public DAOUtil() {
    }

   public static Connection getConn() {
        Connection conn = null;
        String db_url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gb2312";
        String db_user = "root";
        String db_password= "admin";
        String db_driver = "com.mysql.jdbc.Driver";

        try {
            Class.forName(db_driver);
            conn = DriverManager.getConnection(db_url, db_user, db_password);
             } catch (Exception ex) {
             ex.printStackTrace();
        }
        return conn;
    }

  /**
  * dbclose
  * 关闭Connection,Statement
  * @param conn Connection
  * @param stmt Statement
  */
 public static void dbclose(Connection conn, Statement stmt) {
       try {
           if (stmt != null)
               stmt.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
       try {
           if (conn != null)
               conn.close();
       } catch (Exception e) {
          e.printStackTrace();
       }
   }

}

3.建类 PersonDAO

import java.util.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;


public class PersonDAO {
    static PersonDAO pdao;
    public static PersonDAO getInstance() {
        if (pdao == null) {
            synchronized (PersonDAO.class) {
                pdao = new PersonDAO();
            }
        }
        return pdao;
    }

    public PersonDAO() {
        super();    }

    public static void main(String[] args) {
          System.out.println("person==="+PersonDAO.getInstance().getPersonInfo());
    }
   
       private static List getPersonInfo() {
          String name  = "";
          String password = "";
          Connection conn = null;
          List list = new java.util.ArrayList();
          Statement stmt = null;
          String sql =
                  "select name,password from person";
          try {
              conn = DAOUtil.getConn();
              stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery(sql);
              while (rs.next()) {
                  name = (String)rs.getString("name");
                  password = (String)rs.getString("password");
                  list.add(name+","+password);
                 }
          } catch (Exception ex) {
              ex.printStackTrace();
               } finally {
              DAOUtil.dbclose(conn, stmt);
          }
          return list;
      } 
}

4.建testCache.jsp,testCache2.jsp

<%@page contentType="text/html; charset=GBK"%>
<%@page import="com.mcsky.dao.PersonDAO" session="false"%>
<%@page import="java.util.List" session="false"%>
<html>
<body bgcolor="#FFFFFF">
test ,hello world!!<p>
  <table width="85%" border="1" align="center">
   <tr>
      <td width="8%" height="20"> <div align="center">序号</div></td>
      <td width="12%"><div align="center">名字</div></td>
      <td width="17%"><div align="center">密码 </div></td>
       </tr>
<%
//todo connect mysql server (localhost root/admin) and test(db) person(table)
List list =PersonDAO.getInstance().getPersonInfo();
System.out.println("size==="+list.size());
for(int i=0;i<list.size();i++){
String namePass = (String)list.get(i);
java.util.StringTokenizer st = new  java.util.StringTokenizer(namePass,",");
String   name = st.nextToken();
String    pass = st.nextToken();
System.out.println("namePass+++++"+namePass);
%>
  <tr> 
      <td><div align="center"><%=i+1%></div></td>
      <td> <div align="center"><%=name%></div></td>
      <td><div align="center"><%=pass%></div></td>
      </tr>
    <%
        }
%>
  </table>

</body>
</html>

5.把module部署在tomcat的webapp下

5.1 在WEB-INF\lib\log4j-1.2.8.jar,mysql-connector-java-3.0.11-stable-bin.jar,oscache-2.3.2.jar,commons-logging.jar,jgroups-all.jar

5.2 在WEB-INF\classes拷贝cach\etc\下的oscache.tld,oscache.properties

5.3 只对/testCache.jsp缓存

修改WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>test</display-name>
  <taglib><taglib-uri>oscache</taglib-uri><taglib-location>/WEB-INF/classes/ oscache.tld</taglib-location></taglib> 
   <filter>    
   <filter-name>CacheFilter</filter-name>
   <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
   </filter>
   <filter-mapping>
   <filter-name>CacheFilter</filter-name>  
       <url-pattern>/testCache.jsp</url-pattern>
      </filter-mapping>
     
</web-app>

启动tomcat,在mysql的person中增加一条记录,发现已对/testCache.jsp页面进行缓存,而/testCache2.jsp页面未缓存,如去掉黑体,则数据表增加一行,testCache.jsp也随之修改

5.4对所有的jsp缓存,

修改WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>test</display-name>
      <filter>    
        <filter-name>CacheFilter</filter-name>
        <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
          <init-param>        
           <param-name>time</param-name> 
                    <param-value>60</param-value>   
           </init-param>    
          <init-param>                            
          <param-name>scope</param-name>
                     <param-value>session</param-value> 
          </init-param>
          </filter>
          <filter-mapping>
          <filter-name>CacheFilter
          </filter-name>
            <url-pattern>*.jsp</url-pattern>
          </filter-mapping>
</web-app>

其中60s,是失效时间,在mysql的person中增加一条记录,发现已对/testCache.jsp,/testCache2.jsp页面进行缓存,页面不变化,60s后页面变化.

posted on 2007-09-21 11:05 gembin 阅读(351) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(6)

随笔分类(440)

随笔档案(378)

文章档案(6)

新闻档案(1)

相册

收藏夹(9)

Adobe

Android

AS3

Blog-Links

Build

Design Pattern

Eclipse

Favorite Links

Flickr

Game Dev

HBase

Identity Management

IT resources

JEE

Language

OpenID

OSGi

SOA

Version Control

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

free counters