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?
Web browser is an application that interacts with user, takes request
from user regarding which HTML file user wants and makes a request to Web
Server. Once Web Server provides the
requested HTML file, browser displays the html document to user. There are only two popular Web Browsers –
Netscape Navigator ( from Sun Micro systems) and Internet Explorer (from
Microsoft). Web Browsers also supports Java. That means they can run program
written in Java, also called as Applets. They also supports scripting languages
such as VBScript and JavaScript.
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.
Static Pages
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.
Gateway Programming
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 with VBScript and JavaScript
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.
The most common scripting languages on the client are VBScript and
JavaScript.
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.
The script could be written in VBScript and JavaScript. But remember,
if you use VBScript you will be confined to Windows world. Because VBScript can
be run only by IIS and Personal Web Server (both from Microsoft). And the other servers hardly support
VBScript.
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.
Object 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.
ObjectContext 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 VBScript 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.
1. To start PWS use Programs -> Internet Explorer->
Personal Web Server -> Personal Web manager.
2. Then click on Start
button in Main options. This will start PWS, if it is not running.
Note: when PWS is running it displays an icon on taskbar.
To create a virtual directory:
1. Start Personal Web
Server manager by right clicking on it icon in taskbar and selecting properties
option.
Or
Select Programs -> Internet Explorer-> Personal Web Server ->
Personal Web manager.
The main screen of PWS is displayed.
2. Click on Advanced
icon at the bottom-left corner.
Advanced Options are displayed
3. click on Add button
in Virtual Directories group.
4. Add Directory
windows is displayed.
5. Select the directory
in which you want to place ASP files and give an alias to that directory
6. Turn on All
checkboxes in Access group.
7. 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.
<HTML>
<BODY>
CURRENT DATE AND TIME :
<%
DIM DT
DT = now
RESPONSE.WRITE(DT)
%>
</BODY>
</HTML>
The code that is enclosed in <% and %> is VBScript and run by Web
Server.
Save the file under name CURDT.ASP.
Remember that the extension must be ASP.
Running 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
The address is made up of the following.
http is the protocol used to
transmit the file
Localhost is the name
of the machine that is providing the file. LocalHost 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.
CURDT.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.
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.
<HTML>
<BODY>
<H2> SEARCH PAGE </H2>ENTER AUTHOR ID RANGE:
<FORM ACTION=GETAUTHORS.ASP
METHOD=post>
<INPUT NAME=STAUID>
<INPUT NAME=ENDAUID>
<INPUT TYPE=submit
VALUE="GET DETAILS">
</FORM>
</BODY>
</HTML>
Type this code in any text editor and save it under filename
AUTHORINPUT.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.
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 GETAUTHORS.ASP.
<html>
<body>
<h1>List of Authors </h1>
<hr>
<p>
<%
dim con
dim rs
DIM STAU_ID,ENDAU_ID
STAU_ID =
REQUEST.FORM("STAUID")
ENDAU_ID =
REQUEST.FORM("ENDAUID")
set con =
server.createobject("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>"
rs.movenext
loop
rs.close
con.close
set rs = nothing
set con = nothing
%>
<hr>
<p> End of details
</body>
Request.Form is used to take the values of form variables, STAU_ID and
ENDAU_ID.
Testing the application
1. 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
2. Enter 10 and 20 as staring author id and ending
author id.
3. Click on Get Details
button. This will invoke Active Server
Page, GETAUTHORS.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.
IIS application
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 IIS application is made up of a special object called as WebClass,
which in turn contains a collection of
webitems. Each webitem is a
procedure and these procedures determine what should happen when events occur
in webclass.
WebClass Object
A webclass 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.
A webclass typically contains webitems that it uses to provide content
to the browser and expose events.
A webitem can be one of two things:
An HTML Template Webitem
HTML template files are HTML pages that you associate with your
webclass. When the webclass 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 webclass can process before sending
the page to the browser. This allows you to customize your response.
A custom webitem
Custom webitems do not have an associated HTML page they can return to
the user. Instead, a custom webitem 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 webclass's webitems.
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
webclasses, which are, generated automatically when you create a webclass
project.
• One or more HTML
templates and their events.
• One or more custom
webitems and their events.
• An .asp (Active
Server Pages) file used to host the webclass in Internet Information Server
(IIS). The .asp is generated automatically when you create a webclass project,
with the name you specify in the NameInURL property.
• A webclass 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.
<html>
<body>
You can get information regarding any of the following.
<p>
<p><a href="songs.htm">Songs</a>
<p><a href="movies.htm">Movies</a>
</body>
if you run links.
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.
1. Start a new project
and select IIS Application as the type of project.
2. 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.
3. 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)
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
Links.html and click on Ok.
7. Change the name of
template to MainPage.
8. At this stage the
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"
End Sub
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 Webitem 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
MainPage and invoke popup menu using right mouse button. Then select Connect to webitem options. It displays
connect to webitem window as shown in
17. Select Custwebitem1
This connects hyperlink2 to custom webitem. As a result whenever user
selects hyperlink2, respond event of custom webitem will be fired.
18. Write code for
Respond event of custom webitem.
Private Sub Custwebitem1_Respond()
Response.Write "You have
selected Movies"
End Sub
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 webclass1.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 WebClass_Start()
'Write a reply to the user
With Response
.Write
"<html>"
.Write
"<body>"
.Write
"<h1><font face=""Arial"">WebClass1's
Starting
Page</font></h1>"
.Write
"<p>This response was created in the Start event of
WebClass1.</p>"
.Write
"</body>"
.Write
"</html>"
End With
End Sub
Now remove the total code and
enter the following code:
Private Sub WebClass_Start()
Set NextItem = MainPage
End Sub
NextItem determines which item is invoked as soon as Start event is
complete. As Mainpage is set, it causes Respond event of mainpage to occurs.
Write the following code in Respond event of mainpage.
Private Sub MainPage_Respond()
mainpage.WriteTemplate
End Sub
Test Run
Now run the application again. This time you should see the content
Links.html file. Because we have written the html template (mainpage) to
browser, which is equivalent to writing the entire content of the html template
file (links.html).
Click on Songs hypelink. Instead of invoking songs.html, it now runs
Mainpage_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 webitem. So Respond
event of custom webitem 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
No comments:
Post a Comment