2. Agenda
• ASP
• Basic Control Flow
– If Then Else
– Select Case
– For Loop
– Do While Loop
• Form Processing
• Server Side Includes
3. ASP
• Active Server Page
• The default scripting language is
VBScript, but you can use JScript also.
• File extension: .asp
4. ASP
• Syntax:
– to execute some code <% code %>
– to print a value <%= variable/expression
%>
• Variable
• if then else, for, while
• function / procedure
5. Variable
<%
option explicit
%>
<html>
<head><title>hello</title></head>
<body>
<%
dim name
name = "Peter"
dim n
n = 18
%>
<p>Hello <%= name %>!<br>
Your age is <%= n %>
</p>
</body>
</html>
‘option explicit’
enforces variable
declaration.
‘option explicit’
enforces variable
declaration.
By default, you don’t need to
declare variables. But for easier
debugging, declare every
variables. Variables can be string,
integer and some other type.
By default, you don’t need to
declare variables. But for easier
debugging, declare every
variables. Variables can be string,
integer and some other type.
6. ASP using JScript
<%@ Language=Jscript %>
<html>
<head><title>hello</title></head>
<body>
<%
var name = "Peter";
var n = 18;
%>
<p>Hello <%= name %>!<br>
Your age is <%= n %>
</p>
</body>
</html>
7. ASP-Generated HTML
<html>
<head><title>hello</title></head>
<body>
<p>Hello Peter!<br>
Your age is 18
</p>
</body>
</html>
This is what the browser receives.
The browser doesn’t care
whether this page is generated by
ASP or not. And, it cannot see the
ASP source code.
This is what the browser receives.
The browser doesn’t care
whether this page is generated by
ASP or not. And, it cannot see the
ASP source code.
8. If Then Else
<% if strName = “Peter” then %>
How are you Peter!
<% else if strName = “Mary” then %>
Dear Mary!
<% else %>
How do you do! <%= strName %>
<% end if %>
9. Select Case
<%
select case strName
case “Peter”
Response.Write(“…”)
case “Mary”
Response.Write(“…”)
case else
Response.Write(“…”)
end select
%>
10. For Loop
<table border=“1”>
<% for i = 1 to 7 %>
<tr>
<td><%=i%></td>
<td><font size=“<%=i%>”>hello</font>
</td>
</tr>
<% next %>
</table>
11. Do While Loop
<%
dim sum, I
i = 1
sum = 0
do while i < 8
sum = sum + I
i = i + 1
if sum > 10 then exit do
loop
%>
Exit the do loop
immediately
Exit the do loop
immediately
12. Array
<%
dim i
dim A()
redim A(9)
for i = 0 to 9
A(i) = i*i
next
redim preserve A(20)
redim A(15)
dim B(10,10)
%>
Array in VBScript is zero-based,
i.e. it starts at 0.
Dim A(n) wil give an array of size
n+1.
Array in VBScript is zero-based,
i.e. it starts at 0.
Dim A(n) wil give an array of size
n+1.
Resize an array (and
preserve the data)
Resize an array (and
preserve the data)
2D array2D array
13. Form Processing
• Use tag <form> … </form>
• FORM allows the user to submit data to web
server for processing
• A FORM contains one or more input fields
• After filling the FORM, the user can submit
the data usually by pressing a submit button
14. Form Sample
<body>
<p>Please fill in the form:</p>
<form action=“process.asp”>
<!-- items goes here -->
<input type=“submit”
name=“btnSubmit”>
</form>
</body>
URL of an ASP
page to process
the submitted
data
URL of an ASP
page to process
the submitted
data
16. Post Method
<body>
<p>Please fill in the form:</p>
<form action=“process.asp” method=“post”>
<!-- items goes here -->
<input type=“submit” name=“btnSubmit”>
</form>
</body>
The default method of form is “get”.
The data in the form are encoded and
transmitted in the body of the HTTP
request. They are invisible to the user.
The default method of form is “get”.
The data in the form are encoded and
transmitted in the body of the HTTP
request. They are invisible to the user.
If we use method “GET”, the form data are attached to
the URL submitted to the server. e.g.
http://www.test.com/process.asp?name=tom&id=123
If we use method “GET”, the form data are attached to
the URL submitted to the server. e.g.
http://www.test.com/process.asp?name=tom&id=123
17. GET vs. POST
• Encoded form data are attached to the end
of the URL in GET method
– visible to the end user
– can be saved as bookmark
– we can use such URL to store pre-filled request
• Encoded form data are attached as the
request body in POST method
– necessary for large amount of form data, e.g.
file upload
18. Server Side Includes
• <!--#Include file=“header.asp”-->
• <!--#Include virtual=“header.asp”-->
19. Built-in ASP Objects (1/2)
• Application
– Share information among all users of an
application
• Request
– Get information passed in HTTP request, e.g.
user input in FORM and file upload
• Response
– Send information to browser, set cookies, and
redirect to another URL
20. Built-in ASP Objects (2/2)
• Server
– Provides access to methods and
properties on the server
• Session
– Stores information in a session
• ObjectContext
– Transaction management
21. Request Object
• We use these two collections to
retrieve data submitted in a form
– Request.QueryString - query string in URL,
sent by the GET method
– Request.Form - form elements in the
request body, sent by the POST method
• Request.Cookies is a collection of
cookies sent in the HTTP request
22. Response Object
• Send output to the browser
• Response.Write “Hello”
• Response.Buffer = true - enable
buffering of the response output
• Response.Flush - flush the buffered
input immediately
23. Response Object
• Response.Redirect URL - redirect the
browser to another URL
• Response.Expires = n - expires in n
minutes
• Response.End - stop processing of the
ASP file and returns
24. ASP Session
• Originally, HTTP is stateless
– the browser and the server do not
remember information between web
pages
– difficult to write nontrivial web
application
• Cookies in browser and session
variables in ASP provide a solution
25. Typical usage of session
• The session starts when a user logs in.
• The session stores the user’s login
information: user name, preferences,
etc and intermediate data in a
transaction (e.g shopping cart)
• The session ends when the user logs
out.