JSP generates static html files

Source: Internet
Author: User
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>

Related Article

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.