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().

(0)

相关推荐

  • ASP.NET 使用application与session对象写的简单聊天室程序

    ASP.Net中有两个重要的对象,一个是application对象,一个是session对象. Application:记录应用程序参数的对象,该对象用于共享应用程序级信息. Session:记录浏览器端的变量对象,用来存储跨网页程序程序的变量或者对象. 说实话,写了快一年的asp.net,application对象还真没怎么用过.看了看书,根据这两个对象的特性写了一个简单的聊天室程序.真的是非常的简陋. 我的思路是,有两个页面Default页和ChatRoom页,页面布局如图: Default

  • Asp.net内置对象之Server对象(概述及应用)

    一.了解Server对象 Server对象提供对服务器上的方法和属性的访问以及进行HTML编码的功能.这些功能分别由Server对象相应的方法和属性完成. 二.Server对象的常用属性 (1).MachineName(2).ScriptTimeout:属性用于设置脚本程序执行的时间,适当地设置脚本程序的ScriptTimeout可以提高整个Web应用程序的效率.语法如下:Server.ScriptTimeout=time;(以s(秒)为单位) ScriptTimeout属性的最短时间默认为90

  • ASP中Request对象获取客户端数据的顺序(容易忽略)

    在ASP中Request对象是获取客户端提交数据的一个很重要的对象,大家对他也是非常熟悉了.虽然如此,还是经常有人问我下面的几种写法有什么不同,到底应该怎么写? strMessage = Request("msg") strMessage = Request.Form("msg") 而且,我也看过好多人写的代码,一律都是Request("")的写法,当然这样的写法并没有什么错. 只是大家应该注意 Request对象有几个集合来获取客户端提交的数据

  • ASP基础知识Command对象讲解

    Coonamd 对象定义了将对数据源执行的命令,可以用于查询数据库表并返回一个记录集,也可以用于对数据库表进行添加.更改和删除操作. 一.使用Command 对象的步骤: 当在 ASP 页面中使用 Command 对象处理数据时,应首先设置命令类型.命令文本以及相关的活动数据库连接等,并通过 Parameter 对象传递命令参数,然后通过调用 Execute 方法来执行 SQL 语句或调用存储过程,以完成数据库记录的检索.添加.更改和删除任务.其步骤如下: 1.使用 ActiveCommand

  • ASP.NET中使用Application对象实现简单在线人数统计功能

    注:最近在复习ASP.NET,为了加深印象,会制作一些小的demo程序,分享给大家. 1 新建ASP.NET网站,编辑Global.asax文件,修改后的文件内容如下所示. <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Applicati

  • Asp.net内置对象之Request对象(概述及应用)

    前言:Request对象主要用于获取来自客户端的数据,如用户填入表单的数据.保存在客户端的Cookie等. 一.Request对象概述 1.主要属性  ApplicationPath  获取服务器上asp.net应用程序的虚拟应用程序根路径  Browser  获取有关正在请求的客户端的浏览器功能的信息,该属性值为:HttpBrowserCapabilities对  象  ContentEncoding  获取或设置实体主体的字符集.该属性值为表示客户端的字符集Encoding对象  Conte

  • ASP基础入门第六篇(ASP内建对象Request)

    在正式开始学习 ASP 的内建对象和组件之前,先让我们来认识一些基本概念,这将对各位今后的学习大有帮助.请看下表 : Active Server 随 Windows NT 交付的服务器方技术的集合.这些 技术为组件应用程序管理.数据库访问.事务和消息 提供一致的服务器方组件.脚本模型和一套集成的系 统服务. Active Server Pages (ASP) 在服务器上运行 ActiveX 脚本和 ActiveX 组件的服 务器方脚本环境.开发人员可以将脚本和组件结合在 一起创建基于 Web 的

  • ASP的Error对象知识简析

    在VBScript中,有一个On Error Resume Next语句,它使脚本解释器忽略运行期错误并继续脚本代码的执行.接着该脚本可以检查Err.Number属性的值,判别是否出现了错误.如果出现错误,返回一个非零值.在ASP3.0中,也可以使用OnErrorGoto0"转回到"缺省的错误处理.在ASP2.0中实际也进行这种处理,但是没有相应文档说明,这在很多asp数据相关处理文件中司空见惯,加上On Error Resume Next,关闭缺省的错误处理,然后用err抓住, If

  • Asp.net response对象与request对象使用介绍

    1.Response:服务器发给客户端信息,或者说是服务器的向用户发送输出结果. Redirect:让客户端重新定向到指定的 URL. Write:写出指定字符串. 2.request:客户端发给服务器,或者说是从客户端取得信息. form:从使用post提交方式的表单获取表单元素的值. querystring:取回查询字符串中的变量值,适用于get提交方式的表单. 举一个列子:一个登陆页面,还有一个主页面.当登陆页面登陆成功后,就自动跳转到主页面. 1.login.aspx 复制代码 代码如下

  • ASP.NET中Application全局对象用法实例浅析

    本文实例讲述了ASP.NET中Application全局对象用法.分享给大家供大家参考.具体如下: Application是应用全局对象,被全体共享.无论通过哪个页面操作Application,另一个页面都可以读取Application信息. 由于Application是共享的,操作之前先Lock,操作完成后UnLock. 在一个页面设置数据: Application.Lock(); Application.Set("address", "上海"); Applica

随机推荐