熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

jsp自動生成靜態文件(html)

2013-11-15 12:06:53  來源: JSP教程 

  htmljsp

  <%@ page contentType="text/html;charset=utf"%>
<%@page import="cnnbaianewPagenewsPage*cnnbaianewPagenewsImg*javautil*cnnbaianewPagecolumnPage*cnnbaiacompany*cnnbaialink*" %>
<%@page import="javaio*" %>
<%

  //在這裡如果寫成“WEBINF/templates/templatehtm”程序會報錯
String filePath = requestgetRealPath("/")+"test/templatehtm";
outprint(filePath);
String templateContent="";

  FileInputStream fileinputstream = new FileInputStream(filePath);//讀取模塊文件
int lenght = fileinputstreamavailable();
byte bytes[] = new byte[lenght ];
fileinputstreamread(bytes);
fileinputstreamclose();

  templateContent = new String(bytes);
int newsid=;//requestgetParameter("id");
newsCtl nCtl=new newsCtl();
news news=new news();
// newsImg newsimg=new newsImg();
//newsImgCtl nImgCtl=new newsImgCtl();
news=nCtlselectOneById(newsid);
//int rector=newsgetRedactor();
//String source=newsgetSource();
//String datetime=newsgetDateTime();
//int degree=newsgetBrowseDegree();
//int companyid=newsgetCompanyId();
String title=newsgetTitle();
String content=newsgetContent();
if(title==null) title="";
//if(source==null) source="";
//if(datetime==null) datetime="";
if(content==null) content="";
templateContent=templateContentreplaceAll("#title#"title);
templateContent=templateContentreplaceAll("#content#"content);
// 根據時間得文件名
Calendar calendar = CalendargetInstance();
String fileame = StringvalueOf(newsid) +"html";
String file = "filename/"+StringvalueOf(newsid) +"html";
fileame = requestgetRealPath("/")+"/filename/"+fileame;//生成的html文件保存路徑
outprint(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件輸出流
byte tag_bytes[] = templateContentgetBytes("utf");
//String username = "";
//username = new String(usernamegetBytes("ISO_")"GB");
fileoutputstreamwrite(tag_bytes);
fileoutputstreamclose();
responsesendRedirect("/"+file);
//outprint("<script>windowlocation/"+file+";</script>");
%>

  
模版頁面

  templatehtm

  <%@ page contentType="text/html;charset=utf"%>
<html>
<head>
<title>jsp</title>

  <meta httpequiv="keywords" content="keywordkeywordkeyword">
<meta httpequiv="description" content="this is my page">
<meta httpequiv="contenttype" content="text/html; charset=UTF">

  <!<link rel="stylesheet" type="text/css" href="/stylescss">>

  </head>

  <body><!top start><SPAN id=showTop></SPAN><IE:DOWNLOAD
id=oDownload style="BEHAVIOR: url(#default#download)" />
<SCRIPT>
function topOnDownloadDone(downDate){
showTopinnerHTML=downDate
}
oDownloadstartDownload(inc/topjsptopOnDownloadDone)
</SCRIPT>
<!top end>
<TABLE width="" border="" align="center" cellpadding="" cellspacing="">
<tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="dp">#title#</div></td></tr>
<tr><td valign="top" bgcolor="#FFFFFF"><div align="center" class="dp">#content#</div></td></tr>
</TABLE>
<!bottom start><SPAN id=showBottom></SPAN>
<SCRIPT>
function bottomOnDownloadDone(downDate)
{
showBottominnerHTML=downDate
}
oDownloadstartDownload(inc/bottomjspbottomOnDownloadDone)
</SCRIPT><!bottom end></body></html>


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20314.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.