Lab 1 - Seneca - School of Information & Communications Technology

advertisement
Seneca College of Applied Arts and Technology
INT213 – Active Server Pages
Lab 1
This lab will introduce you to the practical concept and usage of Variables, within your ASP
scripts. In addition we will look at Data Types, joining Strings and outputting your scripts. Your
lecture demonstrated these concepts and now you will use them in some simple scripts.
NOTE: ALL LABS CONTAIN ERRORS IN THE SCRIPTS. THESE ERRORS ARE THERE TO
TEACH YOU TO READ THE SCRIPT AND NOT JUST COPY AND PASTE.
Outputing Information – Response.write
In VBScript the method used to output information is the Response.write object. By default all
output is sent starting at the top-left corner of the screen. Formatting with this command is usually
done in concert with HTML. Lab 2 will deal with this in greater detail.
Here is an example:
Open notepad (or any other editor) and type the script exactly as shown.
Name the file: Myfirstscript.asp
Save the file to: c:\inetpub\wwwroot (This assumes you are using the C: drive.)
<%
Response.write “I am taking INT213”
Response.write ”I am not sure about this programming stuff”
%>
After saving, open your Browser and on the address box type
http://127.0.0.1/Myfirstscript.asp
Questions
1) Do you see the output?________________
2) How is it formatted?_____________________________________________________
______________________________________________________
Let’s change the script so the formatting is different. You can modify the previous script example.
<%
Response.write “I am taking INT213.” & “<br>”
Response.write”I am not sure about this programming stuff.”
%>
Question:
1) How is the output different?____________________________________
The “&” (ampersand) character is used to join strings together and the HTML break tag sends
output to a NEWLINE.
1 of 7
Variables
Before you continue, it will be useful for you to, once again, review the rules for using Variables
with VBScript.
Rules





Variables must begin with a letter, not an underscore or a number
They cannot have more than 255 characters
They cannot contain a period (full stop) , a space or a dash
You can use an underscore, but not as the first character
Case sensitivity is not important. The variable ‘test’ is the same as ‘TEST’
Complete the following.
Circle the variables that you feel are not valid variable names. (refer to the rules)
Testme
3blindmice
john_Doe
john.Doe
blindmice3
INT213
john-Doe
myscript
555_Finch
x
myname
your3name
this is a really big one
_myvar1
The concept of Variables is really quite simple. They are like mailboxes that can hold only one
item at a time. Instead of a physical mailbox, our items are stored in RAM. The great thing about
this is that you don’t have to know what is in the box to use it. Just use the Variable name you
gave to the box (but watch out for errors). Variables also take on the characteristic of the Data
Type that is stored in them.
Try This
1) Open Notepad and type the following
2) Save this as INT213lab1a.asp, to the c:\inetpub\Wwroot folder on your drive.
3) Open Internet Explorer (or Firefox) and type http://127.0.0.1/INT213lab1a.asp
Questions:
Did you get an error? __________________________________
Why did the error occur?_________________________________________________________
__________________________________________________________
Re-write the corrected line here____________________________________________________
Now repair the INT213lab1a.asp file and test the script again, using your Browser
Does the output look correct now?__________________
2 of 7
Let’s try another
Remember that Variables can only hold one value at a time. Let’s test this.
1) Open Notepad and type the following
2) Save this as INT213lab1b.asp, to the c:\inetpub\Wwroot folder on your drive.
3) Open Internet Explorer (or Firefox) and type http://127.0.0.1/lab1b.asp
Because we used the same variable name, the output changed each time we changed the value
going into the Variable. Notice that the Data Types also changed. This means Variables really
don’t care what goes into them. That is why you should name Variables thoughtfully.
Questions
What data type of Variable is “Joe”?________________________________________________
What data type of Variable is the number 10?_________________________________________
What does the character ‘&’ do?_________________________________________________
Where Things Go Wrong
Let’s look at some of the things that can go wrong when using Variables. We will then look at how
to avoid these problems.
1) Misspelled Variable names
One of the most common errors when using Variables is to misspell the Variable name.
Because we cannot see the value stored in RAM we would have to rely on the
Response.write command to output the Variable value.
Create a new file called INT213lab1c.asp.
In your browser enter http://127.0.0.1/Lab1c.asp to execute the script
3 of 7
Questions:
1) What was the output of this script?_____________________________________
2) What should the output be?_______________________________
3) Correct the error and write the complete line here.
__________________________________________________________________
Correct the script file and run it again in your Browser.
This leaves us with a question:
How do we prevent spelling/typing errors
when entering Variable names to our Scripts?
The answer is:
Option Explicit
This command forces us to set-up the variable names we will use in our scripts. This is
best done at the beginning of the script. Let’s look at an example.
Type the following into your editor, Save the file and run it in your Browser as
http://127.0.0.1/lab1d.asp
Save this new file as INT21l3ab1d.asp
<%
Option Explicit
Dim intfirstnum, intsecdondnum, inttsum
‘assign values to the variables
intfirstnum = 10
intsecondnum = 20
‘add the two variables and store the result
inttsum = intfirstnum + intsecondvar
‘output the result
Response.write insttsam
%>
Notice that there two new lines in this script.
i)
ii)
Option Explicit – Forces the declaration of variable names
Dim – Assigns the names to RAM where they will wait for data
Here is what happens
As each variable is processed, the name is matched against the list of names in the
‘Dim’ line. If the names do not match, an error occurs. The script will not continue until
the error is corrected.
4 of 7
Questions:
1) Which variables were entered incorrectly?__________________ _______________
2) What would happen if you removed the Option Explicit from the script?
___________________________________________________________________
____________________________________________________________________
***Correct the variable names and run the script again
2) Understanding Data Types
Care should be taken when working with different Data Types. You may not end up with
the result you were looking for. This section will briefly explore Data Types.
We know there are two basic Data Types, Strings and Numbers. Strings are
represented by having double-quotes (“”) surrounding data and Numbers, well, are
obvious. Yet we can use numbers, for example, in different ways that can create some
problems if you don’t pay attention.
Here is an example. Type the following short script.
Save the script as INT213lab1e.asp to your wwwroot folder.
<%
strfirstnum = “35”
intsecondnum = 40
strthirdnum = “50”
inttotal = strfirstnum * intsecondnum
Response.write inttotal
%>
Questions:
1) What is the output?_________________________________________________
How is that possible, since we multiplied a string and an integer?!
5 of 7
Let’s try another one
Save the script as INT213lab1f.asp to your wwwroot folder.
Execute the script.
<%
strfirstnum = “35”
intsecondnum = 40
strthirdnum = “50”
inttotal = strfirstnum * strthirdnum
Response.write inttotal
%>
Hold it a second! We just multiplied two strings. Is that supposed to happen?
OK, let’s try this one
Save the script as INT213lab1g.asp to your wwwroot folder.
<%
strfirstnum = “35”
intsecondnum = 40
strthirdnum = “50”
inttotal = strfirstnum + strthirnnum
Response.write inttotal
%>
Questions:
1) What was the output of script Lab1f.asp? _______________________________
2) What is the output of Lab1g.asp?_________________________________
3) Why do you think the math operation works in one case but not the other?
_________________________________________________________________
_________________________________________________________________
It seems that Variables are able to use values based on what they are doing in a script.
The addition (“+”) sign seems work differently depending on whether two strings or two
numbers are used.
Multiplication (“*”), on the other hand assumes that the values on both sides have to be
numbers and treats them as if they are numbers.
Question:
1) Write an example of a number, in String format, that multiplication will not work with.
__________________________________
6 of 7
Here is a little exercise that tests your knowledge of strings
Sort each of the following lists (You don’t have to write the double-quotes)
List 1- 3, 5, 2, 11, 4, 1
Sorted_________________________________
List 2- “3”, “5”, “2”, “11”, “4”, “1”
Sorted_________________________________
*** Important ***
1) Enter all scripts and complete all questions and exercises. Have your Lab Monitor sign
and date this lab. It is your responsibility to make sure your lab gets checked off on the
marking sheet.
2) You have one week from the date this lab is handed out to complete the lab. There will
be no extensions.
3) If you do not see your mark for this lab on Blackboard, you must show me the signed and
dated lab in order to receive the mark.
Lab Monitor____________________________________
Date_______________
LAB 2 will look at the Response and Request Objects
7 of 7
Download