JSP generates static hmtl files
To reduce the pressure on the server, change the original article management system to generate static html files from JSP files and then access the HTML files directly. The following is a simple example.
1. buildhtml. jsp
<%... @ Page contenttype = "text/html; charset = gb2312" Import = "Java. util. *, java. Io. *" %>
<%...
Try {
String title = "JSP static html file generation ";
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
Byte tag_bytes [] = templatecontent. getbytes ();
Fileoutputstream. Write (tag_bytes );
Fileoutputstream. Close ();
}
Catch (exception e ){
Out. Print (E. tostring ());
}
%>
Template File
2. 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>