首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>jsp教程>资讯:JSP生成静态Html文件的3种方法

JSP生成静态Html文件的3种方法

www.jz123.cn  2010-10-08   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

方法1:

为了减轻服务器压力,将原来的文章管理系统由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

<html>

<head>

<title>JSP教程:http://www.javaweb.cc/language/jsp/ 生成静态模板文件</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<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### </td></tr>

<tr><td>###content###</td></tr>

</table>

</body>

</html>



上一篇:JSP生成图片验证码代码 下一篇:JSP生成数字验证码教程

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图