Recommended: About the separation of Asp code and page In order to avoid maintenance difficulties caused by the misunderstanding of ASP program TML code, this article introduces a method to use templates to separate programs and pages to make programming easier. When using ASP to create a site, an ASP file often appears, program code and HTM
There are many benefits to converting dynamic pages to generate static Html files, such as generating html web pages is conducive to being included by search engines (especially for pages that accept dynamic parameters). When accessing the front desk, it is separated from data access, alleviating the pressure on database access and speeding up the opening of web pages.
Of course, everything has its advantages and disadvantages. Generating HTML pages invisibly consumes a lot of disk space to store these static files. In addition to reading and writing the database during the editing process, you also need to read and write the server disk. Changes to page styles must regenerate all HTML files, etc.
For example, many search engines can submit a list of website page addresses. The inclusion of dynamic files is no longer a problem (such as Google sitemap). We have to measure and grasp the gains and losses by ourselves, but no matter what, we still need to know how to operate. Here I will quote other people's articles to explain several common generation ideas for your reference.
1. The following example directly uses FSO to write html code into a file and then generate a .html file format. This is the most primitive. The advantage is that it is simple, and the disadvantage is that the page modification is inconvenient. The place I usually use is to use it to generate the whole site parameter file. (Usually, the website such as title, name, etc. are stored in the database. I generate config.asp to save these variable calls to avoid frequent access to the database)
| The following is the quoted content: <% filename=test.htm if request(body)<> then set fso = Server.CreateObject(Scripting.FileSystemObject) set htmlwrite = fso.CreateTextFile(server.mappath(&filename&)) htmlwrite.write <html><head><title> & request.form(title) & </title></head> htmlwrite.write <body>Output Title content: & request.form(title) & <br />Output Body content: & request.form(body)& </body></html> 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 very inconvenient to generate an html file according to the above method. The second method is to use template technology to replace the value of the special code in the template with the value accepted from the form or database field, complete the template function, and generate HTML files with all the template codes that were finally replaced. This technology is used more often, and most CMSs use this method.
| The following is the quoted content: template.htm ' //Template file <html> <head> <title>$title$ by CuoXIn.com</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 web template file and read the template content Set htmlwrite=fso.OpenTextFile(Server.MapPath(Template.htm)) strOut=f.ReadAll htmlwrite.close strTitle=The generated web page title strContent=The generated web page content '// Replace the tags in the template with real 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 content to the web page htmlwrite.WriteLine strOut htmlwrite.close Response.Write The static page was 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 use ADODB.Stream or Scripting.FileSystemObject to save it into an html file. Find a piece of code to generate Html for XMLHTTP.
| The following is the quoted content: <% 'Common functions '1. Enter the URL destination web page address, and the return value getHTTPPage is the html code of the destination web 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. Convert Lanma, directly call a web page with Chinese characters with xmlhttp, you will get Lanma, which 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 is created by itself if it does not exist openFile.writeline(sText) Set OpenFile=nothing %> <script> alert(static web page generation is completed); history.back(); </script> |
Summary, these three methods are the most commonly used methods of generating HTML files. I prefer to use the third method because the page changes are very convenient. Even if the dynamic page changes, it is good. Just use XMLHTTP to read and generate it once again.
Share: Cache Compatibility Design for WEB Applications After passing the proxy, since an intermediate layer is added between the client and the service, the server cannot directly obtain the client's IP, and the server-side application cannot directly return to the client through the forwarding address. However, in the HTTP header information of forwarding requests, HTTP_X is added