下面的一段代碼是,使用jsp生成靜態html
<%@ page contenttype="text/html;charset=gbk"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*" %>
<%
//在這里如果寫成“web-inf/templates/template.htm”程序會報錯
string filepath = request.getrealpath("/")+"web-inf/templates/template.htm";
out.print(filepath);
string templatecontent="";
fileinputstream fileinputstream = new fileinputstream(filepath);//讀取模塊文件
//int lenght = fileinputstream.available();
byte bytes[] = new byte[1024];
fileinputstream.read(bytes);
fileinputstream.close();
templatecontent = new string(bytes);
out.print("以下是模板內容:<br>"+templatecontent+"<br> 以下是置換以后的html內容<br><hr>");
templatecontent=templatecontent.replaceall("#title#","文章標題");
templatecontent=templatecontent.replaceall("#author#","作者是誰");//替換掉模塊中相應的地方
templatecontent=templatecontent.replaceall("#content#","文章內容");
// 根據時間得文件名
calendar calendar = calendar.getinstance();
string fileame = string.valueof(calendar.gettimeinmillis()) +".html";
fileame = request.getrealpath("/")+fileame;//生成的html文件保存路徑
out.print(templatecontent);
fileoutputstream fileoutputstream = new fileoutputstream(fileame);//建立文件輸出流
byte tag_bytes[] = templatecontent.getbytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
%>
新聞熱點
疑難解答