JSP generates static Web pages. This method is successfully tested.

Source: Internet
Author: User
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 = 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> <HR> ");
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 = 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>










#title #
# content #
# author #


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.