当前位置: 移动技术网 > IT编程>开发语言>Asp > 生成静态页大全[ASP/PHP/ASPX]

生成静态页大全[ASP/PHP/ASPX]

2017年12月12日  | 移动技术网IT编程  | 我要评论
asp生成静态网页的方法   随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,很多用access作数据库的更会深有体会,静态页加在搜索时,也会被优先考虑。互联网
asp生成静态网页的方法
  随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,很多用access作数据库的更会深有体会,静态页加在搜索时,也会被优先考虑。互联网上流行的做法是将数据源代码写入数据库再从数据库读取生成静态面,这样无形间就加大了数据库。将现有的asp页直接生成静态页,将会节省很多。

  下面的例子是将、index.asp?id=1/index.asp?id=2/index.asp?id=3/这三个动态页面,分别生成ndex1.htm,index2.htm,index3.htm存在根目录下面:


<%
dim strurl,item_classid,id,filename,filepath,do_url,html_temp
html_temp="<ul>"
for i=1 to 3
html_temp = html_temp&"<li>"
item_classid = i
filename = "index"&item_classid&".htm"
filepath = server.mappath("/")&"\"&filename
html_temp = html_temp&filepath&"</li>"
do_url = "http://"
do_url = do_url&request.servervariables("server_name")&"/main/index.asp"
do_url = do_url&"?item_classid="&item_classid
strurl = do_url
dim objxmlhttp
set objxmlhttp = server.createobject("microsoft.xmlhttp")
objxmlhttp.open "get",strurl,false
objxmlhttp.send()
dim binfiledata
binfiledata = objxmlhttp.responsebody
dim objadostream
set objadostream = server.createobject("adodb.stream")
objadostream.type = 1
objadostream.open()
objadostream.write(binfiledata)
objadostream.savetofile filepath,2 
objadostream.close()
next
html_temp = html_temp&"<ul>"
%>
<%
response.write ( "成功生成文件:" )
response.write ( "<br>" )
response.write html_temp
%>

php生成静态网页的方法
  看到很多朋友在各个地方发帖问php生成静态文章系统的方法,以前曾做过这样一个系统,遂谈些看法,以供各位参考。好了,我们先回顾一些基本的概念。
  一,php脚本与动态页面。
  php脚本是一种服务器端脚本程序,可通过嵌入等方法与html文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原理是这样的。由客户端提出请求,请求某一页面 -----> web服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的php解析器对脚本进行解析形成html语言形式 ----> 将解析后的html语句以包的方式传回给浏览器。由此不难看出,在页面发送到浏览器后,php就不存在了,已被转化解析为html语句。客户请求为一动态文件,事实上并没有真正的文件存在在那里,是php解析而成相对应的页面,然后发送回浏览器。这种页面处理方式被称为“动态页面”。
  二,静态页面。
  静态页面是指在服务器端确实存在的仅含html以及js,css等客户端运行脚本的页面。它的处理方式是。由客户端提出请求,请求某一页面 ----> web服务器确认并载入某一页面 ----> web服务器将该页面以包的形式传递回浏览器。由这一过程,我们对比一下动态页面,即可方现。动态页面需由web服务器的php解析器进行解析,而且通常还需连接数据库,进行数据库存取操作,然后才能形成html语言信息包;而静态页面,无须解析,无须连接数据库,直接发送,可大大减轻服务器压力,提高服务器负载能力,大幅提供页面打开速度和网站整体打开速度。但其缺点是,不能动态地对请求进行处理,服务器上必须确实存在该文件。
  三,模板及模板解析。
  模板即尚未填充内容html文件。例如:
 temp.html


<html>
<title>{ title }</title>
<body>
this is a { file } file's templets
</body>
</html>
php处理:
 templetest.php

<?php
$title = "http://siyizhu.com测试模板";
$file = "twomax inter test templet,<br>author:matrix@two_max";

 $fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file }",$file,$content);
$content .= str_replace ("{ title }",$title,$content);

echo $content;
?> 
模板解析处理,即将经php脚本解析处理后得出的结果填充(content)进模板的处理过程。通常借助于模板类。目前较流行的模板解析类有phplib,smarty,fastsmarty等等。模板解析处理的原理通常为替换。也有些程序员习惯将判断,循环等处理放进模板文件中,用解析类处理,典型应用为block概念,简单来说即为一个循环处理。由php脚本指定循环次数,如何循环代入等,再由模板解析类具体实施这些操作。
  好了,对比过静态页面与动态页面各自的优劣,现在我们就来说说,如何用php生成静态文件。
  php生成静态页面并不是指php的动态解析,输出html页面,而是指用php创建html页面。同时因为html的不可写性,我们创建的html若有修改,则需删掉重新生成即可。(当然你也可以选择用正则进行修改,但个人认为那样做倒不如删掉重新生成来得快捷,有些得不偿失。)
  言归正传。用过php文件操作函数的php fans知道,php中有一个文件操作函数fopen,即打开文件。若文件不存在,则尝试创建。这即是php可以用来创建html文件的理论基础。只要用来存放html文件的文件夹有写权限(即权限定义0777),即可创建文件。(针对unix系统而言,win系统无须考虑。)仍以上例为例,若我们修改最后一句,并指定在test目录下生成一个名为test.html的静态文件:

