ASP學習筆記(6)--Response對象以及方法

Response是ASP的6個對象之一,表示的是server對web瀏覽器的迴應。其包含8個方法,9個屬性以及一個集合(collection) 。

8個方法如下:

AddHeader()--Response.AddHeader("my header", "my value")  : add you own html header

AppendToLog()--Response.AppendToLog("my log message") : send message to server log

BinaryWrite()--Response.BinaryWrite(binary data) : write binary data such as pictures

Clear()--Response.Clear() : clear buffered response if Response.Buffer ==true

End()--Response.End() : end the response

Flush()--Response.Flush() : send buffered response if Response.Buffer ==true

Redirect()--Response.Redirect("http://www.abc.com") : redirect the browser to other page

Write()--Response.Write() : output the response to the browser

需要注意的地方是,有兩種方法依賴於Response.Buffer(Clear() Flush())。並且AddHeader() 和Redirect()方法必須用在Writer()方法之前,否則會出錯。看如下例子

<%@ language=javascript %>
<html>
 <head>
  <title>this is a test of Redirect() and Write()</title>
 </head>
 <body>
  <form action="fortest8.asp" method="post">
   <strong>do you want to redirect to google?</strong>
   <select name="redirectVar">
    <option>Yes, I do.</option>
    <option>No, I donot.</option>
    <option>Who is Google?</option>
   </select>
   <input type=submit value=OK>
  </form>
 </body>
</html>

 

fortest8.asp

<%@ language=javascript %>
<%
 var redirectVar=new String(Request.Form("redirectVar"))
 if (redirectVar=="Who is Google?")
  whogoogle();
 if (redirectVar=="Yes, I do.")
  Response.Redirect("http://www.google.com");
 if (redirectVar=="No, I donot.")
  Response.Redirect("test8.asp");
 if (redirectVar=="undefined")
  Response.Redirect("test8.asp")
 function whogoogle(){
  Response.write("<html>/r")
  Response.write("google is a search engine<br>/r")
  Response.write(" you should try it, it is good<br>/r")
  Response.write("</html>/r")
 }
%>

上例中將write()方法使用在一個javascript函數中。當然了,有時候是不需要用write()也可以實現一些輸出,這被成爲write快捷方式。如下例:

<%@ language=javascript %>
<html>
 <head>
  <title>this is a test of write shortcut</title>
 </head>
 <% var thetime=new Date() %>
 <body>
  the time and date is <% =thetime %><br>
  <% var sayhello="hello "
  sayhello +="world"
  %>
  <% =sayhello %>
 </body>
</html>

使用快捷方式最大的好處是我們可以輸出javascript數據類型或者asp的數據類型。但是,請記住,使用快捷方式有一點需要注意,那就是每次只能輸出一個數據類型。而且,我們這個例子中所有的腳本都是線性執行的,但是如果有了RUNAT屬性,這個可就不一定了哦。

 

 

Google
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章