The technology of ASP generating static HTML file

Source: Internet
Author: User
Static Web pages generate static HTML files have many advantages, such as the generation of HTML Web pages to be included, not only included in the quick also included in the whole. The front desk is out of data access, reduces the pressure on access, and speeds up Web page opening.
Like Www.aspid.cn Main station on the use of Tsys to generate HTML files!
So Yin Qing recently to generate HTML more interested, read a lot of articles, but also a little harvest.

1, the following example directly uses the FSO to write HTML code to a file and then generate an. html file <%
Filename= "Test.htm"
Ifrequest ("Body") <> "" Then
Setfso=server.createobject ("Scripting.FileSystemObject")
Sethtmlwrite=fso. CreateTextFile (Server.MapPath ("&filename&"))
Htmlwrite.write "Htmlwrite.write "<body> output title content:" &request.form ("title") & "<br/> Output body Content:" &request.form ("Body" ) & "</body>
Htmlwrite.close
Setfout=nothing
Setfso=nothing
endif
%>
<formname= "Form" method= "POST" action= "" >
<inputname= "title" value= "title" Size=26>
<br>
<textareaname= "Body" >Body</textarea>
<br>
<br>
<inputtype= "Submit" name= "submit" value= "Generate HTML" >
</form>
2, but it's very inconvenient to generate HTML files in the above way, the second approach is to use template technology to replace the value of special code in a template with a value accepted from a form or field, to complete the template function, and to generate an HTML file for all the template code that is eventually replaced. This technique is used more, Most CMS uses this type of method.
Template.htm '//template file

<title> $title $byaspid.cn</title>

<body>
$body $
</body>


Testtemplate.asp '//Generate html<%
Dimfso,htmlwrite
Dimstrtitle,strcontent,strout
'//Create File system objects
Setfso=server.createobject ("Scripting.FileSystemObject")
'//Open file, read template content
Sethtmlwrite=fso. OpenTextFile (Server.MapPath ("template.htm"))
Strout=f.readall
Htmlwrite.close

strtitle= "generated page title"
strcontent= "generated Web page content"

'//Replace the tag in the template with the real content
Strout=replace (Strout, "$title $", strtitle)
Strout=replace (Strout, "$body $", strcontent)

'//Create a static page to be generated
Sethtmlwrite=fso. CreateTextFile (Server.MapPath ("test.htm"), true)

'//write page content
Htmlwrite. Writelinestrout
Htmlwrite.close

Response.Write "Generate static page success!" "

'//Release File system objects
Sethtmlwrite=nothing
Setfso=nothing
%>

3, the third method is to use XMLHTTP to get the HTML content generated by the dynamic page, and then save it as an HTML file with ADODB.stream or Scripting.FileSystemObject. 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
Functiongethttppage (URL)
Dimhttp
Sethttp=server.createobject ("MSXML2.") XMLHTTP ")
Http.open "Get", Url,false
Http.send ()
Ifhttp.readystate<>4then
Exitfunction
endif
Gethttppage=bytestobstr (Http.responsebody, "GB2312")
Sethttp=nothing
Iferr.number<>0thenerr. Clear
Endfunction

' 2, the conversion of XMLHTTP, directly with the use of Chinese characters to call the Web page will be chaos, can be converted through the ADODB.stream component
Functionbytestobstr (Body,cset)
Dimobjstream
Setobjstream=server.createobject ("ADODB.stream")
Objstream. Type=1
Objstream. Mode=3
Objstream. Open
Objstream. Writebody
Objstream. Position=0
Objstream. type=2
Objstream. Charset=cset
Bytestobstr=objstream. ReadText
Objstream. Close
Setobjstream=nothing
Endfunction


Txturl=server. MapPath (".. /index.asp ")

Stext=gethttppage (Txturl)

Setfileobject=server.createobject ("Scripting.FileSystemObject")
Filename= ". /index.htm "
Setopenfile=fileobject.opentextfile (Server.MapPath (filename), 2,true) ' True ' does not exist to establish itself
Openfile.writeline (stext)
Setopenfile=nothing

%>
<script>
Alert ("Static Web page Generation Complete");
History.back ();
</script>


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.