ASP Dynamic Web page generation static HTML Web page file technology

Source: Internet
Author: User
Tags file system
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") <&gt; "" Then


Set fso = Server.CreateObject ("Scripting.FileSystemObject")


Set htmlwrite = FSO. CreateTextFile (Server.MapPath ("&amp;filename&amp;"))


htmlwrite.write "

htmlwrite.write "<body&gt; output title content:" &amp; Request.Form ("title") &amp; "<br/&gt; Output Body content:" &amp; Request.Form ("b Ody ") &amp;" </body&gt;

Htmlwrite.close


Set fout=nothing


Set Fso=nothing


End If


%&gt;


<form name= "form" method= "POST" action= "" &gt;


<input name= "title" value= "title" Size=26&gt;


<br&gt;


<textarea name= "Body" &gt;Body</textarea&gt;


<br&gt;


<br&gt;


<input type= "Submit" name= "submit" value= "Generate HTML" &gt;


</form&gt; 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&gt; $title $ by aspid.cn</title&gt;




<body&gt;


$body $


</body&gt;




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


%&gt;

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 (); 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.