电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

用CashFilter实现页面级缓存实践


发布日期:2023/12/25
 

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后页面变化

上一篇:画图时如何设置线条的粗细

下一篇:“浅薄”绝不该是中国程序员的性格特征