JSP static page generation practices and design ideas [1]

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 (the first choice for SUN enterprise-level applications) 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>
Make a template-processing class or jsp (preferred for SUN Enterprise Applications) file (to illustrate the problem, let's start with a jsp (preferred for SUN Enterprise Applications) 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.

After debugging, it is successful .. Ah

Add the source code ..


JDK 1.5 + ECLIPSE + tomcat (a good JSP running platform) 5.0.28 + MySQL (the best combination with PHP) 5.0

Database TEST, table name news
Field: id int auto-increment, Title varchar (20), Content varchar (200), Author varchar (10)

MakeFile. jsp (preferred for SUN Enterprise applications)

<%
Connection conn = DBconn. getConnection ();
Statement stmt = conn. createStatement ();
ResultSet Rs = stmt.exe cuteQuery ("select * from news ");
System. out. println ("success ");

%>
 
<%

String filePath = request. getRealPath ("/") + "template.htm ";

System. out. println (filePath );

String templateContent;
FileInputStream fileinputstream = new FileInputStream (filePath );
Int lenght = fileinputstream. available (); // available () returns the number of bytes that can be read from the input stream of this file without blocking.

Byte bytes [] = new byte [lenght];

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.