用JSP生成静态页面
导读:收集整理的这篇文章主要介绍了用JSP生成静态页面,觉得挺不错的,现在分享给大家,也给大家做个参考。 主要原理请参考http://blog.hunan2s.com/article.asp?...
收集整理的这篇文章主要介绍了用JSP生成静态页面,觉得挺不错的,现在分享给大家,也给大家做个参考。 主要原理请参考http://blog.hunan2s.com/article.asp?id=233经过一翻调试,成功了。。呵
附上源码。。
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="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> 您可能感兴趣的文章:
- JSP页面的动态包含和静态包含示例及介绍
- jsp生成静态页面的方法
- 动态JSP页生成静态HTML的方法
- jsp中include指令静态导入和动态导入的区别详解
- JSP生成静态页实践及其设计思想[转]
- jsp实现将动态网页转换成静态页面的方法 @H_777_156@
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 用JSP生成静态页面
本文地址: https://pptw.com/jishu/604766.html
