Active Server Pages and IIS Application
What is Web Server
Web server is a simple application that sends the requested html document to web browser. When a request is received from an application (generally web browser), Web server will read the content of the named file and send the content to the requesting application. One of the popular Web Server applications is Internet Information Server (IIS) from Microsoft.
What is Web Browser
Developments in Internet Programming
Internet programming has come a long way since the days of static pages. So many new ways and means are available to handle Internet programming. Let us now understand briefly various developments that have taken place in internet programming in the past, just to get an idea regarding where we started and where we are going.
The first applications on the Internet consisted of static pages that delivered their content to the browser and did not react dynamically to any actions the user performed. Although this model provided access to nicely designed pages, it provided limited interaction between the user and the Web page. That means a static page could not respond to user actions.
Common Gateway Interface (CGI) is server side computing. In this, when browser is sends a request for an HTML page, the Web Server, instead of just sending the static content, runs script on the server and sends dynamic content (that is up-to-date information) to browser.
The script in CGI was generally written in a language called Perl.
There are a few disadvantages with gateway interface. The first of them is that the script is not integrated with HTML and is it difficult to create and maintain. Secondly each request to run the script on the server causes a separate process to be started on the server. This will increase the load on the server and it is also not possible for the process to make use of services of Web Server.
Scripting makes dynamic content possible by embedding scripts (instructions) directly into HTML page and browser can execute the script when it loads HTML page. Basically scripts provide dynamic content in place of static content.
Active Server Pages (ASP)
Asps allow you to write scripts and run them on the server before the html file is sent to browser. The scripts that you put in ASP need to do the following.
- Read the parameter information sent from browser
- Write HTML to browser.
Asps are, in one sense, files similar to HTML files but with the extension. ASP. And Asps are run in Web Server and the output is sent to Web Browser.
ASP concept was first introduced in Internet Information Server version 3. And Personal Web Server, which runs on Windows 98, also supports Asps.
ASP Object Model
Active Server Pages provide an object model that makes it easy for scripting languages to get information regarding parameters and write HTML to browser and do many more.
The following table lists the object in the object model of Active Server Pages.
What it does?
|Application||Allows you to share data between Active server pages of an application. It can also initialize and terminate applications.|
|Request||Allows you to read information provided by browser such as values of form fields, parameters etc.|
|Response||Lets you write HTML data back to browser for display.|
|Server||Allows you to create new object.|
|Session||Allows you to store and retrieve information regarding the current session.|
|Object Context||Allows you to control transactions if you are using Microsoft Transaction Server.|
Table 32.1: Object exposed by Active server pages.
Creating an ASP
Now, I want to show you how simple it is to create an ASP. But to run this Active server page successfully, you need to have either Internet Information Server on windows NT or Personal Web Server(PWS) on Windows 98.
The ASP that we are going to create will display current date and time when you run it. It use VB Script to do that. The following are the steps required to create and run ASP.
Creating a Virtual Directory
Install PWS (Personal Web Server) from Windows 98 CD and start it if it has not started.
- To start PWS use Programs -> Internet Explorer-> Personal Web Server -> Personal Web manager.
- Then click on Start button in Main This will start PWS, if it is not running.
Note: when PWS is running it displays an icon on task bar.
To create a virtual directory:
- Start Personal Web Server manager by right clicking on it icon in task bar and selecting properties option.
Select Programs -> Internet Explorer-> Personal Web Server -> Personal Web manager.
The main screen of PWS is displayed.
- Click on Advanced icon at the bottom-left corner.
Advanced Options are displayed as shown in figure 32.1.
Figure 32.1: Advanced options in Personal Web Server.
3. click on Add button in Virtual Directories
4. Add Directory windows is displayed. Please see figure 32.2.
5. Select the directory in which you want to place ASP files and give an alias to that directory
(as shown in figure 32.2)
Figure 32.2: Adding a new virtual directory to personal web server.
- Turn on All check boxes in Access
- Then click on OK.
Virtual directory, VB60, is added to the list of Virtual Directories. If you ever want to change the physical directory then select virtual directory and then click on Edit Properties button and change the physical directory.
Creating an ASP that display current date and time
Start Notepad and type the following code.
CURRENT DATE AND TIME :
DT = now
Listing 32.1: CURD.ASP to display current date and time.
The code that is enclosed in <% and %> is VB Script and run by Web Server.
Save the file under name CURD.ASP. Remember that the extension must be ASP.
Active server page is run in web browser. But the script that is embedded in it is run by web Server.
To run active server page:
Start it is shown in figure 32.3.
The address is made up of the following.
|HTTP||is the protocol used to transmit the file|
|Local host||is the name of the machine that is providing the file. Local Host is a special name, which refers to the current machine. If your ASP is on some other machine, use IP address or name of the machine.|
|Vb60||is the virtual directory that we have created.|
|CURD.ASP||is the name of the file that is to be executed.|
Developing an application using HTML pages and Active Server Pages
You have just got an idea about what is an Active Server Page. Now, let us delve more into it. We will develop a complete application that takes the range of author ids from user and displays the details of authors whose id is in the range.
The application contains one HTML page to take input from user and one Active server page that reads the data sent from Browser (Starting author id and Ending author id) and prints the details of authors using HTML.
Figure 32.3: Result of CURD asp in Internet Explorer.
Creating HTML page
The html page, used to take input from user, contains two text boxes and one submit button. If user enters starting id and ending id and clicks on submit button then HTML page invokes ASP.
Here is the code for HTML page.
<H2> SEARCH PAGE </H2>ENTER AUTHOR ID RANGE:
<FORM ACTION=GET AUTHORS.ASP METHOD=post>
<INPUT TYPE=submit VALUE=”GET DETAILS”>
Listing 32.2: AUTHOR INPUT.HTML.
Type this code in any text editor and save it under filename AUTHOR INPUT.HTML.
Action attribute of the Form tag specifies which program is to be invoked when user clicks on Submit button (Get Details button). The post option for Method attribute specifies that form values are to be posted to active sever page.
STAUID and ENDAUID are the names given to first and second text boxes respectively.
Figure 32.4: Search form displayed by author input.html.
Creating Active Server Page
The active server page takes the data that is posted to it and sends HTML page that contains the details of authors.
Here is the code for Active server page. Type the code in any text editor and save it under filename GET AUTHORS.ASP.
<h1>List of Authors </h1>
STAU_ID = REQUEST.FORM(“STAUID”)
ENDAU_ID = REQUEST.FORM(“ENDAUID”)
set con = server.create object(“adodb.connection”)
con.open “Provider=Microsoft.jet.oledb.3.51;data source=d:\vb60\biblio.mdb”
set rs = con.execute(“select * from authors where au_id between ” & stau_id & ” and ” & endau_id)
do until rs.eof
response.write rs(0) & “****” & rs(1) & “<br>”
set rs = nothing
set con = nothing
<p> End of details
Listing 32.3: Get Authors.asp
Request.Form is used to take the values of form variables, STAU_ID and ENDAU_ID.
Testing the application
- Run AUTHOR.HTML using Internet Explorer. When it is running, the page contains two text boxes, where user enter starting author id and ending author id ( figure 32.4).
- Enter 10 and 20 as staring author id and ending author id.
- Click on Get Details This will invoke Active Server Page, GET AUTHORS.ASP, which will retrieve the details of authors with ids in the range 10 to 20 and send them back to browser in the form of HTML page. See figure 32.5 for output.
Figure 32.5: Output of getauthors.asp
An IIS application uses HTML to present its user interface and uses compiled Visual Basic code to process requests and respond to events in the browser.
An II application is made up of a special object called as Web Class, which in turn contains a collection of web items. Each web-item is a procedure and these procedures determine what should happen when events occur in web class.
Web Class Object
A web class is a Visual Basic component that resides on a Web server and responds to input from the browser
Note:There is a one-to-one relationship between the Webclass Designer and the webclass. If you want to add additional webclasses to your application, you must add additional designers.
Each Webclass in an IIS application has an associated .asp (Active Server Pages) file that Visual Basic generates automatically during the compile or debug process. The .ASP file hosts the webclass on the Web server.
Figure 32.6: Relationship Between .asp Files and Web Classes
A web class typically contains web items that it uses to provide content to the browser and expose events.
A web item can be one of two things:
An HTML Template Web item
HTML template files are HTML pages that you associate with your web class. When the web class receives a request, it can send the HTML pages to the browser for display. Templates differ from regular HTML pages only in that they often contain replacement areas the web class can process before sending the page to the browser. This allows you to customize your response.
A custom web-item
Custom web items do not have an associated HTML page they can return to the user. Instead, a custom web item is a programmatic resource that consists of one or more event handler. These event handlers are called from the browser, either when the page loads or when a user selects an HTML element. The event handlers can generate a response to the browser or pass processing to another event handler of the web class’s web items.
Structure of IIS Application
An IIS application consists of the following pieces. Many of these are generated for you automatically when you build your project. The pieces include:
- One or more web classes, which are, generated automatically when you create a web class project.
- One or more HTML templates and their events.
- One or more custom web items and their events.
- An .asp (Active Server Pages) file used to host the web class in Internet Information Server (IIS). The .asp is generated automatically when you create a web class project, with the name you specify in the Name InURL
- A web class run-time component, MSWCRUN.DLL, that helps process requests.
- A project DLL (generated automatically on compile) that contains your Visual Basic code and is accessed by the run-time component.
Sample IIS Application
We have seen quite a bit of IIS applications. It is time to put that to work. Until now we have understood what is what. But plain theory is of no use. So let us see how to use ASP to solve a real requirement.
Let us assume you have an HTML page with two hyper links. You want to take control when user clicks on these hyperlinks and send HTML text to browser. That means when user clicks on first hyperlink, it should fire an event in your application and that event executes the required code.
Here is the HTML file. It is stored under name links.html.
You can get information regarding any of the following.
Listing 32.4: linkes.html.
if you run links.html, it appears as shown in figure 32.7.
Figure 32.7: Links.html when run in Internet Explorer.
Here when user clicks on Songs, you want to execute some other code other than invoking the specified file. And the same is the case with Movies hyperlink also.
Here are the steps in creating the sample application.
- Start a new project and select IIS Application as the type of project.
- Invoke project explorer using Ctrl + R. And you see just one WebClass and nothing else. Because IIS application by default contains only one web class.
Whenever you deal with IIS application the first thing that you have to do is, save the project.
- Save the project using File-> Save project. Enter iisdemo as the name of the project and wcdemo for webclass. The default extension for Webclass file is .dsr.(designer)
Figure 32.8: IIS application in project explorer.
4. Double click on WebClass1 in project explorer.
This will open up designer and it displays two nodes under WebClass in tree view on left. One is for HTML template webitems and another one is for custom webitems
5. Select HTML template webitems node and click on right button to invoke popup menu.
Visual Basic displays Add HTML Template window.
6. Select html and click on Ok.
7. Change the name of template to MainPage.
8. At this stage the designer looks as shown in figure 32.9.
Figure 32.9: WebClass in WebClass designer.
9. Double click on Hyperlink1 on the right pane of designer.
10. This will invoke code window and event procedure MainPage_HyperLink1. This event procedure is executed whenever user click on hyperlink1.
11. Write the following code.
Private Sub MainPage_Hyperlink1()
Response.Write “You have selected Songs”
Listing 32.5: Code to intercept selection of Hyperlink
12. Now select custom webitems and click on right button.
13. From popup menu select Add Custom webitem option to create a customer web item.
14. Change name of the custom Web item to Custwebitem1.
15. Remember custom web item is like a general procedure. It is not executed until it is connected to some event.
16. Select Hyperlink2 of Main Page and invoke popup menu using right mouse button. Then select Connect to web item It displays connect to web item window as shown in figure 32.10.
17. Select Custwebitem1 as shown in figure 32.10, and click on Ok.
Figure 32.10: Connect to Web Item
This connects hyperlink2 to custom web-item. As a result whenever user selects hyperlink2, respond event of custom web item will be fired.
Now designer should look like figure 32.11.
Figure 32.11: Web Class designer after custom web item.
18. Write code for Respond event of custom web item.
Private Sub Custwebitem1_Respond()
Response.Write “You have selected Movies”
Listing 32.6: Respond event of custom web item
19. If you run your application now, it will ask you to specify the Start Component, which is webclass1.
For all other prompt, you accept the defaults.
20. Application Invokes asp in Internet Explorer and displays the HTML text that is passed to browser from Start event of Webclass1.
21. Here is the default code in Start event of Webclass1.
Private Sub Web Class_Start()
‘Write a reply to the user
.Write “<h1><font face=””Arial””>WebClass1’s Starting Page</font></h1>”
.Write “<p>This response was created in the Start event of WebClass1.</p>”
Listing 32.7: Default code for Web Class_Start event.
Now remove the total code and enter the following code:
Private Sub Web Class_Start()
Set Next Item = Main Page
Listing 32.8: Start event of web class.
Next Item determines which item is invoked as soon as Start event is complete. As Main page is set, it causes Respond event of main page to occurs. Write the following code in Respond event of main page.
Private Sub MainPage_Respond()
main page.Write Template
Listing 32.9: Respond event.
Now run the application again. This time you should see the content Links.html file. Because we have written the html template (main page) to browser, which is equivalent to writing the entire content of the html template file (links.html).
Click on Songs hyperlink. Instead of invoking songs.html, it now runs Main page_Hyperlink1 event in your application. As the result whatever you send using write method of Response object that is sent to browser.
Click on Movies hyperlink. This will display “You have selected movies” message because you connected this hyper link to custom web item. So Respond event of custom web item occurs, which send html text to browser.
Remember, that IIS application and its component reside on server. Only the events and responses move between browser and server