Jsp (preferred for SUN Enterprise Applications) generate static hmtl files
To reduce the server pressure, the original article management system is changed from the data display of the jsp (preferred for SUN Enterprise Applications) file from the database to the jsp (preferred for SUN Enterprise applications) generate a static html file and directly access the html file. The following is a simple example.
1. buildhtml. jsp (preferred for SUN Enterprise applications)
<% @ Page contentType = "text/html; charset = gb2312" import = "java. util. *, java. io. *" %>
<%
Try {
String title = "jsp (preferred for SUN Enterprise Applications) 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 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 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>