Sunday, 7 July 2013

ASP Tutorial


ASP Tutorial

What is ASP?


Ø  ASP stands for Active Server Pages

Ø  ASP is a program that runs inside IIS

Ø  IIS stands for Internet Information Services

Ø  IIS comes as a free component with Windows 2000

Ø  IIS is also a part of the Windows NT 4.0 Option Pack

Ø  The Option Pack can be downloaded from Microsoft

Ø  PWS is a smaller - but fully functional - version of IIS

Ø  PWS can be found on your Windows 95/98 CD

ASP Compatibility


Ø  ASP is a Microsoft Technology

Ø  To run IIS you must have Windows NT 4.0 or later

Ø  To run PWS you must have Windows 95 or later

Ø  ChiliASP is a technology that runs ASP without Windows OS

Ø  InstantASP is another technology that runs ASP without Windows

What is an ASP File?


Ø  An ASP file is just the same as an HTML file

Ø  An ASP file can contain text, HTML, XML, and scripts

Ø  Scripts in an ASP file are executed on the server

Ø  An ASP file has the file extension ".asp"

How Does ASP Differ from HTML?


Ø  When a browser requests an HTML file, the server returns the file

Ø  When a browser requests an ASP file, IIS passes the request to the ASP engine. The ASP engine reads the ASP file, line by line, and executes the scripts in the file. Finally, the ASP file is returned to the browser as plain HTML



What can ASP do for you?


Ø  Dynamically edit, change or add any content of a Web page

Ø  Respond to user queries or data submitted from HTML forms

Ø  Access any data or databases and return the results to a browser

Ø  Customize a Web page to make it more useful for individual users

Ø  The advantages of using ASP instead of CGI and Perl, are those of simplicity and speed

Ø  Provide security since your ASP code can not be viewed from the browser

Ø  Clever ASP programming can minimize the network traffic

Important: Because the scripts are executed on the server, the browser that displays the ASP file does not need to support scripting at all!

How to install IIS and run ASP on Windows 2000


  1. From your Start Button, go to Settings, and Control Panel
  2. In the Control Panel window select Add/Remove Programs
  3. In the Add/Remove window select Add/Remove Windows Components
  4. In the Wizard window check Internet Information Services, click OK
  5. An Inetpub folder will be created on your harddrive
  6. Open the Inetpub folder, and find a folder named wwwroot
  7. Create a new folder, like "MyWeb", under wwwroot.
  8. Use a text editor to write some ASP code, save the file as "test1.asp" in the "MyWeb" folder
  9. Make sure your Web server is running - The installation program has added a new icon on your task bar (this is the IIS symbol). Click on the icon and press the Start button in the window that appears.
  10. Open your browser and type in "http://localhost/MyWeb/test1.asp", to view your first ASP page

ASP Variables


A variable is used to store information.

If the variable is declared outside a procedure it can be changed by any script in the ASP file. If the variable is declared inside a procedure, it is created and destroyed every time the procedure is executed.

Lifetime of Variables


A variable declared outside a procedure can be accessed and changed by any script in the ASP file.

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

To declare variables accessible to more than one ASP file, declare them as session variables or application variables.

 

 

Session Variables

Session variables are used to store information about ONE single user, and are available to all pages in one application. Typically information stored in session variables are name, id, and preferences.

Application Variables


Application variables are also available to all pages in one application. Application variables are used to store information about ALL users in a specific application.

ASP Forms and User Input


The Request.QueryString and Request.Form commands may be used to retrieve information from forms, like user input.

User Input


The Request object may be used to retrieve user information from forms.

Form example:

<form method="get" action="simpleform.asp">
First Name: <input type="text" name="fname" />
<br />
Last Name: <input type="text" name="lname" />
<br /><br />
<input type="submit" value="Submit" />
</form>

Request.QueryString


The Request.QueryString command is used to collect values in a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on 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:

<body>
Welcome 
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
</body>

Request.Form


The Request.Form command is used to collect values in a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on 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:

