Asp深度揭密
發表時間:2023-08-23 來源:明輝站整理相關軟件相關文章人氣:
[摘要](一)-(整理)一、Asp基本知識 1.Asp是Active Server Pages的簡稱,是解釋型的腳本語言環境; 2.Asp的運行需要Windows操作系統,9x下需要安裝PWS;而NT/20...
(一)-(整理)一、Asp基本知識
1.Asp是Active Server Pages的簡稱,是解釋型的腳本語言環境;
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
j
思考:變量是如何在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的緩沖功能,訪問該頁面時出錯
a
答復對象 錯誤 '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")
%>
效果:
您是本站第 1 位訪客!
您來自 10.1.43.238
思考:既然session和application都能做到計數,那它們之間有什么區別?如果要做到滿100重新開始計數如何實現?