用JSP生成静态页面
程序员文章站
2023-12-10 14:17:10
主要原理请参考经过一翻调试,成功了。。呵附上源码。。jdk 1.5 +eclipse +tomcat 5.0.28 +&n...
主要原理请参考
经过一翻调试,成功了。。呵
附上源码。。
jdk 1.5 +eclipse +tomcat 5.0.28 + mysql 5.0
数据库test ,表名news
字段: id int 自动增长 , 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() 返回可以不受阻塞地从此文件输入流中读取的字节数。
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。
fileinputstream.close();
//templatecontent = new string(bytes);
string title;
string content;
string author;
while(rs.next())
{
templatecontent = new string(bytes);//如果不用这句,则替换一次之后,templatecontent中就没有#**#标志了。所以要重新生成
title = rs.getstring("title");
content = rs.getstring("content");
author = rs.getstring("author");
out.println(title+"********"+content+"****"+author);
out.print("以下是模板内容:<br>"+templatecontent+"<br> 以下是置换以后的html内容<br><hr>");
templatecontent=templatecontent.replaceall("#title#",title);
templatecontent=templatecontent.replaceall("#author#",author);//替换掉模块中相应的地方
templatecontent=templatecontent.replaceall("#content#",content);
// 根据时间得文件名
calendar calendar = calendar.getinstance();
string fileame = string.valueof(calendar.gettimeinmillis()) +".html";
fileame = request.getrealpath("/")+"html/"+fileame;//生成的html文件保存路径
out.print(templatecontent);
fileoutputstream fileoutputstream = new fileoutputstream(fileame);//建立文件输出流
byte tag_bytes[] = templatecontent.getbytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
if(conn!=null)
{
conn.close();
}
if(stmt!=null)
{
stmt.close();
}
%>
//数据库连接文件
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.htm
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="">
<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>
经过一翻调试,成功了。。呵
附上源码。。
jdk 1.5 +eclipse +tomcat 5.0.28 + mysql 5.0
数据库test ,表名news
字段: id int 自动增长 , 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() 返回可以不受阻塞地从此文件输入流中读取的字节数。
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。
fileinputstream.close();
//templatecontent = new string(bytes);
string title;
string content;
string author;
while(rs.next())
{
templatecontent = new string(bytes);//如果不用这句,则替换一次之后,templatecontent中就没有#**#标志了。所以要重新生成
title = rs.getstring("title");
content = rs.getstring("content");
author = rs.getstring("author");
out.println(title+"********"+content+"****"+author);
out.print("以下是模板内容:<br>"+templatecontent+"<br> 以下是置换以后的html内容<br><hr>");
templatecontent=templatecontent.replaceall("#title#",title);
templatecontent=templatecontent.replaceall("#author#",author);//替换掉模块中相应的地方
templatecontent=templatecontent.replaceall("#content#",content);
// 根据时间得文件名
calendar calendar = calendar.getinstance();
string fileame = string.valueof(calendar.gettimeinmillis()) +".html";
fileame = request.getrealpath("/")+"html/"+fileame;//生成的html文件保存路径
out.print(templatecontent);
fileoutputstream fileoutputstream = new fileoutputstream(fileame);//建立文件输出流
byte tag_bytes[] = templatecontent.getbytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
if(conn!=null)
{
conn.close();
}
if(stmt!=null)
{
stmt.close();
}
%>
//数据库连接文件
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.htm
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="">
<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>