JSP generation static page practice and Design idea []_jsp programming
Source: Internet
Author: User
Introduction:
We compare it to two large Web sites for example:
51job and Zhaopin (first of all, I'm not advertising for them, I'm just using them as an example for technical comparisons)
51job is a comparison of the "Advanced" PHP technology, and the wisdom of the use of the relatively backward ASP. But we may obviously feel that the 51job reaction rate compared to Zhaopin is really too slow, why this? A careful person may be aware of it. Although the--asp uses ASP, he uses another, more ingenious technique, to generate static page technology. All the dynamic pages are basically converted into HTML static pages, without accessing the database, of course, the response is fast.
Let's discuss how JSP translates into HTML??
The first thing to do is to make a template. Suffixes are not limited, but generally use *.template examples
<title> #title #</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<link href= ". /css.css "Rel=stylesheet type=text/css>
<body>
<p align= "center" >
#title #<br><br><br>
Author: #author #<br><br>
<BR>
#content #<br><br><br><br>
</P>
</body>
To do a class or JSP file that processes the template (to illustrate the problem we start with a JSP file for example)
FilePath = Request.getrealpath ("/") + "web-inf/templates/template.htm";
Out.print (FilePath);
String templatecontent= "";
FileInputStream FileInputStream = new FileInputStream (filePath);//Read Module file
int lenght = fileinputstream.available ();
byte bytes[] = new Byte[lenght];
Fileinputstream.read (bytes);
Fileinputstream.close ();
TemplateContent = new String (bytes);
Out.print ("Below is the template content:<br>" +templatecontent+ "<br> below is the replacement HTML content <br>Templatecontent=templatecontent.replaceall ("#title #", title);
Templatecontent=templatecontent.replaceall ("#author #", editer);//replace the corresponding place in the module
Templatecontent=templatecontent.replaceall ("#content #", content);
According to the time file name
Calendar calendar = Calendar.getinstance ();
String fileame = string.valueof (Calendar.gettimeinmillis ()) + ". html";
Fileame = Request.getrealpath ("/") +fileame;//the generated HTML file save path
Out.print (TemplateContent);
FileOutputStream FileOutputStream = new FileOutputStream (fileame);//create file output stream
byte tag_bytes[] = Templatecontent.getbytes ();
Fileoutputstream.write (tag_bytes);
Fileoutputstream.close ();
Well, the core technology is this, if you require higher performance, you can use Freemarker to do the template.
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.