Sei sulla pagina 1di 41

Dynamic HTML Using

Active Server Pages (ASP)

1
Need to Study
• Active Server Pages (ASP)
• Active Server Pages (ASP): Concept of ASP, features of ASP,
• Variables: Explicit and Implicit
• Declaration;
• Functions:
• String Manipulation Functions: Ucase O, Lcase O, Len O, Left O,
• Right O, Mid O, Ltrim O, InStr0
• Time & Date Functions: Date O, DayO, Hour 0, Left O, Len O, Minute
• 0, Month O, Monthname O, Now O;
• Arrays: Declaration and use of 1 dimensional and 2 dimensional
• arrays;
• Procedures and Functions, Passing parameters/ arguments;
• Connecting with Databases: Creation of DSN, using OLEDB.

2
Static HTML and
the HTTP Protocol
HTTP  designed as a doc fetching protocol:

1. User clicks on URL with HTTP protocol


2. Browser requests HTML page to web site
3. Server finds/sends HTML page to client “as is”
4. Client’s browser interprets HTML and
presents page to user

3
Dynamic HTML

2 Solutions to Static HTML

1. Client side scripting


Further processing by browser after page is
received

2. Server side scripting


Prior processing by web server before page is sent

4
Dynamic Pages

• A dynamic page is generated each time it is called


• The same page may be posted back to the server for
processing.
• Nevertheless, the page itself is stateless, i.e., it will not
maintain the value of a variable between each loading of
the page
5
Client Side Scripting
• Useful for interactive use with user
• Browser needs to support the scripting
language used
• Most popular: JavaScript, VB Script
• Embed scripts in HTML page
HTML stuff
<SCRIPT LANGUAGE = “JavaScript”>
script code …………
</SCRIPT>
More HTML stuff

6
Server Side Scripting
• Useful to interact with data stored on the server
(databases, images, etc.)
• And when centralized processing is needed
• Sever needs to support the scripting language
• Most popular:
CGI  Perl (Unix)
ASP  VB Script or JScript (Windows)

7
Server Side Scripting (cont’d)
• Embedded scripts in HTML page
HTML code
<% (marks the beginning of ASP script)
………..……….
ASP script code
………………....
%> (marks the end of ASP script)
More HTML code
<% more ASP %>
Etc.

8
Dynamic HTML with ASP
1. Client clicks on URL with .asp file
2. Web server notices file extension .asp
Note: Only Windows NT IIS web server supports ASP
3. Server then processes .asp file
4. Server creates a new HTML file
5. Contains all original HTML stuff
6. Plus processing results from ASP code
7. Dynamically formatted as HTML
8. Server sends the new HTML file to client
9
10
How ASP works

11
Web Applications

http://sundown.wou.edu:81/en/mail.html?lang=en&laurel=on&cal=1

Domain name Port


Page/file Parameter #1
Protocol Path
Start of parameters Parameter #2
12
13
14
15
16
ASP’S object model
• 1. Request Object
• 2. Response Object
• 3. Server Object
• 4. Session Object
• 5. Application Objects

17
Dynamic HTML with ASP
ASP code on server HTML code sent to client
<H3>Welcome to my page</H3> Same <H3>Welcome to my page</H3>
<H2>Here is my product list</H2> <H2>Here is my product list</H2>

<P>
<% ‘Start ASP code <B>Product Price</B>
Open a database connection Dynamically <HR>
SQL queries to database generated <P>Hammer ……... $8.50
Copy results to a record set by ASP <P>Pliers ……….… $7.79
Display records one at a time <P>Screwdriver ..… $4.50
<P>Power Drill ….. $49.99
Close database connection <P>Chainsaw …… $95.95
%> ‘End ASP code <P>Wrench ……….. $6.50

<P>Thank you very much for <P>Thank you very much for
inquiring about our products Same inquiring about our products

18
ASP, SQL, Databases, and HTML
Web Server
MS IIS Web Server
SQL Query
Databases ASP HTML
Query HTML
Results Request

Dynamic
HTML ASP HTML doc
Produced Request Fetched
On-the-Fly

Client Browser
Internet Explorer
Netscape Navigator
19
Common Uses of ASP

• Register as a client (insert a record in database)


• Products & services listing (query database)
• Place orders (inserting records in database)
• Track order status (query database)
• Tech support (query a knowledge base)
• Fill out a survey (insert record(s) in database)