<body>
Welcome 
<%
response.write(request.form("fname"))
response.write(" " & request.form("lname"))
%>
</body>

ASP Cookies


What is a Cookie?


A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.



How to Create a Cookie?


The "Response.Cookies" command is used to create cookies.

Note: The Response.Cookies command must appear BEFORE the <html> tag.

In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:

<%
Response.Cookies("firstname")="Alex"
%>
<%
Response.Cookies("firstname")="Alex" 
Response.Cookies("firstname").Expires=#May 10,2002#
%>

 



How to Retrieve a Cookie Value?


The "Request.Cookies" command is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>


Output:

Firstname=Alex

ASP Session Object


The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application.



The Session object


When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the client and it contains information that identifies the user. This interface is called the Session object.

The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires.



When does a Session Start?


A session starts when:

  • A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure
  • A value is stored in a Session variable
  • A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope



When does a Session End?


A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes.

If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property.

The example below sets a timeout interval of 5 minutes:

<%
Session.Timeout=5
%>

To end a session immediately, you may use the Abandon method:

<%
Session.Abandon
%>

Note: The main problem with sessions is WHEN they should end. We do not know if the user's last request was the final one or not. So we do not know how long we should keep the session "alive". Waiting too long for an idle session uses up resources on the server, but if the session is deleted too soon the user has to start all over again because the server has deleted all the information. Finding the right timeout interval can be difficult!

Tip: If you are using session variables, store SMALL amounts of data in them.



 


 

Store and Retrieve Session Variables


The most important thing about the Session object is that you can store variables in it.

The example below will set the Session variable username to "Donald Duck" and the Session variable age to "50":

<%
Session("username")="Donald Duck"
Session("age")=50
%>

When the value is stored in a session variable it can be reached from ANY page in the ASP application:

Welcome <%Response.Write(Session("username"))%>

The line above returns: "Welcome Donald Duck".

You can also store user preferences in the Session object, and then access that preference to choose what page to return to the user. 

The example below specifies a text-only version of the page if the user has a low screen resolution:

<%If Session("screenres")="low" Then%> 
  This is the text version of the page
<%Else%> 
  This is the multimedia version of the page
<%End If%>

 



Remove Session Variables


The Contents collection contains all session variables.

It is possible to remove a session variable with the Remove method.

The example below removes the session variable "sale" if the value of the session variable "age" is lower than 18:

<%
If Session.Contents("age")<18 then 
  Session.Contents.Remove("sale")
End If 
%>

To remove all variables in a session, use the RemoveAll method:

<%
Session.Contents.RemoveAll()
%>

 



Loop Through the Contents Collection


The Contents collection contains all session variables. You can loop through the Contents collection, to see what's stored in it:

<%
Session("username")="Donald Duck"
Session("age")=50
dim i
For Each i in Session.Contents
  Response.Write(i & "<br />")
Next
%>

Result:

username
age

If you do not know the number of items in the Contents collection, you can use the Count property:

<%
dim i
dim j
j=Session.Contents.Count
Response.Write("Session variables: " & j)
For i=1 to j
  Response.Write(Session.Contents(i) & "<br />")
Next
%>

Result:

Session variables: 2
Donald Duck
50



Loop Through the StaticObjects Collection


You can loop through the StaticObjects collection, to see the values of all objects stored in the Session object:

<%
dim i
For Each i in Session.StaticObjects
  Response.Write(i & "<br />")
Next
%>

ASP Response Object


Response Object


The ASP Response object is used to send output to the user from the server. Its collections, properties, and methods are described below:

Methods


Method
Description
AddHeader
Adds a new HTTP header and a value to the HTTP response
Adds a string to the end of the server log entry
Writes data directly to the output without any character conversion
Clear
Clears any buffered HTML output
End
Stops processing a script, and returns the current result
Flush
Sends buffered HTML output immediately
Redirect
Redirects the user to a different URL
Write
Writes a specified string to the output

 

No comments:

Post a Comment