Dynamic | static | Web
Web page generation of static HTML file has many advantages, such as the creation of HTML Web pages are included in the search engine, not only included in the quick also included in the whole. The front desk is out of data access, reduces pressure on database access, and speeds up Web page opening. So Yin Qing recently to generate HTML more interested, read a lot of articles, but also a little harvest.
1, the following example uses the FSO to write HTML code to a file and then generate an. HTML-formatted file <%
filename= "Test.htm"
If Request ("Body") <> "" Then
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Set htmlwrite = FSO. CreateTextFile (Server.MapPath ("&filename&"))
htmlwrite.write "
htmlwrite.write "<body> output title content:" & Request.Form ("title") & "<br/> Output Body content:" & Request.Form ("b Ody ") &" </body>
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> 2, but according to the above method to generate HTML files is very inconvenient, the second method is to use template technology, the template in the special code to replace the value from the form or database fields accepted values, complete the template function; Generates an HTML file for all the template code that is eventually replaced. This technique is used more, and most of the CMS uses this kind of method.
template.htm '//template file
<title> $title $ by aspid.cn</title>
<body>
$body $
</body>
Testtemplate.asp '//Generate HTML <%
Dim Fso,htmlwrite
Dim strtitle,strcontent,strout
'//Create File system objects
Set fso=server.createobject ("Scripting.FileSystemObject")
'//Open page template file, read the template content
Set Htmlwrite=fso. OpenTextFile (Server.MapPath ("template.htm"))
Strout=f.readall
Htmlwrite.close
strtitle= "generated page title"
strcontent= "generated Web content"
'//Replace tags in template with real content
Strout=replace (Strout, "$title $", strtitle)
Strout=replace (Strout, "$body $", strcontent)
'//Create a static page to be generated
Set Htmlwrite=fso. CreateTextFile (Server.MapPath ("test.htm"), true)
'//write Web content
Htmlwrite. WriteLine Strout
Htmlwrite.close
Response.Write "Generate static page success!" "
'//Release file system objects
Set htmlwrite=nothing
Set fso=nothing
%>
3, the third method is to use XMLHTTP to obtain dynamic page generated HTML content, and then use ADODB.stream or Scripting.FileSystemObject to save HTML files. This sentence is in the blue ideal to see, to XMLHTTP Yin repayment is not familiar with looking for information to understand. Find a section of code that XMLHTTP generate HTML for reference.
<%
' Common functions
' 1, enter URL target page address, return value Gethttppage is the HTML code of the target page
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, conversion of XMLHTTP, directly with the use of the Chinese characters to invoke the Web page will be chaos, can be converted through the ADODB.stream component
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 does not exist self-established
Openfile.writeline (stext)
set openfile=nothing
%>
Alert ("Static Web page Generation complete");
History.back ();