ASP-Based Static HTML file generation

Source: Internet
Author: User
There are many benefits for generating static html files on a webpage. For example, generating an HTML webpage is helpful for indexing by search engines. the front-end is disconnected from data access, which reduces the pressure on database access and speeds up webpage access.
For example, the main site of www.aspid.cn uses tsys to generate HTML files!
Therefore, Yin Qing is interested in generating HTML recently and has read a lot Article , There are also a little gains.

1. The following example uses FSO to directly import html Code A. html file is generated after being written to the file <%
Filename = "test.htm"
If request ("body") <> "then
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set htmlwrite = FSO. createtextfile (server. mappath ("" & filename &""))
Htmlwrite. Write "<HTML> Htmlwrite. write "<body> output title content:" & request. form ("title") & "<br/> output body content:" & request. form ("body") & "</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. However, it is inconvenient to generate HTML files according to the above method. The second method is to use the template technology, replace the value of special code in the template with the value accepted from the form or database field to complete the template function. generate an HTML file for all the final replaced template code. this technology is widely used, and most CMS uses this method.
Template.htm '// template file <HTML>
<Head>
<Title> $ title $ by aspid.cn </title>
</Head>
<Body>
$ Body $
</Body>
</Html>?

Testtemplate. asp '// generate HTML <%
Dim FSO, htmlwrite
Dim strtitle, strcontent, Strout
'// Create a file system object
Set FSO = server. Createobject ("scripting. FileSystemObject ")
'// Open the webpage template file and read the template content
Set htmlwrite = FSO. opentextfile (server. mappath ("template.htm "))
Strout = f. readall
Htmlwrite. Close

Strtitle = "generated webpage title"
Strcontent = "generated webpage content"

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

'// Create the static page to be generated
Set htmlwrite = FSO. createtextfile (server. mappath ("test.htm"), true)

'// Write the webpage content
Htmlwrite. writeline Strout
Htmlwrite. Close

Response. Write "static page generated successfully! "

'// Release the File System Object
Set htmlwrite = nothing
Set FSO = nothing
%>

3. The third method is to use XMLHTTP to obtain the HTML content generated by the dynamic page, and then save it as an HTML file using ADODB. Stream or scripting. FileSystemObject. This sentence can be seen in the blue ideal. I am not familiar with XMLHTTP and am looking for information to learn about it. Find a piece of XMLHTTP HTML Generation Code for reference.
<%

'Common Functions
1. Enter the URL of the target webpage. The returned value gethttppage is the HTML code of the target webpage.
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. If you use XMLHTTP to call a webpage with Chinese characters, you can use the ADODB. Stream component to convert the webpage.
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 indicates that the user-created object does not exist.
Openfile. writeline (stext)
Set openfile = nothing

%>
<SCRIPT>
Alert ("static webpage generated ");
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.