jsp自动生成静态文件(html)

时间:2022-11-12 23:47:14

jsp自动生成静态文件(html):

   html.jsp

     <%@ page contentType="text/html;charset=utf-8"%>
<%@page import="cn.nbaia.newPage.newsPage.*,cn.nbaia.newPage.newsImg.*,java.util.*,cn.nbaia.newPage.columnPage.*,cn.nbaia.company.*,cn.nbaia.link.*" %>
<%@page import="java.io.*" %>
<%

//在这里如果写成“WEB-INF/templates/template.htm”程序会报错
 String filePath = request.getRealPath("/")+"test/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);
  int newsid=2484;//request.getParameter("id");
  newsCtl nCtl=new newsCtl();
  news news=new news();
 // newsImg newsimg=new newsImg();
  //newsImgCtl nImgCtl=new newsImgCtl();
news=nCtl.selectOneById(newsid);
//int rector=news.getRedactor();
//String source=news.getSource();
//String datetime=news.getDateTime();
//int degree=news.getBrowseDegree();
//int companyid=news.getCompanyId();
String title=news.getTitle();
String content=news.getContent();
if(title==null) title="";
//if(source==null) source="";
//if(datetime==null) datetime="";
if(content==null) content="";
 templateContent=templateContent.replaceAll("#title#",title);
 templateContent=templateContent.replaceAll("#content#",content);

 // 根据时间得文件名
 Calendar calendar = Calendar.getInstance();
 String fileame = String.valueOf(newsid) +".html";
 String file = "filename/"+String.valueOf(newsid) +".html";
 fileame = request.getRealPath("/")+"/filename/"+fileame;//生成的html文件保存路径
 out.print(templateContent);
 FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
 byte tag_bytes[] = templateContent.getBytes("utf-8");
 //String username = "00";
//username = new String(username.getBytes("ISO8859_1"),"GB2312");
 fileoutputstream.write(tag_bytes);
 fileoutputstream.close();
response.sendRedirect("../"+file);
 //out.print("<script>window.location.href='../"+file+"';</script>");
 %>
模版页面:

  template.htm

     <%@ page contentType="text/html;charset=utf-8"%>
<html>
  <head>
    <title>jsp</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body><!--top start--><SPAN id=showTop></SPAN><IE:DOWNLOAD
id=oDownload style="BEHAVIOR: url(#default#download)" />
<SCRIPT>
function topOnDownloadDone(downDate){
showTop.innerHTML=downDate
}
oDownload.startDownload('inc/top.jsp',topOnDownloadDone)
</SCRIPT>
<!--top end-->
<TABLE width="778" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="d9p">#title#</div></td></tr>
  <tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="d9p">#content#</div></td></tr>
  </TABLE>
<!--bottom start--><SPAN id=showBottom></SPAN>
<SCRIPT>
function bottomOnDownloadDone(downDate)
{
showBottom.innerHTML=downDate
}
oDownload.startDownload('inc/bottom.jsp',bottomOnDownloadDone)
</SCRIPT><!--bottom end--></body></html>