asp 生成静态页

1)asp生成html的方式
要生成文件肯空要用到FSO(FileSystemObject)组件,通过asp生成静态网页主要有两种方式:
a、生成的内容由多部分连接而成;
b、生成的内容基于模板生成。

2)方式1:生成的内容由多部分连接而成
步骤:
a、设计要输出网页的布局
b、设计生成HTML的asp文件
例子:


输出网页的布局:
<html>
<head>
<title>标题</title>
        <style type="text/css">
<!--
.article_title {
font-size: 22px;
font-weight: bold;
text-align: center;
padding-top: 10px;
padding-bottom: 20px;
}
.content {
text-indent: 18px;
font-size: 16px;
line-height: 230%;
text-align: left;
}
.from {
font-size: 14px;
text-align: right;
padding-right: 15px;
padding-top: 15px;
}
.feature_bar {
font-size: 14px;
color: #999999;
text-align: center;
padding-bottom: 15px;
}
-->
        </style>
</head>
<body> 
        <table width="80%">
          <tr>
            <td><div class="article_title">标题</div>
            <div class="feature_bar">作者:  录入时间:  录入: </div>
         <div class="content">内容</div>
         <div class="from">来源:</div>         </td>
          </tr>
        </table>
</body>
</html>

把源代码中的所有的 " 替换成 "",作用是在ASP中输出双引号。


设计asp文件:
<% Option Explicit %>
<html>
<head>
<title>ASP生成HTML</title>
<style type="text/css">
<!--
.align_right_top {
text-align: right;
vertical-align: top;
}
.align_left_10px {
text-align: left;
padding-left: 10px;
}
-->
</style>
</head>
<body>
<form method="post" action="?action=create">
        <table width="80%">
          <tr>
            <td class="align_right_top" >HTML文件名称:</td>
            <td class="align_left_10px"><input name="HtmlFileName" type="text" id="HtmlFileName" /></td>
          </tr>
          <tr>
            <td class="align_right_top" >文章标题:</td>
            <td class="align_left_10px"><input name="title" type="text" id="title" /></td>
          </tr>
          <tr>
            <td class="align_right_top">作者:</td>
            <td class="align_left_10px"><input name="author" type="text" id="author" /></td>
          </tr>
          <tr>
            <td class="align_right_top">录入:</td>
            <td class="align_left_10px"><input name="editor" type="text" id="editor" /></td>
          </tr>
          <tr>
            <td class="align_right_top">输入时间:</td>
            <td class="align_left_10px"><input name="EditTime" type="text" id="EditTime" /></td>
          </tr>
          <tr>
            <td class="align_right_top">文章内容:</td>
            <td class="align_left_10px"><textarea name="content" cols="55" rows="20" id="content"></textarea></td>
          </tr>
          <tr>
            <td class="align_right_top">来源:</td>
            <td class="align_left_10px"><input name="from" type="text" id="from" /></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><input type="submit" name="Submit" value="提交" /></td>
          </tr>
        </table> 
</form>

<% 
if Trim(Request.QueryString("action"))="create" then
        dim title , author , editor , EditTime , content , from , html
        title=Trim(Request.Form("title"))
        editor=Trim(Request.Form("editor"))
        EditTime=Trim(Request.Form("EditTime"))
        content=Trim(Request.Form("content"))
        from=Trim(Request.Form("from"))
        html="<html>"_             '粘贴上面的修改后的 输出网页布局的源代码
        &"<head>"_                 ‘并用 _       与 &       把各行连接起来 或删除多余空格使源代码写在一行
        &"<title>"&title&"</title>"_
          &"<style type=""text/css"">"_ 
        &".article_title {"_
         &"font-size: 22px;"_
         &"font-weight: bold;"_
         &"text-align: center;"_
         &"padding-top: 10px;"_
         &"padding-bottom: 20px;"_
        &"}"_
        &".content {"_
         &"text-indent: 18px;"_
         &"font-size: 16px;"_
         &"line-height: 230%;"_
         &"text-align: left;"_
        &"}"_
        &".from {"_
         &"font-size: 14px;"_
         &"text-align: right;"_
         &"padding-right: 15px;"_
         &"padding-top: 15px;"_
        &"}"_
        &".feature_bar {"_
         &"font-size: 14px;"_
         &"color: #999999;"_
         &"text-align: center;"_
         &"padding-bottom: 15px;"_
        &"}"_ 
          &"</style>"_
        &"</head>"_
        &"<body>        "_
         &"<table width=""80%"">"_
         &"<tr>"_
          &"<td><div class=""article_title"">"&title&"</div>"_
           &"<div class=""feature_bar"">作者:"&author&"  录入时间:"&EditTime&"  录入:"&editor&" </div>"_
           &"<div class=""content"">"&content&"</div>"_
           &"<div class=""from"">来源:"&from&"</div>         </td>"_
         &"</tr>"_
          &"</table>"_
        &"</body> "_
        &"</html>" 
        dim HtmlFileName ,HtmlFile , fs , FileStream
        HtmlFileName=Trim(Request.Form("HtmlFileName"))
        if instr(HtmlFileName,".html")=false then
         HtmlFileName="NoName.html"
        end if
        HtmlFile=Server.MapPath(HtmlFileName)
        set fs=CreateObject("Scripting.FileSystemObject") 
        set FileStream=fs.CreateTextFile(HtmlFile)
        FileStream.WriteLine Html 
        FileStream.close 
        set FileStream=nothing
        response.Write("<script>alert('生成"&HtmlFileName&"文件成功!');history.go(-1);</script>")
