+ All Categories
Home > Documents > 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The...

1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The...

Date post: 21-Dec-2015
Category:
View: 234 times
Download: 6 times
Share this document with a friend
31
1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who or what is viewing it. • If the Web browser that receives it, an Active Server Page looks just like a normal HTML page. • However, the file located in the server looks very different. In addition to text and HTML tags, you also see server-side scripts. This is what the Active Server Page looks like to the Web server before it is processed and sent in response to a request.
Transcript
Page 1: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

1

Active Server Pages

Active Server Pages (ASPs) are Web pages

ASP = server-side scripts + HTML

The appearance of an Active Server Page depends on who or what is viewing it.

• If the Web browser that receives it, an Active Server Page looks just like a normal HTML page.

• However, the file located in the server looks very different. In addition to text and HTML tags, you also see server-side scripts.

This is what the Active Server Page looks like to the Web server before it is processed and sent in response to a request.

Page 2: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

2

How ASP Works?

1. User brings up a Web site where the default page has the

extension .asp.

2. The browser requests the ASP file from the Web server

3. The server-side scripts begins to run with ASP.

4. ASP processes the requested file sequentially (top-down),

executes any script command contained in the file, and produces

an HTML Web page.

5. The Web page is sent to the browser.

Page 3: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

3

ASP vs HTMLASP = HTML + some other stuff!

What is the difference? Basically we are taking the HTML and adding some elements...

• ASP files end with the .asp extension, so the Web server knows what it is.

• ASP files include server-side code that contain instructions for the server.

• Browser can not display your code!

• ASP files provide dynamic information

Page 4: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

4

Active Server Pages

Server-side scripts ~ HTML tags.

<%...%> ~ <...>

You can insert server-side scripts anywhere in your Web page--

even inside HTML tags.

<h1> <% = “Hello World” %> </h1>

The server looks inside the ASP tags and runs the instructions, sends the

results

Page 5: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

5

Vbscript - Scripting Language

•Default Language of ASP VbScript

<% @ language = VbScript %> (no need to write!)

• ASP JavaScript

<% @ language = Javascript %>

•General Syntax:

<% object.method “parameter1”, “parameter2” %>

<% response.write “print this out!..” %>

Page 6: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

6

ASP Objects

1. Request object, 2. Response object, 3. Server object, 4. Session object, 5. Application Object.

It connects, or relates, these objects to ActiveX Data Objects (ADO)

• Connection object,• Recordset object, • Command Objects.

Page 7: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

7

ASP – Request Object

• gets information from client browsers

• submits it to the Web server

<input name = “Email”> <% =request.form(“Email”)%>

It provides the following basic data collection functions;

Form (collects data from fill-in forms by using post

method)

QueryString (same;but the method is get. Appends data to the

URL given in the Action attribute of a form)

ServerVariables (generates HTTP and server information

about a visitor’s site)

Cookies (stored as small text files on client computers

retrieved by the Web server each time

the client browser opened.)

Page 8: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

8

Examples – Request Object

<%

for each SV in Request.ServerVariablesresponse.write "<P>" & SV & " = " & Request.ServerVariables(SV) & "</P>"

next%>

<%

for each item in Request.Cookiesresponse.write "<P>" & Cookie & "=" & Request.Cookies(item) & "</P>"

next%>

Page 9: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

9

ASP – Response Object

• takes information from Web server

• sends to the client browsers

• redirects the browser to the different URL

• sends cookies to the browser

Examples;

<% Response.Write “Hello World. <BR>” %>

<% Response.Redirect (“page.htm”) %>

Page 10: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

10

ASP-Server Object

• Sets the maximum amount of time a script can run before it

times out• Takes a string and either HTML or URL encode it• Converts a virtual web address to an absolute path on the server

<%

set connection = Server.CreateObject(“ADODB.Connection”)

%>

Page 11: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

11

ASP-Session Object

The process of a Web user’s visit through the pages is called a

session.

• Used to identify users of the application

• Useful for site counters and for login

<% Session(“loggedin”) = “yes”%>

The Web server automatically creates the Session object.

Page 12: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

12

ASP-Application Object

Used to store and share information that can be used on the entire application.

To store Web address;

<% Application(“Homepage”) = “http://www.bim.bilkent.edu.tr/~ikinci %>

<a href = “<% = Application(“homepage”) %>”>Home Page </a>

To store the database connection string;<% conn341 = “DRIVER = {Microsoft Access Driver (*.mdb)};”conn341 = conn341 & “DBQ =“ & ServerMapPath(“dbase/db341.mdb”)Application(“connection”) = conn341%>

<% Set conn = Server.CreateObject(“ADODB.Connection”)Conn.Open Application(“connection”) %>

Page 13: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

13

ADO - ActiveX Data Objects

ADOs provide a mechanism for the Web-based client/server

application to access various databases.

ADOs are designed to interface with the relational

databases through the Open Database Connectivity

(ODBC).

ADO includes 3 objects; Connection object, Recordset object, Command object.

Page 14: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

14

Database

ASP and ADO Object Model Structure

Client Browser

Web server Data server

Requestobject

Responseobject

Serverobject

ApplicationObject

ActiveX dataobject (ADO)

ODBC Driver