20
Example--ASP on Server (Query)
<% Set conn = Server.CreateObject("ADODB.Connection")
conn.open "orders "
Set rs_customers = Conn.Execute("SELECT clientID, clientName,_
shipAddress, telephone FROM clients") %>

<IMG SRC="music22.gif"><B>Alberto's Music Instruments, Inc.<p>


<TABLE BORDER="0"><B>Customer List</B>
<TR><TH>ClientID</TH> <TH>Client Name</TH> <TH>Shipping
Address</TH> <TH>Telephone</TH></TR>

<% do while Not rs_customers.eof %>


<TR><TD><%= rs_customers.Fields("clientID") %></TD>
<TD><%= rs_customers.Fields("clientName") %></TD>
<TD><%= rs_customers.Fields("shipAddress")%></TD>
<TD><%= rs_customers.Fields("telephone") %></TD></TR>
<% rs_customers.MoveNext
loop
Conn.Close %>

</TABLE></BODY></HTML>

21
Example--Response to Browser
<IMG SRC="music22.gif"><B>Alberto's Music Instruments, Inc.<p>
<TABLE BORDER="0"><B>Customer List</B>
<TR><TH>ClientID</TH> <TH>Client Name</TH>
<TH>Shipping Address</TH> <TH>Telephone</TH> </TR>

<TR><TD>josee</TD>
<TD>Alberto Espinosa</TD>
<TD>Schenley Park, GSIA Building, #20</TD>
<TD>412-268-3681<BR></TD> </TR>

<TR><TD>sandy</TD>
<TD>Sandra Slaughter</TD>
<TD>5000 Forbes Avenue, Pittsburgh PA 15213</TD>
<TD>412-268-3681<BR></TD> </TR>

etc.

</TABLE></BODY></HTML>

22
Example--ASP on Server (Data Input)
<B>Customer Registration</FONT></B><P>

<FORM ACTION=
"http://softrade-11.gsia.cmu.edu/data/customerSubmit.asp"
METHOD="POST" ENCTYPE="x-www-form-urlencoded">

<TABLE>
<TR><TD>Please enter a customer ID (4 to 16 characters)</TD>
<TD><INPUT TYPE="text" SIZE="35" NAME="CustomerID" VALUE=" ">
</TD></TR>
<TR><TD>Please enter your name</TD>
<TD><INPUT TYPE="text" SIZE="35" NAME="CustName" VALUE=" ">
</TD></TR>
etc.
</TABLE>

<INPUT TYPE="submit" VALUE="Submit"></TD></TR>

</TABLE></FORM></BODY></HTML>

23
Example--ASP on Server (Data Input)
<!-- customerSubmit.asp -->
<HTML><BODY><CENTER>

<% Set conn = Server.CreateObject("ADODB.Connection")


conn.open "orders”

customerId = Request.Form("customerId")
custName = Request.Form("custName") …. etc.

Set rs = Conn.Execute("INSERT INTO Clients (ClientID, CustName, _


etc. VALUES ('" & customerID & "', '" & customerName & "', _
'" & shippingAddress & "', '" & phone & "') ")

Conn.Close %>

Your Customer Registration has been processed!<BR>


Thank you very much<BR>
<A HREF="http://softrade-11.gsia.cmu.edu/data/orders.html">
Back to main page</A>&nbsp;&nbsp;&nbsp;

</CENTER></BODY></HTML>

24
ASP Features
• Cross-platform & container support.
• High performance.
• Asynchronous via async/await
• Unified MVC & Web API frameworks.
• Multiple environments and development mode.
• Dependency Injection.
• WebSockets & SignalR.
• Cross-Site Request Forgery (CSRF) Protection
• Self hosted” Web Applications
• Action Filters
25
String- Len()
• <%

Dim strtest,varlength

strtext=”Howareyoudoing?”

varlength=Len(strtext)

response.write(varlength)

%>
• Result
is 15
26
• Asp – Right() and Left() functions

• Returns a given
number of characters from the beginning and end of a string.The syntax is :
• Right(string,NumberOfCharecteres)

Left(string,NumberOfCharecteres)
• Eg:
<%
Dim strtest,str1,str2

strtext=”Howareyoudoing?”

str1=Right(strtext,6)

str2=Left(strtext,3)

response.write(“The right six characters


in the string is ” & str1 & “<br>”)
response.write(“The left three characters
in the string is ” & str2)
%>
• The result is
:
• doing?

How 27
• Asp – InStr Function
• The syntax is:

Instr(string,StringToBeLocated)
• This returns the
position at which the StringToBeLocated was found.This returns
0 if StringToBeLocated is not found or if StringToBeLocated is
zero-length.This returns null if string or StringToBeLocated is
null.
• Eg:
<%
Dim strtest,intfound

strtext=”Howareyoudoing?”

intfound = instr(strtest,”you”)

if intfound>0 then

response.write(“String Found”)
28
Else
• Asp – mid() Function
• This function
returns the requested number of characters from a string.This works along the
same principle as Right() or Left() , but it takes an extra parameter.
• The syntax is:

Mid(String, CharToStartWith,
NumberOfCharectersToBeExtracted)
• Eg:
<%
Dim strtest,str1
strtext=”Howareyoudoing?”

str1 = mid(strtext,4,3)

response.write(str1)

%>
• Result
is : are
• if
NumberOfCharectersToBeExtracted is left out, Mid will return all of the characters
starting from CharToStartWith.

29
• Asp – Trim(),LTrim() , RTrim() Functions
• The function LTrim()
removes spaces from the left hand side of the string, RTrim() removes
spaces
from right hand side of a string and Trim() function combines the
two,removing
all the spaces from right and left end of the string.

30
• <% Response.Write(Date()) %>
Or
<%=Date%>
Out Put : 3/28/2019

31
formatdatetime function
• The Format Date Time function takes two arguments: a date and (optional) an integer from 0
through 4. The meanings of these numbers are as follows:
• 0 - This is the default setting. A short date DD/MM/YYYY will be used.
• 1 - A long date defined by the computer's regional settings.
• 2 - A short date defined by the regional settings.
• 3 - (time)A time using the time format defined by the regional settings.
• 4 - (time)A time using military time HH:MM (H = hour, M = Minute)
• Below is an example of all five used to format the Date function.
• ASP Code:
• <% Response.Write("0 = " & FormatDateTime(Date, 0))
Response.Write("<br />1 = " & FormatDateTime(Date, 1))
Response.Write("<br />2 = " & FormatDateTime(Date, 2))
Response.Write("<br />3 = " & FormatDateTime(Date, 3))
Response.Write("<br />4 = " & FormatDateTime(Date, 4)) %>
• Display:
• 0 = 3/28/2019
1 = Friday, October 21, 2005
2 = 3/28/2019
3 = 12:00:00 AM
4 = 00:00
32
ASP Array
<%

a=Array(5,10,15,20)
response.write(a(3))
%>

33
<% Dim myFixedArray(3)
myFixedArray(0) = "Albert Einstein“
myFixedArray(1) = "Mother Teresa"
myFixedArray(2) = "Bill Gates"
myFixedArray(3) = "Martin Luther King Jr." %>

34
Array Iteration
<%
a=Array(5,10,15,20)
for each x in a
response.write(x & "<br />")
next
%>

35
2D Array
• <%
Dim arrCars(2,4)

'arrCars(col,row)
arrCars(0,0) = "BMW"
arrCars(1,0) = "2004"
%>

36
Functions

<%@ LANGUAGE="VBSCRIPT" %>


<%
sub SubroutineName( Parameters to
Pass In )
'Code for Sub...
end sub
%>

37
Functions Return
• <%@ LANGUAGE="VBSCRIPT" %>
<%
function Square(num)
Square = num * num
end function

Response.Write(Square(5))
%> 38
Procedure
• <%@ LANGUAGE="VBSCRIPT" %>
<%
sub PrintProfit(Revenue, Overhead, COGS, Admin)
'Create a variable to store our profit
Dim Profit

Profit = CDbl(Revenue - (Overhead + COGS + Admin))

Response.Write("$" & Profit)


end sub

'Call the sub with various values


'Will output $100.0
PrintProfit 150, 25, 10, 15

'Will output $0.5 (a bad year!!)


PrintProfit 200, 150, 45, 4.5
%>

39
Functions Vs Procedure
• The function must return a value but in
Stored Procedure it is optional. Even a
procedure can returnzero or n values.
Functions can have only input parameters
for it whereas Procedures can have input
or output parameters. Functions can be
called from Procedure whereas Procedures
cannot be called from a Function.
40
Abbrevations
• DSN- Data Source Name
• OLEDB-Object Linking and Embedding Data Base
• ODBC- Open Data Base Connectivity
• ADO- ActiveX Data Object
• ASP – Active Server Pages
• IIS – Internet Information Services
• HTML – Hyper Text Markup Language

41

Potrebbero piacerti anche