Title: DT221/3 Internet Application Development
1DT221/3 Internet Application Development
Active Server Pages IIS Web server
2Readability of ASP pages
- Always always always ensure that code is
- INDENTED COMMENTED ID BLOCK
- Indented - to reflect level of the code
lthtmlgt - ltheadgtajsdlkfjads etc
- 2) Well commented. Comments in ASP appear as
the next subroutine will appear as followed
by a function which will calculate.. - Comments in HTML appear as lt!--- this is a
commentb --gt - HTML comments will be visible by view source in
browser, ASP comments wont.
3Readability of ASP pages
3) Titled At the top of each ASP page, should
have an ID block explaining who write the script,
date, script name, description and any revisions.
Can use either ASP or HTML comments (depending
on whether clients should be able to see the ID
block)
lt_at_ Language VBScript gt ltOption Explicit
gt lt '
' Script name conversion.asp
' Author Jimmy Saville
Date July 7th 2003
Desciption whatever it does..
Revisions
August 8th 2003 added subroutine
'
gt etc
4ASP Syntax
- ASP files for web output usually contain HTML
tags. - Any scripts intended for execution by the server
must be between lt and gt to distinguish them
from HTML - The ASP engine looks specifically for code
between - lt and gt - this code will then be parsed by
the ASP engine. The remainder will be ignored
5ASP Scripting languages
- The default language for code is VBScript,
although other languages can be used e.g.
JavaScript, Jscript. - To set the language , use a language
specification at the first line of the page using
the _at_Language declarer - lt_at_ language Jscript gt
- lthtmlgt etc etc
- You dont have to set the language for VBSCript
as its the default but its good practice to
include it. - All examples here are in VBScript
6ASP- simple example
lt_at_ LANGUAGE VBScriptgt lt Option Explicit
gt lthtmlgtltbodygtltdim namename"John
Wayne"response.write("My name is "
name)gtlt/bodygtlt/htmlgt
- Using View Source at the browser will show HTML
only. - Dim used to create a variable name
- Write methods of Response object used to output
- Option Explicit enforces all variables to be
declared. Good practice
7ASP Declaring variables
- To declare a variable use Dim e.g Dim Actors
- Variables are of type variant (You dont have to
explicitly state string, integer etc.) - To declare an array add the array size on. e.g.
Dim Actors(3) - First element of the array has an index 0 e.g.
Actors(0) Tom - Function Ubound determines the upper index of the
arraye.g. Ubount(Actors) has a value of 4
8ASP Declaring variables - example
lt_at_ LANGUAGEVBSCRIPT gt lt 'First we will
declare a few variables. Dim SomeText Dim
SomeNum 'Next we will assign a value to
these. SomeText "ASP is great!" SomeNum
1 'Note that SomeText is a string and SomeNum
is numeric. 'Next we will display these to the
user. Response.Write(SomeText) Response.Write
(SomeNum) 'You may also add (append) HTML code
to these. Response.Write ("Right now you are
thinking that " SomeText)gt
9ASP- Array example
lt_at_ LANGUAGE VBScript gt lt Option Explicit
gt lthtmlgt . ltbodygt A list of the best actors
in the filmgtltbrgt lt Dim Actors(3), i
Actors(0) Tom Cruise Actors(1) Cillian
Murphy Action(2) Julia Roberts For I
0 to Ubound(Actors) OutStr OutStr
Actors(I) ltbrgt Next
Response.Write(OutStr) gt lt/bodygt etc
Do not have to include the Counter variable as
in VB.
10ASP Object Model
- ASP has seven built-in objects. Each of these
objects has a set of properties , methods and
collections that can be used to enhance ASP
pages - Request (Has a form collection to hold data
entered into a form) - Response (Has a write method to output to web
page) - Session
- Server
- Application
- ObjectContext
- ASPError(new)Detailed info at
http//www.asp101.com/resources/aspobject.asp
Data structures where number of elements is
variable (unlike arrays)
11ASP Object Model - Request
- Request - supplies information regarding the
request the user sends to the Web application - Request objects includes the following
collections - Form (values from the form elements sent by the
browser).Any ltinputgt elements in a HTML ltformgt
are available to to ASP using Request.Form
(elementname) IF the post request method has
been used - QueryString values of the variables in a HTTP
query string, which is data sent to the server in
the URL I.e. www.blahblah.com?id2347name
jim QueryString is populated if the get
method is used -
12ASP Object Model Request HTML Form that
collects 3 pieces of info
lthtmlgt .. header etc ltbodygt ltfont color
blue size 4 face arialgt ltform method
"post" Action "processForm.asp"gt Your name
ltinput type text name txtNamegt ltbrgt
Your favourite colour ltinput type text name
txtColourgt ltbrgt Your favourite film ltinput
type text name txtFilmgt ltbrgt ltinput
type "submit" value "Click to submit
information" lt/formgt lt/fontgt lt/bodygt lt/htmlgt etc
13ASP Object Model Request output form content
onto a web page
lthtml etc ltbodygt ltfont color blue size
4 The data entered into the form was ltbrgt
Name ltresponse.write(Request.Form("txtName"))gt
ltbrgt Favourite colour ltRequest.Form("tx
tColour")gt ltbrgt Favourite Film
ltRequest.Form("txtFilm")gt ltbrgt
lt/fontgt lt/bodygt lt/htmlgt etc
The Form collection of the Request object holds
all the request parameters
Note can use a short cut to output onto the web
page from a script. Instead of Response.write
can just use lt gt . Both ways shown here.
14ASP Subroutines and functions
- Use subroutines and functions in ASP pages in
order to economise on code.. I.e to enable
modules of code to be used and reused - Subroutines define a series of steps that
accomplishes some task, but does not usually
return a value - Functions accomplish a series of steps And
usually return a single value
15ASP Subroutines - example
lthtmlgt ltheadgt lt sub calculate(num1,num2)
response.write(num1num2) end sub gt lt/headgt ltbody
gt The product of the two numbers entered
is ltpgt lt dim x dim y x
request.form("firstNum") y
request.form("secondNum")gt Result ltcall
calculate(x,y)gt lt/pgt lt/bodygt etc
Subroutine always starts with Sub, ends with End
Sub
use Call
Arguments passed to subroutine (num1, num2)
16ASP Function example
lthtmlgt etc ltheadgt lt Function retailPrice(price)
dim tax taxprice.09 retailPrice
pricetax end Function gt lt/headgt etc.. ltbodygt
etc lt dim y y retailPrice(request.form("net
price")) Response.write(The retail price
includig tax " y) gt etc lt/bodygt lt/htmlgt
Function always startswith Function and ends
with End function Make sure that output is
explicitly named in the function
Retrieves a netprice and calculates the retail
price, using the function
17ASP- reusable code
- Very common in a web application to need to
include common code such as headers/footers/naviga
tion bars, subroutines etc across more than one
Web page. - ASP allows the use of a Server Side Include
directive called INCLUDE, which includes the
contents of another file into the current file.
Its the only SSI directive allowed in ASP (e.g.
echo, exec etc are not allowed) - Must surround the include SSI directive as a
HTML comment - Syntax
- lt! include attribute value - -gt
18ASP- reusable code - example
- lt!-- include file include\header.html --gt
- Will include the contents of header.html into the
ASP page. It assumes header.html is in
subdirectory include, relative to to the current
directory. Note good practice to place all
include code into a single directory.. called
include
- lt!-- include virtual /testdir/header.html
--gt
- Will include the contents of header.html into the
ASP page. It assumes header.html is the files
virtual path relative to the server root
directory. Its not concernted with current
directory.
19Comparing ASP versus JSP
- Similarities
- Both are scripting based technologies
- Both enable dynamic web pages
- Both help to separate the page design from the
programming logic - Both provide an alternative to CGI Scripts,
enabling easier and fast page development and
deployment - Compilation ASP was originally interpreted but
ASP.netuses compiled code, similar to JSP
20Using ASP versus JSP ?
- For a development project, how to choose whether
ASP versus JSP may be used? (assuming PHP and
others have been excluded!) - What technology is used already? ASP is usually
used with Microsofts IIS, which requires Windows
operating system -? tied into Microsoft
technology. Whereas JSP is platform independent
-? check whether the company is tied into any
particular vendor products already that will
guide decisiongt - ASP JSP
- Web Server Microsoft IIS Any web
server, include apache, Netscape IIS - Platforms Microsoft Windows Most popular platforms
21Using ASP versus JSP ?
- Development skills - JSP support Javascript,
Java code and tags (custom and JSLT). ASP
supports both Vbscript and JavaScript. Need to
check which skills are already available in the
development team - Simplicity? ASP was traditionally regarded as
simpler to develop (VBScript) than JSP (using
Java code). However, the growth in use of
re-usable tag based actions for JSP is changing
this. If the company is open to using JSP tags,
both are relatively simple. - Re-usability across platform JSP will be
portable across platforms, ASP generally is not.
22Using ASP versus JSP ?
- Debugging/error resolution In JSP, the code is
converted into Java servlets in the background
any runtime errors refer to the servlet code
which can make it difficult to find the error.
ASP is not translated so any line references
are within the ASP page itself. - Code re-use - Both allow common code to be taken
out and re-used. JSP, with its use of custom and
JSLT tags, is probably superior on this. Use of
tags also enables easy global changes without
individual page change. - Performance JSP traditionally considered faster
as code is precompiled into servlets. However,
now that ASP is part of ASP.Net which is also
compiled ? comparable on performance.
23Using ASP versus JSP ?
- Open source JSP (and other java based
technologies from Sun) are developed with input
from the Java community. ASP is proprietary.
Cost/quality/vendor support implications.