pharaohsprince 发表于 2013-2-6 08:45:37

jsp生成静态的hmtl文件

jsp生成静态的hmtl文件
为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例
1.buildhtml.jsp 
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif<%...@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif<%...
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftry{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String title="jsp生成静态html文件";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String content="小样,还搞不定你?";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String editer="hpsoft";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String filePath = "";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif filePath = request.getRealPath("/")+"template.htm";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif out.print(filePath);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String templateContent="";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int lenght = fileinputstream.available();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif byte bytes[] = new byte;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fileinputstream.read(bytes);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fileinputstream.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif templateContent = new String(bytes);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif out.print(templateContent);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif templateContent=templateContent.replaceAll("###title###",title);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif templateContent=templateContent.replaceAll("###content###",content);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif templateContent=templateContent.replaceAll("###author###",editer);//替换掉模块中相应的地方
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif out.print(templateContent);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 根据时间得文件名
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Calendar calendar = Calendar.getInstance();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif byte tag_bytes[] = templateContent.getBytes();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fileoutputstream.write(tag_bytes);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fileoutputstream.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcatch(Exception e){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif out.print(e.toString());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif%>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: jsp生成静态的hmtl文件