dt228/3 web development active server pages & iis web server

23
DT228/3 Web Development Active Server Pages & IIS Web server

Post on 15-Jan-2016

238 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: DT228/3 Web Development Active Server Pages & IIS Web server

DT228/3 Web Development

Active Server Pages& IIS Web server

Page 2: DT228/3 Web Development Active Server Pages & IIS Web server

Readability of ASP pagesAlways always always ensure that code is:

INDENTED COMMENTED ID BLOCK

1) Indented - to reflect level of the code

<html> <head>ajsdlkfjads

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 <!--- this is a commentb -->

HTML comments will be visible by view source in browser, ASP comments won’t.

Page 3: DT228/3 Web Development Active Server Pages & IIS Web server

Readability 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)

<%@ Language = VBScript %><%Option Explicit %><% ' ********************************************* ' *** Script name: conversion.asp *** ' *** Author: Jimmy Saville *** ‘ *** Date: July 7th 2003 *** ‘ *** Desciption: whatever it does.. *** ‘ *** Revisions: *** ‘ *** August 8th 2003: added subroutine *** '************************************************%> etc

Page 4: DT228/3 Web Development Active Server Pages & IIS Web server

ASP Syntax

• ASP files for web output usually contain HTML tags.

• Any scripts intended for execution by the server must be between <% and %> to distinguish them from HTML

• The ASP engine looks specifically for code between <% and %> - this code will then be parsed by the ASP engine. The remainder will be ignored

Page 5: DT228/3 Web Development Active Server Pages & IIS Web server

•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 @Language declarer:<%@ language = “Jscript” %><html> etc etc

•You don’t have to set the language for VBSCript as it’s the default – but it’s good practice to include it.

•All examples here are in VBScript

ASP Scripting languages

Page 6: DT228/3 Web Development Active Server Pages & IIS Web server

ASP- simple example<%LANGUAGE = “VBScript”%><% Option Explicit %><html><body><%dim namename=“John Wayne"response.write("My name is: " & name)%></body></html>

• 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

Page 7: DT228/3 Web Development Active Server Pages & IIS Web server

ASP – Declaring variables

• To declare a variable – use Dim e.g Dim Actors

• Variables are of type variant (You don’t 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

Page 8: DT228/3 Web Development Active Server Pages & IIS Web server

<@ LANGUAGE="VBSCRIPT" %> <%   '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)%>

ASP – Declaring variables - example

Page 9: DT228/3 Web Development Active Server Pages & IIS Web server

ASP- Array example<% LANGUAGE = VBScript %><% Option Explicit %>

<html>….<body> A list of the best actors in the film><br> <% 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) + “<br>” Next Response.Write(OutStr) %></body>etc

Do not have to include the Counter variable

as in VB.

Page 10: DT228/3 Web Development Active Server Pages & IIS Web server

ASP 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:

1. Request (Has a “form” collection to hold data entered into a form)

2. Response (Has a “write method” to output to web page)

3. Session4. Server5. Application6. ObjectContext7. ASPError(new)

Detailed info at : http://www.asp101.com/resources/aspobject.asp

Data structures where number of elements is variable (unlike arrays)

Page 11: DT228/3 Web Development Active Server Pages & IIS Web server

ASP Object Model - Request

1. 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 <input> elements in a HTML <form> 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?id=2347&name=jim QueryString is populated if the “get” method is used

Page 12: DT228/3 Web Development Active Server Pages & IIS Web server

ASP Object Model – Request –HTML Form that

collects 3 pieces of info<html> .. header etc <body> <font color = blue size = 4 face = arial> <form method = "post" Action = "processForm.asp"> Your name: <input type = text name = txtName> <br> Your favourite colour: <input type = text name = txtColour> <br> Your favourite film: <input type = text name = txtFilm> <br> <input type = "submit" value = "Click to submit information"</form></font></body></html>

etc

Page 13: DT228/3 Web Development Active Server Pages & IIS Web server

ASP Object Model – Request – output form content

onto a web page<html etc… <body> <font color = blue size = 4 The data entered into the form was: <br> Name = <%response.write(Request.Form("txtName"))%> <br> Favourite colour = <%=Request.Form("txtColour")%> <br> Favourite Film = <%=Request.Form("txtFilm")%> <br> </font></body></html>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 <%= %> . Both ways shown here.

Page 14: DT228/3 Web Development Active Server Pages & IIS Web server

• 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

ASP: Subroutines and functions

Page 15: DT228/3 Web Development Active Server Pages & IIS Web server

ASP : Subroutines - example

<html><head><%sub calculate(num1,num2) response.write(num1*num2)end sub%></head><body>The product of the two numbers entered is:<p><% dim x dim y x = request.form("firstNum") y = request.form("secondNum")%> Result: <%call calculate(x,y)%></p></body> etc

Subroutine always starts with Sub, ends with End

Sub

use “Call”

Arguments passed to subroutine (num1, num2)

Page 16: DT228/3 Web Development Active Server Pages & IIS Web server

ASP : Function example

<html> etc<head><%Function retailPrice(price) dim tax tax=price*.09 retailPrice =price+taxend Function%></head> etc.. <body> etc<% dim y y = retailPrice(request.form("netprice")) Response.write(“The retail price includig tax " &y)%>etc</body></html>

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

Page 17: DT228/3 Web Development Active Server Pages & IIS Web server

ASP- reusable code

• Very common in a web application to need to include common code such as headers/footers/navigation 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. It’s 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:• <!== #include attribute = value - ->

Page 18: DT228/3 Web Development Active Server Pages & IIS Web server

ASP- reusable code - example

<!-- #include file = “include\header.html -->• 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

<!-- #include virtual = “/testdir/header.html -->

• 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. It’s not concernted with current directory.

Page 19: DT228/3 Web Development Active Server Pages & IIS Web server

Comparing 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

Page 20: DT228/3 Web Development Active Server Pages & IIS Web server

Using 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 Microsoft’s 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 decision> ASP JSP

Web Server Microsoft IIS Any web server,

include apache, Netscape & IIS

Platforms Microsoft Windows Most popular platforms

Page 21: DT228/3 Web Development Active Server Pages & IIS Web server

Using 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.

Page 22: DT228/3 Web Development Active Server Pages & IIS Web server

Using 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.

Page 23: DT228/3 Web Development Active Server Pages & IIS Web server

Using 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.