ASP page static batch generation code sharing (a variety of methods) _ Application techniques

Source: Internet
Author: User
1, ASP two simple way to generate static home page

Why should I generate a static home page?
1, if you read the first page of the database more frequently, the speed is very slow, and occupy a lot of server resources. Using static page access speed of course it's much faster.
2, search engine easy to search
3, if the program problems, can also guarantee access to the home page.
4, the other too much, oneself think:)
Application Mode:
If your home page is index.asp, you can generate Index.htm (the default access order must be index.htm,index.asp). So the first time a visitor visits your site, it opens a index.htm. You can make the homepage of the link into a index.asp, so from any page of the site to click on the link on the homepage is index.asp, so that the timely information to ensure the update of the timeliness (after all index.htm need each manual update).
Method One:
Directly include the first file in the form text box, the first page code to submit the most data, and then generate a static page.
The code is as follows:
Copy Code code as follows:

<%
'------------------------------------------------------------
' Use forms to submit code to generate a static home page
' Make sure your space supports the FSO, and the home code content is less
'------------------------------------------------------------
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" has successfully generated the home page!) ') </script> ")
End Sub
%>
<form name= "Form1" method= "Post" action= "" >
<textarea name= "Content" >
<!--#i nclude file= "index.asp"-->
</textarea>
<br>
<input type= "Submit" name= "Submission" value= "submitted" >
</form>

Disadvantages:
1, if the homepage includes <@. > Marks, prompts an error.
2, if the home code is longer, with the form can not be submitted in the past (the length of the form data has a certain limit).
Solution:
1, remove the <@ > mark in index.asp
2, the use of Ewebeditor, submit support for large data (automatic segmentation)
Advantages:
You can modify the content in real time at build time.
Method Two:
Use XMLHTTP to get index.asp code directly

Copy Code code as follows:

<%
'----------------------------------------------------------
' Use XMLHTTP to generate static home code
' Curl for your home address, make sure your space support 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" has successfully generated the home page!) ') </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, template separation batch generation

The content to be replaced in the template file is {...} Enclosed
For simplicity, the error-handling code is removed (the string parameter to replace in replace cannot be null, but the FSO should also do error checking).
Copy Code code as follows:

<%
' -------------------------------------------------------------------------------------------------------------- -------
' From: Kevin Fung http://www.yaotong.cn
' Author: Kevin Fung id:kevin2008, please keep it as it is reproduced
' Time: 2006/07/05 stragglers Forum starts
' -------------------------------------------------------------------------------------------------------------- --------
Dim start ' The variable is the location of the recordset to which the pointer will point, dynamically obtained via parameters
Dim Template ' template file will read the variable in string
Dim content ' Replace string variable
Dim objconn ' Connection object
Dim connstr ' connection string
Dim SQL ' query statement
Dim cnt:cnt = 1 ' This round loop counter initialization
Start = Request ("Start") ' Gets the starting position of this 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,1,1 ' Open Data set
Rs. AbsolutePosition = Start ' The most critical step is to start,start the pointer to a parameter dynamically
Template = GetTemplate (Server.MapPath ("template.html")) ' template.html is a template file, read through the function gettemplate to the string, and the content to be replaced in the template file is { ...} Enclosed
While the not rs.eof and cnt<= 500 ' 500 is the number of times to set a request to generate a page, according to the actual situation to modify, if too high, the recordset a lot of time will be a timeout error
Content = replace (Template, "{filed_name_1}", RS ("Filed_name_1")) ' replaces template contents 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") ' generates an HTML document for the replaced template string, Htmfiles is the directory where the static files are stored, please manually establish
CNT = cnt + 1 ' counter plus 1
Start = start + 1 ' pointer variable increment
Rs.movenext
Wend
If not rs.eof Then ' makes the next round of requests by refreshing and passes pointer variable start to the next round
Response.Write "<meta http-equiv= ' refresh ' content= ' 0; url=?start= "&start&" ' > '
Else
Response.Write "Generate HTML file complete!" "
End If
Rs. Close ()
Set rs = Nothing
Objconn.close ()
Set objconn = Nothing
function gettemplate (template) ' reads the template's functions, returns a string, template 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, the content is the replaced string, filename is the generated filename
Dim fso,f
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set f = fso. CreateTextFile (filename,true) ' If duplicate filename will overwrite old file
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.