Create a template page: 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>
========================================================== =
Write another JSP page: buildhtml. jsp
<% @ Page contenttype = "text/html; charset = gb2312" Import = "Java. util. *, java. Io. *" %>
<%
Try {
String title = "Li Peng's JSP generation of static html files ";
String content = "small sample, maybe you? ";
String editer = "hpsoft ";
String filepath = "";
Filepath = request. getrealpath ("/") + "template.htm ";
Out. Print (filepath );
String templatecontent = "";
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 );
Out. Print (templatecontent );
Templatecontent = templatecontent. replaceall ("### title ###", title );
Templatecontent = templatecontent. replaceall ("### content ###", content );
Templatecontent = templatecontent. replaceall ("### author ###", editer); // replace the corresponding part of the module
Out. Print (templatecontent );
// Get the file name by Time
Calendar calendar = calendar. getinstance ();
String fileame = string. valueof (calendar. gettimeinmillis () + ". html ";
Fileame = request. getrealpath ("/") + fileame; // the path for saving the generated HTML file
Fileoutputstream = new fileoutputstream (fileame); // create a file output stream
Out. Print ("file output path: <br> ");
Out. Print (fileame );
Byte tag_bytes [] = templatecontent. getbytes ();
Fileoutputstream. Write (tag_bytes );
Fileoutputstream. Close ();
}
Catch (exception e ){
Out. Print (E. tostring ());
}
%>