javascript asp教程第八课--request对象

Request Object:

Request has five (5) Collections, one (1) Property, and one (1) Method. You'll use the Collections far more than the property or the method.

Request Collections:

Below is a table of the Request Collections and descriptions of how they are used.

Request Collections ClientCertificate Request.ClientCertificate("Key[Field]")

Client security info for SSL encryption Cookies Request.Cookies("cookieName")

Holds cookie value stored on the client Form Request.Form("formName")

Holds value sent via HTML Form QueryString Request.QueryString("keyName")

Name/Value pair appended to the end of the URL ServerVariables Request.ServerVariables("variableName")

Hold values sent in the HTTP Headers

ClientCertificate:

Request.ClientCertificate is used with S.S.L. (Secure Sockets Layer). It is beyond the scope of this web site.

Cookies:

We will learn Request.Cookies and Response.Cookies together in Lesson 08. Please be patient.

Form:

Request.Form is probably the workhorse of the Request Collections. The first script is a repeat from Lesson 03.

<%@LANGUAGE="JavaScript"%> <% //No ASP Here, just a regular HTML Page %> <HTML> <STRONG>Type something into the text box and submit it.</STRONG> <FORM ACTION="script08a.asp" METHOD="Post"> <INPUT TYPE="Text" NAME="WebPageVariable"><BR> <STRONG>How Much Money do you make each month?</STRONG><BR> <SELECT NAME="monthlySalary"> <OPTION>Under $5,000,000</OPTION> <OPTION>Above $5,000,000</OPTION> <OPTION>Nobody's darn business.</OPTION> </SELECT><BR> <INPUT TYPE="Submit" VALUE="Submit"> </FORM> </HTML>

Click Here to run script08.asp in a new window. It posts information to script08a.asp which is found below. In turn, script08a.asp posts information to script08b.asp which is also found below.

<%@LANGUAGE="JavaScript"%> <% var WebPageVariable = new String( Request.Form("WebPageVariable") ) WebPageVariable = WebPageVariable.toUpperCase(); var monthlySalary = new String( Request.Form("monthlySalary") ) monthlySalary = monthlySalary.toLowerCase(); %> <HTML> The Web Page Variable you typed is: <%=WebPageVariable%> <BR> The monthly salary you listed is: <%=monthlySalary%> <BR> <FORM ACTION="script08b.asp" METHOD="Get"> <INPUT TYPE="hidden" VALUE="<%=monthlySalary%>" NAME="QueryVariable"> <STRONG>Click the button to see Query Strings</STRONG><BR> <INPUT TYPE="submit" VALUE="Submit"> </FORM> </HTML>

We'll be using Request.Form when we "Post" an HTML form to the server. Notice that the NAME attribute in the HTML form corresponds to the "name" in Request.Form("name"). To be more specific, <INPUT TYPE="Text" NAME="WebPageVariable"> corresponds with Request.Form("WebPageVariable"). We already talked about the need for the new String( ) constructor back in Lesson 03.

QueryString:

We'll be using Request.QueryString when we use an HTML form to "Get" a page from the server. Request.QueryString() is very similar to Request.Form(). Take a look at script08b.asp which I printed below.

<%@LANGUAGE="JavaScript"%> <% var QueryVariable = new String( Request.QueryString("QueryVariable") ) %> <HTML> The QueryString Value is: <%=QueryVariable%> <BR> <% if (QueryVariable != "Lesson 08's new Query!") { QueryVariable="Lesson 08's new Query!" QueryVariable=escape(QueryVariable) %> <A HREF="script08b.asp?QueryVariable=<%=QueryVariable%>">Click Here</A> for the link to <I>script08b.asp?QueryVariable=<%=QueryVariable%></I> <% } //closing bracket for if statement. %> </HTML>

If you haven't already, Click Here to run script08.asp in a new window. Cycle through all the forms and links, and then come back.

You can use Request.QueryString in two different ways. You can either use an HTML form to "Get" a page from the server, which will generate a query string. Or you can manually build a query string and add it to the backside of a link. We'll dissect script08b.asp from top to bottom.

var QueryVariable = new String( Request.QueryString("QueryVariable") )

The line above in script08b.asp corresponds to the line below from script08a.asp

<INPUT TYPE="hidden" VALUE="<%=monthlySalary%>" NAME="QueryVariable">

The NAME="someName" in the HTML form becomes the Request.QueryString("someName") on the next page.

About half way into script08b.asp are the lines I reprinted below.

