欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-20 20:44:34
request object: request has five (5) collections, one (1) property, and one (1) meth...

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