ACTIVE SERVER PAGES!
ASP is a technology from Microsoft for writing server-side scripts that run on Web Servers like IIS and Apache. It is implemented as a DLL Server by name ASP.DLL and acts as an engine that can execute scripts written using scripting languages like VBScript and JavaScript. ASP engine is installed and run as part of the Web Server.
ASP is mainly used for creating dynamic web pages using Server-side scripts. Dynamic web pages can get the user input through the browser and then send a request to the Server to run server-side scripts that create the required content of the web page dynamically. ASP provides a set of components for writing the server-side scripts that run on the server.
Components of an ASP file:
An ASP file is similar to a standard HTML file that contains HTML tags along with some static text for presenting its content on the web browser. In addition, it can include other components such as scripts, built-in objects and server-side components.
Scripts: Scripts are code written using scripting languages such as VBScript and JavaScript.
Built-in Objects: Built-in objects are special objects provided in scripting languages such as VBScript/JavaScript for writing server-side scripts. They are used for Browser-Server interaction and to perform certain tasks that can be accomplished only on Server.
Examples of such objects are Request and Response. The object Request gets the data from the client and the object Response returns information to the client.
Server-side Components: These components are ActiveX components such as ADO, and are installed and run on the server in order to perform some special tasks such as connecting ASP to a database.
ASP files are saved with the extension .asp and are executed by a Web Server. During execution, they are converted into .html files containing required information and then sent back to the browser for display. This process of executing an ASP file is shown in the following figure:
Fig.: Processing an ASP file
The advantages of using ASP scripts are as follows:
Issues of browser incompatibility do not affect the processing of scripts
Users can’t view or copy ASP scripts
Rules for writing ASP code:
All ASP script commands are generally enclosed within script delimiters <% and %>. The scripts written within these delimiters, are processed by the Web Server.
Delimiters can also include any command that is valid for the scripting language specified in the code.
ASP scripts can be written using scripting languages such as VBScript and JavaScript. By default, ASP uses VBScript for server-side scripting. To change the language from VBScript to JavaScript, use the ASP processing directive @ LANGUAGE, which specifies the language for the scripts contained in a specific ASP file. The syntax is as follows:
<% @ language = JavaScript %>
The @ symbol in the above line specifies that the following is a script directive and not a script. The @ symbol is separated from the language keyword by a space character. Following is an example for ASP code written using JavaScript:
<html>
<head><title> Example ASP code using JavaScript </title>
</head>
<body>
JavaScript being called : <br>
<% call Msg %>
</body>
</html>
<script language = “JavaScript” runat = “server”>
function Msg()
{
response.write(“A message from Server”);
}
</script>
Program: ASP written using JavaScript for displaying a message
ASP Objects:
ASP uses built-in objects which are part of the scripting languages for communication with the browser, gathering data sent by an HTTP request and distinguishing between users. Two commonly used such objects are Request and Response.
The Request object is commonly used to access the information passed by a get or post request. This information usually consists of data provided by the user in an HTML form. It also provides access to information such as “cookies” that are available on the client.
The Response object sends information in the form of HTML text to the client. The third object – Server object provides access to methods and properties that relate to the server. These commonly used ASP objects are listed below with their description:
Object Name
|
Description
|
Request
|
Used to access information passed by an HTTP Request
|
Response
|
Used to control the information sent to the client.
|
Server
|
Used to access methods and properties related to the server.
|
Table: ASP Built-in Objects with their description
A simple ASP program for displaying server’s data and time on the client computer is given below:
<% @ language = VBScript %>
<% Option Explicit %>
<html>
<head><title>Date and Time on the Server </title>
<style type = “txt / css”>
t = {background-color : black; color: yellow }
srong = {font-family : arial, sans-serif;
Font-size : 14 pt; color : blue}
p {font-size : 14pt}
</style>
</head>
<body>
<p><strong>A Simple ASP Example</strong></p>
<table border = “6”>
<tr>
<td>
<% = FormatDateTime(Now, vbLongDate) %>
</td>
<td>
<% = Time() %>
</td>
</tr>
<table>
</body>
</html>
In the above example, FormatDateTime() is a VBScript function that returns a string formatted on the Date and Time given as its first parameter. Now is a VBScript function to get the current date, which is of the server. There is one more function used – Time() to get the current time on the server. It returns the time in the format: hh:mm:ss.
At the time of processing the above ASP file, the two lines of ASP code will be replaced with the static text as follows:
The statement
<% = FormatDateTime(Now, vbLongDate) %>
which is equivalent to
<% Call response.write(FormatDateTime(Now, vbLongDate)) %>
will be processed and replaced as:
Tuesday, February 29, 2008
Similarly, the code
<% = Time() %>
which is equivalent to
<% Call response.write(Time()) %>
will be replaced with a text as follows:
10:30:35 AM
ASP code can get the user input from the browser through the Request object. Then the input will be processed and the resultant web page will be sent back to the client for output. For e.g., an order information is entered into a form and then sent to the server for processing. Once the information is received, the server processes the input by executing some ASP code and then generates a web page containing list of items ordered and other details.
The following is an example ASP code that gets the user name and sends it to the server name.asp for generating a dynamic web page containing user name and a welcome message:
<html>
<head><title>ASP Client getting User Name</title>
</head>
<body>
<p style = “font-family: arial, sans-serif”>
Enter Your Name:
</p>
<form action = “name.asp” method = “post”>
<input type = “text” name = “namebox” size = “20”/>
<input type=“submit” name=“Submit” value=“Enter”/>
</form>
</body>
</html>
Program: Welcome.html that gets the user name for name.asp
<html>
<head><title>A Welcome Message</title>
</head>
<body>
<p> Hi <%= Request(“namebox) %> </p> <br/>
<p>Welcome to ASP!</P>
</body>
</html>
Program: Name.asp containing server-script for dynamic web page