Pseudo-static ASP website's benefit:
1. Is friendly to search engine
2. Ease to be listed and searched
3. Reduce the pressure of database
4. Accelerate response of webpage
Sample 1. Invoke FSO to write html file, genenrate *.html
<%
filename="test.html"
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>Output Title: " & request.form("title") & "<br /> Output Body content:" & 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="generate html">
</form>
Sample 2. Use template to manage the code. This method is most used by CMS.
<head>
<title>$title$ by asptest.com</title>
</head>
<body>
$body$
</body>
</html> ?
TestTemplate.asp '// generate html <%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// Create file system objective
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'// Open Webpage template file,read template
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
strOut=f.ReadAll
htmlwrite.close
strTitle="Webpage title"
strContent="Webpage content"
'// Replace template tab by real content
strOut=Replace(strOut,"$title$",strTitle)
strOut=Replace(strOut,"$body$",strContent)
'// Create html file
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.html"),true)
'// Write webpage content
htmlwrite.WriteLine strOut
htmlwrite.close
Response.Write "Generate html file done!"
'// Release file system objective
set htmlwrite=Nothing
set fso=Nothing
%>
Sample 3. Use XMLHTTP to achieve html content, then save html file by ADODB.Stream or Scripting.FileSystemObject.
<%
'Function
'1. Input url address, return value getHTTPPage is the code of objective webpage
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,"UTF-8")
set http=nothing
if err.number<>0 then err.Clear
end function
'2. Translate messy code by 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="../index.htm"
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true means not existing, create one openFile.writeline(sText)
Set OpenFile=nothing
%>
<script>
alert("HTML file done!");
history.back();
</script>
After learning ASP and PHP, I felt at the aspect of generating html, PHP is too much better than ASP.
No comments:
Post a Comment