Intro

A lot of folks like to do ASP pages with VBScript on the server-side, but sometime in 2007 (I think) I switched over to using JavaScript (or JScript or JS) on the server-side. It's cool using JavaScript for both client-side and server-side. There are some places like aspjavascript.com that have info about this. I'm only going to mention a few particulars on this page about ASP JS.

Request and Session variables

One of the most annoying things about Request and Session variables are when you have undefined or empty variables.

Let's say you have page1.asp with a simple form like this:

<form name="form0" method="post" action="page2.asp">
    <input type="text" name="remp" value="" /> remp
    <br />
    <input type="text" name="r777" value="777" /> r777
    <br />
    <input type="text" name="rack" value="ack" /> rack
<%
    Session('semp') = '';
    Session('s777') = '777';
    Session('sack') = 'ack';
%>
    <br />
    <input type="submit" value="Submit"  />
</form>

Let's say that page2.asp simply parses the previous page like this:

<%
Response.Write('Request(\'remp\'): ' + Request('remp') + '.<br />');
Response.Write('Request(\'r777\'): ' + Request('r777') + '.<br />');
Response.Write('Request(\'rack\'): ' + Request('rack') + '.<br />');
Response.Write('Request(\'rund\'): ' + Request('rund') + '.<br />');
Response.Write('Session(\'semp\'): ' + Session('semp') + '.<br />');
Response.Write('Session(\'s777\'): ' + Session('s777') + '.<br />');
Response.Write('Session(\'sack\'): ' + Session('sack') + '.<br />');
Response.Write('Session(\'sund\'): ' + Session('sund') + '.<br />');
Response.Write('<br />');
Response.Write('String(Request(\'remp\')): ' + String(Request('remp')) + '.<br />');
Response.Write('String(Request(\'r777\')): ' + String(Request('r777')) + '.<br />');
Response.Write('String(Request(\'rack\')): ' + String(Request('rack')) + '.<br />');
Response.Write('String(Request(\'rund\')): ' + String(Request('rund')) + '.<br />');
Response.Write('String(Session(\'semp\')): ' + String(Session('semp')) + '.<br />');
Response.Write('String(Session(\'s777\')): ' + String(Session('s777')) + '.<br />');
Response.Write('String(Session(\'sack\')): ' + String(Session('sack')) + '.<br />');
Response.Write('String(Session(\'sund\')): ' + String(Session('sund')) + '.<br />');
Response.Write('<br />');
Response.Write('typeof(Request(\'remp\')): ' + typeof(Request('remp')) + '.<br />');
Response.Write('typeof(Request(\'r777\')): ' + typeof(Request('r777')) + '.<br />');
Response.Write('typeof(Request(\'rack\')): ' + typeof(Request('rack')) + '.<br />');
Response.Write('typeof(Request(\'rund\')): ' + typeof(Request('rund')) + '.<br />');
Response.Write('typeof(Session(\'semp\')): ' + typeof(Session('semp')) + '.<br />');
Response.Write('typeof(Session(\'s777\')): ' + typeof(Session('s777')) + '.<br />');
Response.Write('typeof(Session(\'sack\')): ' + typeof(Session('sack')) + '.<br />');
Response.Write('typeof(Session(\'sund\')): ' + typeof(Session('sund')) + '.<br />');
Response.Write('<br />');
Response.Write('Boolean(Request(\'remp\')): ' + Boolean(Request('remp')) + '.<br />');
Response.Write('Boolean(Request(\'r777\')): ' + Boolean(Request('r777')) + '.<br />');
Response.Write('Boolean(Request(\'rack\')): ' + Boolean(Request('rack')) + '.<br />');
Response.Write('Boolean(Request(\'rund\')): ' + Boolean(Request('rund')) + '.<br />');
Response.Write('Boolean(Session(\'semp\')): ' + Boolean(Session('semp')) + '.<br />');
Response.Write('Boolean(Session(\'s777\')): ' + Boolean(Session('s777')) + '.<br />');
Response.Write('Boolean(Session(\'sack\')): ' + Boolean(Session('sack')) + '.<br />');
Response.Write('Boolean(Session(\'sund\')): ' + Boolean(Session('sund')) + '.<br />');
Response.Write('<br />');
Response.Write('Request(\'remp\').length: ' + Request('remp').length + '.<br />');
Response.Write('Request(\'r777\').length: ' + Request('r777').length + '.<br />');
Response.Write('Request(\'rack\').length: ' + Request('rack').length + '.<br />');
Response.Write('Request(\'rund\').length: ' + Request('rund').length + '.<br />');
Response.Write('Session(\'semp\').length: ' + Session('semp').length + '.<br />');
Response.Write('Session(\'s777\').length: ' + Session('s777').length + '.<br />');
Response.Write('Session(\'sack\').length: ' + Session('sack').length + '.<br />');
Response.Write('Session(\'sund\').length: ' + 'err!' + '.<br />');
Response.Write('<br />');
Session('snul') = null;
Response.Write('typeof(Session(\'snul\')): ' + typeof(Session('snul')) + '.<br />');

