Server Side Programming ASP 1
Server Side Programming
Server Side Programming ASP 2
Content
Introduction Server Side Programming (ASP) ASP Architecture Programming Techniques Sample Examples
Server Side Programming ASP 3
Introduction
Internet allows for client-server systems Programs can be invoked on either/both
clients and servers:â Client side includes Java Applets, ActiveX
components with JScript or VBScript.â Server side includes CGI and ASP
Data can be kept on servers distributed across network and accessed via client
Server Side Programming ASP 4
Why use Internet to access data? People can access the data using a familiar
browser interface. No special software required.
Data can be maintained centrally or distributed.
Data can be integrated with other information in an institutionâs web pages
Information can be made available to anyone (with permission) on the Internet
Server Side Programming ASP 5
How can database be accessed by web browser?
Web browser Web browser
Web browser
Server Side Programminge.g. Microsoft IIS server :
ASP (VBScript, JScript, SQL)
Internet
Database
Server Side Programming ASP 6
Client Side Scripts They are browser-specific, so the same
code may be interpreted differently depending on the browser being used.
They can't be used to refer to a database They can lessen the burden on server Particularly useful for validating data before
sending to server side to be processed.
Server Side Programming ASP 7
Server Side Programming (ASP)
Server Side Programming ASP 8
Content
Server Side programming overview Example
â Microsoft Internet Information Services (IIS)â Active Server Page(ASP)â VBScript/JScript
Server Side Programming ASP 9
Common Gateway Interface (CGI) CGI Is a standard for interfacing
external applications with web servers (Common Gateway Interface, 1995).
Involves a new process each time it is invoked therefore can be slow with multiple users.
Alternatives: ISAPI (ASP), NSAPI.
Server Side Programming ASP 10
IIS IIS is a web server that runs on Win NT,
2000, Server2003 and XP Server. Can use component-based
(COM/ActiveX) client/server applications as well as JScript (ECMAScript) and VBScript
Server Side Programming ASP 11
What is ASP?
âA server - side scripting environment that you can use to create and run dynamic, interactive, high - performance Web server applicationsâ. Microsoft
Can include COM/ActiveX components and/or compiled code
Runs on IIS
Server Side Programming ASP 12
What are Active Server Pages?
Server-side environment Can provide compile-free application
environment Combine HTML, scripts, COM and
ActiveX server components. Uses Server and Client Information to
create and send dynamic web pages to clients
Server Side Programming ASP 13
Active Server Pages
Is program that runs inside IIS IIS-Internet Information Services As free software component with
windows 2000 Option pack can be downloaded from
Microsoft
Server Side Programming ASP 14
Using ASP User usually completes HTML form in a
browser and submits this to a .asp file on the server (note that you need a .asp file on the server)
.asp file contains both HTML and scripts/components
Scripts/components invoked and produce new HTML code
resulting page: original HTML + new HTML (generated by asp) - this is sent to client.
Server Side Programming ASP 15
Using ASP Server evaluates tag starting with <%
and ending with %> Scripts/components contained within
tags Web servers supporting ASP:
â Personal Web Server (Windows 95, 98, NT)
â Internet Information Services (Windows NT Server, 2000 Server, Server 2003, XP)
Server Side Programming ASP 16
ASP Basics
ASP file is a text file with the extension .asp
it contains any combination of:â Textâ HTML Tagsâ ASP Script Commands/Components
Server Side Programming ASP 17
Client-Server Structure
IIS
Client
request
Running ASPScript...
results
Server Side Programming ASP 18
ASP Architecture Is microsoft technology for sending client
dynamic web content including XHTML,Dynamic HTML,ActiveXControls ,Client Side Scripts and Java Applets
ASP Processes the request and sends response to client
When client request ASP ,its loaded into memory and parsed by scripting engine named ASP.DLL
Server Side Programming ASP 19
ASP Script ASP Script could be VBScript or Jscript A script is a series of commands or
instructions. Script command instructs the web
server to perform an action. VBScript is similar to Visual Basic and
Visual Basic for Application (VBA).
Server Side Programming ASP 20
VBScript Basics Not case sensitive Declaring Variables VBScript does not require variable
declarations, but it is good scripting practice to declare all variables before using them.
To declare a variable in VBScript, use the Dim, Public, or Private statement.
Server Side Programming ASP 21
Declaring VariablesDim: Declares variables and allocates storage space.
Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure. Ex. Dim A,B
Private: Used at script level to declare private variables and allocate storage space. Private variables are available only to the script in which they are declared
Public: Used at script level to declare public variables and allocate storage space. Variables declared using the Public statement are available to all procedures in all scripts in all projects.
Server Side Programming ASP 22
VBScript Basics
VBScript Operators:â Arithmetic: +, -, *, /, ^â Comparison: =, <>, <, >, <=, >=â Logical (for Boolean variables): Not, And,
Or, Xor
Server Side Programming ASP 23
VBScript Basics
Conditional Statement:â IF ⌠Then⌠Else
⢠If condition Then [Statement]⢠elseif condition-n Then [elseifstatment]âŚâ˘ else [elsestatements]⢠End if
Server Side Programming ASP 24
VBScript Basics Loop:
â Do ⌠Loopâ While âŚWendâ For ⌠Nextâ For Each âŚNext
Procedures:â Subâ Function
Server Side Programming ASP 25
ASP Demo ASP uses <% and %> to enclose script commands.
<html><head><title>ASP Example 1</title></head><body> <p>This page was last refreshed on <%= Date %> </p> <p>Now it is <%= Now %> </p></body></html>
Note 1: The VBScript function Now returns the current time, Date returns current date.
Note 2: No data is input by the user
Server Side Programming ASP 26
ASP Demo The response.write command is used to
write output to browser Exp
<html><body>
<%
Response.write(âHello Worldâ)
%></body></html>
Server Side Programming ASP 27
Including Javascript
<%@ language=âjavascriptâ %>
<html>
<body>
<%
Response.write(âHello Worldâ)
%>
</body>
</html>
Server Side Programming ASP 28
Global.asa File Contains Application Events Session Events <object> Declarations TypeLibrary Declarations The #include directives Note
â Global File.asa must stored in root directory of ASP application, and each application can only contain only one Global.asa file
Server Side Programming ASP 29
Running ASP
Save file with .asp extension on server Client accesses the file in similar
fashion to .htm (or .html) file Server detects .asp extension and runs
script within ASP tags Results sent to clienthttp://128.255.21.191/Example1/Example1.asp
Server Side Programming ASP 30
Example 2 <html> <head><title>ASP Example 2</title></head> <body> <form method="POST" action="resultofexample2.asp"><p>A= <input type="text" name="ValueofA" size="20"></p><p>B= <input type="text" name="ValueofB" size="20"></p><p><input type="submit" value="Calculate" name="B1"></p></form></body></html>
http://128.255.21.191/Example2/Example2.htm
Server Side Programming ASP 31
Example 2 see web siteContained in HTML code:
<%@ language="vbscript" %>
<% dim A, B
A=Request.form("ValueofA")
B=Request.form("ValueofB")
%>
âŚâŚâŚ.
<p>A+B= <%= eval(A)+eval(B) %> </p>
<p>A-B= <%= eval(A)-eval(B) %> </p>
<p>A*B= <%= eval(A)*eval(B) %> </p>
âŚâŚ.
Server Side Programming ASP 32
example 2 asp
<html> <head><title>Example 2 ASP Page</title></head> <body> <p>Example Page <%@ language="vbscript" %> <% dim A, BA=Request.form("ValueofA")B=Request.form("ValueofB") %> </p> <p>A+B= <%= eval(A)+eval(B) %> </p> <p>A-B= <%= eval(A)-eval(B) %> </p> <p>A*B= <%= eval(A)*eval(B) %> </p></body></html>
Server Side Programming ASP 33
Database Overview
Server Side Programming ASP 34
What is a Database? A database is an organized collection of
related data, typically stored on disk, and accessible by possibly many concurrent users.
Most common is relational database, which is a tabular database in which the data is defined so that it can reorganized and accessed in a number of different ways
Server Side Programming ASP 35
Database Management Systems
DBMS A program to access, manipulate and
present the data to a user. Example DBMS:
â Oracleâ Sybase (Microsoft SQL)â IBMâs DB2, IMS and SQL/DSâ dBaseâ Access
Server Side Programming ASP 36
Relational Database Structure
set of tables containing data fitted into predefined categories.
each table(relation) contains one or more data categories in columns.
each row contains a unique instance of data for the categories defined by column.
Server Side Programming ASP 37
Example: Access
Relational database Fairly good GUI. Can use SQL
Server Side Programming ASP 38
Relationships in Access
Multiple tables in one database Matching key fields between table
â A key is usually a field with the same name in both tables.
â Such key is primary key for one table, foreign key in the other table.
Server Side Programming ASP 39
SQL SQL: Structured Query Language It is used to query from and update
database. Systems using SQL:
⢠Oracle⢠Sybase⢠Microsoft SQL server⢠Access
Server Side Programming ASP 40
SQL
Standard SQL commands:â Selectâ Insertâ Updateâ Delete â Create
Server Side Programming ASP 41
SQL Exampledatabase - example3.mdbtablename - table1columnname - ID, ItemName, Price
Server Side Programming ASP 42
SQL Example To find out the âPriceâ when âItemNameâ
equals to Candy
SELECT Price FROM Table1 WHERE ItemName = âCandyâ
Note:
Can SELECT * to return whole row as an object - see Example 3 on website
Server Side Programming ASP 43
SQL Basic
Insert tablename (fieldname1, fieldname2âŚ)
values (value1, value2)
update tablename
set column_name1=expression1
Server Side Programming ASP 44
SQL Basic
Create tablename (fieldname1 type, fieldname2 type)
Delete [*] from tablename where clause (deletes whole row)
Server Side Programming ASP 45
Database Integration Requirements
Server (IIS) Database
⢠Internet database connector (ODBC) not always required can use ADODB
Dynamic Web Page (asp)⢠User Input using HTML forms⢠Data Transmission⢠Retrieving data from database⢠Display results
Server Side Programming ASP 46
Operation
database
Server
Client
HTML
ASP
Server Side Programming ASP 47
Using web browser to access a database Web pages and forms are used to
provide access to the database. Database can be queried or updated. Platform-independent front-end. Database vendor-independent front-end Client machine does not need database
networking software
Server Side Programming ASP 48
Example Implementation
Step 1: HTML file (containing forms) for user input on server.
Step 2: Database on server Step 3: Setup database connection
(ODBC)
Server Side Programming ASP 49
Step I: HTML File with Forms Example:<form method="Post" action="Find.asp">
<p> <select name = "item" method ="post" size ="1"><option selected value="Gum">Gum</option><option selected value="Suckers">Suckers</option><option selected value="Taffy">Taffy</option><option selected value="Skittles">Skittles</option><option selected value="M&Ms">M&Ms</option><option selected value="Lifesavers">Lifesavers</option><option selected value="Snickers">Snickers</option></select><input type="submit" value="Tell me the price"></p></form>
This captures user input and sends result to result.asp
Server Side Programming ASP 50
Server Side Programming ASP 51
HTML Forms Examplehttp://128.255.21.191/Example3/Example3.htm
Server Side Programming ASP 52
Step II: Database
Under the home directory, create the access database named âCandy.mdbâ
tablename - Products columnname - ID, ItemName, Price
Server Side Programming ASP 53
<%@ LANGUAGE='VBSCRIPT'%><html><head></head><body><%dim priceSet MyConn = Server.CreateObject("ADODB.Connection")set rs = Server.CreateObject("ADODB.Recordset")MyConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Candy.mdb")item = Request.form("item")Set RS = MyConn.execute("Select * from Products where Candy = '" & item & "'")
if rs.Eof thenPrice = ""elsePrice = RS("Price")End ifResponse.Write("It will cost you $" & price)
Set MyConn = nothingSet rs = nothing
%></body></html>
Server Side Programming ASP 54
Running ASP
Check the extension (asp) server evaluates tag starting with <%
and ending with %> server replaces the tags with normal
HTML using scripts results page: original Markup + new
Markup(generated by asp)
Server Side Programming ASP 55
Result
Server Side Programming ASP 56
Java2 Enterprise Edition
EJBs (Enterprise JavaBeans) ----picture PCWeek