Comparison of several methods for ASP generating static HTML files

Source: Internet
Author: User
Tags comparison file system html page reference
comparison | static

There are a number of benefits to generating static HTML files for dynamic page conversions, such as generating HTML pages that are useful for search engines (especially for pages that accept dynamic parameters). When you visit the front desk, you get rid of the data access, reduce the pressure on the database access, and speed up the page opening.

Of course, everything is beneficial must have disadvantages, the generation of HTML page virtually also consumes a lot of disk space to store these static files, in the process of editing the page in addition to read and write the database, but also to read and write server disk, page style changes must regenerate all HTML files, and so on.

Like many search engines, can submit the site's page address list, dynamic file collection problem has not been a problem (such as Google sitemap). Gains and losses will be measured by their own grasp, but in any case, we still have to know how to operate. Here is a reference to someone else's article explains several common generation ideas, for everyone reference reference.

1, the following example directly uses the FSO to write HTML code to a file and then generate an. HTML-formatted file. This is the most original, the advantage is simple, the disadvantage is that the modification of the page is inconvenient, I generally used the place is to use it to generate the whole station parameter file. (usually site such as title, name, etc. configuration saved in the database, I will generate it config.asp save these variable calls, avoid frequent access to the database)

<%
Filename= "Test.htm"
If request ("Body") <> "" Then
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Set htmlwrite = FSO. CreateTextFile (Server.MapPath ("&filename&"))
Htmlwrite.write " Htmlwrite.write " Htmlwrite.close
Set fout=nothing
Set fso=nothing
End If
%>

2, but according to the above method to generate HTML file is very inconvenient, the second method is to use the 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
Head>
$body $
testtemplate.asp '//Generate HTML
<%
Dim FSO, Htmlwrite
Dim strtitle,strcontent,strout
'//Create File System Object
Set Fso=server.createobject (" Scripting.FileSystemObject ")
'//Open page template file, read template content
Set htmlwrite=fso. OpenTextFile (Server.MapPath ("template.htm"))
Strout=f.readall
Htmlwrite.close
strtitle= "generated page title"
strcontent= "generated Web page content"
//Replace tags in template with real content
Strout=replace (Strout, "$title $", strtitle)
Strout=replace (strout , "$body $", strcontent)
'//Create a static page to generate
Set Htmlwrite=fso. CreateTextFile (Server.MapPath ("test.htm"), true)
'//write page content
Htmlwrite. WriteLine strout
Htmlwrite.close
Response.Write "Generate static page success!" "
'//Free File system Object
Set htmlwrite=nothing
Set fso=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. Find a section of XMLHTTP generated HTML code 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, 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

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
%>
<script>
Alert ("Static Web page Generation Complete");
History.back ();
</script>

Summary, these three kinds of methods are more commonly used to generate HTML file method, I prefer to use the third way, because the page changes very convenient, even if the dynamic page changes mostly good, as long as XMLHTTP read to generate once.



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.