%>

The results would look something like this:

Request('remp'): .
Request('r777'): 777.
Request('rack'): ack.
Request('rund'): undefined.
Session('semp'): .
Session('s777'): 777.
Session('sack'): ack.
Session('sund'): undefined.

String(Request('remp')): .
String(Request('r777')): 777.
String(Request('rack')): ack.
String(Request('rund')): undefined.
String(Session('semp')): .
String(Session('s777')): 777.
String(Session('sack')): ack.
String(Session('sund')): undefined.

typeof(Request('remp')): object.
typeof(Request('r777')): object.
typeof(Request('rack')): object.
typeof(Request('rund')): object.
typeof(Session('semp')): string.
typeof(Session('s777')): string.
typeof(Session('sack')): string.
typeof(Session('sund')): undefined.

Boolean(Request('remp')): true.
Boolean(Request('r777')): true.
Boolean(Request('rack')): true.
Boolean(Request('rund')): true.
Boolean(Session('semp')): false.
Boolean(Session('s777')): true.
Boolean(Session('sack')): true.
Boolean(Session('sund')): false.

Boolean(String(Request('remp'))): false.
Boolean(String(Request('r777'))): true.
Boolean(String(Request('rack'))): true.
Boolean(String(Request('rund'))): true.
Boolean(String(Session('semp'))): false.
Boolean(String(Session('s777'))): true.
Boolean(String(Session('sack'))): true.
Boolean(String(Session('sund'))): true.

Request('remp').length: undefined.
Request('r777').length: undefined.
Request('rack').length: undefined.
Request('rund').length: undefined.
Session('semp').length: 0.
Session('s777').length: 3.
Session('sack').length: 3.
Session('sund').length: err!.

typeof(Session('snul')): object.

Note that on the surface, the straight renditions appear very much like when you convert them to strings first. However, a deeper look reveals some puzzling items:

  • typeof(Request variables) is object, regardless of whether the variable was defined.
  • typeof(Session variables) is string, if the variable was defined, otherwise it's undefined.
  • Boolean(Request variables) is true, regardless of the value or whether the variable was defined. (This makes sense since objects always evaluate to true.)
  • Boolean(Session variables) is as if it were done on a string conversion of defined variables, otherwise it's undefined.
  • (Request variables).length is undefined. That is the object does not have a length member.
  • (Session variables).length is as if it were done on a string conversion of defined variables, otherwise it errs!

A Request variable is always typeof object. You cannot tell whether a Request variable is undefined or has a value of 'undefined'. My suggestion: Convert to string and check for 'undefined' and empty string.

A Session variable, if defined, defaults to typeof string (although it could be set to null, etc.). You can tell whether a Session variable is undefined or has a value of 'undefined'. My recommendation: Do the usual boolean check, which will rule out undefined, empty string, 0, etc., but not 'undefined'

Miscellany

Here's an JScript/ASP server-side oddity.

<%
var arr;
arr = new Array(); //This code will err if this line is commented out
arr[3] = 'foo';
Response.Write(arr[3]);
%>


GeorgeHernandez.comSome rights reserved