将动态页面转换生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录(特别是对接受动态参数的页面).前台访问时,脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.当然,凡事有利必有弊,生成HTML页面无形中也耗费大量的磁盘空间以存放这些静态文件,在编辑页面过程中除读写数据库外,也要读写服务器磁盘,页面样式的改动必须重新生成全部HTML文件,等等。三种常见的生成思路参考:
直接利用FSO把html代码写入到文件中然后生成.html格式的文件是最原始的,优点是简单,缺点是页面的修改不方便,一般用到的地方是利用它生成整站参数文件。(通常网站如标题名称等配置保存在数据库,将它生成config.asp保存这些变量调用,避免频繁访问数据库)
<%
filename="boaer.com.htm"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form" method="post" action="">
<input name="title" value="Title" size=26>
<br>
<textarea name="body">Body</textarea>
<br>
<br>
<input type="submit" name="Submit" value="生成html">
</form>第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.template.htm ' //模板文件
<html>
<head>
<title>$title$ by boaer.com</title>
</head>
<body>
$body$
</body>
</html>
TestTemplate.asp '// 生成Html
<%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 创建文件系统对象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'// 打开网页模板文件,读取模板内容
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
strOut=f.ReadAll
htmlwrite.close
strTitle="生成的网页标题"
strContent="生成的网页内容"
'// 用真实内容替换模板中的标记
strOut=Replace(strOut,"$title$",strTitle)
strOut=Replace(strOut,"$body$",strContent)
'// 创建要生成的静态页
Set htmlwrite=fso.CreateTextFile(Server.MapPath("boaer.com.htm"),true)
'// 写入网页内容
htmlwrite.WriteLine strOut
htmlwrite.close
Response.Write "生成静态页成功!"
'// 释放文件系统对象
set htmlwrite=Nothing
set fso=Nothing
%>第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。找到一段XMLHTTP生成Html的代码参考一下.<%'常用函数
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
txtURL=server.MapPath("../index.asp")
sText = getHTTPPage(txtURL)
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename="http://boaer.com/index.htm"
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing
%>
<script>
alert("静态网页生成完毕");
history.back();
</script>
这三种方式是比较常用的生成HTML文件方法,第三种方式页面改动时非常方便,就算动态页改动多大都好,只要重新用XMLHTTP读取生成一次即可。
2023-08-29 广告
dim code
code="页面详细代码"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set Ck = fso.CreateTextFile(server.mappath("1.html"),true)
Ck.Write code
Ck.close
set Ck=nothing
set fso=nothing
你把下面的代码复制到一个asp文件中,再通过iis把这张网页打开就会发现在这个网页的文件夹中会多出一张test001.asp网页,数据就是保存在这里面.
<%
content="要保存的内容"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(server.mappath("test001.asp"))
fout.Write content
fout.close
set fout=nothing
set fso=nothing
%>
你所说的就是利用这种原理保存数据的.asp生成文件,有些网站打开是.html的文件,只要更改上面代码的test001.asp为test001.html就会出来一张test001.html的网页,是不是很好玩啊,呵呵.
共有两个页面index.htm是首页.程序比较简单,主要是用了ASP里的文件操作对象。
<!--index.htm---------------------->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><form name="form1" method="post" action="send.asp">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCFFFF">
<tr>
<td height="20"><div align="center">发送消息</div></td>
</tr>
<tr>
<td><div align="center">
<textarea name="msg" cols="100" rows="6"></textarea>
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
'//send.asp
<%
function chan_time(shijian)'转换日期时间函数
s_year=year(shijian)
if len(s_year)=2 then s_year="20"&s_year
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
s_hour=hour(shijian)
if s_hour<10 then s_hour="0"&s_hour
s_minute=minute(shijian)
if s_minute<10 then s_minute="0"&s_minute
chan_time=s_year & s_month & s_day & s_hour & s_minute
end function
function chan_data(shijian) '转换日期时间函数
s_year=year(shijian)
if len(s_year)=2 then s_year="20"&s_year
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
chan_data=s_year & s_month & s_day
end function
function chan_file(shijian)'转换日期时间函数
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
s_hour=hour(shijian)
if s_hour<10 then s_hour="0"&s_hour
s_minute=minute(shijian)
if s_minute<10 then s_minute="0"&s_minute
s_ss=second(shijian)
if s_ss<10 then s_ss="0"&s_ss
chan_file = s_month & s_day & s_hour & s_minute & s_ss
end function
top="<html><head><title>news</title></head><body>"
botom="</body></html>"
msg=request.Form("msg")
msg=replace(msg,vbcrlf,"")
msg=replace(msg,chr(9),"")
msg=replace(msg," "," ")
msg=replace(msg,"\r\n","<br>")
msg=replace(msg,"\n","<br>")
msg=top&msg&botom
Set fs=Server.CreateObject("Scripting.FileSystemObject")
all_tree2=server.mappath("news")&"\"&chan_data(now)
if (fs.FolderExists(all_tree2)) then'判断今天的文件夹是否存在
else
fs.CreateFolder(all_tree2)
end if
pass=chan_file(now)
randomize '使用系统计时器来初始化乱数产生器
pass=rnd(pass)
pass=get_pass(pass)
pass=left(pass,10)
file1=pass
files=file1&".txt"
filez=all_tree2&"\"&files
set ts = fs.createtextfile(filez,true) '写文件
for z=1 to len(msg)
write_now=mid(msg,z,1)
ts.write(write_now)
next
' ts.writeline(all_msg)
ts.close
set ts=nothing '文件生成
if err.number<>0 or err then%>
<script language="javascript">
alert("不能完成")
</script>
<%else%>
<script language="javascript">
alert("已完成")
history.back();
</script>
<%end if
Set MyFile = fs.GetFile(filez)
all_tree2=server.mappath("news")&"\"&chan_data(now)
if (fs.FolderExists(all_tree2)) then
else
fs.CreateFolder(all_tree2)
end if
MyFile.name= left(MyFile.name,len(MyFile.name)-4)&".htm"
set MyFile=nothing
set fs=nothing
set fdir=nothing
function get_pass(pass)
pass=cstr(pass)
pass=replace(pass," ","")
pass=replace(pass," ","")
pass=replace(pass,"-","")
pass=replace(pass," ","")
pass=replace(pass,":","")
pass=replace(pass,".","")
pass=replace(pass,"+","")
pass=replace(pass,"_","")
pass=replace(pass,"<","")
pass=replace(pass,">","")
pass=replace(pass,"!","")
pass=replace(pass,"@","")
pass=replace(pass,"#","")
pass=replace(pass,"$","")
pass=replace(pass,"%","")
pass=replace(pass,"^","")
pass=replace(pass,"&","")
pass=replace(pass,"*","")
pass=replace(pass,"(","")
pass=replace(pass,")","")
pass=replace(pass,"=","")
pass=replace(pass,"\","")
pass=replace(pass,"/","")
pass=replace(pass,"|","")
get_pass=pass
end function
%>
'//
把send.asp和index.htm放到你的ASP目录下,然后再建一个news文件夹。打开浏览器,在地址栏里输入:http://你的机器名/你的ASP虚拟目录名/index.html输入文字,然后提交,OK!看看你的ASP目录里新建的news文件夹里是不是多了个新的文件夹,而且是以当前日期命名的。里面有你刚刚提交的文字,看看是不是生成了HTML文件。怎么样,你可以在此基础上做个简单的新闻发布系统。
也就是说要用ASP获取数据后传给JS,再由JS根据模板生成一个HTML文件,并把这个文件名再传给数据库,或是生成父网页,过程很麻烦