SoFunction
Updated on 2025-03-02

JavaScript asp tutorial Lesson 8 -- Request object

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 ("Key[Field]")
Client security info for SSL encryption
Cookies ("cookieName")
Holds cookie value stored on the client
Form ("formName")
Holds value sent via HTML Form
QueryString ("keyName")
Name/Value pair appended to the end of the URL
ServerVariables ("variableName")
Hold values sent in the HTTP Headers

ClientCertificate:

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

Cookies:

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

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="" 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 in a new window. It posts information to which is found below. In turn, posts information to which is also found below.

<%@LANGUAGE="JavaScript"%>
<%
var WebPageVariable = new String( ("WebPageVariable") )
WebPageVariable = ();

var monthlySalary = new String( ("monthlySalary") )
monthlySalary = ();
%>
<HTML>
The Web Page Variable you typed is: <%=WebPageVariable%> <BR>
The monthly salary you listed is: <%=monthlySalary%> <BR>
<FORM ACTION="" 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 when we "Post" an HTML form to the server. Notice that the NAME attribute in the HTML form corresponds to the "name" in ("name"). To be more specific, <INPUT TYPE="Text" NAME="WebPageVariable"> corresponds with ("WebPageVariable"). We already talked about the need for the new String( ) constructor back in Lesson 03.

QueryString:

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

<%@LANGUAGE="JavaScript"%>
<%
var QueryVariable = new String( ("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="?QueryVariable=<%=QueryVariable%>">Click Here</A> 
for the link to <I>?QueryVariable=<%=QueryVariable%></I>
<%
	} //closing bracket for if statement.
%>
</HTML>

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

You can use 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 from top to bottom.

var QueryVariable = new String( ("QueryVariable") )

The line above in corresponds to the line below from

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

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

About half way into 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 () 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 (). 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 to tell you the number.

The Request Shortcut:

() and () share a shortcut. ("WebPageVariable") can be abbreviated as Request("WebPageVariable") and ("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><%=("ALL_RAW")%></TD></TR>
<TR><TD>REMOTE_ADDR</TD>
<TD><%=("REMOTE_ADDR")%></TD></TR>
<TR><TD>HTTP_USER_AGENT</TD>
<TD><%=("HTTP_USER_AGENT")%></TD></TR>
<TR><TD>URL</TD>
<TD><%=("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:

() is the lone method and TotalBytes is the lone property. () 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 ().