Recently, a friend's website needs to generate a static website, but sometimes there will be problems when generating static. We have compiled some methods, and you can choose according to your website needs. 1. Two simple methods for generating static home pages in ASP
Why generate a static home page?
1. If your home page reads the database a lot, the speed is very slow and it takes up a lot of server resources. Of course, using static page access speed is much faster
2. Easily found by search engines
3. If there is a problem with the program, the home page can be accessed.
4. There are too many others, think about it yourself :)
Application method:
If your homepage is index.asp, you can generate index.htm (the default access sequence must be index.htm, index.asp). In this way, when a visitor visits your website for the first time, he opens index.htm. You can make the link on the homepage of the website into index.asp, so that when you click on the link on the homepage from any page of the website, index.asp will appear. This ensures the timeliness of information updates (after all, index.htm needs to be updated manually every time).
Method one:
Directly include the homepage file in the form text box, submit the homepage code as data, and then generate a static page.
The code is as follows:
Copy the code code as follows:
<%
'------------------------------------------------ ----------
'Use form submission to generate code for static home page
'Make sure your space supports FSO and has less code content on the homepage
'------------------------------------------------ ----------
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('Home page 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>
shortcoming:
1. If the home page includes the <@ ..> tag, an error will be prompted.
2. If the homepage code is too long, it cannot be submitted using a form (there is a certain limit on the length of the form data).
Solution:
1. Remove the <@ > tag in index.asp
2. Use eWebEditor to submit and support big data (can be automatically segmented)
advantage:
Content can be modified in real time as it is generated.
Method two:
Directly use XMLHTTP to obtain the code of index.asp
Copy the code code as follows:
<%
'------------------------------------------------ ----------
'Use XMLHTTP to generate static home page code
'Curl is your homepage address, make sure your space supports FSO
'------------------------------------------------ ----------
dim read,Curl,content
Curl=http://www.xx0123.com/index.asp
read=getHTTPage(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('Home page 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. Template separation and batch generation
The content to be replaced in the template file is enclosed in {...}
In order to strive for simplicity, the error handling code has been removed (the string parameter to be replaced in replace cannot be a null value, and of course fso should also do error checking).
Copy the code code as follows:
<%
'------------------------------------------------ -------------------------------------------------- ------------------
' From: kevin fung http://www.yaotong.cn
' Author: kevin fung Laggard ID: kevin2008, please keep it as it is when reprinting
' Time: 2006/07/05 Laggard Forum first release
'------------------------------------------------ -------------------------------------------------- -------------------
Dim start 'This variable is the record set position that the pointer will point to, dynamically obtained through parameters
Dim Template 'The template file will read this variable into a string
Dim content 'Replaced string variable
Dim objConn 'Connection object
Dim ConnStr 'Connection string
Dim sql 'query statement
Dim cnt:cnt = 1 'Initialize the loop counter of this cycle
start = request(start) 'Get 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,1,1 'Open the data set
rs.AbsolutePosition = start 'The most critical step is to point the pointer to start, which is dynamically obtained through parameters
Template = getTemplate(Server.MapPath(template.html))' template.html is a template file, and the string is read through the function getTemplate. The content to be replaced in the template file is enclosed in {...}
While Not rs.eof And cnt<= 500 '500 is to set the number of cycles for a request to generate a page. Modify it according to the actual situation. If it is too high, a timeout error will occur when there are many record sets.
content = Replace(Template,{filed_name_1},rs(filed_name_1)) 'Replace template content with field value
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 HTML document by replacing the Template string. htmfiles is the directory where static files are stored. Please create it manually.
cnt = cnt + 1 'Counter plus 1
start = start + 1 'Pointer variable increments
rs.movenext
wend
If Not rs.eof Then 'Make the next round of requests by refreshing and pass the pointer variable start to the next round
response.write <meta http-equiv='refresh' content='0;URL=?start=&start&'>
Else
response.write generation of HTML file is completed!
End if
rs.Close()
Setrs=Nothing
objConn.Close()
Set objConn = Nothing
Function getTemplate(template)' function to read the template, return a string, 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 into 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
%>