JSP static page generation practices and design ideas

Source: Internet
Author: User

Introduction:
Let's compare two large websites:
51job and Zhaopin (first, I declare that I am not making advertisements for them. I only use them as an example for technical comparison)
51job uses a relatively "advanced" php technology, while Zhaopin uses a relatively backward asp. however, we may obviously feel that the reaction speed of 51job is too slow than that of Zhaopin. Why? Careful people may notice it. Although zstack uses asp, it uses another Clever technology, asp, to generate static pages. All dynamic pages are converted to html static pages without accessing the database. Of course, the response is faster.
Next we will discuss how to convert jsp into html ??
First, create a template. The suffix is not limited, but it is generally used in the *. template example.
<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>
<P align = "center">
# Title # <BR>
Author: # author # <BR>
<BR>
# Content # <BR>
</P>
</Body>
</Html>
Create a class or jsp file for processing the template (to illustrate the problem, we will start with a jsp file as an example)
FilePath = request. getRealPath ("/") + "WEB-INF/templates/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 ("The following is the template content: <br>" + templateContent + "<br> The following is the html content after replacement <br> TemplateContent = templateContent. replaceAll ("# title #", title );
TemplateContent = templateContent. replaceAll ("# author #", editer); // replace the corresponding part of the module
TemplateContent = templateContent. replaceAll ("# content #", content );
// 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

Out. print (templateContent );
FileOutputStream fileoutputstream = new FileOutputStream (fileame); // create a file output stream
Byte tag_bytes [] = templateContent. getBytes ();
Fileoutputstream. write (tag_bytes );
Fileoutputstream. close ();
Well, the core technology is like this. If you require higher performance, you can use freemarker as a template.

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.