ASP
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.
• 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
• 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
• 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.
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:
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:
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(" ")
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(" ")
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:
Eg;
Request.ServerVariables("remote_addr“)
Syntax:
Counter = Request.TotalBytes
Counter - Specifies a variable to receive the total number of bytes that the client sends in the request.
Example:
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).
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]
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]
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:
The IsClientConnected property indicates if the client has 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
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.
<%Response.Buffer=true%>
<%
%>
</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
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:
<%Response.Buffer=true%>
<%
Resonse.Write(“After End")
%>
</body></html>
Output:
Before End and flush
After flush before End
Redirect Method
Syntax
Response.Redirect URL
<%
Response.Redirect "https://www.w3schools.com"
%>
Write Method
<%
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
Example: