用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("以下是模板内容:
"+templateContent+"
以下是置换以后的html内容
#title# |
#content# |
#author# |