手机
当前位置:查字典教程网 >编程开发 >JSP教程 >用JSP生成静态页面
用JSP生成静态页面
摘要:主要原理请参考http://blog.hunan2s.com/article.asp?id=233经过一翻调试,成功了。。呵附上源码。。JD...

主要原理请参考http://blog.hunan2s.com/article.asp?id=233

经过一翻调试,成功了。。呵

附上源码。。

JDK1.5+ECLIPSE+TOMCAT5.0.28+MYSQL5.0

数据库TEST,表名news

字段:idint自动增长,Titlevarchar(20),Contentvarchar(200),Authorvarchar(10)

makeFile.jsp

<%

Connectionconn=DBconn.getConnection();

Statementstmt=conn.createStatement();

ResultSetRs=stmt.executeQuery("select*fromnews");

System.out.println("success");

%>

<%

StringfilePath=request.getRealPath("/")+"template.htm";

System.out.println(filePath);

StringtemplateContent;

FileInputStreamfileinputstream=newFileInputStream(filePath);

intlenght=fileinputstream.available();//available()返回可以不受阻塞地从此文件输入流中读取的字节数。

bytebytes[]=newbyte[lenght];

fileinputstream.read(bytes);//read(byte[]b)从此输入流中将最多b.length个字节的数据读入一个字节数组中。

fileinputstream.close();

//templateContent=newString(bytes);

Stringtitle;

Stringcontent;

Stringauthor;

while(Rs.next())

{

templateContent=newString(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);

//根据时间得文件名

Calendarcalendar=Calendar.getInstance();

Stringfileame=String.valueOf(calendar.getTimeInMillis())+".html";

fileame=request.getRealPath("/")+"Html/"+fileame;//生成的html文件保存路径

out.print(templateContent);

FileOutputStreamfileoutputstream=newFileOutputStream(fileame);//建立文件输出流

bytetag_bytes[]=templateContent.getBytes();

fileoutputstream.write(tag_bytes);

fileoutputstream.close();

}

if(conn!=null)

{

conn.close();

}

if(stmt!=null)

{

stmt.close();

}

%>

//数据库连接文件

importjava.sql.*;

publicclassDBconn{

publicDBconn(){

//TODOAuto-generatedconstructorstub

}

publicstaticConnectiongetConnection()

{

Connectionconn=null;

try{

Class.forName("org.gjt.mm.mysql.Driver");

conn=DriverManager.getConnection("jdbc:mysql://"+"localhost"+"/"+"test"+

"?useUnicode=true&characterEncoding=GB2312","root","111111");

}

catch(Exceptione)

{

e.printStackTrace();

}

returnconn;

}

/*publicstaticvoidmain(String[]args)throwsException

{

Connectioncon=getConnection();

System.out.println(con.isClosed());

}

*/

}

//模板文件

template.htm

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>

<title>#title#</title>

</head>

<body>

<tablewidth="380"height="107"border="0"cellpadding="0"cellspacing="1"bgcolor="#FFCC99">

<tr>

<tdheight="16"bgcolor="#FFCC99"><divalign="center">#title#</div></td>

</tr>

<tr>

<tdbgcolor="#FFFFFF">#content#</td>

</tr>

<tr>

<tdheight="13"align="right"bgcolor="#FFFFFF">#author#</td>

</tr>

</table>

</body>

</html>

【用JSP生成静态页面】相关文章:

通过JSP预编译消除性能瓶颈

使用JSP读取客户端信息

JSP中把动态页面转换为静态页面

一个实用的JSP分页代码

JSP教程(六)-怎么在JSP中跳转到别一页面

怎么在JSP中跳转到别一页面

在JSP页面中的应用JavaBean

JSP中的汉字编码问题

JSP和Struts解决用户退出问题

JSP中的倒数计时和自动跳转页面

精品推荐
分类导航