0% found this document useful (0 votes)
87 views

ASP

The document provides an introduction to ASP (Active Server Pages), explaining that ASP is a program that runs inside IIS (Internet Information Services) and is Microsoft's solution for building dynamic websites. It describes how ASP differs from HTML in that ASP files generate dynamic pages while HTML generates static pages. It then outlines the processing steps for HTML pages versus ASP pages, and provides details on ASP syntax including scripts, variables, arrays, lifetime of variables, and built-in ASP objects like Request, Response, and ServerVariables.

Uploaded by

Karn Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

ASP

The document provides an introduction to ASP (Active Server Pages), explaining that ASP is a program that runs inside IIS (Internet Information Services) and is Microsoft's solution for building dynamic websites. It describes how ASP differs from HTML in that ASP files generate dynamic pages while HTML generates static pages. It then outlines the processing steps for HTML pages versus ASP pages, and provides details on ASP syntax including scripts, variables, arrays, lifetime of variables, and built-in ASP objects like Request, Response, and ServerVariables.

Uploaded by

Karn Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

ASP

Introduction to ASP

What is ASP?
• ASP stands for Active Server Pages.
• ASP is a program that runs inside IIS.
• IIS stands for Internet Information Services.
• ASP is Microsoft’s solution to building advanced Web sites.

How Does ASP Differ from HTML?

• HTML file generates static pages, but ASP file generates dynamic pages.
• HTML file has the extension .html/.htm, but ASP file has the extension .asp
• When a browser sends a requests to the web server for an HTML file the server
returns the file as it is to the browser, but when a browser sends a request to the
web server for an ASP file, IIS passes the request to the ASP engine having a
special program ASP.dll. This ASP file is processed line by line and executes the
server side scripts(<% %>) in the file. Finally, the ASP file is returned to the
browser as plain HTML.
• Processing of an HTML Page

Browser Request Web Server

HTML File Memory-HTML file

Steps involved in HTML page Processing are:


1) A user enters request for Web Page in browser.
2) The browser sends the request for the web page to web server such as IIS.
3) The web server receives the request and recognizes that the request is for an HTML file by examining the extension
of the requested file (.Html or .htm )
4) The Web Server retrieves the proper HTML file from disk or memory and sends the file back to the browser.
5) The HTML file is interpreted by the user’s browser and the results are displayed in the browser window.
Processing of an ASP Page

Browser Request Web Server

HTML File Processing Memory-ASP File


ASP page processing Steps:

1) A user enters request for Web Page in browser.


2) The browser sends the request for the web page to web server such as IIS.
3) The web server receives the request and recognizes that the request is for an ASP file by examining the extension
of the requested file (.asp)
4) The Web Server retrieves the proper ASP file from disk or memory.
5) The Web Server sends the file to a special program named ASP.dll
6) The ASP file is processed from top to bottom and converted to a Standard HTML file by ASP.dll.
7) The HTML file is sent back to the browser.
8) The HTML file is interpreted by the user’s browser and the results are displayed in the browser window.
ASP Syntax

• The Basic Syntax Rule


• An ASP file normally contains HTML tags, just as a standard HTML file.
• In addition, an ASP file can contain server side scripts, surrounded by the
delimiters <% and %>. Server side scripts are executed on the server, and can
contain any expressions, statements, procedures, or operators that are valid for the
scripting language you use.
• Scripts
• Script is nothing but a set of commands that are written to perform a specific task
• These commands are VBScript or JavaScript commands
• There are two types of Scripts:
• Client-Side Script : Runs On Browser (default : JavaScript)
• Server-Side Script : Runs On Server (default : VBScript)
• Scripts in an ASP file are executed on the server.
ASP Syntax

• Scripts
• Client-Side Script is embedded into the HTML file using tags:
<script language=JavaScript/VbScript>
{JavaScript/Vbscript Code}
</script>
• Server-Side Script is embedded into the ASP file using tags:
<script language=Vbscript/JavaScript RunAt=SERVER>
{Vbscript/JavaScript Code}
</Script>

OR

<%@ Language = VBScript/JavaScript %>


<% {VBScript/JavaScript Code} %>
Scripts

Difference Between using <Script> Tag and <% %> delimiters

• <Script> tag is executed immediately no matter where it appears.


By using <Script> tag it is possible to mix multiple scripting languages within single ASP page.

• Before using <% %> delimiters, this line of code is mandatory <%@ Language = VBScript/JavaScript
%> which specifies the language being used.
Example

<%@Language=VBScript%>

<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
Variables
• Variables are used to store information
• This example demonstrates how to create a variable, assign a value to it, and insert the variable
value into a text.

<%
Dim name
name=“Tripti Arora"
Response.Write("My name is: " & name)
%>
Arrays
• Arrays are used to store a series of related data items.
• This example demonstrates how you can make an array that stores names.
<%
Dim name(5)
name(0) = "Jan Egil"
name(1) = "Tove"
name(2) = "Hege"
name(3) = "Stale"
name(4) = "Kai Jim"
name(5) = "Borge"
For i = 0 to 5
Response.Write(name(i) & "<br />")
Next
%>
Lifetime of Variables

