转贴-response对象(2000.03.22)
response对象
response对象非常有用而且功能丰富。这里我们将要给大家介绍的是其最基本的功能。
当然它的所有功能比我们在这里所要介绍的多得多,但我们认为这里所要介绍的20%的
内容是你会在80%的时间中都会用到的。我们认为response对象主要的功能如下:
# response.write
# response.write也可以简写为<%=%>这样的格式简单的嵌入到html语言中
# response.redirect方法可以使浏览器尝试连接到其他 url
# response.end可以有效地终止一段脚本的继续解析
response对象第二部分——buffer控制
response object error asp 0156 : 80004005
header error
whatever.asp, line #
the http headers are already written to the client browser.
any http header modifications must be made before writing page content.
这样的错误信息是否也同样困扰过你?
不过只要你把"<%response.buffer=true%>”这一行加在任何一页混合着redirects和
网页内容的网页的第一行,你就不会再看到这样的错误了!这一行会解除浏览器发出:
"headers are already sent”信息。
buffer属性指示是否缓冲页输出。也就是说添加这个属性后页面所有的信息会先放入
缓冲页中,等到当前页的所有服务器脚本处理完毕之后才向浏览器写入。如果当浏览
器已经写了一些什么内容之后,你就再也不能使用response.redirect了,因为浏览器
不能又写内容又redirects(改变方向)。
<%response.buffer=true%>主要是告诉浏览器在一下事件发生之前不要写入任何东西:
a) response.end执行前
b) response.flush执行前
c) 当前页的所有服务器脚本处理完毕之前
d) response.redirect命令已被发送,并且没有任何内容通过response.flush被发送之前。
<%response.buffer=true%>的唯一缺点就是缓冲将在服务器未处理完当前页的所有脚本之前
阻止在客户端显示响应。如果页面要花一点时间来组织(比如从数据库中提取上千条的记录),
这样在网页没有被完全提交给浏览器之前人们看不到任何信息,时间久了网页看上去就似乎
是没有响应了。为了避免这样的情况,通常使用response.flush可以让用户看已经组织完成
的部分页面内容。
response对象第二部分——redirect方法
redirect 方法使浏览器尝试连接到其他 url,这个很类似于html中<meta refresh>的功能。
以下是这个方法的一个例子:
<html><head>
<title>formjump.asp</title>
</head><body bgcolor="#ffffff">
<form action="formjumprespond.asp" method="get">
<select name="wheretogo">
<option selected value="fun">fun</option>
<option value="news">daily news</option>
<option value="docs">asp iis3 roadmap/docs</option>
<option value="main">mainpage of activeserverpages.com</option>
<option value="sample">iis 3 sample asp scripts</option>
</select>
<input type=submit value="choose destination">
</form>
</body></html>
the responder that reacts to this form is:
<%response.buffer=true%>
<html><head>
<title>formjumprespond.asp</title>&
<body bgcolor="#ffffff">
<%
my asp program that redirects to url
thisurl="http://www.joyeasy.net/"
where=request.querystring("wheretogo")
select case where
case "main"
response.redirect thisurl & "/"
case "samples"
response.redirect thisurl & "/aspsamp/samples/samples.htm"
case "docs"
response.redirect thisurl & "/iasdocs/aspdocs/roadmap.asp"
case "news"
response.redirect "http://www.sina.com"
case "fun"
response.redirect "http://www.nease.com"
end select
response.write "all dressed up and i dont know where to go<br>"
response.write "i recommend –> " & "<br>"
response.write server.htmlencode(thisurl & "/learn/test/res2.asp?where=fun") & "<br>"
response.write "for a good laugh!" & "<p>"
%>
</body></html>
======================================================================转贴-如何利用asp实现邮箱访问(2000.03.21)
如何利用asp实现邮箱访问
您在访问网站时是否会在有些页面上见到这种功能—您在可以访问此网站的同时,
还可以查看您免费邮箱中是否有新邮件。这个功能是不是让您觉得很心动、很神秘呢?
下面,我就用asp来举个例子让您知道是如何实现这一功能的。
首先你可以去一些提供免费邮件服务的站点,申请一个账号然后登录。在打开邮箱时,
请您注意地址栏中的内容。现在以371为例,你会发现其内容通常是:
http://www.371.net/prog/login?user=fighter&pass=mypassword。
其中"fighter"是您的账号,"mypassword"是您的密码。这时我们可以从这里得到3个信息。
第1条是我们得到了处理文件的url及文件名:"http://www.371.net/prog/login";
第2条是记录您账号的变量名:user;第3条是记录您密码的变量名:pass。
我们知道这些信息后,就可着手写html文件和asp文件了。
/*html源文件内容如下:*/
<html>
<head>
<meta name="generator" content="microsoft visual studio 6.0">
</head>
<title>city club 首页</title>
<style type="text/css">
<!–
td { font-size: 9pt}
body { font-size: 9pt}
select { font-size: 9pt}
a {text-decoration: none; color: #003366; font-size: 9pt}
a:hover {text-decoration: underline; color: #ff0000; font-size: 9pt}
–>
</style>
<script language="javascript">
function check(tt) {
if (window.document.form1.selectmail.selectedindex==0) {
alert("请选择您的邮箱服务器!")
window.document.form1.selectmail.focus()
return false
}
if (tt.account.value=="") {
alert("帐号不能为空!请填写。")
tt.account.focus()
return false
}
if (tt.account.value.length<3) {
alert("帐号长度不能小于3位!请填写。")
tt.account.focus()
return false
}
if (tt.password.value=="") {
alert("密码不能为空!请填写。")
tt.password.focus()
return false
}
if (tt.password.value.length<3) {
alert("密码长度不能小于3位!请填写。")
tt.password.focus()
return false
}
else
return true
}
</script>
<body topmargin=12>
<table border=0 bgcolor=d3d3d3>
<td>
<form action="postoffice.asp" method=post onsubmit="return check(this)" name=form1
target="_blank"> <!–此处用target="_blank",是为了弹出新窗口来查看您的邮箱–>
<select style="font-size:9pt;background-color:add8e6" name="selectmail">
<option name="mailsite" value="city club便民邮局" selected>city club便民邮局</option>
<option name=mailsite value=990.net/prog/login?;user;pass;>990</option>
<option name=mailsite value=www.371.net/prog/login?;user;pass;>371</option>
<option name=mailsite value=www.188.net/prog/login?;user;pass;>188</option>
<option name=mailsite value=web.163.net/cgi/login?;user;pass;>163</option>
<option name=mailsite value=freemail.263.net/cgi/login?;user;pass;>263</option>
<option name=mailsite value=mail.777.net.cn/v2.0/html/mailbox.php3?;user;pass;>777</option>
<option name=mailsite value=www.126.com/cgi/login?;email;password;>126</option>
<option name=mailsite value=www.2911.net/cgi-bin/login?;username;password;>2911</option>
<option name=mailsite value=hotmail.yn.cninfo.net/prog/login?;user;pass;>云南169</option>
<option name=mailsite value=freemail.china.com/prog/login?;user;pass;>china</option>
<option name=mailsite value=freemail.hongkong.com/prog/login?;user;pass;>香港免费电邮</option>
<option name=mailsite value=freemail.netease.com/prog/login?;user;pass;>netease</option>
<option name=mailsite value=lc3.law5.hotmail.com/cgi-bin/dologin?;login;passwd;>hotmail</option>
<option name=mailsite value=www.netaddress.com/tpl/door/login?;userid;passwd;>use.net</option>
<option name=mailsite value=www.88998.com/cgi-win/login?;username;password;>88998.com</option>
<option name=mailsite value=www.mail.com/mailcom/login.jhtml?;mn;pw;>@mail.com</option>
</select><br>
帐号:<input type=text name=account size=12 style="font-size:9pt"><br>
密码:<input type=password name=password size=12 style="font-size:9pt"><br>
</td><tr><td align=center><input type=submit value="收信" style="font-size:9pt">
<input type=reset value="重填" style="font-size:9pt">
</td>
</form>
</td>
</table>
</body>
</html>
/*html源文件内容结束*/
/*postoffice.asp源文件内容如下:*/
<%@ language=vbscript %>
<%
response.buffer = true
—————————————————-
author : peter.yu
created date : 2000/3/13
file name : postoffice.asp
all rights reserved.所有权归city club
—————————————————-
%>
<html>
<head>
<meta name="generator" content="microsoft visual studio 6.0">
</head>
<title>city club 便民邮局 (all rights reserved所有权归city club)</title>
<body>
<%
dim str(3)
str1 =trim(request.form("selectmail")) /*获取的邮件服务器及用户账号和密码信息*/
for i = 1 to 3 /*将以上获取的信息进行分割,并赋予给数组变量*/
p = instr(1,str1,";")
str(i-1) = mid(str1,1,p-1)
str1 = mid(str1,p+1)
next
if instr(1,str(0),"http://")=0 then
websiteurl = "http://" & str(0)
else
websiteurl = str(0) /*邮件服务器地址及指定处理的文件名*/
end if
usernam = str(1) /*账号变量名*/
password = str(2) /*密码变更名*/
/*合并字符,得到诸如"http://www.371.net/prog/login?user=fighter&pass=mypassword的字符"*/
mailurl = websiteurl & usernam & "=" & trim(request.form("account"))
mailurl = mailurl & chr(38) & password & "=" & trim(request.form("password"))
response.redirect mailurl /*打开邮箱*/
%>
</body>
</html>
/*postoffice.asp源文件内容结束*/
不是很难吧,呵呵。其实这个不是很难的,关键在于您能多多观察,找出其中的规律。
这样我们就可以利用这些规律做很多很多有意义有趣的事了。
=================================================
转贴-避免重复定义数组(2000.03.20)
当我们在使用dim时,避免重新定义数组。因为你可能要用redim去重新定义数
组的大小。至于要做这样的操作的话,如果你的机器内存不是很大,那么最好在
一开始就考虑到最坏的打算去设置数组的长度或者设置最佳状态时的长度,在非
常必要时才使用redim。当然这样并不意味着要去增加内存,如果你不是很需要的
话。
以下举例说明不恰当的使用redim
<%
dim myarray()
redim myarrray(2)
myarray(0) = "hello"
myarray(1) = "good-bye"
.
.
.
some other code where you end up needing
more space happens then …
redim preserve myarray(5)
myarray(2) = "more stuff"
myarray(3) = "even more stuff"
myarray(4) = "yet more stuff"
%>
其实在开始就定义myarray(5),而以后需要的话再用redim去增加他的大小,
这样的话可能会占用一些内存,但速度就要快得多了。
