日期:2014-05-18  浏览次数:20744 次

如何生成静态页
我的页面上要调用数据库中的数据,但是每次在刷新页面的时候都要重新调用数据库,我想在第一次启动页面时调用数据库,并且生成静态页,下次刷新时不用在去数据库中读取,而是直接调用静态页;如果数据库的内容发生改变时,再重新读取。

不知道大家用什么方法实现的呢?

------解决方案--------------------
沙发,我也想知道,不访问数据库如何能知道数据库是不是改过?
------解决方案--------------------
嗯,可以
不过要保证只有你这个程序唯一维护着数据,这样的话修改之后可以保证数据的正确性
------解决方案--------------------
可以用velocity模版生成静态页面。
------解决方案--------------------
总之要有一个事件来触发它才行,不然不访问数据库怎么知道是不是更新了,
------解决方案--------------------
刚解决了生成静态页的问题,发给你吧!根据自己的架构可以改改!!
test.jsp
<%@ page contentType= "text/html; charset=gb2312 " import= "java.util.*,java.io.* "%>
<%
try{
String title= "This is Title ";
String content= "This is Content Area ";
String editer= "LaoMao ";
String filePath = " ";
filePath = request.getRealPath( "/ ")+ "/template.htm ";
//out.print(filePath+ " <br> ");
String templateContent= " ";
FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
//out.print(templateContent);
templateContent=templateContent.replaceAll( "###title### ",title);
templateContent=templateContent.replaceAll( "###content### ",content);
templateContent=templateContent.replaceAll( "###author### ",editer);//替换掉模块中相应的地方
//out.print(templateContent);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) + ".html ";
fileame = request.getRealPath( "/ ")+fileame;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}

%>

template.htm
<html>
<head>
<title> ###title### </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>
<body>
<table width= "500 " border= "0 " align= "center " cellpadding= "0 " cellspacing= "2 ">
<tr>
<td align= "center "> ###title### </td>
</tr>
<tr>
<td align= "center "> author:###author###&nbsp;&nbsp; </td>
</tr>
<tr>
<td> ###content###
</td>
</tr>
</table>
</body>
</html>

我是按这个例子写的,成功了!你看看吧

------解决方案--------------------
推荐使用freemarker或者velocity。
------解决方案--------------------
可以把JSP页南生与的.java文件上着手,把JspWriter out = null;换成PrintWriter out=null;

然后运行.java中的_jspService方法,把内容输出到一个.html文件即可!


------解决方案--------------------
我也不会,学习,望懂的人,把方法公布下
------解决方案--------------------