• A variable declared outside a procedure(subroutine or a function) can be accessed and changed by any script in
the ASP page in which it is declared

• A variable declared inside a procedure is created and destroyed every time the procedure is executed. No scripts
outside that specific procedure can access or change that variable

• To make a variable accessible to several ASP pages, declare it either as a session variable or as an application
variable

Session Variables
• Session variables store information about one single user, and are available to all pages in one application.

• Common information stored in session variables are Username and UserID. To create a session variable, store it
in a Session Object

Application Variables
• Application variables are also available to all pages in one application. Application variables are used to hold
information about all users in a specific application.

• To create an application variable, store it in an Application Object


HTTP Protocol(Request and Response Protocol)

Request
Client or
browser Response Server

• Request : The Browser/Client sends an input to the Web Server. This can be used to
gain access to any information that is passed with an HTTP request. This includes
parameters passed from an HTML form using either the POST method or the GET
method.
• Response : Output is send back to the Browser from the Web Server.
ASP Objects:

ASP has 7 built-in Objects:


1. Request
2. Response
3. Application
4. Session
5. Server
6. ObjectContext
7. Error

These Objects have

a. Collection : An Object’s collections constitute different sets of keys and value pairs related to that object.

a. Properties : An Object’s properties can be set to specify the state of the object.

b. Methods : An Object’s methods determines the things one can do with the ASP Object.

Eg : OBJECT : A Book. METHOD : Read a book. PROPERTIES : No. of pages. COLLECTION : Each page(key) has a
particular text(value).
Request Object
The Request object is used to get information from a visitor.

Collections:
I. QueryString
II. Form
III. ServerVariables
IV. Cookies

Properties
I. TotalBytes
ASP Forms and User Input
User Input
To get information from forms, you can use the Request Object
Example:

<form method="get" action="../pg.asp">


First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Send">
</form>

There are two ways to get form information: The Request.QueryString command and the Request.Form
command.
Request Object - Collections

Request.QueryString

• This collection is used to retrieve the values of the variables in the HTTP query string.

• Information sent from a form with the GET method is visible to everybody (in the address field)
and the GET method limits the amount of information to send.

• If a user typed “Bill" and "Gates" in the form example above, the url sent to the server would
look like this:

http://www.asp.com/pg.asp?fname=Bill&lname=Gates
Example of asp:
<%
response.write(request.querystring("fname"))
response.write("&nbsp;")
response.write(request.querystring("lname"))
%>

Output:
Bill Gates
Request Object – Collections

Request.Form

• It is used to retrieve the values of form elements posted to the HTTP request body, using the POST
method of the <Form> Tag.

• Information sent from a form with the POST method is invisible to others.

• The POST method has no limits, you can send a large amount of information.

• If a user typed "Bill" and "Gates" in the form example above, the url sent to the server would look like
this:
http://www.asp.com/pg.asp

<%
response.write(request.form("fname"))
response.write("&nbsp;")
response.write(request.form("lname"))
%>

Output:
Bill Gates
Form Validation

• The form input should be validated on the browser, by client side scripts.

• Browser validation has a faster response time, and reduces the load on the server.

• You should consider using server validation if the input from a form is inserted into a database.
ServerVariables Collection

• It is used to retrieve the values of predetermined environment variables.These values originate when client
requests the server.

Syntax:

Request.ServerVariables (server environment variable)

