熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

用CashFilter實現頁面級緩存實踐

2022-06-13   來源: JSP教程 

  mysql在本地localhost的test數據庫 建person表暫以字段idnamepassword 表中可輸入一行值

  建類 DAOUtil

  import  javasqlConnection;
import  javasqlDriverManager;

  public class DAOUtil {
    public DAOUtil() {
    }

  public static Connection getConn() {
        Connection conn = null;
        String db_url = jdbc:mysql://:/test?useUnicode=true&characterEncoding=gb;
        String db_user = root;
        String db_password= admin;
        String db_driver = commysqljdbcDriver;

  try {
            ClassforName(db_driver);
            conn = DriverManagergetConnection(db_url db_user db_password);
             } catch (Exception ex) {
             exprintStackTrace();
        }
        return conn;
    }

  /**
  * dbclose
  * 關閉ConnectionStatement
  * @param conn Connection
  * @param stmt Statement
  */
 public static void dbclose(Connection conn Statement stmt) {
       try {
           if (stmt != null)
               stmtclose();
       } catch (Exception e) {
         eprintStackTrace();
       }
       try {
           if (conn != null)
               connclose();
       } catch (Exception e) {
          eprintStackTrace();
       }
   }

  }

  建類 PersonDAO

  import javautil*;
import javasqlConnection;
import javasqlStatement;
import javasqlResultSet;

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

  public PersonDAO() {
        super();    }

  public static void main(String[] args) {
          Systemoutprintln(person===+PersonDAOgetInstance()getPersonInfo());
    }
  
       private static List getPersonInfo() {
          String name  = ;
          String password = ;
          Connection conn = null;
          List list = new javautilArrayList();
          Statement stmt = null;
          String sql =
                  select namepassword from person;
          try {
              conn = DAOUtilgetConn();
              stmt = conncreateStatement();
              ResultSet rs = stmtexecuteQuery(sql);
              while (rsnext()) {
                  name = (String)rsgetString(name);
                  password = (String)rsgetString(password);
                  listadd(name++password);
                 }
          } catch (Exception ex) {
              exprintStackTrace();
               } finally {
              DAOUtildbclose(conn stmt);
          }
          return list;
      } 
}

  建testCachejsptestCachejsp

  <%@page contentType=text/html; charset=GBK%>
<%@page import=commcskydaoPersonDAO session=false%>
<%@page import=javautilList session=false%>
<html>
<body bgcolor=#FFFFFF>
test hello world!!<p>
  <table width=% border= align=center>
   <tr>
      <td width=% height=> <div align=center>序號</div></td>
      <td width=%><div align=center>名字</div></td>
      <td width=%><div align=center>密碼 </div></td>
       </tr>
<%
//todo connect mysql server (localhost root/admin) and test(db) person(table)
List list =PersonDAOgetInstance()getPersonInfo();
Systemoutprintln(size===+listsize());
for(int i=;i<listsize();i++){
String namePass = (String)listget(i);
javautilStringTokenizer st = new  javautilStringTokenizer(namePass);
String   name = stnextToken();
String    pass = stnextToken();
Systemoutprintln(namePass++++++namePass);
%>
  <tr>
      <td><div align=center><%=i+%></div></td>
      <td> <div align=center><%=name%></div></td>
      <td><div align=center><%=pass%></div></td>
      </tr>
    <%
        }
%>
  </table>

  </body>
</html>

  把module部署在tomcat的webapp下

   在WEBINF\lib\logjjarmysqlconnectorjavastablebinjaroscachejarcommonsloggingjarjgroupsalljar

   在WEBINF\classes拷貝cach\etc\下的oscachetldoscacheproperties

   只對/testCachejsp緩存

  修改WEBINF\webxml

  <?xml version= encoding=UTF?>
<!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>
<webapp>
  <displayname>test</displayname>
  <taglib><tagliburi>oscache</tagliburi><tagliblocation>/WEBINF/classes/ oscachetld</tagliblocation></taglib>
   <filter>   
   <filtername>CacheFilter</filtername>
   <filterclass>comopensymphonyoscachewebfilterCacheFilter</filterclass>
   </filter>
   <filtermapping>
   <filtername>CacheFilter</filtername> 
       <urlpattern>/testCachejsp</urlpattern>
      </filtermapping>
    
</webapp>

  啟動tomcat在mysql的person中增加一條記錄發現已對/testCachejsp頁面進行緩存而/testCachejsp頁面未緩存如去掉黑體則數據表增加一行testCachejsp也隨之修改

  對所有的jsp緩存

  修改WEBINF\webxml

  <?xml version= encoding=UTF?>
<!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>
<webapp>
  <displayname>test</displayname>
    <filter>   
        <filtername>CacheFilter</filtername>
        <filterclass>comopensymphonyoscachewebfilterCacheFilter</filterclass>
          <initparam>       
           <paramname>time</paramname>
                    <paramvalue></paramvalue>  
           </initparam>   
          <initparam>                           
          <paramname>scope</paramname>
                     <paramvalue>session</paramvalue>
          </initparam>
          </filter>
          <filtermapping>
          <filtername>CacheFilter
          </filtername>
            <urlpattern>*jsp</urlpattern>
          </filtermapping>
</webapp>

  其中s是失效時間在mysql的person中增加一條記錄發現已對/testCachejsp/testCachejsp頁面進行緩存頁面不變化s後頁面變化


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19148.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.