Using JSP to generate static page _jsp programming

Source: Internet
Author: User
Tags stmt
Main principle please refer to http://blog.hunan2s.com/article.asp?id=233

After a turn of debugging, success. Oh

Attached 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, Content varchar, 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 being blocked.

byte bytes[] = new Byte[lenght];

Fileinputstream.read (bytes); Read (byte[] b) reads 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 do not use this sentence, after the replacement, there is no #** #标志了 in TemplateContent. So to regenerate
title = rs.getstring ("title");
Content = rs.getstring ("content");
Author = rs.getstring ("author");
Out.println (title+ "********" +content+ "* * *" +author);
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 #", author);//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 ("/") + "html/" +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 ();

}

if (conn!=null)
{
Conn.close ();
}
if (stmt!=null)
{
Stmt.close ();
}



%>

Database connection files

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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> #title #</title>

<body>
<table width= "380" height= "border=" 0 "cellpadding=" 0 "cellspacing=" 1 "bgcolor=" #FFCC99 ">
<tr>
&LT;TD height= "bgcolor=" "#FFCC99" ><div align= "center" > #title #</div></td>
</tr>
<tr>
&LT;TD bgcolor= "#FFFFFF" > #content #</td>
</tr>
<tr>
&LT;TD height= align= "right" bgcolor= "#FFFFFF" > #author #</td>
</tr>
</table>
</body>
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.