
 
 | 
| 技术资料  > ASP技术 > 客户端相关 : ASP精华文摘-001(转) |  
ASP精华文摘-001(转) March 25,2004 |  
转贴-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 don't 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去增加他的大小,  
这样的话可能会占用一些内存,但速度就要快得多了。 |  
 
 | 
  
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved