Seneca College of Applied Arts and Technology

advertisement
INT213 – Lab 2
(Revised 3/7/2016)
This lab will focus on the ‘Request’ and ‘Response’ Objects. In your lecture regarding these topics, you saw how
these two objects interacted with the server and the browser. The ‘Request’ gets information from the client and the
‘Response’ object returns information from the server.
Request Object
In a nutshell, the ‘Request’ object is used to retrieve data from the client. You have all used this object when you log
into a web site, such as SIRIS. The data entered into TextBoxes, for example, is sent to the server where the server
script processes the data.
Examples of the ‘Request’ objects collections are; Form collections and ServerVariables collection.
VBScript already has some pre-defined Variables that are used by the Request Object. Let’s look at Server
Variables
ServerVariables - Examples
When a browser sends data from a web page to a server, it also sends information about the client in the HTTP
headers. Information such as the type of Browser, client IP address, etc. are passed to the server.
For example, type the following into Notepad++ (save as INT213lab2a.asp) as usual and run the script.
<%
Response.Write “ The server name is
“ & request.servervariables(“SERVER_NAME”) & “<BR>”
Response.Write “ The server software is “ & request.servervariables(“SERVER_SOFTWARE”) & “<BR>”
Response.Write “ The server address is “ & request.servervariables(“LOCAL_ADDR”) & “<BR>”
Response.Write “ The client address is “ & request.servervariables(“REMOTE_ADDR”) & “<BR>”
Response.Write “ The file location is
“ & request.servervariables(“PATH_TRANSLATED”) & “<BR>”
Response.Write “ The server protocol is “ & request.servervariables(“SERVER_PROTOCOL”) & “<BR>”
%>
What is your Server Name?________________________________________________________
What Server Software is being used?_________________________________________________
What is the client IP address?_______________________________________________________
What server protocol is being used?__________________________________________________
As you can see the information requested (and also sent to the server) can be useful if you need to find out
something about the client. This information can be stored in variables and used for various reporting and
programming purposes.
Look at the above script again (which you saved as Lab2a.asp). Do you notice anything unusual about it? There is
almost no HTML included in the ASP page. Everything being requested and processed by the server is in the
script. VBScript will allow you to output information on the screen, without HTML, as long as you do not require any
special formatting. In fact, you can write script that will contain no HTML at all. It is unusual to do so, but possible.
Another interesting fact about Server-side script can be seen in the Source of the web page in you Browser. This
can be found in the View menu in Internet Explorer.
**** Do This****
Run INT213lab2a.asp again.
In your Browser click on the ‘View’ menu item and open the ‘Source’
Questions:
What do you see:
_________________________________________________________________________________________
Is there any script displayed in this page?__________________
Request (Using Forms)
Interaction with the user of a web page is essential in making the page interactive. The REQUEST object allows the
user to enter some form of input which the server then processes. Text Boxes, Check Boxes, Buttons are examples
of objects that allow a user to interact with a web page. We talked about HTML FORMS during the lecture, and their
importance to the Request Object. You will see a lot of this in the coming weeks.
The next example allows a user to enter their username and password into the text boxes and then displays a
message.
Create an INTlab2b.asp file and save it to your WWroot directory
<%
Option Explicit
'Set up the variables which will hold the data
Dim strUname, strPasswd
‘Ask the Form to give you the data values from the Text Boxes
strUname = Request.Form("txtName")
strPasswd = Request.Form("txtPasswd")
‘Write the values to the screen
Response.write "Your User Name is: " & strUname & "<BR>"
Response.write "Your Password is : " & strPasswd & "<BR>"
%>
<HTML><HEAD><TITLE>Student Login Form</TITLE></HEAD>
<BODY BGCOLOR = “#CCFFCC”>
<FORM METHOD=POST ACTION=”INT213lab2b.asp”>
Enter Your Username:
<INPUT TYPE=TEXT NAME=txtName><BR><BR>
Enter your password:
<INPUT TYPE=TEXT Name=txtPasswd><BR>
<P>
<INPUT TYPE=SUBMIT VALUE=”Click to Login”>
</FORM>
</BODY>
</HTML>
Questions:
How many text boxes are seen?________________
2 of 6
What are the textbox Variable names?__________________
_______________________
When you execute the script, have the values you entered been output to the screen?_________
In your previous lab you entered (hard-coded) variable values directly into the script. Now you are able to have the
user enter data themselves. The HTML line <INPUT TYPE=TEXT NAME=txtName> is assigned the value
entered into the textbox. The ‘name’ of the box becomes a Variable name. The relationship between the textbox
‘name’ and the script is as follows
<INPUT TYPE=TEXT
NAME=txtName>
strUname = Request.Form("txtName")
The data in the textbox
variable is read and
sent to the server when
the ‘Submit’ button is
pressed
The server Requests
the data and uses the
textbox variable and
assigns it to another
variable we setup
Response Object
The ‘Response’ object deals with data the server sends to the calling Browser. All requests for information, sent by
the Client, are processed by the server and returned to the Browser in the form of HTML strings. In addition, the
Response object deals with requests from the calling script to alter the manner in which scripts are processed by
the server. So, in effect, the ‘Response’ object can have a great deal of control over a server.
Response.Write
The ‘Response’ object has a wide range of properties and methods. You have been using one of these in this lab
and Lab1. That is the “Response.write’ method. You should be familiar with its use by now. Let’s find out.
**** DO This ****
I am an expert in VBScript.
1) In Notepad++ create the server-side script that will output the above line using the write method of the
Response object. You should be able to write it in the standard method and also the “short-form”
(imbedded) method. We looked at this in the lecture. Check your notes (remember I asked you to keep
notes!!!)
2) Write each script line below and test it using your browser
Long method:_________________________________________________________________________________
Short method:________________________________________________________________________________
The Response.write method is used more often than any other Response object. ASP would be useless without it.
In your previous labs you saw the Response.write method used to output simple string data along with the <BR>
HTML command. Response.write can be used in different ways.
3 of 6
Response.write (mixed with HTML)
Embedding script in HTML allows us to format the output of a web page. The ASP Script Tags (<% %>) can be
used anywhere in a web page. Therefore, we can mix Script and HTML at will. This allows us to place script in
Forms, Buttons, Tables etc. You will see this quite often in this course and your assignment will use it extensively.
Here is a small example of mixing HTML and script.
Create INT213lab2c.asp
<% Option Explicit%>
<HTML>
<BODY>
This is a test of HTML and ASP script
<BR>
<%
Dim myname
myname = “Mickey”
%>
‘The following line uses the short form of the Response.write command.
‘You could also write the line as - My name is <% Response.write myname %> <BR>
My name is <%= myname %> <BR>
<%
Response.write “What is your name?”
%>
</BODY>
</HTML>
Questions:
Refer to the script above.
1) Looking at the <BR> tag, how is it used differently than in our previous scripts?
_______________________________________________________________________________________
2) What script character (that joins strings together) is missing? ________________
**** Do This ****
Write a single line script (including script tags, no variables) that outputs the following:
Your script must use the short-form version of the Response.Write command.
My name is Wilbur
____________________________________________________________________________________________
4 of 6
Response.end
There are times, especially when a Loop is involved, when you need to stop your script during execution.
‘Response.end’ can perform this function.
Once the script encountered the Response.end command, the script stopped all processing. Be careful where you
place the Response.end command and extensively test your script to make sure the placement of Response.end
applies to the situation.
Here is a simple example
Create File: INT213lab2d.asp
<%
Response.write “My ”
Response.write “Name “
‘Response.end
Response.write “Is “
Response.write “Barney Rubble”
%>
Run this script.
1) What is the output?__________________________________________________________
Next, remove the single-quote from the Response.end line
Run the script again
2) What is the output now?______________________________________________________
*** Do This ***
1) Edit the INT213lab2b.asp script. Add a Response.end command between the following HTML lines
<INPUT TYPE=TEXT
NAME=txtName><BR><BR>
Enter your password:
Put your Response.end
line between these lines
3) Save the script as INT213lab2e.asp and run the script.
Question:
What happened?
______________________________________________________________________________________
5 of 6
Response.redirect
The Response.redirct command is a relatively simple but useful method in ASP. Basically the method takes a user
to the page URL. It is the client system that makes the request for the page redirection and the server responds by
going to the new page.
In HTML the redirection command is:
<META HTTP-EQUIV=REFRESH CONTENT=”http://www.microsoft.com”>
VBScript offers an alternative to this method and because of Conditional statements, allows for a greater degree of
control over the URL redirection.
Example 1
a) create page2.asp
<HTML.>
<BODY>
This is page two which was called using Response.redirect in Lab2f.asp
</BODY>
</HTML>
b) create Lab2f.asp
<% Response.redirect “page2.asp” %>
Save these to your hard drive and test lab2f.asp in your Browser
What page is displayed?__________________________
The Lab2f.asp page, when loaded, immediately displayed page2.asp because the only instruction in
INT213lab2f.asp page was the redirection method of Response.redirect.
Lab Supervisor_________________________________________
Date______________________________
Next Lab will be focused on Conditionals
If…Then…Else…End if
If…Then…Elseif…Else…End if
6 of 6
Download