System DSN

Sessionobject

Page 15: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

15

Variables

Every application uses variables, and ASP scripts are no exception!

But there is no obligation in Visual Basic and VbScript

No need to define the variables and variable types at the beginning of the program. But if you want you can...

If you add the following line after the language info to the page;

<% Option Explicit %> You must declare your variables. i.e. dim number1

dim number2

Page 16: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

16

Variables

Basic Variable Types: numeric, alpha-numeric, and logical.

No need to know other variable types, because when you assign a value to a variable, Visual Basic understands your variable type!variable_1 = 15variable_2 = “15”variable_3 = true

If you want to learn type of any variable;

<% variable_1 = 15Response.write vartype(variable_1) ‘it writes 2 to the page%>

List of Variant type variables of the VBScriptvbNull 1 contains no data, vbInteger 2 integer type, vbLong 3 .....

Page 17: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

17

VBScript Looping – flow controlsLooping constructs provide the foundation for any application that must repetitively perform a task.Such as adding 1 to a variable, reading a text file, or processing an e-mail message.VBScript and Jscript provide several looping mechanism. For... Next, For each ... in ...Next, Do.. Loop While, Do...While...Loop, Do..Until...Loop, Do...Loop Until..., While ...Wend

conditional operatorsConditional operators, together with variables and looping constructs, for the fundamental building blocks of applications.Such as sending a page to the client browser that consists of the current time and date and a greeting

If..Then, If...then...else, If...then...Elseif...then else, Select...Case, Switch..case

Page 18: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

18

How to Create ASP Pages?

1. Create your web page - basic design - with MS Frontpage

2. Switch to HTML tab.3. Add ASP codes into their respective places.4. Save your file with the extension .asp.

1. Design the followings in FrontPage:

Page 19: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

19

How to Create ASP Pages ?

2. Switch to HTML tab.

Page 20: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

20

How to Create ASP Pages?3. Add ASP codes into their respective places.

Page 21: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

21

Reponse.Write ( )

Response object is the part of ASP that handles responding to the request of a browser

If you want to display information to Web page Write method of the response object will be used.

<%Response.Write(“Hello World”)%>

NOTE : equal sign (=) is a short-cut of response.write object

<% = “Hello World” %>

Page 22: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

22

How to Save ASP Pages?

4. Save your file with the extension .asp.

Write asp extension with the file name.

Select Active Server Pages option as file type. So, you do not need to write file extension.

OR

Page 23: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

23

How to transfer to the Server?

Page 24: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

24

How to Display ASP Pages?

Page 25: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

25

Another example! (“for – next”)

We want to write “hosgeldiniz” in 5 different font size!

Page 26: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

26

Handling HTML codes in ASP codes

<% response.write “ “ & “ “ & i%>

<% Response.write “<BR>” %>

htmlTuna Tuna<B>Tuna</B> Tuna<font size = “2”>Tuna</font> response.write “<font size = “2”>Tuna</font>“

typeMismatch

Page 27: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

27

response.writeresponse.write “<font size = “2”>Tuna</font>“

Handling Error:1st way: Do not use “ “ in HTML! But;

<font face = lucida handwriting> face = lucida?

<%response.write<table width = 100%>%> ?

2nd way: use ‘ ‘ instead of “ “

Examples:Example 1:Adding 2 numbers<%num1 = 40num2 = 30response.write num1 + num2%>

Page 28: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

28

response.writeExample 2:finding y

<% a=1b=5c=2y= b*b - 4*a*cresponse.write y%>

<%num1 = 10num2 = 20sq1 = num1 * num1sq2 = num2 * num2sum = num1 + num2response.write sq2 &","& sq1 & "," & sum

Example 3:finding the square of 2 numbers and the addition of their square

Page 29: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

29

response.write & if..then..else..end ifExample 4:VAT included Price

<%VAT = 18PRICE = 3000000VAT_INC = PRICE + (PRICE * VAT)/100Response.Write "VAT included price is"& " " & VAT_INC%>

<%num = 89IF num >0 THEN

response.write num &" "& "is a positive number.“ELSE

response.write num &" "& "is a negative number.“END IF%>

Example 5:positive or negative?

Page 30: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

30

response.write & if..then..else..end ifExample 6: If a number less than 10, output it's

square otherwise the number only.<% num = 9IF num > 10 THEN

sq = num * numresponse.write sq

ELSEresponse.write num

END IF %>

<% num1 = 3num2 = 5IF num1>num2 THEN

num = num1 - num2ELSE

num = num2 - num1END IF Response.Write num %>

Example 7: Subtract the smaller from the larger

Page 31: 1 Active Server Pages Active Server Pages (ASPs) are Web pages ASP = server-side scripts + HTML The appearance of an Active Server Page depends on who.

31

response.write & if..then..else..end if Example 8: SNG room 100$ others 150$>

<% room_type = "DBL“num_of_night = 10IF room_type = "SNG" THEN

price = 100 * num_of_night ELSE

price = 150 * num_of_nightEND IFResponse.Write "room price is " & price %>

Example 9: PASS, FAIL

<% final = 50IF final >= 50 THEN

Response.Write "PASS“ELSE

Response.Write "FAIL“END IF%>


Recommended