Use JSP to generate static pages

Source: Internet
Author: User

Please refer to the main principle of http://blog.hunan2s.com/article.asp? Id = 233

After debugging, it is successful .. Ah

Add the source code ..

JDK 1.5 + ECLIPSE + TOMCAT 5.0.28 + MYSQL 5.0

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

MakeFile. jsp

<%
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];

Fileinputstream. read (bytes); // read (byte [] B) reads data of up to B. length bytes into a byte array from the input stream.

Fileinputstream. close ();
// TemplateContent = new String (bytes );
String title;
String content;
String author;
While (Rs. next ())
{
TemplateContent = new String (bytes); // if this sentence is not used, the # ** # flag does not exist in templateContent after it is replaced. So we need to generate a new one.
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> TemplateContent = templateContent. replaceAll ("# title #", title );
TemplateContent = templateContent. replaceAll ("# author #", author); // 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 ("/") + "Html/" + fileame; // save path of 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 ();

}

If (conn! = Null)
{
Conn. close ();
}
If (stmt! = Null)
{
Stmt. close ();
}

%>

// Database connection file

Import java. SQL .*;
Public class DBconn {

Public DBconn (){

// 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>

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.