<% if (QueryVariable != "Lesson 08's new Query!") { QueryVariable="Lesson 08's new Query!" QueryVariable=escape(QueryVariable) %>

We've already converted Request.QueryString() into a JavaScript string at the top of the script. So, now we can do a string comparison.

If the QueryVariable hasn't already been set equal to "Lesson 08's new Query!" then we do that. Then we use the escape( ) method to convert white space and special characters into Unicode. (URL's should contain neither whitespace, nor most special characters.)

In lesson 14 we'll see a better way to encode URL's. When we study the Server Object, we'll see Server.URLEncode(). But for now, just know that escape() works.

You can have more than one QueryString on each page. If you lose count of your QueryStrings, then you use Request.QueryString.Count to tell you the number.

The Request Shortcut:

Request.Form() and Request.QueryString() share a shortcut. Request.Form("WebPageVariable") can be abbreviated as Request("WebPageVariable") and Request.QueryString("QueryVariable") can be abbreviated as Request("QueryVariable").

ServerVariables:

Server Variables represent the HTTP Headers sent to the server by the client. I won't demonstrate them all, because there are too many.

<%@LANGUAGE="JavaScript"%> <HTML> <TABLE BORDER="1"> <TR><TD>ALL_RAW</TD> <TD><%=Request.ServerVariables("ALL_RAW")%></TD></TR> <TR><TD>REMOTE_ADDR</TD> <TD><%=Request.ServerVariables("REMOTE_ADDR")%></TD></TR> <TR><TD>HTTP_USER_AGENT</TD> <TD><%=Request.ServerVariables("HTTP_USER_AGENT")%></TD></TR> <TR><TD>URL</TD> <TD><%=Request.ServerVariables("URL")%></TD></TR> </TABLE> </HTML>

Click Here to run the script in a new window.

Demonstrated above are four (4) server variables. There are (give or take) about 50 server variables available. You can look up the full list of server variables for yourself on the internet.

Misc. Notes:

Request.BinaryRead() is the lone method and TotalBytes is the lone property. Request.BinaryRead(Request.TotalBytes) retrieves data from an HTML form using "POST." You must supply the TotalBytes as an argument. It stores the data into an array. BinaryRead cannot be used at the same time as Request.Form().

时间: 2024-09-13 19:31:19

javascript asp教程第八课--request对象的相关文章

javascript asp教程第八课--request对象_ASP基础

Request Object: Request has five (5) Collections, one (1) Property, and one (1) Method. You'll use the Collections far more than the property or the method. Request Collections: Below is a table of the Request Collections and descriptions of how they

javascript asp教程第十一课--Application 对象_ASP基础

Overview: The Application Object represents a collection of ASP pages. The Application object has zero (0) properties, two (2) collections, two (2) methods, and two (2) events. Get Started: Below are a couple scripts for lesson11. <%@LANGUAGE="Jav

javascript asp教程第十一课--Application 对象

Overview: The Application Object represents a collection of ASP pages. The Application object has zero (0) properties, two (2) collections, two (2) methods, and two (2) events. Get Started: Below are a couple scripts for lesson11. <%@LANGUAGE="Jav

javascript asp教程第三课 new String() 构造器

开始: new String() 是本课程计划中较早出现的另一个让人感觉到奇怪的地方.但和转义字符一样, new String()是创建一个成功的asp javascript应用的必须元素.下面是本课的两个脚本: 下面是实际上承担重量的脚本: 行为中的new String( ): 现在我们来看看下面的asp行. Request.Form 我们将在后面有独立的课程来讲授.下面才是我们现在要讲的重点.在request.form中所持有的数据(来自用户的数据)并不是一个javascript数据类型.相

javascript asp教程第五课--合二为一_ASP基础

两条防线,一个函数: 试问你如何能保证客户端和服务器端具有相同的功能?表单域的验证闪现在我们眼前.别人把你的html复制到另外一个脚本,然后改变客户端的表单域验证--这并不是一件难事.摆在眼前的解决方法是将表单域的验证放置在服务器端.但那又意味着因为使用者的一个小错误,都要给服务器端要返回一串的错误信息.那么,我们何不同时拥有二者呢?不仅仅如此,我们还可以在客户端和服务器端使用同一个javascript函数来保证二者的完全一致性. 看看下面这一小段,请特别注意一下checkMyZip()函数.

javascript asp教程第六课-- response方法_ASP基础

response 对象:reponse是asp中六个对象之一.它代表了服务器端对浏览器的回应.response有8种方法,9种属性和一个集.在这一课,我们就重点讲述方法.方法:在javascript中,asp方法使用括号.请注意依赖response.buffer的两个方法,我们将在下一课讲到他们.同样应该注意到addheader()和redirect(),因为他们必须优先于write()执行.所有的方法都在上面描述和演示了.下面我将详细讲述每一个方法.我将花点额外的时间来讲述我们用的最多的两个方

javascript asp教程第六课-- response方法

response 对象: reponse是asp中六个对象之一.它代表了服务器端对浏览器的回应.response有8种方法,9种属性和一个集.在这一课,我们就重点讲述方法. 方法: 在javascript中,asp方法使用括号.请注意依赖response.buffer的两个方法,我们将在下一课讲到他们.同样应该注意到addheader()和redirect(),因为他们必须优先于write()执行. 所有的方法都在上面描述和演示了.下面我将详细讲述每一个方法.我将花点额外的时间来讲述我们用的最多

javascript asp教程第五课--合二为一

两条防线,一个函数: 试问你如何能保证客户端和服务器端具有相同的功能?表单域的验证闪现在我们眼前.别人把你的html复制到另外一个脚本,然后改变客户端的表单域验证--这并不是一件难事.摆在眼前的解决方法是将表单域的验证放置在服务器端.但那又意味着因为使用者的一个小错误,都要给服务器端要返回一串的错误信息.那么,我们何不同时拥有二者呢?不仅仅如此,我们还可以在客户端和服务器端使用同一个javascript函数来保证二者的完全一致性. 看看下面这一小段,请特别注意一下checkMyZip()函数.

javascript asp教程第七课--response属性_ASP基础

Below is a table of Response Properties along with examples and explanations. Response Properties Buffer Response.Buffer = trueAllows for the buffering of outputCacheControl Response.CacheControl="Public" Sets Cache to "Public" or &quo