Search This Blog

Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts
Tuesday, 1 November 2011

Managing Client information with Cookies

0 comments
  COOKIES IN ASP! 
Cookies are bits of information saved on a client computer for later use of the Web Server in the same browsing or in the future browsing session. For e.g., cookies could be used in a shopping application to keep track of the client’s shopping cart items.
Cookies are small files sent by an ASP as part of a response to a client. Every HTTP-based interaction between a client and a server includes a header that contains the information about the request or the response. When an ASP receives a request, the header includes the request type (e.g., post or get) and cookies stored on the client machine by the server. When the server formulates its response, the header information includes any cookies the server wants to store on the client computer.
Cookies are stored and retrieved from the client’s machine using Response and Request objects of the server ASP. The Cookies collection of the Request object is used to retrieve the values of the cookies sent in the HTTP request and the cookies collection of the Response object is used to set the value of a cookie.
The life time of a cookie is determined by its creator, the developer of the web site. When the specified time is over, the cookie deletes itself from the client. Depending on the maximum age of a cookie, the web browser either maintains the cookie for the duration of the browser session (i.e., until the user closes the web browser) or stores the cookie on the client computer for future use.
Some clients do not allow cookies to be written on their machine. A refusal to accept cookies may prevent the client from being able to use the web site that attempted to write the cookie. The following is an example for storing and retrieving from the client machine by a server:

Program: Client.html that gets input form the User to be stored in  Cookies


Program: Cookies.asp that stores and retries Cookies from the Client

Continue reading →

Session Handling in ASP

0 comments
SESSION TRACKING IN ASP! 
Handling Session and Cookies are two different ways in which Web Server maintains information about individual clients to distinguish between them. Many web sites provide custom web pages or functionality on a client-by-client basis. For e.g., some web sites (e.g., yahoo) allow the users to customize their home page to suit their needs.
Another example of a service that is customized on a client-by-client basis is a shopping cart for shopping on the web. When a purchase is made, the server must distinguish between clients so the business can assign the proper items and charge each client the proper amount.
A third method of customization on a client-by-client basis is marketing to specific audiences. Companies often track the pages people visit so they can display advertisement based on a person’s browsing trends.
Session Tracking:
A Session is nothing but the duration of a web site in which it maintains its client’s information for the purpose of customizing its web pages for individual clients. In ASP, a session is an object maintained by the server to store its client’s information that distinguishes clients individually.
The server performs session tracking by creating a session object when a specific person visits a site. When he visits the web site the first time, the server assigns the user a unique Session ID. When the client makes additional requests to navigate to other pages of the web site, the client’s Session ID is used to keep track of him individually.
A session can be terminated in two ways: One way is to set the TimeOut property of the session object, which specifies the number of minutes that a session exists before it expires. The default value for this property is 20 minutes. Another way of terminating a session is by calling the session method Abandon.
Session Object’s Variables:
A Session object is usually used to store user preferences such as shopping habits. For e.g., if a user enters the user name on the first page on a web site, the session object may be used to store the name of the user for the rest of the session. This information is saved in a variable known as a Session Variable. Session variables continue to exist till the web server destroys the session object when the session is terminated or timed out.
The syntax to create a session variable and store a value in it is as follows:
Session (“SessionVariableName”) = value
The syntax to read a session variable is as follows:
          value = Session (“SessionVariableName”)
The following is an example which gets the user name from the user in a text box (Session1.asp) and then stores it in a session variable named uname (Session2.asp) that would be used by another web page generated by Session3.asp.

Program: Session1.asp that gets the user input

Program: Session2.asp that creates a Session variable “uname”

Program: Session3.asp that creates a dynamic web page having User Name

Continue reading →
Friday, 28 October 2011

Introduction to Active Server Pages!

1 comments
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:
  1. Issues of browser incompatibility do not affect the processing of scripts
  2. Users can’t view or copy ASP scripts
Rules for writing ASP code:
  1. All ASP script commands are generally enclosed within script delimiters <% and %>. The scripts written within these delimiters, are processed by the Web Server.
  2. Delimiters can also include any command that is valid for the scripting language specified in the code.
  3. 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
Continue reading →