发新话题
打印

[推荐]Asp深度揭密

[推荐]Asp深度揭密

(一) 
一、Asp基本知识 

1.Asp是Active Server ages的简称,是解释型的脚本语言环境; 
2.Asp的运行需要Windows操作系统,9x下需要安装PWS;而NT/2000/XP则需要安装Internet Information Server(简称IIS); 
3.Asp和JSP的脚本标签是“<%%>”,PHP的则可以设定为多种; 
4.Asp的注释符号是“'”; 
5.使用附加组件,可以扩展Asp的功能。 

例子: 

HelloWorld_1.asp 
<%="Hello,world"%> 

效果: 
Hello,world 


HelloWorld_2.asp 
<% 
for i=1 to 10 
response.write "Hello,world" 
next 
%> 

效果: 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 
Hello,world 

注意:Asp不区分大小写;变量无需定义也可使用,转换方便;语法检查很松。 


二、Asp内置对象的使用: 

可以使用下面的任何ASP内置对象,而不必在ASP脚本中特别声明。 

1. Request: 

定义:可用来访问从浏览器发送到服务器的请求信息,可用此对象读取已输入HTML表单的信息。 

集: 
Cookies:含有浏览器cookies的值 
Form:含有HTML表单域中的值 
QueryString:含有查询字符串的值 
ServerVariables:含有头和环境变量中的值 

例子: 

request_url.asp 
<% 
'获取用户输入,并存入变量 
user_id=request.querystring("user_id") 
user_name=request.querystring("user_name") 

'判断用户输入是否正确 
if user_id="" then 
response.write "User_id is null,please check it" 
response.end 
end if 
if user_name="" then 
response.write "User_name is null,please check it" 
response.end 
end if 

'打印变量 
response.write user_id&"<br>" 
response.write user_name 
%> 

效果: 
当访问http://10.1.43.238/course/request_url.asp?user_name=j时: 
User_id is null,please check it 
当访问http://10.1.43.238/course/request_url.asp?user_name=j&user_id=my_id时: 
my_id 


思考:变量是如何在URL中传递和被Asp页面获取的? 


