Comparison of several methods of ASP generating static HTML files

Source: Internet
Author: User
Tags comparison end file system html page reference access

There are many benefits to generating static HTML files from dynamic page conversions, such as creating HTML pages that are useful for search engines (especially for pages that accept dynamic parameters). Front-desk access, out of the data access, reduce the pressure on the database access, speed up the speed of Web pages open.

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 using the FSO to write HTML code to the 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)

The following are the referenced contents:

<%
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 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 the value, complete the template function, the final replacement of all the template code to generate HTML files. This technology is used more, most of the CMS is the use of such methods.


template.htm '//template file
$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 template contents
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 obtain dynamic page generated HTML content, and then use ADODB.stream or Scripting.FileSystemObject to save HTML files. Find a section of XMLHTTP generated HTML code reference.

The following are the referenced contents:

<%
' 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.