JSP generates static hmtl files

Source: Internet
Author: User

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.