Eg;
Request.ServerVariables("remote_addr“)

Server Variables Collection:


1. Request_Method – returns GET or POST depending on how the request was made.
2. Query_String - Returns the query information stored in the string following the question mark (?) in the HTTP
request
3. Remote_Addr - returns the IP address of User
4. Logon_User - returns logon account of the user
5. ALL_HTTP - returns whole HTTP string
6. HTTP_User_Agent - returns the type of the client browser
7. HTTP_Accept_Language- determines which language is supported by the client’s browser
8. Path_Info - gives the full name of current file starting from the Web root directory.
9. Remote_Host - provides the visitor’s text URL
Note:https://www.w3schools.com/ASP/coll_servervariables.asp
TotalBytes
• It specifies the total number of bytes the client has sent in the body of the request.

• This property is read-only

Syntax:

Counter = Request.TotalBytes

Counter - Specifies a variable to receive the total number of bytes that the client sends in the request.

Example:

<% Dim bytecount


bytecount = Request.TotalBytes %>
Response Object
• It can be used to send output to the client.
• Its method and properties control how the information is sent to the client.

Properties
 Buffer
 CacheControl
 ContentType
 Expires
 ExpiresAbsolute
 IsClientConnected
 Status

Methods
 AddHeader
 AppendToLog
 Clear
 End
 Flush
 Redirect
 Write

Collection
 Cookies
Buffer
• It provides control of when the data is to be sent to the client.Normally the O/P from the page is sent
immediately to the browser after each command. Eg.:For Loop.

• When it is set to True (default value)the server won’t respond to client until whole page is processed or
until Flush or End method are called (i.e, whole page is kept in a buffer and showed after completion).

Advantage : The whole image seen at once and not in parts.


Disadvantage : User looses patience.

Syntax: response.Buffer[=flag]

Flag:

A boolean value that specifies whether to buffer the page output or not.False indicates no buffering. The
server will send the output as it is processed.

True indicates buffering. The server will not send output until all of the scripts on the page have been
processed, or until the Flush or End method has been called.
Examle of Buffer

<%response.Buffer=true%>
<html>
<body>
<%
for i=1 to 100
response.write(i & "<br>")
next
%>
</body>
</html>

In this example, there will be no output sent to the browser before the loop is finished. If buffer was set to
False, then it would write a line to the browser every time it went through the loop.
CacheControl Property

• The CacheControl property sets whether a proxy server can cache the output generated by ASP or not. By default, a
proxy server will not keep a cache copy.

response.CacheControl[=control_header]

Cache Control Header - Value is Public or Private

Default value is Private – This setting tells the proxy server that the contents of an ASP are private to a particular user
and should not be cached.

Expires Property

• The Expires property sets how long (in minutes) a page will be cached on a browser before it expires. If a user returns
to the same page before it expires, the cached version is displayed.

response.Expires[=number]

number The time in minutes before the


page expires
Example
<%response.Expires=1440%>
ExpiresAbsolute Property

The ExpiresAbsolute property sets a date and time when a cached page on a browser will expire. If a user returns to the
same page before this date/time, the cached version is displayed.

response.ExpiresAbsolute[=[date][time]]

date Specifies the date on which the page will expire.If this parameter is not specified, the page will expire at the
specified time on the day that the script is run.
time Specifies the time at which the page will expire.If this parameter is not specified, the page will expire at
midnight of the specified day.

The following code indicates that the page will expire at 4:00 PM on October 11, 2012:

<%response.ExpiresAbsolute=#October 11,2020 16:00:00#%>


IsClientConnected Property

The IsClientConnected property indicates if the client has disconnected from the server.

• This indicates whether client is connected/disconnected from the server.

• Importance : When a user requests your ASP page and then moves to another Web-Site, your web-
server unnecessarily is under pressure.

Syntax:

response.IsClientConnected

<%
If response.IsClientConnected=true then
response.write("The user is still connected!")
else
response.write("The user is not connected!")
end if
%>
Response Object - Methods

Clear Method

• The Clear method clears any buffered HTML output.

Note: This method does not clear the response headers, only the response body.
Note: If response.Buffer is false, this method will cause a run-time error.

Example:
<%
response.Buffer=true
%>
<html>
<body>
<p>This is some text I want to send to the user.</p>
<p>No, I changed my mind. I want to clear the text.</p>
<%
response.Clear
%>
</body></html>

Output:
(nothing)
Flush Method

• The Flush method sends buffered HTML output immediately.(first it will send data to the client and then flush out the
data from the buffer)
• Note: If response.Buffer is false, this method will cause a run-time error.

<%Response.Buffer=true%>
<html><body>
<p>I write some text, but I will control when the
text will be sent to the browser.</p>
<p>The text is not sent yet. I hold it back!</p>
<p>OK, let it go!</p>
<%
Response.Flush
%>
</body></html>

Output:
I write some text, but I will control when the
text will be sent to the browser.

The text is not sent yet. I hold it back!

OK, let it go!


Example of flush and clear

<%Response.Buffer=true%>

<%

Resonse.Write(“Before clear and flush<br>")


Response.Flush

Resonse.Write(“After flush before clear")


response.Clear

Resonse.Write(“After clear and flush")

%>
</body></html>

Output
Before clear and flush
After clear and flush
Example of flush

<%Response.Buffer=true%>

<%
Dim a,b
Resonse.Write(“It is now“&now)
Resonse.Write(“<br>")

Response.Flush

For a=1 to 200000000


b=b+1
Next

Resonse.Write(“It is now“&now)

%>
</body></html>
End Method

• The End method stops processing a script, and returns the current result. The remaining contents of the file are
not processed. When the file reaches its end then contents are automatically send to Client there is no
need to call Response.End

<html>
<body>
<p>I am writing some text. This text will never be
<%
Response.End
%>
finished! It's too late to write more!</p>
</body>
</html>

Output:

I am writing some text. This text will never be


Example of End

<%Response.Buffer=true%>

<%

Resonse.Write(“Before End and flush<br>")


Response.Flush

Resonse.Write(“After flush before End")


response.End

Resonse.Write(“After End")

%>
</body></html>

Output:
Before End and flush
After flush before End
Redirect Method

The Redirect method redirects the user to a different URL.

Syntax
Response.Redirect URL

URL: Required. The URL that the user (browser) is redirected to

<%
Response.Redirect "https://www.w3schools.com"
%>

Write Method

The Write method writes a specified string to the output.

<%
Response.Write "Hello World"
%>

Output:

Hello World
Example:
<%
name="John"
Response.Write(name)
%>

Output:

John

Example
<%
Response.Write("Hello<br>World")
%>

Output:

Hello
World
AppendToLog Method

This method adds a string to the end of the Web server log entry for this request.

response.AppendToLog string

string Required. The text to append


to the log file (cannot contain
any comma characters)

Example:

<%Response.AppendToLog "My log message"%>

You might also like