Week 12

Links for Week 12

ASP

Introduction

Servlets are one way to create a server side program to handle page generation and file handling. Active Server Pages (ASP) is alternative server side technology created by Microsoft. ASP pages will run on Microsoft servers and are relatively easy to write since they are incorporated directly into html documents. A page can link directly to an asp page and that page will appear to be a standard html page, even when you look at its source code. This allows the programmer to open files or establish contact with a database without letting the user see the file path or passwords. There are even more secure methods for storing passwords and file paths that cannot be cracked by hackers.

When a server handles a request for an ASP file (one with an asp suffix), it will first execute the asp to generate the web page then send the page. This means you may customize the web page on the server before the page is tranmitted to the client. Each request for an ASP page results in a new thread being generated, so there may be more than one version of the page generated at the same time and running concurrently. This is a big improvement over CGI pages.

Actual coding of ASP pages can be done in either JavaScript or VBScript. There are advantages to both script types. If you create your ASP pages using JavaScript, the syntax is the same for client side and server side scripts. This makes it easy to switch from one type of script to the other. The problem you have is that many of the functions that are used in client side scripts are replaced with functions that come from Visual Basic. This can cause confusion when you start.

On the other hand if you have a complicated script, you might find VBScript easier to use since you can write the program first in Visual Basic, debug it and the convert it to VBScript. The problem with this is that not all of the Visual Basic functions transfer directly to VBScript. One inconsistency that you should be aware of is that if you use + for concatenation, VBScript assumes this is numerical addition and causes lots of problems. You must use & for concatenation. Another problem is that not all of the Virtual Basic functions can be used in VBScript.

When you create an ASP page that will use Java script, you must place the following code at the very beginning of your HTML document. The first line indicates that the language used in this file is JavaScript and the second indicates that the entire page will be created before it is transfered.

<%@ Language = JavaScript%>
<% Response.Buffer = true%>

VBScript ASP pages start with a very similar set of lines. The main difference is that you want the Option Explicit line to follow the language specification line. Option Explicit indicates that variables must be defined before they are used. The definition of a variable only specifies a name, since VBScripts do not have types. The main thing that Option Explicit does for you is check to see if your variable names are misspelled.

<%@ Language = VBScript%>
<% Option Explicit%>
<% Response.Buffer = true%>

One of the applications of ASP is processing data submitted in a form. The example you can see by clicking here will take a name and create a new page with that name inserted in it.

Writing ASP

Creating Variables

Server-side JavaScript variables are created the same way as the client side variables. Simply write var followed by the variable name. VBScript variables are created in much the same way as Visual Basic variables, except no type is specified. So an integer variable would be declared as follows

var intMyVariable

You can create and initialize your variables anywhere in your script, but I would recommend putting all of your functions either in the head section of you file or in an external file. Keep as much of the code as possible in the same tag. I prefer initializing all of the variables at one time at the beginning of the body and then just referencing them when needed in the rest of the body.

Actual Code Syntax

When you create an ASP page, you can intersperse HTML code with your ASP code. The difference is that ASP code always starts with the tag <% and ends with the tag %>. Between these two tags you can only have ASP code. It is often better not to intersperce code but to generate all of the code in ASP. Consider the following small piece of an ASP document:

<B>Name</B> <%Response.Write(strName)%> 

It would be cleaner code to just use the following:

<%Response.Write("<B>Name</B>" & strName)%>

A Simple ASP Example

One of the uses of ASP is to process information entered on a client side form. A very simple form like this makes up the entire page Register.htm. This form will submit any name that contains the string "rad". Brad for example. The message is posted to the ASP page Register_Me.asp. The source code for the form follows.

<FORM NAME = "form1"ACTION="register_me.asp" METHOD= "POST"><BR>
Enter Name with rad in it
First Name <INPUT TYPE=TEXT NAME=txtFirstName > <INPUT TYPE=SUBMIT VALUE="Submit"> <INPUT TYPE=RESET VALUE="Reset" ><!-- <INPUT TYPE=BUTTON NAME=Substring VALUE="Substring" onClick="print()" > --></FORM>

The ASP file register_me.asp that handles this Post call is given below. Notice that to retrieve information from the form, ASP has an function called Request that retrieves a string that was entered in the form. The arguement of this function is just the Name of the item that you want to return. In this case txtFirstName. In this case the result of this request is stored in a variable strInComming. while there is no variable type, the str prefix (from Visual Basic) makes it easier to remember the type you are storing in a variable.

In the next line is a bit of nonsense, but it illustrates the use of some Visual Basic functions that can be used in a VBScript. The Mid function gets a substring of the first arguement that starts at the second arguement and has length specified by the third arguement. InStr tests if the string "rad" is a substring of strInComming anywhere after position 1. If it is the position of the first letter is returned, otherwise -1 is returned. Finally, the Response object is the new page you are generating. In this case the strSubStr is placed in the page you are generating at the possition of the call to the Write function.

<%@ Language = VBScript%>
<% Response.Buffer = true%>

<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Red Edit  version 2.7.1">
<TITLE>Created by Red Edit</TITLE>
</HEAD>
<BODY> 

<P>The information you entered is</P> 
First Name: <% 
dim strInComming, strSubStr, intRadPos 
strInComming = Request("txtFirstName")
intRadPos=InStr(1,strInComming,"rad")
if intRadPos>0 then
	strSubStr =Mid(strInComming,intRadPos,5) 
else
	strSubStr="rad not found"
end if
Response.Write(strSubStr)

%>


</BODY>
</HTML>

VBScript vs JavaScript (JScript)

While JavaScript is the standard language used to create client side programs, VBScript has an advantage on the server side. While all the web related functions are nearly identical for JavaScript and VBScript, other functions such as string handling are not completely implemented (as far as I can tell) for JavaScript. This means if you want to any string parsing, you are out of luck in JavaScript. In addition VBScript has an advantage because there are lots of Visual Basic programmers so the are lots of VBScripts available as freeware. Finally, you can always test your VBScript as a Visual Basic program so can take advantage of the Visual Basic debugging tools. The only problem is that not all the syntax transfers directly. This is a minor price to pay for the debugger.

Case Study

Earlier you were introduced to a pair of case studies in which JavaScript was used to verify form information, and set date information in comboboxes. The asp for both server side pages was created in VBScript since each required some string handling. For a discussion of the vacancy form handler click here .