end if
%>

</body>
</htm>
把上面的asp文件保存放到服务器上即可运行

3)方式2:生成的内容基于模板生成
思想:
给模板asp传递参数,使用“MSXML2.XMLHTTP”读取基于参数传递的asp模板的网页源代码,
再使用FSO组件生成静态网页。
步骤:
a、设计有参数传递的asp模板
b、设计asp控制页

设计asp模板:(保存成template.asp)
<% Option Explicit %>
<% 
dim HtmlFileName ,title , author , editor ,EditTime ,content ,from
HtmlFileName=Trim(Request.QueryString("HtmlFileName"))
title=Trim(Request.QueryString("title"))
author=Trim(Request.QueryString("author"))
editor=Trim(Request.QueryString("editor"))
EditTime=Trim(Request.QueryString("EditTime"))
content=Trim(Request.QueryString("content"))
from=Trim(Request.QueryString("from"))
%>
<html>
<head>
<title><%= title %></title>
     <style type="text/css">
<!--
.article_title {
font-size: 22px;
font-weight: bold;
text-align: center;
padding-top: 10px;
padding-bottom: 20px;
}
.content {
text-indent: 18px;
font-size: 16px;
line-height: 230%;
text-align: left;
}
.from {
font-size: 14px;
text-align: right;
padding-right: 15px;
padding-top: 15px;
}
.feature_bar {
font-size: 14px;
color: #999999;
text-align: center;
padding-bottom: 15px;
}
-->
     </style>
</head>
<body> 
     <table width="80%">
       <tr>
         <td><div class="article_title"><%= title %></div>
         <div class="feature_bar">作者:<%= author %>  录入时间:<%= EditTime %>  录入:<%= editor %> </div>
      <div class="content"><%= content %></div>
      <div class="from">来源:<%= from %></div>      </td>
       </tr>
     </table>
</body>
</html>

设计asp文件:(保存成html.asp)
<% Option Explicit %>
<html>
<head>
<title>ASP生成HTML</title>
<style type="text/css">
<!--
.align_right_top {
text-align: right;
vertical-align: top;
}
.align_left_10px {
text-align: left;
padding-left: 10px;
}
-->
</style>
</head>
<body>
<form method="post" action="?action=create">
     <table width="80%">
       <tr>
         <td class="align_right_top" >HTML文件名称:</td>
         <td class="align_left_10px"><input name="HtmlFileName" type="text" id="HtmlFileName" /></td>
       </tr>
       <tr>
         <td class="align_right_top" >文章标题:</td>
         <td class="align_left_10px"><input name="title" type="text" id="title" /></td>
       </tr>
       <tr>
         <td class="align_right_top">作者:</td>
         <td class="align_left_10px"><input name="author" type="text" id="author" /></td>
       </tr>
       <tr>
         <td class="align_right_top">录入:</td>
         <td class="align_left_10px"><input name="editor" type="text" id="editor" /></td>
       </tr>
       <tr>
         <td class="align_right_top">输入时间:</td>
         <td class="align_left_10px"><input name="EditTime" type="text" id="EditTime" /></td>
       </tr>
       <tr>
         <td class="align_right_top">文章内容:</td>
         <td class="align_left_10px"><textarea name="content" cols="55" rows="20" id="content"></textarea></td>
       </tr>
       <tr>
         <td class="align_right_top">来源:</td>
         <td class="align_left_10px"><input name="from" type="text" id="from" /></td>
       </tr>
       <tr>
         <td colspan="2" align="center"><input type="submit" name="Submit" value="提交" /></td>
       </tr>
     </table> 
</form>

<%
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

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


%>

<%  
if Trim(Request.QueryString("action"))="create" then
     dim title , author , editor , EditTime , content , from , html
     title=Trim(Request.Form("title"))
     editor=Trim(Request.Form("editor"))
     EditTime=Trim(Request.Form("EditTime"))
     content=Trim(Request.Form("content"))
     from=Trim(Request.Form("from"))
 
      '读取传递参数后的模版源代码,地址根据具体情况而定
html=getHTTPPage(" http://127.0.0.1/template.asp"_
     &"?title="&title&"&editor="&editor&"&EditTime="_
     &EditTime&"&content="&content&"&from="&content&"")
 
     dim HtmlFileName ,HtmlFile , fs , FileStream
     HtmlFileName=Trim(Request.Form("HtmlFileName"))
     if instr(HtmlFileName,".html")=false then
      HtmlFileName="NoName.html"
     end if
     HtmlFile=Server.MapPath(HtmlFileName)
     set fs=CreateObject("Scripting.FileSystemObject") 
     set FileStream=fs.CreateTextFile(HtmlFile)
     FileStream.WriteLine Html 
     FileStream.close 
     set FileStream=nothing
     response.Write("<script>alert('生成"&HtmlFileName&"文件成功!');history.go(-1);</script>")
end if
%>

</body>
</htm>

把template.asp与html.asp 放在同一目录通过服务器运行后即可。

4)结论
通过比较可以看出,通过方式一生成的html文件源代码比较乱,而通过模板生成的html文件源代码跟原先模板的源代码一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值