当前页面: 开发资料首页 → Java 专题 → JSP生成静态页实践及其设计思想
JSP生成静态页实践及其设计思想
摘要: JSP生成静态页实践及其设计思想
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="278" height="86" align="center" valign="top">
</td>
<td width="429" valign="top">
如果将所有的动态页都转换成了html静态页,不用访问数据库,网页反应应该更快。
下面我们讨论一下jsp怎么转换成html??
首先要做一个模板。后缀不限,但一般都用*.template例子
<head>
#title#
<link> href="../css.css" rel=stylesheet type=text/css>
</head>
<body>
#title#
作者:#author#
#content#
</body>
</td>
</tr>
<tr>
<td height="20" colspan="2">
做一个处理模板的类或者jsp文件、(为说明问题我们从简单入手以一个jsp文件为例)
filePath = request.getRealPath("/")+"WEB-INF/templates/template.htm";
out.print(filePath);
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+"
以下是置换以后的html内容
");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",editer);//替换掉模块中相应的地方
templateContent=templateContent.replaceAll("#content#",content);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
嗯,核心技术就是这样了,如果大家要求的性能更高,可以改用freemarker做模板。freemarker的使用方法可以参阅我的另一篇文章《Freemarker 模板工具的作用及使用》
</td>
</tr>
</table>
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: 《JSP程序设计》试卷一套
后一篇: 深入研究Servlet线程安全性问题