站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题JSP生成静态的hmtl文件

JSP生成静态的hmtl文件

摘要: JSP生成静态的hmtl文件

jsp生成静态的hmtl文件
为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例

1.buildhtml.jsp

<%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
<%
try{
String title="jsp生成静态html文件";
String content="小样,还搞不定你?";
String editer="hpsoft";
String filePath = "";
filePath = request.getRealPath("/")+"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);
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());
}

%>

模板文件

2. template.htm


<head>
###title###

<link> href="../css.css" rel=stylesheet type=text/css>
</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###&nbsp;&nbsp;</td>
</tr>
<tr>
<td>###content###
</td>

</tr>

</table>
</body>

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-2624228443779279&dt=1114338484625&format=250x250_as&output=html&color_bg=FFFFFF&color_text=666666&color_link=000000&color_url=000000&color_border=FFFFFF&u_h=768&u_w=1024&u_ah=736&u_aw=1024&u_cd=32&u_tz=480&u_his=18&u_java=true" frameborder="0" width="250" scrolling="no" height="250" allowtransparency="65535"></iframe>

↑返回目录
前一篇: 减少jsp文件目录的维护成本,需要一种什么样的JSP发布框架?
后一篇: jsp中config.java文件的public String systemURL该怎么写