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. *" %>
<%
// Here if the program is written as "WEB-INF/templates/template.htm", an error will be reported
String filePath = request. getRealPath ("/") + "test/template.htm ";
Out. print (filePath );
String templateContent = "";
FileInputStream fileinputstream = new FileInputStream (filePath); // read the module File
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 = 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 );
// Get the file name by Time
Calendar calendar = Calendar. getInstance ();
String fileame = String. valueOf (newsid) + ". html ";
String file = "filename/" + String. valueOf (newsid) + ". html ";
Fileame = request. getRealPath ("/") + "/filename/" + fileame; // save path of the generated html file
Out. print (templateContent );
FileOutputStream fileoutputstream = new FileOutputStream (fileame); // create a file output stream
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 page:
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. js', 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. js', bottomOnDownloadDone)
</SCRIPT> <! -- Bottom end --> </body>
Source: csdn