request_form.htm 
<style type="text/css"> 
<!-- 
.input {background-color: #FFFFFF; border-bottom: black 1px solid;border-left: black 1px solid; border-right: black 1px solid;border-top: black 1px solid; color: #000000;font-family: Georgia; font-size: 9pt;color: midnightblue;} 
a:link {color: #1B629C; text-decoration: none} 
a:hover {color: #FF6600; text-decoration: underline} 
a:visited {text-decoration: none} 
--> 
</style> 

<center> 
<form name="course" action="request_form.asp" method="post"> 
User_id:<input type="text" name="user_id" maxlength="20" class="input"><br><br> 
User_name:<input type="text" name="user_name" maxlength="30" class="input"> 
</form> 
<br><br> 
<a href="javascript:document.course.submit();"> 提 交 
</center> 

request_form.asp 
<% 
'获取用户输入,并存入变量 
user_id=request.form("user_id") 
user_name=request.form("user_name") 

'判断用户输入是否正确 
if user_id="" then 
response.write "User_id is null,please check it" 
response.end 
end if 
if user_name="" then 
response.write "User_name is null,please check it" 
response.end 
end if 

'打印变量 
response.write user_id&"<br>" 
response.write user_name 
%> 

注意:form的action的指向,request_form.asp和request_url.asp在源代码上的区别? 

2. Response: 

定义:用来向浏览器回发信息,可用此对象从脚本向浏览器发送输出。 

集: 
Cookies:在浏览器中加入一个cookie 

方法: 
End:结束脚本的处理 
Redirect:将浏览器引导至新页面 
Write:向浏览器发送一个字符串 

属性: 
Buffer:缓存一个ASP 
CacheControl:由代理服务器控制缓存 
ContentType: 规定响应的内容类型 
Expires:浏览器用相对时间控制缓存 
ExpiresAbsolute:浏览器用绝对时间控制缓存 

例子: 

response_redirect.asp 
<% 
'去google看看吧 
response.redirect "http://www2.google.com" 
response.end 
%> 


response_cookies.asp 
<% 
'设置和读取cookies 
response.cookies("time_now")=now() 
response.write request.cookies("time_now") 
%> 

效果: 
当访问http://10.1.43.238/course/response_cookies.asp时: 
2002-9-1 16:20:40 


response_buffer.asp 
<%'response.buffer=true%> 
<a href="a">a 
<%response.redirect "request_form.htm"%> 

效果: 
①.当关闭IIS的缓冲功能,访问该页面时出错 

答复对象 错误 'ASP 0156 : 80004005' 
头错 
/course/response_buffer.asp,行3 
HTTP 头已经写入到 客户浏览器。任何 HTTP 头的修改必须在写入页内容之前。 
②.当关闭IIS的缓冲功能,去掉文件第一行的注释,则页面重定向成功 
③.当打开IIS的缓冲功能,无论是否去掉文件第一行的注释,页面重定向都成功 

3. Server 

定义:可在服务器上使用不同实体函数,如在时间到达前控制脚本执行的时间。还可用来创建其他对象。 

方法: 
CreateObject:创建一个对象实例 
HTMLEncode:将字符串转化为使用特别的HTML字符 
MapPath:把虚拟路径转化成物理路径 
URLEncode:把字符串转化成URL编码的 
ScriptTimeout:在终止前,一个脚本允许运行的秒数 

例子: 

server_htmlencode.asp 
<% 
'html encode 
response.write server.htmlencode("a""time_now") 
%> 

效果: 
a"time_now 
查看源文件时显示为:a"time_now 

思考:为什么不是a""time_now这种效果?源文件是怎么了? 


server_mappath.asp 
<% 
'mappath 
response.write server.mappath("server_mappath.asp") 
%> 

效果: 
G:\asp_www\test\course\server_mappath.asp 

思考:如何获取站点根目录的实际路径?如何获取某个目录的实际路径? 


server_urlencode.asp 
<% 
'url encode 
response.write server.urlencode("a\time_now") 
%> 

效果: 
a%5Ctime%5Fnow 

4. Application 

定义:用来存储、读取用户共享的应用程序信息,如可以用此对象在网站的用户间传送信息,当服务器重启后信息丢失。 

方法: 
Lock:防止其它用户访问Application集 
Unlock:使其它用户可以访问Application集 

事件: 
OnEnd:由终止网络服务器、改变Global.asa文件触发 
OnStart:由应用程序中对网页的第一次申请触发 

例子: 

application_counter.asp 
<% 
'一个使用Application制作的简单计数器 
Application.lock 
Application("clicks")=Application("clicks")+1 
Application.unlock 

response.write "您是本站第 "&Application("clicks")&" 位访客!" 
response.write "<br><br>您来自 "&request.servervariables("remote_addr") 
%> 

效果: 
您是本站第 1 位访客! 

您来自 10.1.43.238 

思考:本例中lock和unlock有何作用? 

5. Session 

定义:存储、读取特定用户对话信息,如可存储用户对网站的访问信息,当服务器重启后信息丢失。 

方法: 
Abandon:处理完当前页面后,结束一个用户会话 

属性: 
Timeout:用户会话持续时间(分钟数) 

事件: 
OnEnd:在Session Timeout时间以外,用户不再申请页面触发该事件 
OnStart:由用户对网页的第一次申请时触发 

例子: 

session_counter.asp 
<% 
'一个使用Session制作的简单计数器 
session("clicks")=session("clicks")+1 

response.write "您是本站第 "&session("clicks")&" 位访客!" 
response.write "<br><br>您来自 "&request.servervariables("remote_addr") 
%> 

TOP

发新话题