<?php
$title = "http://siyizhu.com测试模板";
$file = "twomax inter test templet,<br>author:matrix@two_max";
 $fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{file}",$file,$content);
$content .= str_replace ("{title}",$title,$content);
// echo $content; 
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //打开文件指针,创建文件
/*
 检查文件是否被创建且可写
*/
if (!is_writable ($filename)){
die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die ("生成文件".$filename."失败!");

fclose ($handle); //关闭指针

die ("创建文件".$filename."成功!");
?>
实际应用中常见问题解决方案参考:
  一,文章列表问题:  
  在数据库中创建字段,记录文件名,每生成一个文件,将自动生成的文件名存入数据库,对于推荐文章,只需指向存放静态文件的指定文件夹中的该页面即可。利用php操作处理文章列表,存为字符串,生成页面时替换此字符串即可。如,在页面中放置文章列表的表格加入标记{articletable},而在php处理文件中:

<?php
$title = "http://siyizhu.com测试模板";
$file = "twomax inter test templet,<br>author:matrix@two_max";
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{file}",$file,$content);
$content .= str_replace ("{title}",$title,$content); 
// 生成列表开始
$list = '';
$sql = "select id,title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$result['title'].'</a><br>';
}
$content .= str_replace ("{articletable}",$list,$content); 
//生成列表结束
// echo $content; 
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //打开文件指针,创建文件
/*
 检查文件是否被创建且可写
*/
if (!is_writable ($filename)){
die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die ("生成文件".$filename."失败!");

fclose ($handle); //关闭指针 
die ("创建文件".$filename."成功!");
?>
二,分页问题。
  如我们指定分页时,每页20篇。某子频道列表内文章经数据库查询为45条,则,首先我们通过查询得到如下参数:1,总页数;2,每页篇数。第二步,for ($i = 0; $i < allpages; $i++),页面元素获取,分析,文章生成,都在此循环中执行。不同的是,die ("创建文件".$filename."成功!";这句去掉,放到循环后的显示,因为该语句将中止程序执行。例:

<?php
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage = '20';
$sql = "select id from article where channel='$channelid'";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "";
} else {
$indexpath = "index_".$i."html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$title.'</a><br>';
}
$content = str_replace ("{articletable}",$list,$content);
if (is_file ($indexpath)){
@unlink ($indexpath); //若文件已存在,则删除
}
$handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
/*
  检查文件是否被创建且可写
*/
if (!is_writable ($indexpath)){
echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
}
if (!fwrite ($handle,$content)){ //将信息写入文件
echo "生成文件".$indexpath."失败!"; //修改为echo

fclose ($handle); //关闭指针
}
fclose ($fp);
die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
?>
大致思路如此,其中如其它数据生成,数据输入输出检查,分页内容指向等可酌情在页面中加入。
  在实际文章系统处理过程当中,还有许多问题有待考虑,与动态页面不同之处,需注意的地方还有很多。但大致思路即是如此,其它方面可举一反三而得。

asp.net生成静态网页的方法
环境:microsoft .net framework sdk v1.1 
os:windows server 2003 中文版
asp.net生成静态html页
在asp中实现的生成静态页用到的filesystemobject对象!
在.net中涉及此类操作的是system.io 
以下是程序代码 注:此代码非原创!参考别人代码

//生成html页
public static bool writefile(string strtext,string strcontent,string strauthor) 
{
string path = httpcontext.current.server.mappath("/news/");
encoding code = encoding.getencoding("gb2312");
// 读取模板文件
string temp = httpcontext.current.server.mappath("/news/text.html");
streamreader sr=null;
streamwriter sw=null;
string str=""; 
try
{
sr = new streamreader(temp, code);
str = sr.readtoend(); // 读取文件
}
catch(exception exp)
{
httpcontext.current.response.write(exp.message);
httpcontext.current.response.end();
sr.close();
}

string htmlfilename=datetime.now.tostring("yyyymmddhhmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.replace("showarticle",strtext); //模板页中的showarticle
str = str.replace("biaoti",strtext);
str = str.replace("content",strcontent);
str = str.replace("author",strauthor);
// 写文件
try
{
sw = new streamwriter(path + htmlfilename , false, code);
sw.write(str);
sw.flush();
}
catch(exception ex)
{
httpcontext.current.response.write(ex.message);
httpcontext.current.response.end();
}
finally
{
sw.close();
}
return true;
此函数放在conn.cs基类中了
在添加新闻的代码中引用 注:工程名为hover

if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))
{
response.write("添加成功");
}
else
{
response.write("生成html出错!");
}
模板页text.html代码

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>showarticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</html>
biaoti
<br>
content<br>
author
</body>
</html>

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网