ASP page static batch generation code sharing (multiple methods)

Source: Internet
Author: User

1. ASP two simple methods for generating static Homepage

Why do I need to generate a static homepage?
1. If you read a large number of databases on the home page, it is slow and occupies a lot of server resources. Static Page access is faster.
2. search engines are easy to find
3. If the program fails, the home page can be accessed.
4. There are too many others to think about :)
Application Method:
If your first page is index.asp, you can generate index.htm (index.htm, index. asp ). In this case, the first time you visit your website, index.htm is enabled. Renewal needs to be updated manually each time ).
Method 1:
Directly include the homepage file in the form text box, submit the homepage code with the most data, and generate a static page.
The Code is as follows:Copy codeThe Code is as follows: <%
'------------------------------------------------------------
'Use form submission to generate static homepage code
'Make sure your space supports FSO, And the homepage code has less content.
'------------------------------------------------------------
Dim content
Content = Trim (Request. Form ("content "))
If content <> "" then
Call makeindex ()
End if
Sub makeindex ()
Set Fso = Server. CreateObject ("Scripting. FileSystemObject ")
Filen = Server. MapPath ("index.htm ")
Set Site_Config = FSO. CreateTextFile (Filen, true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response. Write ("<script> alert ('the homepage has been generated successfully! ') </Script> ")
End sub
%>
<Form name = "form1" method = "post" action = "">
<Textarea name = "content">
<! -- # I nclude file = "index. asp" -->
</Textarea>
<Br>
<Input type = "submit" name = "Submit" value = "submit">
</Form>

Disadvantages:
1. If the <@...> mark is included in the homepage, an error is prompted.
2. If the homepage code is too long, it cannot be submitted using a form (the data length of a form is limited ).
Solution:
1. Remove the <@> mark in index. asp.
2. Use eWebEditor to submit big data (which can be automatically split)
Advantages:
Content can be modified in real time during production.
Method 2:
Use XMLHTTP to directly obtain the index. asp code

Copy codeThe Code is as follows: <%
'----------------------------------------------------------
'Use XMLHTTP to generate static homepage code
'Curl is your home address and ensure that your space supports FSO
'-----------------------------------------------------------
Dim read, Curl, content
Curl = "http://www.xx0123.com/index.asp"
Read = getHTTPPage (Curl)
If read <> "" then
Content = read
Call makeindex ()
End if
Sub makeindex ()
Set Fso = Server. CreateObject ("Scripting. FileSystemObject ")
Filen = Server. MapPath ("index.htm ")
Set Site_Config = FSO. CreateTextFile (Filen, true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response. Write ("<script> alert ('the homepage has been generated successfully! ') </Script> ")
End sub
Function getHTTPPage (url)
Dim http
Set http = Server. createobject ("Microsoft. 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
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
%>

2. Separate templates for batch generation

The content to be replaced in the template file is included in {...}
For simplicity, the error handling code is removed (the string parameter to be replaced in replace cannot be a null value, and fso should also perform an error check ).Copy codeThe Code is as follows: <%
'Timeout '---------------------------------------------------------------------------------------------------------------------
'From: kevin fung http://www.yaotong.cn
'Author: kevin fung owner ID: kevin2008. Keep it as it is.
'Time:/07/05 getting started
'Timeout '----------------------------------------------------------------------------------------------------------------------
Dim start' this variable is the position of the record set to be pointed to by the pointer, Which is dynamically obtained through Parameters
The Dim template' Template file will read this variable as a string
Dim content 'string variable after replacement
Dim objconn' connection object
Dim ConnStr 'Connection string
Dim SQL 'query statement
Dim cnt: cnt = 1' current round counter Initialization
Start = request ("start") 'obtains the starting position of the current round pointer.
If IsNumeric (start) Then start = CLng (start) Else start = 1
If start = 0 Then start = 1' If start
ConnStr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath ("DataBase. mdb ")
SQL = "select * from table_name"
Set objConn = Server. CreateObject ("ADODB. Connection ")
ObjConn. Open ConnStr
Set rs = Server. CreateObject ("ADODB. Recordset ")
Rs. open SQL, objConn, 'open the dataset
Rs. AbsolutePosition = start' is the most critical step. The pointer is directed to start, and start is dynamically obtained through parameters.
Template = getTemplate (Server. mapPath ("template.html") 'template.html is the template file. The function getTemplate is used to read the string. The content to be replaced in the template file is {...} include
While Not rs. eof And cnt <= 500 '500 is the number of cycles for a request to generate a page. It is modified according to the actual situation. If it is too high, a timeout error occurs when many record sets exist.
Content = Replace (Template, "{filed_name_1}", rs ("filed_name_1") 'replaces the Template content with Field Values
Content = Replace (content, "{filed_name_2}", rs ("filed_name_2 "))
......
Content = Replace (content, "{filed_name_n}", rs ("filed_name_n "))
GenHtml content, Server. mapPath ("htmfiles/" & rs ("id ")&". html ") 'generate the HTML document using the replaced Template string. htmfiles is the directory for storing static files. Please manually create
Cnt = cnt + 1' counter plus 1
Start = start + 1' increment pointer variable
Rs. movenext
Wend
If Not rs. eof then', refresh the next request and pass the pointer variable start to the next round.
Response. write "<meta http-equiv = 'refresh' content = '0; URL =? Start = "& start &" '>"
Else
Response. write "HTML file generated! "
End if
Rs. Close ()
Set rs = Nothing
ObjConn. Close ()
Set objConn = Nothing
Function getTemplate (template) 'refers to the Function used to read the template. A string is returned. The template is the file name.
Dim fso, f
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set f = fso. OpenTextFile (template)
GetTemplate = f. ReadAll
F. close
Set f = nothing
Set fso = Nothing
End Function
Sub genHtml (content, filename) 'writes the replaced content to the HTML document. content is the replaced string, and filename is the generated file name.
Dim fso, f
Set fso = Server. CreateObject ("Scripting. FileSystemObject ")
Set f = fso. CreateTextFile (filename, true) 'If the file name is repeated, the old file will be overwritten.
F. Write content
F. Close
Set f = Nothing
Set fso = Nothing
End Sub
%>

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.