您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

JSP生成静态页实践及其设计思想[2]

来源:未知 责任编辑:智问网络 发表时间:2013-10-30 11:08 点击:

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

1.buildhtml.jsp(SUN企业级应用的首选)

<%@ 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("/")+"test/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());
}

%>

2. template.htm

<html>
<head>
<title>###title###</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:###author###&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td>###content###
 </td>
 
  </tr>

</table>
</body>
</html>

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • JSP与Servlet
    • 自己动手写MiniBBS系列(基本篇)之用户登录
    • JSP取当前日期
    • JDBC 入门(一)
    • 打开一个jsp页面默认查询所有数据,调用action
    • 使用JSP标签库验证用户的输入(2)完
    • WIN98/2000下的jsp服务器
    • 自定义JSP标签(tag)浅议
    • Struts学习傻瓜式入门篇
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1