Introduction:
Let’s take two large websites as an example for comparison:
51job and Zhaopin Recruitment (I must first state that I am not advertising for them, I am only using them as examples for technical comparison)
51job uses relatively "advanced" PHP technology, while Zhaopin uses relatively backward ASP. But we may clearly feel that 51job's response speed is too slow compared to Zhaopin. Why is this? An attentive person may notice it. Although Zhilian uses ASP, it uses another more clever technology - ASP static page generation technology. All dynamic pages are basically converted into HTML static pages without accessing the database. Of course, the response is fast.
Let's discuss how to convert jsp into html??
First make a template. There is no limit to the suffix, but *.template is generally used as an 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><BR><BR>
Author: #author#<BR><BR>
<BR>
#content#<BR><BR><BR><BR>
</P>
</body>
</html>
Make a class or jsp file that processes templates (to illustrate the problem, let’s start with a simple jsp file as an 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("The following is the template content:<br>"+templateContent+"<br> The following is the html content after replacement<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",editer);//Replace the corresponding places in the module
templateContent=templateContent.replaceAll("#content#",content);
// Get file name based on time
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//The generated html file saving path
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 instead.
After some debugging, it was successful. . Attached
is the source code. .
JDK 1.5 +ECLIPSE +TOMCAT 5.0.28 + MYSQL 5.0
database TEST, table name news
Fields: id int automatically grows, Title varchar(20), Content varchar(200), Author varchar(10)
makeFile.jsp
<%
Connection conn = DBconn.getConnection();
Statement stmt = conn.createStatement();
ResultSet Rs = stmt.executeQuery("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 this file input stream without blocking.
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) Read up to b.length bytes of data from this input stream into a byte array.
fileinputstream.close();
//templateContent = new String(bytes);
String title;
String content;
String author;
while(Rs.next())
{
templateContent = new String(bytes);//If you don't use this sentence, after replacing it once, there will be no #**# mark in templateContent. So to regenerate
title = Rs.getString("Title");
content = Rs.getString("Content");
author = Rs.getString("Author");
out.println(title+"********"+content+"****"+author);
out.print("The following is the template content:<br>"+templateContent+"<br> The following is the html content after replacement<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",author);//Replace the corresponding places in the module
templateContent=templateContent.replaceAll("#content#",content);
// Get file name based on time
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+"Html/"+fileame;//Generated html file saving path
out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//Create a file output stream
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
if(conn!=null)
{
conn.close();
}
if(stmt!=null)
{
stmt.close();
}
%>
//Database connection file
import java.sql.*;
public class DBconn {
publicDBconn() {
// TODO Auto-generated constructor stub
}
public static Connection getConnection()
{
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://" + "localhost" + "/" + "test" +
"?useUnicode=true&characterEncoding=GB2312","root","111111");
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
/*public static void main(String[] args) throws Exception
{
Connection con=getConnection();
System.out.println(con.isClosed());
}
*/
}
//Template file
template.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>#title#</title>
</head>
<body>
<table width="380" height="107" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFCC99">
<tr>
<td height="16" bgcolor="#FFCC99"><div align="center">#title#</div></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">#content#</td>
</tr>
<tr>
<td height="13" align="right" bgcolor="#FFFFFF">#author#</td>
</tr>
</table>
</body>
</html>