0% found this document useful (0 votes)
319 views122 pages

Computer Networks Data Packet Switching Internet Protocol

Uploaded by

nithyahimalini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views122 pages

Computer Networks Data Packet Switching Internet Protocol

Uploaded by

nithyahimalini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 122

Unit-1

Internet principles

The Internet (also known simply as the Net) is the worldwide, publicly accessible system of
interconnected computer networks that transmit data by packet switching using the standard Internet
Protocol (IP).

It consists of millions of smaller domestic, academic, business, and government networks, which together
carry various information and services, such as electronic mail, online chat, and the interlinked Web pages
and other documents of the World Wide Web.

Contrary to some common usage, the Internet and the World Wide Web are not synonymous: the Internet
is a collection of interconnected computer networks, linked by copper wires, fiber-optic cables, wireless
connections etc.; the Web is a collection of interconnected documents, linked by hyperlinks and URLs,
and is accessible using the Internet. The Internet also provides many other services including e-mail, file
sharing and others described below.

History of the Internet and World Wide Web

In the late 1960s, one of the authors (HMD) was a graduate student at MIT. His research
at MIT’s Project MAC (now the Laboratory for Computer Science—the home of the World Wide Web
Consortium) was funded by ARPA—the Advanced Research Projects Agency of the Department of
Defense. ARPA sponsored a conference at which several dozen ARPA-funded graduate students were
brought together at the University of Illinois at Urbana-Champaign to meet and share ideas. During this
conference, ARPA rolled out the blueprints for networking the main computer systems of about a dozen
ARPA-funded universities and research institutions. They were to be connected with communications
lines operating at a then-stunning 56 Kbps (i.e., 56,000 bits per second)—this at a time when most people
(of the few who could) were connecting over telephone lines to computers at a rate of 110 bits per second.
There was great excitement at the conference. Researchers at Harvard talked about communicating with
the Univac 1108 “supercomputer” at the University of Utah to handle calculations related to their
computer graphics research. Many other intriguing possibilities were raised. Academic research about to
take a giant leap forward. Shortly after this conference, ARPA proceeded to implement the ARPANET,
which eventually evolved into today’s Internet.

Things worked out differently from what was originally planned. Rather than enabling researchers
to share each other’s computers, it rapidly became clear that enabling researchers to communicate quickly
and easily via what became known as electronic mail (e-mail, for short) was the key early benefit of the
ARPANET. This is true even today on the Internet, as e-mail facilitates communications of all kinds
among a billion people worldwide.

One of the primary goals for ARPANET was to allow multiple users to send and receive
information simultaneously over the same communications paths (e.g., phone lines). The network
operated with a technique called packet switching, in which digital data was sent in small bundles called
packets. The packets contained address, error-control and sequencing information. The address
information allowed packets to be routed to their destinations. The sequencing information helped in
reassembling the packets— which, because of complex routing mechanisms, could actually arrive out of
order—into their original order for presentation to the recipient. Packets from different senders were
intermixed on the same lines. This packet-switching technique greatly reduced transmission costs, as
compared with the cost of dedicated communications lines.
The network was designed to operate without centralized control. If a portion of the network failed, the
remaining working portions would still route packets from senders to receivers over alternative paths for
reliability.

The protocol for communicating over the ARPANET became known as TCP—the Transmission
Control Protocol. TCP ensured that messages were properly routed from sender to receiver and that they
arrived intact.

As the Internet evolved, organizations worldwide were implementing their own networks for both
interorganization (i.e., within the organization) and interorganization (i.e., between organizations)
communications. A wide variety of networking hardware and software appeared. One challenge was to get
these different networks to communicate. ARPA accomplished this with the development of IP—the
Internet Protocol, truly creating a “network of networks,” the current architecture of the Internet. The
combined set of protocols is now commonly called TCP/IP.

Initially, Internet use was limited to universities and research institutions; then the military began
using the Internet. Eventually, the government decided to allow access to the Internet for commercial
purposes. Initially, there was resentment in the research and military communities—these groups were
concerned that response times would become poor as “the Net” became saturated with users.

In fact, the exact opposite has occurred. Businesses rapidly realized that they could tune their
operations and offer new and better services to their clients, so they started spending vast amounts of
money to develop and enhance the Internet. This generated fierce competition among communications
carriers and hardware and software suppliers to meet this demand. The result is that bandwidth (i.e., the
information-carrying capacity) on the Internet has increased tremendously and costs have decreased
significantly.

The World Wide Web allows computer users to locate and view multimedia-based documents on
almost any subject over the Internet. Though the Internet was developed decades ago, the web is a
relatively recent creation. In 1989, Tim Berners-Lee of CERN (the European Organization for Nuclear
Research) began to develop a technology for sharing information via hyperlinked text documents.
Berners-Lee called his invention the Hyper Text Markup Language (HTML). He also wrote
communication protocols to form the backbone of his new information system, which he called the World
Wide Web.

In particular, he wrote the Hypertext Transfer Protocol (HTTP)—a communications protocol


used to send information over the web. Web use exploded with the availability in 1993 of the Mosaic
browser, which featured a user-friendly graphical interface. Marc Andreessen, whose team at NCSA
developed Mosaic, went on to found Netscape, the company that many people credit with initiating the
explosive Internet economy of the late 1990s.

In the past, most computer applications ran on computers that were not connected to one another,
whereas today’s applications can be written to communicate among the world’s computers. The Internet
mixes computing and communications technologies. It makes our work easier. It makes information
instantly and conveniently accessible worldwide.
It enables individuals and small businesses to get worldwide exposure. It is changing the way business
is done. People can search for the best prices on virtually any product or service. Special-interest
communities can stay in touch with one another. Researchers can be made instantly aware of the latest
breakthroughs. The Internet and the web are surely among humankind’s most profound creations.

HTML elements

Some Fundamental HTML Elements


HTML Element Syntax

 An HTML element starts with a start tag / opening tag


 An HTML element ends with an end tag / closing tag
 The element content is everything between the start and the end tag
 Some HTML elements have empty content
 Empty elements are closed in the start tag
 Most HTML elements can have attributes

ELEMENTS FOR THE HEAD SECTION

The HEAD section of a document may only contain the following elements. If any other elements, or text,
occur inside the HEAD section, the browser should assume the HEAD ends here, and start rendering the
BODY. See the syntax rules for an explanation of the syntax used in the overview.
 TITLE - Document title
 ISINDEX - Primitive search
 META - Meta-information
 LINK - Site structure
 BASE - Document location
 SCRIPT - Inline script
 STYLE - Style information

ELEMENTS FOR THE BODY SECTION

Block-level elements
The BODY of a document consists of multiple block elements. If plain text is found inside the
body, it is assumed to be inside a paragraph P. See the syntax rules for an explanation of the syntax
used in the overview.

Headings Lists
 H1 - Level 1 header  UL - Unordered list
 H2 - Level 2 header  OL - Ordered list
 H3 - Level 3 header  DIR - Directory list
 H4 - Level 4 header  MENU - Menu item list
 H5 - Level 5 header  LI - List item
 DL - Definition list
 H6 - Level 6 header o DT - Definition term
o DD- Definition

Text containers Others


 P - Paragraph  DIV - Logical division
 PRE - Preformatted text  CENTER - Centered division
 BLOCKQUOTE - Large 
quotation  FORM - Input form
 HR - Horizontal rule
 ADDRESS - Address
information  TABLE - Tables
Text-level elements
These elements are used to mark up text inside block level elements. Some block level elements
exclude certain text level elements, and some text level elements may only appear inside specific
block level elements. This is documented in the section on that block level element.
Logical markup Physical markup
 EM - Emphasized text  TT - Teletype
 STRONG - Strongly emphasized  I - Italics
 DFN - Definition of a term  B - Bold
 CODE - Code fragment  U - Underline
 SAMP - Sample text  STRIKE - Strikeout
 KBD - Keyboard input  BIG - Larger text
 VAR - Variable  SMALL - Smaller text
 SUB - Subscript
 CITE - Short citation
 SUP - Superscript
Special markup Forms
 A - Anchor  INPUT - Input field, button, etc.
 BASEFONT - Default font size  SELECT - Selection list
 IMG - Image  OPTION - Selection list option
 APPLET - Java applet
 PARAM- Parameters for Java applet  TEXTAREA - Input area
 FONT - Font modification Tables
 BR - Line break  CAPTION - Table caption
 TR - Table row
 MAP - Client-side image map  TH - Header cell
 TD - Table cell

 ADDRESS - Address information  INPUT - Input field, button, etc.


 APPLET - Java applet  ISINDEX - Primitive search
 AREA - Hotzone in image map  I - Italics
 A - Anchor  KBD - Keyboard input
 BASE - Document location  LINK - Site structure
 BASEFONT - Default font size  LI - List item
 BIG - Larger text  MAP - Client-side image map

 BLOCKQUOTE - Large quotation  MENU - Menu item list


 BODY - Document body  META - Meta-information
 BR - Line break  OL - Ordered list
 B - Bold  OPTION - Selection list option
 CAPTION - Table caption  PARAM - Parameter for Java applet
 CENTER - Centered division  PRE - Preformatted text
 CITE - Short citation  P - Paragraph
 CODE - Code fragment  SAMP - Sample text
 DD - Definition  SCRIPT - Inline script
 DFN - Definition of a term  SELECT - Selection list
 DIR - Directory list  SMALL - Smaller text
 DIV - Logical division  STRIKE - Strikeout
 DL - Definition list  STRONG - Strongly emphasized
 DT - Definition term  STYLE - Style information
 EM - Emphasized text  SUB - Subscript
 SUP - Superscript
 FONT - Font modification
 TABLE - Tables
 FORM - Input form
 TD - Table cell
 H1 - Level 1 header
 TEXTAREA - Input area
 H2 - Level 2 header
 TH - Header cell
 H3 - Level 3 header
 TITLE - Document title
 H4 - Level 4 header
 TR - Table row
 H5 - Level 5 header
 TT - Teletype
 H6 - Level 6 header
 UL - Unordered list
 HEAD - Document head
 U - Underline
 HR - Horizontal rule
 HTML - HTML Document
 VAR - Variable

Lists
An unordered list:
An ordered list:  List item
1. The first list item  List item
2. The second list item
 List item
3. The third list item

HTML Unordered Lists

An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.

The list items are marked with bullets (typically small black circles).

<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>

How the HTML code above looks in a browser:

 Coffee
 Milk

HTML Ordered Lists

An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.

The list items are marked with numbers.

<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>

How the HTML code above looks in a browser:

1. Coffee
2. Milk

HTML Definition Lists

A definition list is a list of items, with a description of each item.

The <dl> tag defines a definition list.

The <dl> tag is used in conjunction with <dt> (defines the item in the list) and <dd> (describes the item in
the list):

<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>

How the HTML code above looks in a browser:

Coffee
- black hot drink
Milk
- white cold drink

HTML List Tags

Tag Description
<ol> Defines an ordered list
<ul> Defines an unordered list
<li> Defines a list item
<dl> Defines a definition list
<dt> Defines an item in a definition list
<dd> Defines a description of an item in a definition list

HTML Tables

Tables are defined with the <table> tag.


A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag).
td stands for "table data," and holds the content of a data cell. A <td> tag can contain text, links, images,
lists, forms, other tables, etc.

Table Example

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

How the HTML code above looks in a browser:

row 1, cell 1 row 1, cell 2


row 2, cell 1 row 2, cell 2

HTML Tables and the Border Attribute

If you do not specify a border attribute, the table will be displayed without borders. Sometimes this can be
useful, but most of the time, we want the borders to show.

To display a table with borders, specify the border attribute:

<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>

HTML Table Headers

Header information in a table are defined with the <th> tag.

The text in a th element will be bold and centered.

<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr> </table>

How the HTML code above looks in a browser:

Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

HTML Table Tags

Tag Description
<table> Defines a table
<th> Defines a table header
<tr> Defines a table row
<td> Defines a table cell
<caption> Defines a table caption
<colgroup> Defines a group of columns in a table, for formatting
<col /> Defines attribute values for one or more columns in a table
<thead> Groups the header content in a table
<tbody> Groups the body content in a table
<tfoot> Groups the footer content in a table

HTML Forms

HTML forms are used to pass data to a server.

A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A
form can also contain select lists, text area, field set, legend, and label elements.

The <form> tag is used to create an HTML form:

<form>
.
input elements
.
</form>

HTML Forms - The Input Element

The most important form element is the input element.


The input element is used to select user information.

An input element can vary in many ways, depending on the type attribute. An input element can be of type
text field, checkbox, password, radio button, submit button, and more.

The most used input types are described below.

Text Fields

<input type="text" /> defines a one-line input field that a user can enter text into:

<form>
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
</form>

How the HTML code above looks in a browser:

First name:
Last name:

Note: The form itself is not visible. Also note that the default width of a text field is 20 characters. 

Password Field

<input type="password" /> defines a password field:

<form>
Password: <input type="password" name="pwd" />
</form>

How the HTML code above looks in a browser:

Password:

Note: The characters in a password field are masked (shown as asterisks or circles). 

Radio Buttons

<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE one of a limited
number of choices:
<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>

How the HTML code above looks in a browser:


Male
Female

Checkboxes

<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE options of a
limited number of choices.

<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>

How the HTML code above looks in a browser:

I have a bike
I have a car

Submit Button

<input type="submit" /> defines a submit button.

A submit button is used to send form data to a server. The data is sent to the page specified in the form's
action attribute. The file defined in the action attribute usually does something with the received input:

<form name="input" action="html_form_action.asp" method="get">


Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

How the HTML code above looks in a browser:

Submit
Username:

If you type some characters in the text field above, and click the "Submit" button, the browser will send
your input to a page called "html_form_action.asp". The page will show you the received input.

HTML Form Tags

Tag Description
<form> Defines an HTML form for user input
<input /> Defines an input control
<textarea> Defines a multi-line text input control
<label> Defines a label for an input element
<fieldset> Defines a border around elements in a form
<legend> Defines a caption for a fieldset element
<select> Defines a select list (drop-down list)
<optgroup> Defines a group of related options in a select list
<option> Defines an option in a select list
<button> Defines a push button

HTML Frames

With frames, you can display more than one HTML document in the same browser window. Each HTML
document is called a frame, and each frame is independent of the others.

The disadvantages of using frames are:

 The web developer must keep track of more HTML documents


 It is difficult to print the entire page

The HTML frameset Element

The frameset element holds two or more frame elements. Each frame element holds a separate document.

The frameset element states only HOW MANY columns or rows there will be in the frameset.

The HTML frame Element

The <frame> tag defines one particular window (frame) within a frameset.

In the example below we have a frameset with two columns.

The first column is set to 25% of the width of the browser window. The second column is set to 75% of
the width of the browser window. The document "frame_a.htm" is put into the first column, and the
document "frame_b.htm" is put into the second column:

<frameset cols="25%,75%">
   <frame src="frame_a.htm" />
   <frame src="frame_b.htm" />
</frameset>

Note: The frameset column size can also be set in pixels (cols="200,500"), and one of the columns can be
set to use the remaining space, with an asterisk (cols="25%,*").

HTML Frame Tags

Tag Description
<frameset> Defines a set of frames
<frame /> Defines a sub window (a frame)
<noframes> Defines a noframe section for browsers that do not handle frames
<iframe> Defines an inline sub window (frame)
types of web protocol

A protocol is a set of rules defining how computers communicate how address work, how data is split into
packets etc.
The protocols most commonly used are
 TCP/IP
 UDP
 FTP
 SNMP
 SMTP etc...

5. Explain briefly about the Hypertext Transport Protocol (HTTP)? (11 marks)

HTTP, the Hypertext Transport Protocol, is the standard protocol for communication between the web
browsers and the web servers.
It is a stateless protocol that specifies how a client and a server establish a connection, how the client
request data from the server, how the server responds to the server, and finally how the connection is
closed.
HTTP is a form of Communication Protocol
The Basic Structure of HTTP Communication follows: Request-Response Model

Request:
• Client sends a Request to a Server by:
– clicking a link on a webpage
– submitting a form
– typing a web page address in the browser address field
HTTP Request Message:
• The Browser uses the URL to create the request message
• The HTTP request message consists of:
– Request/ start line
– requests headers / header field
– Blank line
– request / message body (optional)
Start line /Request line:
Consists of three parts:
1. Request Method (Example: GET)
2. Request-URI portion of web address
3. HTTP version (Example: HTTP/1.1)

REQUEST –URI:
• Second part of the Start line is known as Request-URI
• Uniform Resource Identifier (URI) is a string of characters used to identify a name or a resource
on the Internet.
• Such identification enables interaction with representations of the resource over a network
(typically the World Wide Web) using specific protocols

The URL concept:


• URL means ‘Uniform Resource Locator’
• It’s a standard way of specifying any kind of information available on the Internet
• Four elements of a URL specification:
– Method (i.e., the protocol for object retrieval)
– Host (i.e., location hostname or IP-address)
– Port (i.e., port-number for contacting server)
– Path (i.e., pathname of the resource’s file)

The URL Format:


URN, URC, URL:

• URNs are used for identification,


• URCs for including meta-information.
• URLs for locating or finding resources

Comparison URN vs. URL:


• A URN is similar to a person's name, while a URL is like a street address.
• The URN defines something's identity, while the URL provides a location. Essentially, "what" vs.
"where".

HTTP Response:
• The Server receive a request and uses its URL to decide how to handle it
• it might simply fetch an html file and return its content back to the client
• it might execute an external program
• Browser does not care how the request is handled all it needs is a response
• The browser reads the HTML tags in the response and formats the page onto your screen.

HTML Response-Example:
• HTTP/1.1 200 OK
• Last-Modified: Mon, 20Mar 2006 23:23:07 GMT
• Date:Tue, 30 Mar 2006 18:00:01 GMT
• Status: 200
• Content-Type: text/html
• Servlet-Engine: Tomcat Web Server/5.0
• Content-Length: 59
• <html>
• <body>
• <h1> hello world!</h1>
• </body> </html>

Simple Mail Transfer Protocol (SMTP)

Within the Internet, e-mail is delivered by having the source machine establish a TCP connection to port
25 of the destination machine. Listening to this port is an e-mail daemon that speaks SMTP (Simple Mail
Transfer Protocol). This daemon accepts incoming connections and copies messages from them into the
appropriate mailboxes. If a message cannot be delivered, an error report containing the first part of the
undeliverable message is returned to the sender.
SMTP is a simple ASCII protocol. After establishing the TCP connection to port 25, the sending machine,
operating as the client, waits for the receiving machine, operating as the server, to talk first. The server
starts by sending a line of text giving its identity and telling whether it is prepared to receive mail. If it is
not, the client releases the connection and tries again later.

If the server is willing to accept e-mail, the client announces whom the e-mail is coming from and whom
it is going to. If such a recipient exists at the destination, the server gives the client the go-ahead to send
the message. Then the client sends the message and the server acknowledges it. No checksums are needed
because TCP provides a reliable byte stream. If there is more e-mail, that is now sent. When all the e-mail
has been exchanged in both directions, the connection is released. A sample dialog for sending the
message of Fig., including the numerical codes used by SMTP, is shown in Fig. The lines sent by the
client are marked C:. Those sent by the server are marked S.

Simple Mail Transfer Protocol (SMTP) is the de facto standard for e-mail transmissions across the
Internet. Formally SMTP is defined in RFC 821 (STD 10) as amended by RFC 1123 (STD 3). The
protocol used today is also known as ESMTP and defined in RFC 2821.

SMTP is a relatively simple, text-based protocol, in which one or more recipients of a message are
specified (and in most cases verified to exist) along with the message text and possibly other encoded
objects. The message is then transferred to a remote server using a procedure of queries and responses
between the client and server. Either an end-user's email client, a.k.a. MUA (Mail User Agent), or a
relaying server's MTA (Mail Transport Agents) can act as an SMTP client.
An email client knows the outgoing mail SMTP server from its configuration. A relaying server typically
determines which SMTP server to connect to by looking up the MX (Mail eXchange) DNS record for
each recipient's domain name (the part of the email address to the right of the at (@) sign). Conformant
MTAs (not all) fall back to a simple A record in the case of no MX. Some current mail transfer agents will
also use SRV records, a more general form of MX, though these are not widely adopted. (Relaying servers
can also be configured to use a smart host.)

The SMTP client initiates a TCP connection to server's port 25 (unless overridden by configuration). It is
quite easy to test an SMTP server using the telnet program (see below). SMTP is a "push" protocol that
does not allow one to "pull" messages from a remote server on demand. To do this a mail client must use
POP3 or IMAP. Another SMTP server can trigger a delivery in SMTP using ETRN.

A few comments about Fig. may be helpful. The first command from the client is indeed HELO. Of the
various four-character abbreviations for HELLO, this one has numerous advantages over its biggest
competitor. Why all the commands had to be four characters has been lost in the mists of time.

In Fig. , the message is sent to only one recipient, so only one RCPT command is used. Such commands
are allowed to send a single message to multiple receivers. Each one is individually acknowledged or
rejected. Even if some recipients are rejected (because they do not exist at the destination), the message
can be sent to the other ones.

Transferring a message from [email protected] to [email protected].


Finally, although the syntax of the four-character commands from the client is rigidly specified, the syntax
of the replies is less rigid. Only the numerical code really counts. Each implementation can put whatever
string it wants after the code.

To get a better feel for how SMTP and some of the other protocols described in this chapter work, try
them out. In all cases, first go to a machine connected to the Internet. On a UNIX system, in a shell, type
telnet mail.isp.com 25 substituting the DNS name of your ISP's mail server for mail.isp.com. On a
Windows system, click on Start, then Run, and type the command in the dialog box. This command will
establish a telnet (i.e., TCP) connection to port 25 on that machine. Port 25 is the SMTP port (see or some
common ports). You will probably get a response something like this:

Trying 192.30.200.66...
Connected to mail.isp.com
Escape character is '^]'.
220 mail.isp.com Smail #74 ready at Thu, 25 Sept 2002 13:26 +0200

The first three lines are from telnet telling you what it is doing. The last line is from the SMTP server on
the remote machine announcing its willingness to talk to you and accept e-mail. To find out what
commands it accepts, type

HELP

From this point on, a command sequence such as the one in Fig. is possible, starting with the client's
HELO command. It is worth noting that the use of lines of ASCII text for commands is not an accident.
Most Internet protocols work this way.

Using ASCII text makes the protocols easy to test and debug. They can be tested by sending commands
manually, as we saw above, and dumps of the messages are easy to read.

Even though the SMTP protocol is completely well defined, a few problems can still arise. One problem
relates to message length. Some older implementations cannot handle messages exceeding 64 KB.
Another problem relates to timeouts. If the client and server have different timeouts, one of them may give
up while the other is still busy, unexpectedly terminating the connection. Finally, in rare situations, infinite
mailstorms can be triggered. For example, if host 1 holds mailing list A and host 2 holds mailing list B
and each list contains an entry for the other one, then a message sent to either list could generate a never-
ending amount of e-mail traffic unless somebody checks for it.

To get around some of these problems, extended SMTP (ESMTP) has been defined in RFC 2821. Clients
wanting to use it should send an EHLO message instead of HELO initially. If this is rejected, then the
server is a regular SMTP server, and the client should proceed in the usual way. If the EHLO is accepted,
then new commands and parameters are allowed.

POP3 (Post Office Protocol Version 3)

Unfortunately, this solution creates another problem: how does the user get the e-mail from the ISP's
message transfer agent? The solution to this problem is to create another protocol that allows user transfer
agents (on client PCs) to contact the message transfer agent (on the ISP's machine) and allow e-mail to be
copied from the ISP to the user. One such protocol is POP3 (Post Office Protocol Version 3), which is
described in RFC 1939.

The situation that used to hold (both sender and receiver having a permanent connection to the Internet) is
illustrated in Fig. A situation in which the sender is (currently) on-line but the receiver is not is illustrated
in Fg.

Figure (a) Sending and reading mail when the receiver has a permanent Internet connection and the
user agent runs on the same machine as the message transfer agent. (b) Reading e-mail when the
receiver has a dial-up connection to an ISP.

POP3 begins when the user starts the mail reader. The mail reader calls up the ISP (unless there is already
a connection) and establishes a TCP connection with the message transfer agent at port 110. Once the
connection has been established, the POP3 protocol goes through three states in sequence:

1. Authorization.
2. Transactions.
3. Update.

The authorization state deals with having the user log in. The transaction state deals with the user
collecting the e-mails and marking them for deletion from the mailbox. The update state actually causes
the e-mails to be deleted.

This behavior can be observed by typing something like:

telnet mail.isp.com 110

where mail.isp.com represents the DNS name of your ISP's mail server. Telnet establishes a TCP
connection to port 110, on which the POP3 server listens. Upon accepting the TCP connection, the server
sends an ASCII message announcing that it is present. Usually, it begins with +OK followed by a
comment. An example scenario is shown in Fig starting after the TCP connection has been established. As
before, the lines marked C: are from the client (user) and those marked S: are from the server (message
transfer agent on the ISP's machine).

Using POP3 to fetch three messages.

During the authorization state, the client sends over its user name and then its password. After a successful
login, the client can then send over the LIST command, which causes the server to list the contents of the
mailbox, one message per line, giving the length of that message. The list is terminated by a period.

Then the client can retrieve messages using the RETR command and mark them for deletion with DELE.
When all messages have been retrieved (and possibly marked for deletion), the client gives the QUIT
command to terminate the transaction state and enter the update state. When the server has deleted all the
messages, it sends a reply and breaks the TCP connection.

While it is true that the POP3 protocol supports the ability to download a specific message or set of
messages and leave them on the server, most e-mail programs just download everything and empty the
mailbox. This behavior means that in practice, the only copy is on the user's hard disk. If that crashes, all
e-mail may be lost permanently.

Let us now briefly summarize how e-mail works for ISP customers. Elinor creates a message for Carolyn
using some e-mail program (i.e., user agent) and clicks on an icon to send it. The e-mail program hands
the message over to the message transfer agent on Elinor's host. The message transfer agent sees that it is
directed to [email protected] so it uses DNS to look up the MX record for xyz.com (where xyz.com is
Carolyn's ISP). This query returns the DNS name of xyz.com's mail server. The message transfer agent
now looks up the IP address of this machine using DNS again, for example, using gethostbyname. It then
establishes a TCP connection to the SMTP server on port 25 of this machine. Using an SMTP command
sequence analogous to that of Fig., it transfers the message to Carolyn's mailbox and breaks the TCP
connection.

In due course of time, Carolyn boots up her PC, connects to her ISP, and starts her e-mail program. The e-
mail program establishes a TCP connection to the POP3 server at port 110 of the ISP's mail server
machine. The DNS name or IP address of this machine is typically configured when the e-mail program is
installed or the subscription to the ISP is made. After the TCP connection has been established, Carolyn's
e-mail program runs the POP3 protocol to fetch the contents of the mailbox to her hard disk using
commands similar to those of Fig... Once all the e-mail has been transferred, the TCP connection is
released. In fact, the connection to the ISP can also be broken now, since all the e-mail is on Carolyn's
hard disk. Of course, to send a reply, the connection to the ISP will be needed again, so it is not generally
broken right after fetching the e-mail.

Multipurpose Internet Mail Extensions (MIME)

In the early days of the ARPANET, e-mail consisted exclusively of text messages written in English and
expressed in ASCII. For this environment, RFC 822 did the job completely: it specified the headers but
left the content entirely up to the users. Nowadays, on the worldwide Internet, this approach is no longer
adequate. The problems include sending and receiving

1. Messages in languages with accents (e.g., French and German).


2. Messages in non-Latin alphabets (e.g., Hebrew and Russian).
3. Messages in languages without alphabets (e.g., Chinese and Japanese).
4. Messages not containing text at all (e.g., audio or images).

A solution was proposed in RFC 1341 and updated in RFCs 2045–2049. This solution, called MIME
(Multipurpose Internet Mail Extensions) is now widely used. We will now describe it. For additional
information about MIME, see the RFCs.

The basic idea of MIME is to continue to use the RFC 822 format, but to add structure to the message
body and define encoding rules for non-ASCII messages. By not deviating from RFC 822, MIME
messages can be sent using the existing mail programs and protocols. All that has to be changed are the
sending and receiving programs, which users can do for themselves.

MIME defines five new message headers, as shown in Fig.. The first of these simply tells the user agent
receiving the message that it is dealing with a MIME message, and which version of MIME it uses. Any
message not containing a MIME-Version: header is assumed to be an English plaintext message and is
processed as such.

RFC 822 headers added by MIME.

The Content-Description: header is an ASCII string telling what is in the message. This header is needed
so the recipient will know whether it is worth decoding and reading the message. If the string says: ''Photo
of Barbara's hamster'' and the person getting the message is not a big hamster fan, the message will
probably be discarded rather than decoded into a high-resolution color photograph.

The Content-Id: header identifies the content. It uses the same format as the standard Message-Id:
header.

The Content-Transfer-Encoding: tells how the body is wrapped for transmission through a network that
may object to most characters other than letters, numbers, and punctuation marks. Five schemes (plus an
escape to new schemes) are provided. The simplest scheme is just ASCII text. ASCII characters use 7 bits
and can be carried directly by the e-mail protocol provided that no line exceeds 1000 characters.
The next simplest scheme is the same thing, but using 8-bit characters, that is, all values from 0 up to and
including 255. This encoding scheme violates the (original) Internet e-mail protocol but is used by some
parts of the Internet that implement some extensions to the original protocol. While declaring the encoding
does not make it legal, having it explicit may at least explain things when something goes wrong.
Messages using the 8-bit encoding must still adhere to the standard maximum line length.

Even worse are messages that use binary encoding. These are arbitrary binary files that not only use all 8
bits but also do not even respect the 1000-character line limit. Executable programs fall into this category.
No guarantee is given that messages in binary will arrive correctly, but some people try anyway.

The correct way to encode binary messages is to use base64 encoding, sometimes called ASCII armor. In
this scheme, groups of 24 bits are broken up into four 6-bit units, with each unit being sent as a legal
ASCII character. The coding is ''A'' for 0, ''B'' for 1, and so on, followed by the 26 lower-case letters, the
ten digits, and finally + and / for 62 and 63, respectively. The = = and = sequences indicate that the last
group contained only 8 or 16 bits, respectively. Carriage returns and line feeds are ignored, so they can be
inserted at will to keep the lines short enough. Arbitrary binary text can be sent safely using this scheme.

For messages that are almost entirely ASCII but with a few non-ASCII characters, base64 encoding is
somewhat inefficient. Instead, an encoding known as quoted-printable encoding is used. This is just 7-bit
ASCII, with all the characters above 127 encoded as an equal sign followed by the character's value as two
hexadecimal digits.

In summary, binary data should be sent encoded in base64 or quoted-printable form. When there are valid
reasons not to use one of these schemes, it is possible to specify a user-defined encoding in the Content-
Transfer-Encoding: header.

The last header shown in Fig is really the most interesting one. It specifies the nature of the message body.
Seven types are defined in RFC 2045, each of which has one or more subtypes. The type and subtype are
separated by a slash, as in

Content-Type: video/mpeg

The subtype must be given explicitly in the header; no defaults are provided. The initial list of types and
subtypes specified in RFC 2045 is given in Fig. Many new ones have been added since then, and
additional entries are being added all the time as the need arises.

Let us now go briefly through the list of types. The text type is for straight ASCII text. The text/plain
combination is for ordinary messages that can be displayed as received, with no encoding and no further
processing. This option allows ordinary messages to be transported in MIME with only a few extra
headers.

The MIME types and subtypes defined in RFC 2045.


The text/enriched subtype allow a simple markup language to be included in the text. This language
provides a system-independent way to express boldface, italics, smaller and larger point sizes, indentation,
justification, sub- and superscripting, and simple page layout. The markup language is based on SGML,
the Standard Generalized Markup Language also used as the basis for the World Wide Web's HTML. For
example, the message

The <bold> time </bold> has come the <italic> walrus </italic> said ... would be displayed as

The time has come the walrus said...

It is up to the receiving system to choose the appropriate rendition. If boldface and italics are available,
they can be used; otherwise, colors, blinking, underlining, reverse video, etc., can be used for emphasis.
Different systems can, and do, make different choices.

When the Web became popular, a new subtype text/html was added (in RFC 2854) to allow Web pages to
be sent in RFC 822 e-mail. A subtype for the extensible markup language, text/xml, is defined in RFC

3023. We will study HTML and XML later in this chapter.

The next MIME type is image, which is used to transmit still pictures. Many formats are widely used for
storing and transmitting images nowadays, both with and without compression. Two of these, GIF and
JPEG, are built into nearly all browsers, but many others exist as well and have been added to the original
list.

The audio and video types are for sound and moving pictures, respectively. Please note that video includes
only the visual information, not the soundtrack. If a movie with sound is to be transmitted, the video and
audio portions may have to be transmitted separately, depending on the encoding system used. The first
video format defined was the one devised by the modestly-named Moving Picture Experts Group
(MPEG), but others have been added since. In addition to audio/basic, a new audio type, audio/mpeg was
added in RFC 3003 to allow people to e-mail MP3 audio files.

The application type is a catchall for formats that require external processing not covered by one of the
other types. An octet-stream is just a sequence of uninterpreted bytes. Upon receiving such a stream, a
user agent should probably display it by suggesting to the user that it be copied to a file and prompting for
a file name. Subsequent processing is then up to the user.

The other defined subtype is postscript, which refers to the PostScript language defined by Adobe Systems
and widely used for describing printed pages. Many printers have built-in PostScript interpreters.
Although a user agent can just call an external PostScript interpreter to display incoming PostScript files,
doing so is not without some danger. PostScript is a full-blown programming language. Given enough
time, a sufficiently masochistic person could write a C compiler or a database management system in
PostScript. Displaying an incoming PostScript message is done by executing the PostScript program
contained in it. In addition to displaying some text, this program can read, modify, or delete the user's
files, and have other nasty side effects.

The message type allows one message to be fully encapsulated inside another. This scheme is useful for
forwarding e-mail, for example. When a complete RFC 822 message is encapsulated inside an outer
message, the rfc822 subtype should be used.

The partial subtype makes it possible to break an encapsulated message into pieces and send them
separately (for example, if the encapsulated message is too long). Parameters make it possible to
reassemble all the parts at the destination in the correct order.

Finally, the external-body subtype can be used for very long messages (e.g., video films). Instead of
including the MPEG file in the message, an FTP address is given and the receiver's user agent can fetch it
over the network at the time it is needed. This facility is especially useful when sending a movie to a
mailing list of people, only a few of whom are expected to view it (think about electronic junk mail
containing advertising videos).

The final type is multipart, which allows a message to contain more than one part, with the beginning and
end of each part being clearly delimited. The mixed subtype allows each part to be different, with no
additional structure imposed. Many e-mail programs allow the user to provide one or more attachments to
a text message. These attachments are sent using the multipart type.

In contrast to multipart, the alternative subtype, allows the same message to be included multiple times but
expressed in two or more different media. For example, a message could be sent in plain ASCII, in
enriched text, and in PostScript. A properly-designed user agent getting such a message would display it
in PostScript if possible. Second choice would be enriched text. If neither of these were possible, the flat
ASCII text would be displayed. The parts should be ordered from simplest to most complex to help

recipients with pre-MIME user agents make some sense of the message (e.g., even a pre-MIME user can
read flat ASCII text).

The alternative subtype can also be used for multiple languages. In this context, the Rosetta Stone can be
thought of as an early multipart/alternative message.

A multimedia example is shown in Fig.. Here a birthday greeting is transmitted both as text and as a song.
If the receiver has an audio capability, the user agent there will fetch the sound file, birthday.snd, and play
it. If not, the lyrics are displayed on the screen in stony silence. The parts are delimited by two hyphens
followed by a (software-generated) string specified in the boundary parameter.

Note that the Content-Type header occurs in three positions within this example. At the top level, it
indicates that the message has multiple parts. Within each part, it gives the type and subtype of that part.
Finally, within the body of the second part, it is required to tell the user agent what kind of an external file
it is to fetch. To indicate this slight difference in usage, we have used lower case letters here, although all
headers are case insensitive. The content-transfer-encoding is similarly required for any external body that
is not encoded as 7-bit ASCII.

A multipart message containing enriched and audio alternatives.


Getting back to the subtypes for multipart messages, two more possibilities exist. The parallel subtype is
used when all parts must be ''viewed'' simultaneously. For example, movies often have an audio channel
and a video channel. Movies are more effective if these two channels are played back in parallel, instead
of consecutively.

Finally, the digest subtype is used when many messages are packed together into a composite message.
For example, some discussion groups on the Internet collect messages from subscribers and then send
them out to the group as a single multipart/digest message.

Internet Message Access Protocol (IMAP)

For a user with one e-mail account at one ISP that is always accessed from one PC, POP3 works fine and
is widely used due to its simplicity and robustness. However, it is a computer-industry truism that as soon
as something works well, somebody will start demanding more features (and getting more bugs). That
happened with e-mail, too. For example, many people have a single e-mail account at work or school and
want to access it from work, from their home PC, from their laptop when on business trips, and from
cybercafes when on so-called vacation. While POP3 allows this, since it normally downloads all stored
messages at each contact, the result is that the user's e-mail quickly gets spread over multiple machines,
more or less at random; some of them not even the users.

This disadvantage gave rise to an alternative final delivery protocol, IMAP (Internet Message Access
Protocol), which is defined in RFC 2060. Unlike POP3, which basically assumes that the user will clear
out the mailbox on every contact and work off-line after that, IMAP assumes that all the e-mail will
remain on the server indefinitely in multiple mailboxes. IMAP provides extensive mechanisms for reading
messages or even parts of messages, a feature useful when using a slow modem to read the text part of a
multipart message with large audio and video attachments. Since the working assumption is that messages
will not be transferred to the user's computer for permanent storage, IMAP provides mechanisms for
creating, destroying, and manipulating multiple mailboxes on the server. In this way a user can maintain a
mailbox for each correspondent and move messages there from the inbox after they have been read.
IMAP has many features, such as the ability to address mail not by arrival number as is done in , but by
using attributes (e.g., give me the first message from Bobbie). Unlike POP3, IMAP can also accept
outgoing e-mail for shipment to the destination as well as deliver incoming e-mail.

The general style of the IMAP protocol is similar to that of POP3 as shown in Fig., except that are there
dozens of commands. The IMAP server listens to port 143. A comparison of POP3 and IMAP is given in
Fig.. It should be noted, however, that not every ISP supports both protocols and not every e-mail program
supports both protocols. Thus, when choosing an e-mail program, it is important to find out which
protocol(s) it supports and make sure the ISP supports at least one of them.

A comparison of POP3 and IMAP

9Domain Name System (DNS)? (11 marks)

It is a distributed database implemented in hierarchy of name servers. Mapping of host name, to IP


address.
It consists of

 Local name server


 Authoritative name server.
It contacts local name server first, if there is response then it contacts the Authoritative name server.

Although programs theoretically could refer to hosts, mailboxes, and other resources by their network
(e.g., IP) addresses, these addresses are hard for people to remember. Also, sending e-mail to
[email protected] means that if Tana’s ISP or organization moves the mail server to a different
machine with a different IP address, her e-mail address has to change. Consequently, ASCII names were
introduced to decouple machine names from machine addresses. In this way, Tana's address might be
something like [email protected]. Nevertheless, the network itself understands only numerical addresses,
so some mechanism is required to convert the ASCII strings to network addresses. In the following
sections we will study how this mapping is accomplished in the Internet.

Way back in the ARPANET, there was simply a file, hosts.txt,that listed all the hosts and their IP
addresses. Every night, all the hosts would fetch it from the site at which it was maintained. For a network
of a few hundred large timesharing machines, this approach worked reasonably well.

However, when thousands of minicomputers and PCs were connected to the net, everyone realized that
this approach could not continue to work forever. For one thing, the size of the file would become too
large. However, even more important, host name conflicts would occur constantly unless names were
centrally managed, something unthinkable in a huge international network due to the load and latency. To
solve these problems, DNS (the Domain Name System) was invented.

The essence of DNS is the invention of a hierarchical, domain-based naming scheme and a distributed
database system for implementing this naming scheme. It is primarily used for mapping host names and e-
mail destinations to IP addresses but can also be used for other purposes. DNS is defined in RFCs 1034
and 1035.

Very briefly, the way DNS is used is as follows. To map a name onto an IP address, an application
program calls a library procedure called the resolver, passing it the name as a parameter. We saw an
example of a resolver, gethostbyname. The resolver sends a UDP packet to a local DNS server, which
then looks up the name and returns the IP address to the resolver, which then returns it to the caller.
Armed with the IP address, the program can then establish a TCP connection with the destination or send
it UDP packets.

The DNS Name Space

Managing a large and constantly changing set of names is a nontrivial problem. In the postal system, name
management is done by requiring letters to specify (implicitly or explicitly) the country, state or province,
city, and street address of the addressee. By using this kind of hierarchical addressing, there is no
confusion between the Marvin Anderson on Main St. in White Plains, N.Y. and the Marvin Anderson on
Main St. in Austin, Texas. DNS works the same way.

Conceptually, the Internet is divided into over 200 top-level domains, where each domain covers many
hosts. Each domain is partitioned into subdomains, and these are further partitioned, and so on. All these
domains can be represented by a tree, as shown in. The leaves of the tree represent domains that have no
subdomains (but do contain machines, of course). A leaf domain may contain a single host, or it may
represent a company and contain thousands of hosts.

A portion of the Internet domain name space.

The top-level domains come in two flavors: generic and countries. The original generic domains were com
(commercial), edu (educational institutions), gov (the U.S. Federal Government), int (certain international
organizations), mil (the U.S. armed forces), net (network providers), and org (nonprofit organizations).
The country domains include one entry for every country, as defined in ISO 3166.

In November 2000, ICANN approved four new, general-purpose, top-level domains, namely, biz
(businesses), info (information), name (people's names), and pro (professions, such as doctors and
lawyers). In addition, three more specialized top-level domains were introduced at the request of certain
industries. These are aero (aerospace industry), coop (co-operatives), and museum (museums). Other top-
level domains will be added in the future.

As an aside, as the Internet becomes more commercial, it also becomes more contentious. Take pro, for
example. It was intended for certified professionals. But who is a professional? And certified by whom?
Doctors and lawyers clearly are professionals. But what about freelance photographers, piano teachers,
magicians, plumbers, barbers, exterminators, tattoo artists, mercenaries, and prostitutes? Are these
occupations professional and thus eligible for pro domains? And if so, who certifies the individual
practitioners?

In general, getting a second-level domain, such as name-of-company.com, is easy. It merely requires


going to a registrar for the corresponding top-level domain (com in this case) to check if the desired name
is available and not somebody else's trademark. If there are no problems, the requester pays a small annual
fee and gets the name. By now, virtually every common (English) word has been taken in the com
domain. Try household articles, animals, plants, body parts, etc. Nearly all are taken.

Each domain is named by the path upward from it to the (unnamed) root. The components are separated
by periods (pronounced ''dot''). Thus, the engineering department at Sun Microsystems might be
eng.sun.com., rather than a UNIX-style name such as /com/sun/eng. Notice that this hierarchical naming
means that eng.sun.com does not conflict with a potential use of eng in eng.yale.edu., which might be used
by the Yale English department.

Domain names can be either absolute or relative. An absolute domain name always ends with a period
(e.g., eng.sun.com.), whereas a relative one does not. Relative names have to be interpreted in some
context to uniquely determine their true meaning. In both cases, a named domain refers to a specific node
in the tree and all the nodes under it.

Domain names are case insensitive, so edu, Edu, and EDU mean the same thing. Component names can be
up to 63 characters long, and full path names must not exceed 255 characters.

In principle, domains can be inserted into the tree in two different ways. For example, cs.yale.edu could
equally well be listed under the us country domain as cs.yale.ct.us. In practice, however, most

organizations in the United States are under a generic domain, and most outside the United States are
under the domain of their country. There is no rule against registering under two top-level domains, but
few organizations except multinationals do it (e.g., sony.com and sony.nl).

Each domain controls how it allocates the domains under it. For example, Japan has domains ac.jp and
co.jp that mirror edu and com. The Netherlands does not make this distinction and puts all organizations
directly under nl. Thus, all three of the following are university computer science departments:

 cs.yale.edu (Yale University, in the United States)


 cs.vu.nl (Vrije University, in The Netherlands)
 cs.keio.ac.jp (Keio University, in Japan)

To create a new domain, permission is required of the domain in which it will be included. For example, if
a VLSI group is started at Yale and wants to be known as vlsi.cs.yale.edu, it has to get permission from
whoever manages cs.yale.edu. Similarly, if a new university is chartered, say, the University of Northern
South Dakota, it must ask the manager of the edu domain to assign it unsd.edu. In this way, name conflicts
are avoided and each domain can keep track of all its subdomains. Once a new domain has been created
and registered, it can create subdomains, such as cs.unsd.edu, without getting permission from anybody
higher up the tree.
Naming follows organizational boundaries, not physical networks. For example, if the computer science
and electrical engineering departments are located in the same building and share the same LAN, they can
nevertheless have distinct domains. Similarly, even if computer science is split over Babbage Hall and
Turing Hall, the hosts in both buildings will normally belong to the same domain.

Resource Records

Every domain, whether it is a single host or a top-level domain, can have a set of resource records
associated with it. For a single host, the most common resource record is just its IP address, but many
other kinds of resource records also exist. When a resolver gives a domain name to DNS, what it gets back
are the resource records associated with that name. Thus, the primary function of DNS is to map domain
names onto resource records.

A resource record is a five-tuple. Although they are encoded in binary for efficiency, in most expositions,
resource records are presented as ASCII text, one line per resource record. The format we will use is as
follows:

Domain_name Time_to_live Class Type Value

The Domain_name tells the domain to which this record applies. Normally, many records exist for each
domain and each copy of the database holds information about multiple domains. This field is thus the
primary search key used to satisfy queries. The order of the records in the database is not significant.

The Time_to_live field gives an indication of how stable the record is. Information that is highly stable is
assigned a large value, such as 86400 (the number of seconds in 1 day). Information that is highly volatile
is assigned a small value, such as 60 (1 minute). We will come back to this point later when we have
discussed caching.

The third field of every resource record is the Class. For Internet information, it is always IN. For non-
Internet information, other codes can be used, but in practice, these are rarely seen.

The Type field tells what kind of record this is. The most important types are listed in Fig.

An SOA record provides the name of the primary source of information about the name server's zone
(described below), the e-mail address of its administrator, a unique serial number, and various flags and
timeouts.

The principal DNS resource record types for IPv4.


The most important record type is the A (Address) record. It holds a 32-bit IP address for some host.
Every Internet host must have at least one IP address so that other machines can communicate with it.
Some hosts have two or more network connections, in which case they will have one type A resource
record per network connection (and thus per IP address). DNS can be configured to cycle through these,
returning the first record on the first request, the second record on the second request, and so on.

The next most important record type is the MX record. It specifies the name of the host prepared to accept
e-mail for the specified domain. It is used because not every machine is prepared to accept e-mail. If
someone wants to send e-mail to, for example, [email protected], the sending host needs to find a mail
server at microsoft.com that is willing to accept e-mail. The MX record can provide this information.

The NS records specify name servers. For example, every DNS database normally has an NS record for
each of the top-level domains, so, for example, e-mail can be sent to distant parts of the naming tree. We
will come back to this point later.

CNAME records allow aliases to be created. For example, a person familiar with Internet naming in
general and wanting to send a message to someone whose login name is paul in the computer science
department at M.I.T. might guess that [email protected] will work. Actually, this address will not work,
because the domain for M.I.T.'s computer science department is lcs.mit.edu. However, as a service to
people who do not know this, M.I.T. could create a CNAME entry to point people and programs in the
right direction. An entry like this one might do the job:

cs.mit.edu 86400 IN CNAME lcs.mit.edu

Like CNAME, PTR points to another name. However, unlike CNAME, which is really just a macro
definition, PTR is a regular DNS data type whose interpretation depends on the context in which it is
found. In practice, it is nearly always used to associate a name with an IP address to allow lookups of the
IP address and return the name of the corresponding machine. These are called reverse lookups.

HINFO records allow people to find out what kind of machine and operating system a domain
corresponds to. Finally, TXT records allow domains to identify themselves in arbitrary ways. Both of
these record types are for user convenience. Neither is required, so programs cannot count on getting them
(and probably cannot deal with them if they do get them).
Finally, we have the Value field. This field can be a number, a domain name, or an ASCII string. The
semantics depend on the record type. A short description of the Value fields for each of the principal
record types is given in Fig.

For an example of the kind of information one might find in the DNS database of a domain, see-. This
figure depicts part of a (semi hypothetical) database for the cs.vu.nl domain shown in The database
contains seven types of resource records.

A portion of a possible DNS database for cs.vu.nl

The first non-comment line of Fig. gives some basic information about the domain, which will not
concern us further. The next two lines give textual information about where the domain is located. Then
come two entries giving the first and second places to try to deliver e-mail sent to [email protected]. The
zephyr (a specific machine) should be tried first. If that fails, the top should be tried as the next choice.

After the blank line, added for readability, come lines telling that the flits is a Sun workstation running
UNIX and giving both of its IP addresses. Then three choices are given for handling e-mail sent to
flits.cs.vu.nl. First choice is naturally the flits itself, but if it is down, the zephyr and top are the second
and third choices. Next comes an alias, www.cs.vu.nl, so that this address can be used without designating
a specific machine. Creating this alias allows cs.vu.nl to change its World Wide Web server without
invalidating the address people use to get to it. A similar argument holds for ftp.cs.vu.nl.

The next four lines contain a typical entry for a workstation, in this case, rowboat.cs.vu.nl. The
information provided contains the IP address, the primary and secondary mail drops, and information
about the machine. Then comes an entry for a non-UNIX system that is not capable of receiving mail
itself, followed by an entry for a laser printer that is connected to the Internet.

What are not shown (and are not in this file) are the IP addresses used to look up the top-level domains.
These are needed to look up distant hosts, but since they are not part of the cs.vu.nl domain, they are not in
this file. They are supplied by the root servers, whose IP addresses are present in a system configuration
file and loaded into the DNS cache when the DNS server is booted.
There are about a dozen root servers spread around the world, and each one knows the IP addresses of all
the top-level domain servers. Thus, if a machine knows the IP address of at least one root server, it can
look up any DNS name.

Name Servers

In theory at least, a single name server could contain the entire DNS database and respond to all queries
about it. In practice, this server would be so overloaded as to be useless. Furthermore, if it ever went
down, the entire Internet would be crippled.

To avoid the problems associated with having only a single source of information, the DNS name space is
divided into non overlapping zones. One possible way to divide the name space in fig .Each zone contains
some part of the tree and also contains name servers holding the information about that zone. Normally, a
zone will have one primary name server, which gets its information from a file on its disk, and one or
more secondary name servers, which get their information from the primary name server. To improve
reliability, some servers for a zone can be located outside the zone.

Part of the DNS name space showing the division into zones.

Where the zone boundaries are placed within a zone is up to that zone's administrator. This decision is
made in large part based on how many name servers are desired, and where. For example, in Fig., Yale
has a server for yale.edu that handles eng.yale.edu but not cs.yale.edu, which is a separate zone with its
own name servers. Such a decision might be made when a department such as English does not wish to
run its own name server, but a department such as computer science does. Consequently, cs.yale.edu is a
separate zone but eng.yale.edu is not.

When a resolver has a query about a domain name, it passes the query to one of the local name servers. If
the domain being sought falls under the jurisdiction of the name server, such as ai.cs.yale.edu falling
under cs.yale.edu, it returns the authoritative resource records. An authoritative record is one that comes
from the authority that manages the record and is thus always correct. Authoritative records are in contrast
to cached records, which may be out of date.

If, however, the domain is remote and no information about the requested domain is available locally, the
name server sends a query message to the top-level name server for the domain requested. To make this
process clearer, consider the example of Fig.. Here, a resolver on flits.cs.vu.nl wants to know the IP

address of the host linda.cs.yale.edu. In step 1, it sends a query to the local name server, cs.vu.nl. This
query contains the domain name sought, the type (A) and the class (IN).

How a resolver looks up a remote name in eight steps.


Let us suppose the local name server has never had a query for this domain before and knows nothing
about it. It may ask a few other nearby name servers, but if none of them know, it sends a UDP packet to
the server for edu given in its database , edu-server.net. It is unlikely that this server knows the address of
linda.cs.yale.edu, and probably does not know cs.yale.edu either, but it must know all of its own children,
so it forwards the request to the name server for yale.edu (step 3). In turn, this one forwards the request to
cs.yale.edu (step 4), which must have the authoritative resource records. Since each request is from a
client to a server, the resource record requested works its way back in steps 5 through 8.

Once these records get back to the cs.vu.nl name server, they will be entered into a cache there, in case
they are needed later. However, this information is not authoritative, since changes made at cs.yale.edu
will not be propagated to all the caches in the world that may know about it. For this reason, cache entries
should not live too long. This is the reason that the Time_to_live field is included in each resource record.
It tells remote name servers how long to cache records. If a certain machine has had the same IP address
for years, it may be safe to cache that information for 1 day. For more volatile information, it might be
safer to purge the records after a few seconds or a minute.

It is worth mentioning that the query method described here is known as a recursive query, since each
server that does not have the requested information goes and finds it somewhere, then reports back. An
alternative form is also possible. In this form, when a query cannot be satisfied locally, the query fails, but
the name of the next server along the line to try is returned. Some servers do not implement recursive
queries and always return the name of the next server to try.

It is also worth pointing out that when a DNS client fails to get a response before its timer goes off, it
normally will try another server next time. The assumption here is that the server is probably down, rather
than that the request or reply got lost.

web browser

A web browser is used to display web pages. Common web browsers are Netscape Navigator and Internet
Explorer.
The Web browser is the interpreter of our Web sites. It is very important to understand the Web browser
being supported and what capabilities it has. The two most common browsers at the time of this book’s
publication are Microsoft’s Internet Explorer (which accounts for the majority of browser users) and
Netscape’s Communicator (Navigator). While these two browsers account for most users accessing public
Web sites, there are numerous other versions of browsers in use.

The problem with published browser usage reports is that they don’t necessarily reflect your browsing
audience. Consider a site that publishes Macintosh software—its browser usage pattern might actually
show a fair number of users with OmniWeb, a Macintosh-specific browser that has a notable number of

rabid followers. However, most sites probably wouldn’t consider OmniWeb something to even think
about.
Components of Web Medium

Depending on your users, the types of browsers will vary. From statistics showing that surveyed sites
favor a particular browser, it does not necessarily follow that your site will exhibit the same browser usage
patterns—though it is pretty likely. Look at your own log files to determine browser usage patterns. If you
are building an intranet site, you might not even have to look at your logs to understand what browsers are
in use.
Given a mix of browsers made up of the top two vendors with a smattering of other browsers, the
question becomes how this information relates to site design and technology use. One possibility is to look
at the various browsers and their capabilities, and then design for some common set of features.

Considering the variations among browsers, the common ground isn’t terribly advanced. The safest design
platform for some still seems to be what Netscape 3.x supports, though more and more designers are
embracing design for the 4.x and 5.x generation browsers and using CSS, Flash, and JavaScript more
often.
The only problem with moving to the next generation is that the gap between what different generations of
browsers support can be rather large. Because of this, sites (and users) significantly favor Internet
Explorer over Netscape. (The installed base for IE browsers includes between 85% to 90% of all users at
the time of this writing.) With the advent of Netscape’s Mozilla-based browsers (Netscape 6 and 7, and
Mozilla 1.0), things may get more interesting because these browsers promise more support for standards-
based Web page development than Netscape’s 4.x generation browsers.
Even so, there will not be an overnight adoption of new, non-IE browsers around the Web. As the
installed base increases, the longer it will take for consumers to embrace new technologies. Therefore,
public sites should consider developing for at least one, if not two, generations prior to the current release
of a browser. Even more than six years after the release of the 2.x generation browsers, some public sites
still support that generation of browsers perfectly.

OTHER BROWSERS

Besides Internet Explorer and Firefox, many other web browsers are available, including Opera
(www.opera.com) and Safari (www.apple.com/safari). All these browsers differ in functionality,
performance and features. Also, they employ different HTML layout engines, which determine how a web
page displays in a browser. Firefox 2 uses Gecko as its layout engine, Safari uses a modified version of the
KHTML layout engine and Opera and IE7 have their own engines. Gecko and KHTML are both free and
open source.
Opera, as well as IE7 and FF2, is a browser designed to be accessible to all users, including those with
visual or mobility impairments. Opera software also recently developed a lightweight “Mini” version of
the browser that runs effectively on mobile devices.
Safari, originally created for Apple’s Mac OS, features an elegantly simple interface and impressive
speed, especially when executing JavaScript.

Because browsers use different HTML layout engines, they may display the same web page differently.
Additionally, some capabilities supported in one browser may not be supported in another. The existence
of different browser functionality and features makes cross-browser compatibility difficult to achieve.
web servers
A web server is a computer system that delivers web pages. Every web server has an IP address and
possibly a domain name.
Example, if you enter the URL http://www.yahoo.com//index.html in your browser, this sends a request to
the server whose domain name is yahoo.com. The server then fetches the page named index.html and
sends it to your browser.
• All of the machines on the Internet can be categorized as two types:
– servers
– client
• Those machines that provide services (like Web servers or FTP servers) to other machines are
servers.
• And the machines that are used to connect to those services are clients.

Like the Web browser, the Web server frames the environment of each Web transaction. The term “Web
server” is usually understood to mean both the hardware and software. The major issue with hardware is
whether the Web server is capable of handling the memory, disk, and network input/output requirements
resulting from site traffic. The interplay of operating systems, such as UNIX or Windows 2000, and Web
server software also is closely related to performance, as is security.
From Apache to Zeus, all Web server software platforms handle basic HTTP transactions, but all tend to
offer more than basic file serving facilities. Most Web server platforms provide basic security and
authentication services, logging, and programming facilities. An in-depth discussion of the popular servers
and their facilities is presented in Chapter 17; here, we will focus only on the programming aspects of
sites.

Apache is the most popular UNIX web server today. Apache was originally based on code and ideas
found in the most popular HTTP server of the time, NCSA httpd 1.3 (early 1995). It has since evolved into
a far superior system which can rival (and probably surpass) almost any other UNIX based HTTP server
in terms of functionality, efficiency and speed. Take a look at the web server feature chart to see how
Apache ranks among the competition.

Open Market provides software products that are used to develop the infrastructure for Internet commerce.
They pride themselves on scalability, content flexibility, lower entry and maintenance costs, and enhanced
security.
Netscape sells several web server software packages. The Netscape Enterprise Server offers built
in advanced services such as Internet-based access controls, automatic link management, and revision
control. The FastTrack Server is an easy-to-use entry-level Web server designed to let novices create and
manage a Web site.

IBM's Secure Server is provided for AIX, HP-UX, and Solaris, as well as NT and OS/2. Version
4.2 servers include enhanced scalability, browser-specific response capability, enhanced CGI support,
PICS support, and HTTP Version 1.1 compliance. The IB servers have consistent configuration,
management, and API interfaces across all of their supported platforms.

Jigsaw is W3C's sample implementation of HTTP, a full blown HTTP server entirely written in
Java. Its design goals were: will run on any machine running Java, can be extended by writing new
resource objects (a replacement for CGI), minimization of file system accesses.

WebSTAR is a Mac HTTP server which performs dynamic web server file caching, has the ability
to run server side Java applets, contains an administration plug-in that lets one administer essential server
functions from any web browser on the Internet, honors keep-alive requests, supports a 20,000
username/passwd database, has integrated support for image maps, supports common log format, supports
cgi-bin folder, does on the fly bin-hexing of Mac files, and supports an expanded command set for server-
side includes.

Microsoft IIS is tightly integrated with NT and includes native support for ActiveX, VBScript and
JScript. The index server includes built-in support for HTML, text, and Microsoft Office documents, and
NetShow provides streaming audio and video support for IIS. Crystal Reports is the visual reporting tool
for IIS that lets you create presentation quality reports and integrate them into database applications.

DYNAMIC HTML

Dynamic HTML is a collective term for a combination of Hypertext Markup Language (HTML) tags and
options that can make Web pages more animated and interactive than previous versions of HTML. Much
of dynamic HTML is specified in HTML 4.0. Simple examples of dynamic HTML capabilities include
having the color of a text heading change when a user passes a mouse over it and allowing a user to "drag
and drop" an image to another place on a Web page. Dynamic HTML can allow Web documents to look
and act like desktop applications or multimedia productions.

The Concepts and Features in Dynamic HTML

 An object-oriented view of a Web page and its elements


 Cascading style sheets and the layering of content
 Programming that can address all or most page elements
 Dynamic fonts

An Object-Oriented View of Page Elements

Each page element (division or section, heading, paragraph, image, list, and so forth) is viewed as an
"object." (Microsoft calls this the "Dynamic HTML Object Model." Netscape calls it the "HTML Object
Model." W3C calls it the "Document Object Model.") For example, each heading on a page can be named,

given attributes of text style and color, and addressed by name in a small program or "script" included on
the page.
This heading or any other element on the page can be changed as the result of a specified event such a
mouse passing over or being clicked or a time elapsing. Or an image can be moved from one place to
another by "dragging and dropping" the image object with the mouse. (These event possibilities can be
viewed as the reaction capabilities of the element or object.) Any change takes place immediately (since
all variations of all elements or objects have been sent as part of the same page from the Web server that
sent the page). Thus, variations can be thought of as different properties of the object.

Not only can element variations change text wording or color, but everything contained within a heading
object can be replaced with new content that includes different or additional HTML as well as different
text. Microsoft calls this the "Text Range technology."

Although JavaScript, Java applet, and ActiveX controls were present in previous levels of Web pages,
dynamic HTML implies an increased amount of programming in Web pages since more elements of a
page can be addressed by a program.

A feature called dynamic fonts lets Web page designers include font files containing specific font styles,
sizes, and colors as part of a Web page and to have the fonts downloaded with the page. That is, the font
choice no longer is dependent on what the user's browser provides.

Style Sheets and Layering

A describes the default style characteristics (including the page layout and font type style and size for text
elements such as headings and body text) of a document or a portion of a document. For Web pages, a
style sheet also describes the default background color or image, hypertext link colors, and possibly the
content of page. Style sheets help ensure consistency across all or a group of pages in a document or a
Web site.

Dynamic HTML includes the capability to specify style sheets in a "cascading style sheet" fashion (that is,
linking to or specifying different style sheets or style statements with predefined levels of precedence
within the same or a set of related pages). As the result of user interaction, a new style sheet can be made
applicable and result in a change of appearance of the Web page. You can have multiple layers of style
sheet within a page, a style sheet within a style sheet within a style sheet. A new style sheet may only vary
one element from the style sheet above it.

Layering is the use of alternate style sheets or other approaches to vary the content of a page by providing
content layers that can overlay (and replace or superimpose on) existing content sections. Layers can be
programmed to appear as part of a timed presentation or as the result of user interaction.

DHTML is the art of combining HTML, JavaScript, DOM, and CSS.

What you should already know

Before you continue you should have a basic understanding of the following:
 HTML
 CSS
 JavaScript
If you want to study these subjects first, find the tutorials on our Home Page.
DHTML is NOT a Language

1. DHTML stands for Dynamic HTML.


2. DHTML is NOT a language or a web standard.
3. To most people DHTML means the combination of HTML, JavaScript, DOM
and CSS.

According to the World Wide Web Consortium (W3C):


"Dynamic HTML is a term used by some vendors to describe the combination of HTML,
style sheets and scripts that allows documents to be animated."

HTML

 The W3C HTML 4 standard has rich support for dynamic content:
 HTML supports JavaScript
 HTML supports the Document Object Model (DOM)
 HTML supports HTML Events
 HTML supports Cascading Style Sheets (CSS)
DHTML is about using these features, to create dynamic and interactive web pages.

JavaScript

 JavaScript is the most popular scripting language on the internet, and it works in all
major browsers.
 DHTML is about using JavaScript to control access and manipulate HTML elements.
 You can read more about JavaScript in the next chapter of this tutorial.

HTML DOM

The HTML DOM is a W3C standard. It describes the Document Object Model for HTML.
The HTML DOM defines a standard way for accessing and manipulating HTML documents.
DHTML is about using the DOM to access and manipulate HTML elements.
You can read more about the HTML DOM in a later chapter of this tutorial.

HTML Events

HTML events are a part of the HTML DOM.


DHTML is about creating web pages that reacts to (user) events.
You can read more about events in a later chapter of this tutorial.

CSS
CSS defines how to display HTML elements.
DHTML is about using JavaScript and the HTML DOM to change the style and positioning of
HTML elements.

HTML DOM

The HTML DOM is:


 A Document Object Model for HTML
 A standard programming interface for HTML
 Platform- and language-independent
 A W3C standard
The HTML DOM defines the objects and properties of all HTML elements, and the
methods (interface) to access them.
In other words: The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.

Change an HTML Element

The following example changes the content of an h1 element:

Example

<html>
<body>
<h1 id="header">Old Header</h1>
<script type="text/javascript">
document.getElementById("header").innerHTML="New Header";
</script>
</body>
</html>
Unit-2

basic concepts in script languages

Scripting languages
 JavaScript is a Scripting language (web site development environment) created by Netscape.
 Hence JavaScript works best with the Netscape suite of Client and Server products.
 JavaScript is the native scripting language of Netscape Navigator.
 VBScript is the native Scripting language of HTML.

Server side scripting


 In Server side scripting the script program is executed at Server Side the required html
program is sent to the client.
 The job of the server is more in server side scripting.

Client side scripting


 Here the script program is processed and executed in the client side itself.
 So that it reduces the burden of the server.

JAVA SCRIPT

1. JavaScript is an object-oriented language that allows creation of interactive Web pages.


2. JavaScript allows user entries, which are loaded into an HTML form to be processed as required.

Javascript introduction

Objectives
 To be able to write simple java script programs
 To be able to use input and output statements
 To understand basic memory concepts
 To be able to use arithmetic operators
 To understand the precedence of arithmetic operators
 To be able to write decision making statements
 To be able to use relational and equality operators

Advantages

1. It is an interpreted language, which requires no compilation steps.


2. Embedded within HTML.
3. Minimal Syntax – easy to learn
4. Quick Development
5. Designed for simple, small programs
6. High performance
7. Procedural Capabilities – support facilities such as condition checking, looping and branching.
8. Designed for programming user events – like VB Java Script is also based on Events.
9. Easy Debugging and Testing
10. Platform Independent/ Architecture Neutral
What is JavaScript?

JavaScript is a scripting language mainly used for writing dynamic Web pages. When a script
written in JavaScript is embedded in a Web page, it will be executed by the Web browser on the client
machine.
JavaScript supports functions as first-class functions - Functions are really objects. Like regular objects,
functions can be created during execution, stored in data structure, and passed to other functions as
arguments.
• JavaScript is a client – side scripting language for the World Wide Web that is similar to the
syntax of the Java programming language.
• JavaScript is designed to provide limited programming functionality.

Why JavaScript?
• By executing more web functionality on the user’s machine, webmasters can optimize their servers
to serve more pages.
• The decrease in traffic from constant interaction with the server can also improve a server's
performance.
• Because the local machine is doing the script processing, the user can view web pages much faster

Introducing JavaScript Syntax


• A simple JavaScript program:
<html>
<head>
<Title> Hello World </Title>
</head>
<body>
<script language=“JavaScript”>
document.write(“Hello,World wide web”);
</script>
</body>
</html>

object based scripting for the web

JavaScript
 JavaScript was the first client-side scripting language developed by Netscape.
 The purpose of javascript is to provide a true programming language for use by web page authors
to add more interactivity to web pages.
 The first version of javascript contained most of the core functionality for the scripting language.

Jscript
 In order for Microsoft to support javascript standard, it had to create its own implementation of
javascript.
 The Microsoft implementation of javascript is jscript.

VBScript
 Around the time of Jscript’s release, Microsoft introduced another client-side scripting language
called Visual Basic Scripting Edition, or VBScript.
 VBScript is based upon Visual Basic Product.

Placing Scripts in Web Pages


• The client – side scripts can be integrated within a web page.
The <script> tag
 In order to embed client – side scripts in to web pages, an html tag is needed to tell the browser
that the following code is a script. This is done through the <script> tag.
 The <script> tag has the following syntax:
 <script language=[scripting language]>
 For example,
 <script language = “JavaScript”>
 The <script> tag has a closing tag, </script>, to denote the end of the script.

Placing Scripts in HTML code

 Client – Side scripts can be placed anywhere in the html text. The most common place for scripts
is head section.
 Using a script tag in the header section is a good place to define all the subroutines.
 JavaScript embedded in the <head> section
<head>
<title> Java Scripting </title>
<Script Language=“javascript”>
function display_message(string_messgae)
{
window.staus = string_message;
}
</script>
</head>
 The script can be included in the body of the html document, when the script needs to write some
dynamic code to the document as it’s being parsed upon loading.
 JavaScript example for the html body section
<script language=“javascript”>
if ( useragent == “Netscape”)
{
document.write(“Thank you for using Netscape”)
}
</script>
• Javascript example embedded in html tags
<a href=“toc.html” onMouseOver=‘window.status=“Return to table of contents”;return true’
onMouseOut=‘window.status=“ “; return true’>

Running client – side scripts

• Client side scripts are run by loading the web page in which they are embedded into a web browser
that supports that scripting language.
• Depending on how the script is to be run, the web browser either executes the code immediately or
waits until the user performs some action.

What can Client – Side Scripts Do?


• Client – Side scripts provide a mechanism for building more interactivity into web pages.
• Client – side scripts are full featured programming languages with conditional statements, control
structures, and data structures.
Tasks that can be performed by using client-side scripting are validation of form data, status bar messages,
and image rollovers and cookies manipulation.

Limitations of Scripting
 One of the biggest problems with client – side script is that the script embedded into html pages is
viewable by everyone who downloads the web page.
 Another limitation is the scope in which they can run.
 Client - Side scripts are also somewhat limited in the actions they can perform.
 Client – side scripts must contain commands that are independent of the platform on which they
are run.
 Client – Side scripts do not possess all of the power of CGI scripts. CGI script is needed to handle
the form data after it has been validated. CGI script is also needed to access information in a
database.

Basic concepts in java script

Statements
• It is simply a line of code that contains some kind of instructions.
• Example
document.write(“<h2> Hello, WWW </h2>”);

Blocks
• The grouping of statements is a block:
{
document.write(“<h2> Each line is a statement </h2>”);
document.write(“<P> These statements, <BR>”);
document.write(“are part of a block”);
}

Comments
• A single line comment is denoted with two slashes ( // )
// this is comment, it will be ignored by the browser.
• Multi line comments
/* some text */

Data Types
• There are 5 basic data types in JavaScript: string, number, Boolean, object and function.

Variables
• Variables are "containers" for storing information.

Rules for JavaScript variable names:


• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character

Declaring (Creating) JavaScript Variables


• Creating variables in JavaScript is most often referred to as "declaring" variables.
• You can declare JavaScript variables with the var statement:
var x;
var carname;
• After the declaration shown above, the variables are empty (they have no values yet).
• However, you can also assign values to the variables when you declare them:
Varx=5;
var carname="Volvo";

Expressions
• The methods that can be employed to manipulate the data are called expressions.
• There are 2 types of expressions: numerical and logical
• Numerical expressions deal with the number data type.
• Logical expression might compare two data values, including strings, to see if they match.

Numerical expressions
• Addition, subtraction, multiplication and division are all types of numeric expressions.
• Numeric operators are: +, -, *, /, %

Logical Expressions
• These are expressions that, when evaluated, can return either a true or a false.
• Logical Operators
&& - And
|| - Or
! - Not
== - Equal
!= - Not Equal
> - Greater than
>=
<
<=

java script structures

Flow Control
• Conditional statements are used to perform different actions based on different conditions.
• In JavaScript we have the following conditional statements:
• if statement - use this statement to execute some code only if a specified condition is true
• if...else statement - use this statement to execute some code if the condition is true and another
code if the condition is false
• if...else if....else statement - use this statement to select one of many blocks of code to be executed
• switch statement - use this statement to select one of many blocks of code to be executed .

If Statement
• Use the if statement to execute some code only if a specified condition is true.
Syntax
if (condition)
 {
  code to be executed if condition is true
 }
Example
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
 {
  document.write("<b>Good morning</b>");
 }
</script>

If … Else Statement
• Use the if....else statement to execute some code if a condition is true and another code if the
condition is not true.
Syntax
if (condition)
 {
  code to be executed if condition is true
 }
else
 {
  code to be executed if condition is not true
 }
Example
• <script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
var d = new Date();
var time = d.getHours();
if (time < 10)
 {
  document.write("Good morning!");
 }
else
 {
  document.write("Good day!");
 }
</script>

If...else if...else Statement


• Use the if....else if...else statement to select one of several blocks of code to be executed.
Syntax
if (condition1)
 {
  code to be executed if condition1 is true
 }
else if (condition2)
 {
  code to be executed if condition2 is true
 }
else
 {
  code to be executed if condition1 and condition2 are not true
 }

JavaScript Switch Statement


• Conditional statements are used to perform different actions based on different conditions.
• Use the switch statement to select one of many blocks of code to be executed.
• Syntax
switch(n)
{
case 1:
  execute code block 1
  break;

case 2:
  execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

Example:
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday"); break;
case 6:
  document.write("Super Saturday");   break;
case 0:
  document.write("Sleepy Sunday");   break;
default:
  document.write("I'm looking forward to this weekend!");
}
</script>

JavaScript for Loop

• Loops execute a block of code a specified number of times, or while a specified condition is true.
• JavaScript Loops
• Often when you write code, you want the same block of code to run over and over again in a row.
Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
• In JavaScript, there are two different kind of loops:
• for - loops through a block of code a specified number of times
• while - loops through a block of code while a specified condition is true

The for Loop


• The for loop is used when you know in advance how many times the script should run.

Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
Example
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{

document.write("The number is " + i);


document.write("<br />");
}
</script>
</body>
</html>

JavaScript While Loop


• The while loop loops through a block of code while a specified condition is true.
Syntax
• while (var<=endvalue)
 {
  code to be executed
 }
Example
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
 {
  document.write("The number is " + i);
  document.write("<br />");
  i++;
 }
</script>
</body>
</html>

5. Describe briefly about java script functions? (6 marks)

What is JavaScript Function?

• A function is simply a block of code with a name, which allows the block of code to be called by
other components in the scripts to perform certain tasks.
• Functions can also accept parameters that they use complete their task.

JavaScript actually comes with a number of built-in functions to accomplish a variety of tasks
Java script Function is nothing but it is a reusable code-block that is execute when the function is called.
Function is defined in the head section of the code. The syntax of JavaScript function is as follows:

function fname(prameter1,parameter2, ...)


{
   JavaScript code 1
    JavaScript code 2
   ....
}

While defining JavaScript function it’s important to remember that the keyword function should be in
lowercase otherwise JavaScript won't understand it as function. If you write function in uppercase the
code will generate error. In the JavaScript semicolon is optional but it’s better to put semi-colon as part of
best practices. All the java script code is written inside the curly braces.
Example:
 <html>
    <head>
      <script language="javascript">
         function showmessage(){
          alert("How are you");
         }
       </script>
  </head>
  <body>
    <form>
      <input type="button" value="Click
Here!" 
      onclick="showmessage()" >
    </form>
  </body>
</html>

Function starts with the function keyword and code of the function is enclosed in {..} brackets.  Functions
are written inside the head section of the html document, because function does not execute when the page
loads. The alert function displays the message "How are you", when you clicked on the button ("Click
Here")..  

To test the program click here.

What is the use of JavaScript Functions:


The java Script Function is very useful in writing the JavaScript code. It is used to group many JavaScript
codes under one name. For example you can write a function to validate email address.

Built-in functions

JavaScript provides many built in functions that ease the development of JavaScript programs. Here is the
list of the built-in JavaScript functions:
JavaScript Build in Function Function Description
The alert() built-in function displays the
alert()
alert dialog box.
The confirm() built-in function display the
confirm() confirmation dialog box. and ask the user
to determine from the two option .
The focus() built -in  function built the
focus() pointed object active and put the curser on
the text field.
indexOf()  
The prompt() built -in function display the
prompt() prompt dialog box. Inquiring the user for
input.
The select() built -in function used  to
select()
select the pointed object.
The write() built in function used to write
write()
something on the document.
   

Function arguments: Through variable you can pass argument to function. The output of the function
looks on the arguments given by you.

Example:

<html> 
<head> 
<script language="javascript"> 
function myfunction(text) 

confirm(text) 

</script> 
</head> 

<body> 
<form> 
<input type="button" 
onclick="myfunction('Do you want to delete
it!')" 
value="Delete"> 

<input type="button" 
onclick="myfunction('Do you want to save it!')" 
value="Save"> 
</form> 
</body> 
</html> 
 
java script arrays

• The array concept is used to store a set of values in a single variable name. This is a very important
concept in any programming language.

Array Definition:
• The array must first be defined before it is accessed or used. There are a variety of ways this can be
accomplished in JavaScript. Below are some of the ways of defining arrays in JavaScript.
• Arrays are JavaScript objects that are capable of storing a sequence of values. These values are
stored in indexed locations within the array. The length of an array is the number of elements of an
array accessed by using the name of the array followed by the index value of the array element
enclosed in square brackets.
Defining an Array in JavaScript:
• Array is defined in JavaScript by making use of the keyword new.
• General format of defining array in JavaScript is as follows:
var varaiblename = new Array( )
• For example, if a programmer wants to define an array exforsys, it is written as follows:
var exforsys = new Array( )
• The format for adding elements in this structure is as follows:
var varaiblename = new Array( )
variablename[0]=”element1”
variablename[1]=”element2”
variablename[2]=”element3”
variablename[3]=”element4”
…………………………..
• var Exforsys = new Array( )
Exforsys[0]=”Training”
Exforsys[1]=”Division”
Exforsys[2]=”Institute”
Exforsys[3]=”Company”
• It is also possible that if the programmer is certain of the array length, it can be defined while
defining the array itself as:
var Exforsys = new Array(4)

Shorter form:
• The elements can also be defined in a shorter form as:
var variablename=new Array(“element1”,”element2”,”elements3”,….)

Accessing Arrays in JavaScript


• You can access an array element by referring to the name of the array and the element's index
number.

Displaying Array Elements


document.write(Exforsys[1])
The length of an array starts from 0 to n-1.

The array can be created in the following methods


1. arrayName = new Array(Array length) => array size is explicitly specified, array size is predefined.
2. arrayName = new Array() =>The second example creates an array of the size 0.
java script java script objects

JavaScript Objects

This section does not describe the handling of objects in JavaScript since it is assumed the reader
possesses object oriented knowledge. It provides information about the creation of JavaScript objects,
templates, constructors, and more.

Object Creation

Generally objects may be created using the following syntax:

name = new Object()

For instance Array, Date, Number, Boolean, and String objects can be created this way as in the example
below. Most objects may be created this way except for the Math object which cannot be instantiated (an
instance of it may not be created).

ratings = new Array(6,9,8,4,5,7,8,10)


var home = new String("Residence")
var futdate = new Date()
var num1 = new Number()

String objects may also be created as follows:

var string = "This is a test."

Object Template

Objects are created using templates. Templates to objects are like cookie cutters to cookies. Cookie cutters
are used to create instances of cookies and object templates are used to create instances of objects. For
now, the template is shown. The following creates an object, olist.

function olist(elements)
{
this.elements = elements
this.listitems = new Array(elements)
this.getItem = list_getItem
this.setItem = list_setItem
}

The methods list_setItem and list_getItem are written as follows:

function list_getItem(element)

{
return this.listitems(element)
}

function list_setItem(element, stringval)


{
this.listitems(element) - stringval
}
Creating the Object
var list1 = new olist(10)

Changing the Object


list1.setItem(0,"This is the first item in the list")
list1.setItem(1,"This is the second item in the list")
list1.setItem(2,"This is the third item in the list")

The Prototype property

The prototype property can be used to create new properties of created objects (whether they are user
defined or system defined) as follows:

list1.prototype.type = "1"

The Function Constructor

Creation:

minus2 = new Function("x","return x-2")

Usage:

y = minus2(10)

The value of y is now 8.

Object use

When writing JavaScript, you are embedding the JavaScript in an object. These objects may have
functions. An example function is the alert() function. It may be called as follows:

alert("An error occurred!")

However, the window object is the highest level JavaScript object, therefore the following code does the
same thing:

window.alert("An error occurred!")

The following code using the "this" object declaration will perform the same:

this.alert("An error occurred!")

Object and Property Reference

Objects and their properties are normally referenced using a dot between each object or its property or
method. as follows:

document.write("This is a test.")
However, a method called array notation, may be used. Array notation is required when the first character
of a property name is a digit since the dot method may not be used in this case. The above example in
array notation is:

document ["write"] ("This is a test.")

Levels of Objects

JavaScript has basically three types of objects which are:

 Top level objects


 Objects that are properties of other objects
 Objects that are not properties of other objects

JavaScript has three kinds of popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click "OK" to proceed.

Syntax
alert("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body></html>

Confirm Box

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
confirm("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
 {
  alert("You pressed OK!");
 }
else
 {
  alert("You pressed Cancel!");
 }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show confirm box" />

</body>
</html>

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering
an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax
prompt("sometext","defaultvalue");
Example
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")

 {
  document.write("Hello " + name + "! How are you today?");
 }
}
</script>
</head>
<body>

<input type="button" onclick="show_prompt()" value="Show prompt box" />

</body>
</html>

<html>
<head>
<script type="text/javascript">
function disp_prompt()
{
var fname=prompt("Please enter your name:","Your name")
document.getElementById("msg").innerHTML="Greetings " + fname
}
</script>
</head>
<body>
<input type="button" onclick="disp_prompt()" value="Display a prompt box" />
<br /><br />
<div id="msg"></div>
</body>
</html>

Each HTML element has an innerHTML property that defines both the HTML code and the text that
occurs between that element's opening and closing tag. By changing an element's innerHTML after some
user interaction, you can make much more interactive pages.

However, using innerHTML requires some preparation if you want to be able to use it easily and reliably.
First, you must give the element you wish to change an id. With that id in place you will be able to use the
getElementById function, which works on all browsers.

After you have that set up you can now manipulate the text of an element. To start off, let's try changing
the text inside a bold tag.

<script type="text/javascript">
function changeText(){
document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
</script>
<p>Welcome to the site <b id='boldStuff'>dude</b> </p>
<input type='button' onclick='changeText()' value='Change Text'/>

java script regular expression


What is Regular Expressions?

A regular expression is an object that describes a pattern of characters.

When you search in a text, you can use a pattern to describe what you are searching for.

A simple pattern can be one single character.

A more complicated pattern can consist of more characters, and can be used for parsing, format checking,
substitution and more.

Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on
text.

Syntax

var txt=new RegExp(pattern,modifiers);

or more simply:

var txt=/pattern/modifiers;
 pattern specifies the pattern of an expression
 modifiers specify if a search should be global, case-sensitive, etc.

RegExp Modifiers

Modifiers are used to perform case-insensitive and global searches.

The i modifier is used to perform case-insensitive matching.

The g modifier is used to perform a global match (find all matches rather than stopping after the first
match).

Example 1

Do a case-insensitive search for "w3schools" in a string:

var str="Visit W3Schools";


var patt1=/w3schools/i;

The marked text below shows where the expression gets a match:

Visit W3Schools

Example 2

Do a global search for "is":


var str="Is this all there is?";
var patt1=/is/g;

The marked text below shows where the expression gets a match:

Is this all there is?

Example 3

Do a global, case-insensitive search for "is":

var str="Is this all there is?";


var patt1=/is/gi;

The marked text below shows where the expression gets a match:

Is this all there is?

test()

The test() method searches a string for a specified value, and returns true or false, depending on the result.

The following example searches a string for the character "e":

Example
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

Since there is an "e" in the string, the output of the code above will be:

true

exec()

The exec() method searches a string for a specified value, and returns the text of the found value. If no
match is found, it returns null.

The following example searches a string for the character "e":

Example 1
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:

VB SCRIPTS

 VBScript is a Microsoft scripting language.


 VBScript is the default scripting language in ASP.
 Client-side VBScript only works in Internet Explorer!

VBScript

 VBScript is a scripting language


 A scripting language is a lightweight programming language
 VBScript is a light version of Microsoft's programming language Visual Basic

How does it Work?

When a VBScript is inserted into an HTML document, the Internet browser will read the HTML and
interpret the VBScript. The VBScript can be executed immediately, or at a later event.

The HTML <script> tag is used to insert a VBScript into an HTML page.

Put a VBScript into an HTML Page

The example below shows how to use VBScript to write text on a web page:

Example
<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>

The example below shows how to add HTML tags to the VBScript:

Example (IE Only


<html>
<body>
<script type="text/vbscript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>

Example Explained
To insert a VBScript into an HTML page, we use the <script> tag. Inside the <script> tag we use the type
attribute to define the scripting language.

So, the <script type="text/vbscript"> and </script> tells where the VBScript starts and ends:

<html>
<body>
<script type="text/vbscript">
...
</script>
</body>
</html>

The document.write command is a standard VBScript command for writing output to a page.

By entering the document.write command between the <script> and </script> tags, the browser will
recognize it as a VBScript command and execute the code line. In this case the browser will write Hello
World! to the page:

<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>

How to Handle Simple Browsers

Browsers that do not support scripting, will display VBScript as page content.To prevent them from doing
this, the HTML comment tag should be used to "hide" the VBScript.Just add an HTML comment tag <!--
before the first VBScript statement, and a --> (end of comment) after the last VBScript statement, like
this:

<html>
<body>
<script type="text/vbscript">
<!--
document.write("Hello World!")
-->
</script>
</body>
</html>

VBScripts can be placed in the body and in the head section of an HTML document.

Where to Put the VBScript

VBScripts in a page will be executed immediately while the page loads into the browser. This is not
always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such
as when a user clicks a button. When this is the case we put the script inside a function or a sub procedure,
you will learn about procedures in a later chapter.

Scripts in <head>

Put your functions and sub procedures in the head section, this way they are all in one place, and they do
not interfere with page content.

Example (IE Only)


<html>
<head>
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>

<body onload="myFunction()">
</body>
</html>

Scripts in <body>

If you don't want your script to be placed inside a function, and especially if your script should write page
content, it should be placed in the body section.

Example (IE Only)


<html>
<head>
</head>

<body>
<script type="text/vbscript">
document.write("This message is written by VBScript")
</script>
</body>

</html>

Scripts in <head> and <body>

You can place an unlimited number of scripts in your document, and you can have scripts in both the body
and the head section.

Example (IE Only)


<html>
<head>
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>

<body>
<button onclick="myFunction()">Click me</button>
<script type="text/vbscript">
document.write("This message is written by VBScript")
</script>
</body>
</html>

Using an External VBScript

If you want to run the same VBScript on several pages, without having to write the same script on every
page, you can write a VBScript in an external file.

Save the external VBScript file with a .vbs file extension.

Note: The external script cannot contain the <script> tag!

To use the external script, point to the .vbs file in the "src" attribute of the <script> tag:

Example
<html>
<head>
<script type="text/vbscript" src="ex.vbs"></script>
</head>
<body>
</body>
</html>

VBSCRIPT variables
Variables are "containers" for storing information.

VBScript Variables

As with algebra, VBScript variables are used to hold values or expressions.

A variable can have a short name, like x, or a more descriptive name, like carname.

Rules for VBScript variable names:

 Must begin with a letter 


 Cannot contain a period (.)
 Cannot exceed 255 characters
In VBScript, all variables are of type variant that can store different types of data.

Declaring (Creating) VBScript Variables

Creating variables in VBScript is most often referred to as "declaring" variables.

You can declare VBScript variables with the Dim, Public or the Private statement. Like this:

Dim x
Dim carname

Now you have created two variables. The name of the variables are "x" and "carname".

You can also declare variables by using its name in a script. Like this:

carname="Volvo"

Now you have also created a variable. The name of the variable is "carname". However, this method is not
a good practice, because you can misspell the variable name later in your script, and that can cause strange
results when your script is running.

If you misspell for example the "carname" variable to "carnime", the script will automatically create a new
variable called "carnime".  To prevent your script from doing this, you can use the Option Explicit
statement. This statement forces you to declare all your variables with the dim, public or private statement.

Put the Option Explicit statement on the top of your script. Like this:

Option Explicit
Dim carname
carname=some value

Assigning Values to Variables

You assign a value to a variable like this:

carname="Volvo"
x=10

The variable name is on the left side of the expression and the value you want to assign to the variable is
on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of
"10".

Lifetime of Variables

How long a variable exists is its lifetime.


When you declare a variable within a procedure, the variable can only be accessed within that procedure.
When the procedure exits, the variable is destroyed. These variables are called local variables. You can
have local variables with the same name in different procedures, because each is recognized only by the
procedure in which it is declared.

If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of
these variables starts when they are declared, and ends when the page is closed.

11. Write a short note on VBSCRIPT array variables? (5 marks)

An array variable is used to store multiple values in a single variable.

In the following example, an array containing 3 elements is declared:

Dim names(2)

The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a
fixed-size array. You assign data to each of the elements of the array like this:

names(0)="Tove"
names(1)="Jani"
names(2)="Stale"

Similarly, the data can be retrieved from any element using the index of the particular array element you
want. Like this:

mother=names(0)

You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the
numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and
7 columns:

Dim table(4,6)

Assign data to a two-dimensional array:

Example (IE Only)


<html>
<body>

<script type="text/vbscript">
Dim x(2,2)
x(0,0)="Volvo"
x(0,1)="BMW"
x(0,2)="Ford"
x(1,0)="Apple"
x(1,1)="Orange"
x(1,2)="Banana"
x(2,0)="Coke"
x(2,1)="Pepsi"
x(2,2)="Sprite"
for i=0 to 2
    document.write("<p>")
    for j=0 to 2
        document.write(x(i,j) & "<br />")
    next
    document.write("</p>")
next
</script>

</body>
</html>
VBSCRIPT procedures
VBScript has two kinds’ procedures:

 Sub procedure
 Function procedure

VBSCRIPT SUB PROCEDURES

A Sub procedure:

 is a series of statements, enclosed by the Sub and End Sub statements


 can perform actions, but does not return a value
 can take arguments
 without arguments, it must include an empty set of parentheses ()

Sub mysub()
  some statements
End Sub

or

Sub mysub(argument1,argument2)
  some statements
End Sub
Example (IE Only)
Sub mysub()
  alert("Hello World")
End Sub

VBSCRIPT FUNCTION PROCEDURES

A Function procedure:
 is a series of statements, enclosed by the Function and End Function statements
 can perform actions and can return a value
 can take arguments that are passed to it by a calling procedure
 without arguments, must include an empty set of parentheses ()
 returns a value by assigning a value to its name

Function myfunction()
  some statements
  myfunction=some value
End Function

or

Function myfunction(argument1,argument2)
  some statements
  myfunction=some value
End Function
Example (IE Only)
function myfunction()
  myfunction=Date()
end function

How to Call a Procedure

There are different ways to call a procedure. You can call it from within another procedure, on an event,
or call it within a script.

Example (IE Only)

Call a procedure when the user clicks on a button:

<body>
<button onclick="myfunction()">Click me</button>
</body>

Procedures can be used to get a variable value:

carname=findname()

Here you call a Function called "findname", the Function returns a value that will be stored in the variable
"carname".

Function procedures can calculate the sum of two arguments:


Example (IE Only)
Function myfunction(a,b)
myfunction=a+b
End Function

document.write(myfunction(5,9))

The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14.

When you call a procedure you can use the Call statement, like this:

Call MyProc(argument)

Or, you can omit the Call statement, like this:

MyProc argument

13. Write short notes on VBSCRIPT conditional statements? (6 marks)

Conditional statements are used to perform different actions for different decisions.

In VBScript we have four conditional statements:

 If statement - executes a set of code when a condition is true


 If...Then...Else statement - select one of two sets of lines to execute
 If...Then...ElseIf statement - select one of many sets of lines to execute
 Select Case statement - select one of many sets of lines to execute

If...Then...Else

Use the If...Then...Else statement if you want to

 execute some code if a condition is true


 select one of two blocks of code to execute

If you want to execute only one statement when a condition is true, you can write the code on one line:

If i=10 Then alert("Hello")

There is no ..Else.. in this syntax. You just tell the code to perform one action if a condition is true (in this
case If i=10).

If you want to execute more than one statement when a condition is true, you must put each statement on
separate lines, and end the statement with the keyword "End If":

If i=10 Then
alert("Hello")
i = i+1
End If

There is no ..Else.. in the example above either. You just tell the code to perform multiple actions if the
condition is true.

If you want to execute a statement if a condition is true and execute another statement if the condition is
not true, you must add the "Else" keyword:

Example (IE Only)


<html>
<body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i < 10 Then
  document.write("Good morning!")
Else
  document.write("Have a nice day!")
End If
End Function
</script>
</head>

<body onload="greeting()">
</body>

</html>

In the example above, the first block of code will be executed if the condition is true, and the other block
will be executed otherwise (if i is greater than 10).

If...Then...ElseIf

You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:

Example (IE Only)


<html>
<body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i = 10 Then
  document.write("Just started...!")
ElseIf i = 11 then
  document.write("Hungry!")
ElseIf i = 12 then
  document.write("Ah, lunch-time!")
ElseIf i = 16 then
  document.write("Time to go home!")
Else
  document.write("Unknown")
End If
End Function
</script>
</head>

<body onload="greeting()">
</body>

</html>

Select Case

You can also use the "Select Case" statement if you want to select one of many blocks of code to execute:

Example (IE Only)


<html>
<body>
<script type="text/vbscript">
d=weekday(date)
Select Case d
  Case 1
    document.write("Sleepy Sunday")
  Case 2
    document.write("Monday again!")

  Case 3
    document.write("Just Tuesday!")
  Case 4
    document.write("Wednesday!")
  Case 5
    document.write("Thursday...")
  Case 6
    document.write("Finally Friday!")
  Case else
    document.write("Super Saturday!!!!")
End Select
</script>

</body>
</html>

VBSCRIPT looping statements

Looping statements are used to run the same block of code a specified number of times.
In VBScript we have four looping statements:

 For...Next statement - runs code a specified number of times


 For Each...Next statement - runs code for each item in a collection or each element of an array
 Do...Loop statement - loops while or until a condition is true
 While...Wend statement - Do not use it - use the Do...Loop statement instead

For...Next Loop

Use the For...Next statement to run a block of code a specified number of times.

The For statement specifies the counter variable (i), and its start and end values. The Next statement
increases the counter variable (i) by one.

Example
<html>
<body>

<script type="text/vbscript">
For i = 0 To 5
  document.write("The number is " & i & "<br />")
Next
</script>

</body>
</html>

The Step Keyword

With the Step keyword, you can increase or decrease the counter variable by the value you specify.

In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats.

For i=2 To 10 Step 2


  some code
Next

To decrease the counter variable, you must use a negative Step value. You must specify an end value that
is less than the start value.

In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats.

For i=10 To 2 Step -2


  some code
Next

Exit a For...Next

You can exit a For...Next statement with the Exit For keyword.
For i=1 To 10
  If i=5 Then Exit For
  some code
Next

For Each...Next Loop

A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an
array.

Example
<html>
<body>
<script type="text/vbscript">
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

For Each x In cars


  document.write(x & "<br />")
Next
</script>
</body>
</html>

Do...Loop

If you don't know how many repetitions you want, use a Do...Loop statement.

The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes
true.

Repeat Code While a Condition is True

You use the While keyword to check a condition in a Do...Loop statement.

Do While i>10
  some code
Loop

If i equals 9, the code inside the loop above will never be executed.

Do
  some code
Loop While i>10

The code inside this loop will be executed at least one time, even if i is less than 10.
Repeat Code Until a Condition Becomes True

You use the until keyword to check a condition in a Do...Loop statement.

Do Until i=10
  some code
Loop

If i equals 10, the code inside the loop will never be executed.

Do
  some code
Loop Until i=10

The code inside this loop will be executed at least one time, even if i is equal to 10.

Exit a Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.

Do Until i=10
  i=i-1
  If i<10 Then Exit Do
Loop

The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than
10.

Example programs in VBSCRIPT

PROGRAM1:

<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>
Hello World!

<html>
<body>
<script type="text/vbscript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>

OUTPUT:
Hello World!

PROGRAM2:

<html>
<head>
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>
<body onload="myFunction()">
<p>We usually use the head section for functions (to be sure that the functions are loaded before they are
called).</p>
</body>
</html>
<html>
<body>
<script type="text/vbscript">
document.write("This message is written by VBScript")
</script>
</body>
</html>
document.write("Today's date is " & Date())

document.write("<br />")
document.write("The time is " & Time())
p>VBScripts' function <b>WeekdayName</b> is used to get a weekday:</p>
<script type="text/vbscript">
document.write("<p>")
document.write(WeekDayName(1))
document.write("<br />")
document.write(WeekDayName(2))
document.write("</p><p>")
document.write("Get the abbreviated name of a weekday:")
document.write("<br />")
document.write(WeekDayName(1,True))
document.write("<br />")
document.write(WeekDayName(2,True))
document.write("</p><p>")
document.write("Get the current weekday:")
document.write("<br />")
document.write(WeekdayName(weekday(Date)))
document.write("<br />")
document.write(WeekdayName(weekday(Date), True))
document.write("</p>")

OUTPUT:
VBScripts' function WeekdayName is used to get a weekday:

Sunday
Monday

Get the abbreviated name of a weekday:


Sun
Mon

Get the current weekday:


Tuesday
Tue

PROGRAM3:
<html>
<body>
<script type="text/vbscript">
i=0
Do While i < 10
document.write(i & "<br />")
i=i+1
Loop
</script>
</body>
</html>

OUTPUT:
0
1
2
3
4
5
6
7
8
9

PROGRAM4:

<html>
<body>
<script type="text/vbscript">
Randomize()
document.write(Rnd())
</script>
</body>
</html>
OUTPUT:
0.2730219

JAVA SERVER PAGE (JSP)

Java Server Pages (JSP) is a Java technology that allows software developers to dynamically generate
HTML, XML or other types of documents in response to a Web client request. The technology allows
Java code and certain pre-defined actions to be embedded into static content.
The JSP syntax adds additional XML-like tags, called JSP actions, to be used to invoke built-in
functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as
extensions to the standard HTML or XML tags. Tag libraries provide a platform independent way of
extending the capabilities of a Web server.

JSPs are compiled into Java Servlets by a JSP compiler. A JSP compiler may generate a servlet in Java
code that is then compiled by the Java compiler, or it may generate byte code for the servlet directly. JSPs
can also be interpreted on-the-fly reducing the time taken to reload changes.

Java Servlet technology and Java Server Pages (JSP pages) are server-side technologies that have
dominated the server-side Java technology market; they've become the standard way to develop
commercial web applications.

Java developers love these technologies for myriad reasons, including: the technologies are fairly easy to
learn, and they bring the Write Once, Run Anywhere paradigm to web applications. More importantly, if
used effectively by following best practices, servlets and JSP pages help separate presentation from
content.

Best practices are proven approaches for developing quality, reusable, and easily maintainable servlet- and
JSP-based web applications. For instance, embedded Java code (scriptlets) in sections of HTML

documents can result in complex applications that are not efficient, and difficult to reuse, enhance, and
maintain. Best practices can change all that.
In this article, I'll present important best practices for servlets and JSP pages; I assume that you have basic
working knowledge of both technologies.
This article:
 Presents an overview of Java servlets and Java Server pages (JSP pages)
 Provides hints, tips, and guidelines for working with servlets and JSP pages
 Provides best practices for servlets and JSP pages
 Overview of Servlets and JSP Pages

Similar to Common Gateway Interface (CGI) scripts, servlets support a request and response
programming model. When a client sends a request to the server, the server sends the request to the
servlet. The servlet then constructs a response that the server sends back to the client. Unlike CGI scripts,
however, servlets run within the same process as the HTTP server.

When a client request is made, the service method is called and passed a request and response object. The
servlet first determines whether the request is a GET or POST operation. It then calls one of the following
methods: doGet or doPost. The doGet method is called if the request is GET, and doPost is called if the
request is POST. Both doGet and doPost take request (HttpServletRequest) and response
(HttpServletResponse).
In the simplest terms, then, servlets are Java classes that can generate dynamic HTML content using print
statements. What is important to note about servlets, however, is that they run in a container, and the APIs
provide session and object life-cycle management?
Consequently, when you use servlets, you gain all the benefits from the Java platform, which include the
sandbox (security), database access API via JDBC, and cross-platform portability of servlets.

INTRODUCTION TO JAVASERVER PAGES (JSP)


JAVASERVER PAGES (JSP)
• JavaServer Pages (JSP) is an extension of servlet technology.
• JSPs simplify the delivery of dynamic web content. They allow web programmers to
create dynamic content by reusing predefined components and by interacting with
components using server-side scripting.
• JSPs can reuse JavaBeans and create custom tag libraries that encapsulate complex,
dynamic functionality.
JSP classes and interfaces can be found in packages javax.servlet.jsp and javax.servlet.jsp.tagext
• There are four key components to JSPs
1. Directives: messages to the JSP container (server component executing the JSP) that enable
the programmer to specify page settings, include content from other resources and specify
custom tag libraries to use in a JSP.
2. Actions: encapsulate functionality based on the information send to the server as part of a
specific client request. They can also create Java objects for use in JSP scriplets.
3. Scripting elements: enable the programmer to insert Java code that interacts with
components in a JSP to perform request processing.
4. Tag libraries: are part of the tag extension mechanism that enables programmers to create
custom tags. Typically, most useful for web page designers with little knowledge of Java.

• In some ways, JSPs look like standard XHTML or XML documents.


• JSPs normally include XHTML or XML markup. Such markup is known as fixed-template data or
fixed-template text.
Fixed-template data/text often helps a programmer decide whether to use a servlet or a JSP. Recall that
JSPs are most often used when most of the content send to the client is fixed-template data and little or
none of the content is generated dynamically with Java code. Servlets are more commonly used when
only a small amount of the content returned to the client is fixed-template data.

• When a JSP-enabled server receives the first request for a JSP, the JSP container translates the JSP
into a Java servlet that handles the current request as well as all future requests to the JSP.
• Literal text in the JSP becomes string literals in the servlet that represents the translated JSP.
• Any errors that occur in compiling the new servlet result in translation-time errors.
• The JSP container places the Java statements that implement the JSP’s response in method
_jspService at translation time.
• If the new servlet compiles properly, the JSP container invokes method _jspService to process the
request.
• The JSP may respond directly or may invoke other web application components to assist in
processing the request. Any errors that occur during request processing are known as request-time
errors.
• Overall, the request-response mechanism and the JSP life-cycle are the same as those of a servlet.
• JSPs can override methods jspInit and jspDestroy (similar to servlet methods init and destroy),
which the JSP container invokes when initializing and terminating a JSP.
• A JSP programmer defines these methods using JSP declarations which are part of the scripting
mechanism.
The JSP technology--which abstracts servlets to a higher level--is an open, freely available specification
developed by Sun Microsystems as an alternative to Microsoft's Active Server Pages (ASP) technology,
and a key component of the Java 2 Enterprise Edition (J2EE) specification. Many of the commercially
available application servers (such as BEA WebLogic, IBM WebSphere, Live JRun, Orion, and so on)
support JSP technology.

Running JSP Applications

How Do JSP Pages Work?

A JSP page is basically a web page with traditional HTML and bits of Java code. The file extension of a
JSP page is .jsp rather than .html or .htm, which tells the server that this page requires special handling
that will be accomplished by a server extension or a plug-in.

When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet. At this point the
servlet is handled by the servlet engine, just like any other servlet. The servlet engine then loads the
servlet class (using a class loader) and executes it to create dynamic HTML to be sent to the browser, as
shown in Figure 1. The servlet creates any necessary object, and writes any object as a string to an output
stream to the browser.

• Much like a servlet, a JSP must first be processed by a web server before it can be displayed in a
web browser. The web server must support JSPs and the JSP page must be stored in a file with a
.jsp extension. The web server translates the JSP into a Java servlet, compiles the servlet, and
executes it. The result of the execution is sent to the browser for display.

Request/Response flow calling a JSP page


The next time the page is requested, the JSP engine executes the already-loaded servlet unless the JSP
page has changed, in which case it is automatically recompiled into a servlet and executed.

Don't overuse Java code in HTML pages: Putting all Java code directly in the JSP page is OK for very
simple applications. But overusing this feature leads to spaghetti code that is not easy to read and
understand. One way to minimize Java code in HTML pages is to write separate Java classes that perform
the computations. Once these classes are tested, instances can be created.

Choose the right include mechanism: Static data such as headers, footers, and navigation bar content is
best kept in separate files and not regenerated dynamically. Once such content is in separate files, they can
be included in all pages using one of the following include mechanisms:

Include directive: <%@ include file="filename" %>


Include action: <jsp:include page="page.jsp" flush="true" />
The first include mechanism includes the content of the specified file while the JSP page is being
converted to a servlet (translation phase), and the second include includes the response generated after the
specified page is executed. I'd recommend using the include directive, which is fast in terms of
performance, if the file doesn't change often; and use the include action for content that changes often or if
the page to be included cannot be decided until the main page is executed.

Another include mechanism is the <c:import> action tag provided by the JavaServer pages Standard Tag
Library (JSTL). You can use this tag to bring in, or import, content from local and remote sources. Here
are some examples:
<c:import url="./copyright.html"/>
<c:import url="http://www.somewhere.com/hello.xml"/>

Don't mix business logic with presentation: For advanced applications, and when more code is
involved, it's important not to mix business logic with front-end presentation in the same file. Separating
business logic from presentation permits changes to either side without affecting the other. However,
production JSP code should be limited to front-end presentation. So, how do you implement the business
logic part? That is where JavaBeans technology comes into play. This technology is a portable, platform-
independent component model that lets developers write components and reuse them everywhere.

In the context of JSP pages, JavaBeans components contain business logic that returns data to a script on a
JSP page, which in turn formats the data returned from the JavaBeans component for display by the
browser. A JSP page uses a JavaBeans component by setting and getting the properties that it provides.

The benefits of using JavaBeans components to augment JSP pages are:

Reusable components: Different applications will be able to reuse the components.

Separation of business logic and presentation logic: You can change the way data is displayed without
affecting business logic. In other words, web page designers can focus on presentation and Java
developers can focus on business logic.Protects your intellectual property by keeping source code secure.

If you use Enterprise JavaBeans (EJBs) components with your application, the business logic should
remain in the EJB components which provide life-cycle management, transaction support, and multi-client
access to domain objects (Entity Beans). Please refer to the Enterprise BluePrints for further details on
this.
Use custom tags: Embedding bits of Java code (or scriptlets) in HTML documents may not be suitable for
all HTML content developers, perhaps because they do not know the Java language and don't care to learn
its syntax. While JavaBeans components can be used to encapsulate much of the Java code, using them in
JSP pages still requires content developers to have some knowledge of Java syntax.
JSP technology allows you to introduce new custom tags through the tag library facility.
As a Java developer, you can extend JSP pages by introducing custom tags that can be deployed and used
in an HTML-like syntax. Custom tags also allow you to provide better packaging by improving the
separation between business logic and presentation logic. In addition, they provide a means of
customizing presentation where this cannot be done easily with JSTL.

Some of the benefits of custom tags are:

They can eliminate scriptlets in your JSP applications. Any necessary parameters to the tag can be passed
as attributes or body content, and therefore no Java code is needed to initialize or set component
properties.
They have simpler syntax. Scriptlets are written in Java code, but custom tags can be used in an HTML-
like syntax.
They can improve the productivity of nonprogrammer content developers, by allowing them to perform
tasks that cannot be done with HTML.
They are reusable. They save development and testing time. Scriptlets are not reusable, unless you call
cut-and-paste "reuse."
In short, you can use custom tags to accomplish complex tasks the same way you use HTML to create a
presentation.

Support for the Model-View-Controller Paradigm

JSP Model 1 Architecture

The Model 2 architecture, as shown in Figure 3, integrates the use of both servlets and JSP pages. In this
mode, JSP pages are used for the presentation layer, and servlets for processing tasks. The servlet acts as a
controller responsible for processing requests and creating any beans needed by the JSP page. The
controller is also responsible for deciding to which JSP page to forward the request. The JSP page
retrieves objects created by the servlet and extracts dynamic content for insertion within a template.

JSP Model 2 Architecture


This model promotes the use of the Model View Controller (MVC) architectural style design pattern. Note
that several frameworks already exist that implement this useful design pattern, and that truly separate
presentation from content. The Apache Struts is a formalized framework for MVC. This framework is best
used for complex applications where a single request or form submission can result in substantially
different-looking results.

Tag Libraries and Files JSP OBJECTS

The following programming guidelines are handy when writing custom tag libraries:

 Keep it simple: If a tag requires several attributes, try to break it up into several tags.
 Make it usable: Consult the users of the tags (HTML developers) to achieve a high degree
of usability.
 Try not to re-invent the wheel: There are several JSP tag libraries available, such as the
Jakarta Taglibs Project. Check to see if what you want is already available.
 Do not reinvent the wheel: While custom tags provide a way to reuse valuable
components, they still need to be created, tested, and debugged. In addition, developers
often have to reinvent the wheel over and over again and the solutions may not be the most
efficient. This is the problem that the Java Server Pages Standard Tag Library (JSTL)
solves, by providing a set of reusable standard tags. JSTL defines a standard tag library that
works the same everywhere, so you no longer have to iterate over collections using a
scriptlet (or iteration tags from numerous vendors).
 The JSTL includes tags for looping, reading attributes without Java syntax, iterating over
various data structures, evaluating expressions conditionally, setting attributes and scripting
variables in a concise manner, and parsing XML documents.
 Use the JSTL Expression Language: Information to be passed to JSP pages is
communicated using JSP scoped attributes and request parameters. An expression language
(EL), which is designed specifically for page authors, promotes JSP scoped attributes as the
standard way to communicate information from business logic to JSP pages.
 Integrating Servlets and JSP Pages: The JSP specification presents two approaches for
building web applications using JSP pages: JSP Model 1 and Model 2 architectures. These
two models differ in the location where the processing takes place. In Model 1 architecture,

as shown in Figure, the JSP page is responsible for processing requests and sending back
replies to clients.
 This example shows how to set the value of a JavaBean property to the value of a request
parameter with the same name using the JSP standard action jsp:setProperty and how to get
the values of JavaBean properties using JSP standard action jsp:getProperty .

<html>
<head>
<title>Example - Set Property from Associated Parameter</title>
</head>
<body>
<pre>
<jsp:useBean id="bean"
class="com.stevefarmer.support.TestClass"
scope="session"/>
<jsp:setProperty name="bean" property="classPropInt" />
<jsp:setProperty name="bean" property="classPropFloat" />
<jsp:setProperty name="bean" property="classPropBool" />
<jsp:setProperty name="bean" property="classPropString"/>
bean.classPropInt = <jsp:getProperty name="bean"
property="classPropInt" />
bean.classPropFloat = <jsp:getProperty name="bean"
property="classPropFloat" />
bean.classPropBool = <jsp:getProperty name="bean"
property="classPropBool" />
bean.classPropString = '<jsp:getProperty name="bean"
property="classPropString"/>'
</pre>
</body>
</html>

JavaBeans Classes and JSP

Example- Create Bean with Interface Type

This example shows how to create a new JavaBean using the JSP standard action jsp:useBean and its class
and type attributes and how to get the values of JavaBean properties using JSP standard action
jsp:getProperty .

<html>
<head>
<title>Example - Create Bean with Interface Type</title>
</head>
<body>
<pre>
<jsp:useBean id="bean"
class="com.stevefarmer.support.TestClass"
type="com.stevefarmer.support.TestType"/>
bean.typePropInt = <jsp:getProperty name="bean"
property="typePropInt" />
bean.typePropFloat = <jsp:getProperty name="bean"
property="typePropFloat" />
bean.typePropBool = <jsp:getProperty name="bean"
property="typePropBool" />
bean.typePropString = '<jsp:getProperty name="bean"
property="typePropString"/>'
</pre>
</body>
</html>

SCRIPTING IN JSP PAGES http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Bios.html

JSP scripting elements allow you to use Java programming language statements in your JSP pages.
Scripting elements are typically used to create and access objects, define methods, and manage the flow of
control. Many tasks that require the use of scripts can be eliminated by using custom tag libraries, in
particular the JSP Standard Tag Library. Because one of the goals of JSP technology is to separate static
data from the code needed to dynamically generate content, very sparing use of JSP scripting is
recommended. Nevertheless, there may be some circumstances that require its use.

There are three ways to create and use objects in scripting elements:
 Instance and class variables of the JSP page's servlet class are created in
declarations and accessed in scriptlets and expressions.
 Local variables of the JSP page's servlet class are created and used in scriptlets and
expressions.
 Attributes of scope objects (see Using Scope Objects) are created and used in
scriptlets and expressions.
 JSPs often present dynamically generated content as part of an XHTML document that is
sent to the client in response to a request.
 In some cases, the content is static, but is output only if certain conditions are met during a
request (e.g., providing values in a form that submits a request).
 JSP programmers can insert Java code and logic in a JSP using scripting.

• JSP scripting components include scriplets, comments, expressions, declarations, and


escape sequences.
• Scriplets are blocks of code delimited by <% and %>. They contain Java statements that
the container places in method _jspService at translation time.
• Comments come in three flavors in JSPs: JSP comments, XHTML comments, and scripting
language comments.
– JSP comments are delimited by <%-- and --%>. Can be placed throughout the JSP
except inside scriplets.
– XHTML comments are delimited by <!-- and -->. Can be placed anywhere in the
JSP except inside scriplets.
– Scripting language comments are Java comments (Java is currently the only JSP
scripting language which is allowed). Scriplets can use either // or /* and */ as in
normal Java.
• JSP comments and scripting language comments are ignored and do not appear in the
response to a client. When clients view the source code of a JSP response, they will see
only the XHTML comments in the source code.
– The different comment styles are useful for separating comments that the user
should be able to see from those that document logic processed on the server-side.
• Expressions are delimited by <%= and %> and contain a Java expression that is evaluated
when a client requests the JSP containing the expression. The contained converts the result
of a JSP expression to a String object, then outputs the String as part of the response to the
client.
• Declarations are delimited by <%! and %>. Declarations enable the JSP programmer to
define variables and methods for use in a JSP. Variables become instance variables of the
servlet class that represents the translated JSP. Similarly, methods become members of the
class that represents the translated JSP. Declaration of variables and methods in a JSP use
Java syntax such as:

<%! int increment = 0; %>

• Escape sequences are necessary to include special characters or character sequences that
the JSP container normally uses to delimit JSP code.
– Example: literal: <%, escape sequence is: <\%
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- welcome.jsp -->
<!-- JSP that processes a "get" request containing data. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<!-- head section of document -->
<head>
<title>A JSP that processes "get" requests with data</title>
</head>

<!-- body section of document -->


<body>
<% // begin scriptlet
String name = request.getParameter( "firstName" );
if ( name != null )
{
%> <%-- end scriptlet to insert fixed template data --%>
<h1>
Hello <%= name %>, <br />
Welcome to JavaServer Pages Technology!
</h1>
<% // continue scriptlet
} // end if
else {
%> <%-- end scriptlet to insert fixed template data --%>
<form action = "welcome.jsp" method = "get">
<p>Type your first name and press Submit</p>

<p><input type = "text" name = "firstName" />


<input type = "submit" value = "Submit" />
</p>
</form>
<% // continue scriptlet
} // end else
%> <%-- end scriptlet --%>
</body>
</html> <!-- end XHTML document -->

STANDARD ACTIONS

Example – Set Property from value

This example shows how to set the value of a JavaBean property using the JSP standard action
jsp:setProperty and its value attribute and how to get the values of JavaBean properties using JSP standard
action jsp:getProperty .

<html>
<head>
<title>Example - Set Property from Value</title>
</head>
<body>
<pre>

<jsp:useBean id="bean"
class="com.stevefarmer.support.TestClass"
scope="page"/>
<jsp:setProperty name="bean"
property="classPropInt" value="11" />
<jsp:setProperty name="bean"
property="classPropFloat" value="1.1" />
<jsp:setProperty name="bean"
property="classPropBool" value="true" />
<jsp:setProperty name="bean"
property="classPropString" value="String 1"/>
bean.classPropInt = <jsp:getProperty name="bean"
property="classPropInt" />
bean.classPropFloat = <jsp:getProperty name="bean"
property="classPropFloat" />
bean.classPropBool = <jsp:getProperty name="bean"
property="classPropBool" />
bean.classPropString = '<jsp:getProperty name="bean"
property="classPropString"/>'
</pre>
</body>
</html>

JSP DIRECTIVES

<%@ page language="lang" %>

Sets the JSP script language to lang. Defaults to Java. All JSP 1.0 implementations must support Java.
Some implementations, like Resin, may support other scripting languages, e.g. JavaScript.

<%@ page import="package" %>

Adds to the Java package import list for the generated Java file.

Note: Only relevant when using Java.

<%@ page errorPage="path" %>

Defines a page to display if an error occurs in the JSP page.

Robust applications can return informative error pages when something goes wrong in a file, for example
if a database is overloaded. path is returned as the response file. The error page can use the additional
implicit variable exception containing the thrown exception.

path is relative to the current page. Its root is relative to the application root.
The error page itself can be a JSP page. If the error page is a JSP page, it can use the implicit variable
exception to get information about the thrown exception.

<%@ page isErrorPage="true" %>

Gives an error page access to the exception implicit variable. Default to false.

errorpage.jsp

<%@ page isErrorPage="true" %>


<h1>Received error <%= exception.message %></h1>

<%@ include file="path" %>

Includes the raw file path at translation time.

The include directive is a replacement for an SSI include (or the C '#include'). It includes the contents of
the file at path into the JSP file. The included file is parsed as JSP, so it can have active elements like
expressions, declarations and scriptlets.

path is relative to the current page, and its root is the application root.

<html><head>
<title><%= title %></title>
</head>
<body color=white>

page.jsp

<% var title = "Hello, World"; %>


<%@ include file='header.jsp' %>

<h1><%= title %></h1>

<%@ page buffer=sizekb %>

Gives the size of the page buffer in kb or none for no buffer. Default 8kb. If buffer is none, all output is
immediately flushed.

JSP 1.0 gives page writers flexibility by buffering its output before sending the response to HTTP. The
buffering allows error recovery and forwarding, even after generating some content. Once the buffer has
filled, it will be flushed. So applications must still detect their errors early.
The following example generates an XML section (for variety). If the form's query is missing the 'name'
parameter, it will redirect the results.

<?xml version='1.0'?>
<form>
<%
if (request.form["name"] == null)
pageContext.forward("redo-form.jsp");
for (var name in request.form) {
out.print("<" + name + ">");

out.print(request.form[name]);
out.println("</" + name + ">");
}
%>
</form>

<%@ page autoFlush="true" %>


Tells JSP to flush the page buffer when it fills. Default is true.
If autoFlush is false, the JSP engine will throw an exception if the buffer overflows.

<%@ page session="true" %>


Tells JSP that the page participates in a session. Defaults to true.
The session declaration makes the session implicit variable available to a JSP page.

<%@ page isThreadSafe="true" %>

Tells the JSP that multiple pages can execute in parallel. Defaults to true.

JSP pages are always responsible for synchronization of shared variables, such as the session and
application variables. In some rare cases, a page may use servlet variables (created with a declaration), and
be too lazy to handle the synchronization.

Even with isThreadSafe=false, the JSP engine may create multiple instances of the JSP servlet. So the
page author can never absolve herself of synchronization issues.

In the following example, a JSP engine might create 3 servlet instances of the page. So three calls to the
same page may return counts of 17, 3 and 398. In addition, the JSP engine is free to destroy and recreate
the servlet at any time, essentially resetting the counter to 0.

<%@ page isThreadSafe="false" %>


<%! var count = 0; %>
<h1>Welcome, visitor <%= count++ %>

<%@ page info="description" %>

Gives a brief description for the page.


<%@ page contentType="description" %>
Sets the content type and character encoding of the page.
contentType can also set the character encoding, for example to utf-8.
<%@ page contentType="text/plain; charset=utf-8" %>
<%! var count = 0; %>
<h1>Welcome, visitor <%= count++ %>

<%@ page extends="Java class" %>

Changes the generated servlet's class.

In general, a filter is a better solution than using the extends directive.

<%@ taglib prefix="x" uri="foo" %>

Configures tags with prefix x to use the tag library foo.

<%@ taglib prefix='x' uri='http://www.caucho.com/mytag/test' %>


<x:mytag/>

16. Explain the Accessing a Database from an ASP Page (11 marks)

The common way to access a database from inside an ASP page is to:

1. Create an ADO connection to a database


2. Open the database connection
3. Create an ADO recordset
4. Open the recordset
5. Extract the data you need from the recordset
6. Close the recordset
7. Close the connection

Extract Data from the Recordset

After a recordset is opened, we can extract data from recordset.  

Suppose we have a database named "Northwind", we can get access to the "Customers"
table inside the database with the following lines:

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
for each x in rs.fields
response.write(x.name)
response.write(" = ")
response.write(x.value)
next
%>
Display records in an HTML table

<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT Companyname, Contactname FROM Customers", conn
%>

<table border="1" width="100%">


<%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
       <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>
    </tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>

ADO RecordCount Property

Syntax
objRecordset.RecordCount

Example
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT * FROM Customers"
rs.Open sql,conn
if rs.Supports(adApproxPosition)=true then
i=rs.RecordCount
response.write("The number of records is: " & i)
end if
rs.Close
conn.Close
%>

Update record in table

<html>
<body>
<h2>Update Record</h2>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
cid=Request.Form("customerID")
if Request.form("companyname")="" then
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn
%>
<form method="post" action="demo_update.asp">
<table>
<%for each x in rs.Fields%>
<tr>
<td><%=x.name%></td>
<td><input name="<%=x.name%>" value="<%=x.value%>"></td>
<%next%>
</tr>
</table>
<br /><br />
<input type="submit" value="Update record">
</form>
<%
else
sql="UPDATE customers SET "
sql=sql & "companyname='" & Request.Form("companyname") & "',"

sql=sql & "contactname='" & Request.Form("contactname") & "',"


sql=sql & "address='" & Request.Form("address") & "',"
sql=sql & "city='" & Request.Form("city") & "',"
sql=sql & "postalcode='" & Request.Form("postalcode") & "',"
sql=sql & "country='" & Request.Form("country") & "'"
sql=sql & " WHERE customerID='" & cid & "'"
on error resume next
conn.Execute sql
if err<>0 then
response.write("No update permissions!")
else
response.write("Record " & cid & " was updated!")
end if
end if
conn.close
%>
</body>
</html>

Delete a Record in a Table

<html>
<body>
<h2>Delete Record</h2>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
cid=Request.Form("customerID")
if Request.form("companyname")="" then
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn
%>
<form method="post" action="demo_delete.asp">
<table>
<%for each x in rs.Fields%>
<tr>
<td><%=x.name%></td>
<td><input name="<%=x.name%>" value="<%=x.value%>"></td>
<%next%>
</tr>
</table>
<br /><br />
<input type="submit" value="Delete record">
</form>
<%
else
sql="DELETE FROM customers"
sql=sql & " WHERE customerID='" & cid & "'"
on error resume next
conn.Execute sql

if err<>0 then
response.write("No update permissions!")
else
response.write("Record " & cid & " was deleted!")
end if
end if
conn.close
%>
</body>
</html>

Sort the Data

<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM
Customers ORDER BY CompanyName"
rs.Open sql, conn
%>

<table border="1" width="100%">


<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close%>
</table>
</body>
</html>

Selected Data

<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")

conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers
WHERE CompanyName LIKE 'A%'"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close%>
</table>
</body>
</html>

No. Of Visitors

<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
fcount=RS.ReadLine
RS.Close

fcount=fcount+1

'This code is disabled due to the write access security on our server:
'Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
'RS.Write fcount
'RS.Close

Set RS=Nothing
Set FS=Nothing
%>
<html>
<body>
<p>
This page has been visited <%=fcount%>  times.
</p>
</body>
</html>

Counter.txt
0

ADROTATOR
<%
url=Request.QueryString("url")
If url<>"" then Response.Redirect(url)
%>
<html><body>
<%
set adrotator=Server.CreateObject("MSWC.AdRotator")
adrotator.TargetFrame="target='_blank'"
response.write(adrotator.GetAdvertisement("text/advertisements2.txt"))
%>
<p>NOTE: Because images are changed randomly, and because this page has few images
to choose from, it will often display the same advertisement twice in a row.</p>
<p><a href="text/advertisements2.txt"><img border="0"
src="/images/btn_view_text.gif"></a></p>
</body></html>
REDIRECT demo_adrotator2.asp
*
/banners/rd_htmlref.jpg
http://www.w3schools.com/html/html_reference.asp
Complete HTML 4.0 Reference
20
/banners/w3schools.gif
http://www.w3schools.com
Web Tutorials from W3Schools
20
/banners/rd_xhtml.jpg
http://www.w3schools.com/xhtml/default.asp
XHTML Tutorial
20

SESSION AND COOKIES?

SESSION TRACKING
Session tracking is a mechanism that servlets use to maintain state about a series of requests from
the same user (that is, requests originating from the same browser) across some period of time.

SESSION-TRACKING BASICS
• Every user of a site is associated with a javax.servlet.http.HttpSession object that servlets can use to store
or retrieve information about that user
• Basic session tracking functionalities,
• Obtain a session (an HttpSession object) for a user.
• Store or get data from the HttpSession object.
• Invalidate the session (optional).

GET SESSION

• A servlet uses its request object's getSession() method to retrieve the current HttpSession object:
• public HttpSession HttpServletRequest.getSession(boolean create)
• This method returns the current session associated with the user making the request.
• If the user has no current valid session, this method creates one if create is true or returns null if create is
false.
• To ensure the session is properly maintained, this method must be called at least once before any output
is written to the response.

PUT VALUE

• You can add data to an HttpSession object with the putValue() method:
• public void HttpSession.putValue(String name, Object value)
• This method binds the specified object value under the specified name. Any existing binding with the
same name is replaced.
GET VALUE

• To retrieve an object from a session, use getValue():


• public Object HttpSession.getValue(String name)
• This methods returns the object bound under the specified name or null if there is no binding.
• You can also get the names of all of the objects bound to a session with
getValueNames():
• public String[] HttpSession.getValueNames()
• This method returns an array that contains the names of all objects bound to this session or an empty
(zero length) array if there are no bindings.

REMOVE VALUE

• Finally, you can remove an object from a session with removeValue():


• public void HttpSession.removeValue(String name)
• This method removes the object bound to the specified name or does nothing if there is no binding
• Each of these methods can throw a java.lang.IllegalStateException if the session being accessed is
invalid.

THE SESSION LIFE CYCLE

• Sessions do not last forever.


• A session either expires automatically, after a set time of inactivity (for the Java Web Server the default
is 30 minutes), or manually, when it is explicitly invalidated by a servlet.
• When a session expires (or is invalidated), the HttpSession object and the data values it contains are
removed from the system.
• Beware that any information saved in a user's session object is lost when the session is invalidated.
• If you need to retain information beyond that time, you should keep it in an external location (such as a
database) and store a handle to the external data in the session object (or your own persistent cookie)

URL REWRITING

METHODS INVOLVED IN MANAGING THE SESSION LIFE CYCLE:


• public boolean HttpSession.isNew() This method returns whether the session is
new or not.
• public void HttpSession.invalidate()
• This method causes the session to be immediately invalidated. All objects stored in
the session are unbound.
• public long HttpSession.getCreationTime()
• This method returns the time at which the session was created
• public long HttpSession.getLastAccessedTime()
• This method returns the time at which the client last sent a request associated with this session.

COOKIES

In Javascript, cookies are a mechanism for storing persistent data on the client in a file called cookies.txt.
Because Hypertext Transport Protocol (HTTP) is a stateless protocol, cookies provide a way to maintain
information between client requests.
Each cookie is a small item of information with an optional expiration date and is
added to the cookie file in the following format:
name=value;expires=expDate;
Here,
 name is the name of the datum being stored, and value is its value.
 expDate is the expiration date, in GMT date format: Wdy, DD-Mon-YY HH:MM:SS GMT
Below is a simple function named setCookie() to create a cookie called myname. It also uses a function
getCookie() to get the value of a cookie, if cookie name exist and is given as an argument.
We end the discussion of javascript by providing you with the organizational chart which features you the
hierarchy of objects of HTML 4.0 that can be easily accessed using the javascript

developing n-tier web application

MULTI-TIER APPLICATIONS

In software engineering, multi-tier architecture (often referred to as n-tier architecture) is a client-server


architecture in which, the presentation, the application processing and the data management are logically
separate processes. For example, an application that uses middleware to service data requests between a
user and a database employs multi-tier architecture. The most widespread use of "multi-tier architecture"
refers to three-tier architecture.

The concepts of layer and tier are often used interchangeably. However, one fairly common point of view
is that there is indeed a difference, and that a layer is a logical structuring mechanism for the elements that
make up the software solution, while a tier is a physical structuring mechanism for the system
infrastructure.
Three-tier is a client-server architecture in which the user interface, functional process logic ("business
rules"), computer data storage and data access are developed and maintained as independent modules,
most often on separate platforms.

The three-tier model is considered to be a software architecture and a software design pattern.
Apart from the usual advantages of modular software with well-defined interfaces, the three-tier

architecture is intended to allow any of the three tiers to be upgraded or replaced independently as
requirements or technology change. For example, a change of operating system in the presentation tier
would only affect the user interface code.

Typically, the user interface runs on a desktop PC or workstation and uses a standard graphical user
interface, functional process logic may consist of one or more separate modules running on a workstation
or application server, and an RDBMS on a database server or mainframe contains the computer data
storage logic. The middle tier may be multi-tiered itself (in which case the overall architecture is called an
"n-tier architecture").

The 3-Tier architecture has the following three tiers:

Presentation Tier
This is the topmost level of the application. The presentation tier displays information related to such
services as browsing merchandise, purchasing, and shopping cart contents. It communicates with other
tiers by outputting results to the browser/client tier and all other tiers in the network.

Application Tier (Business Logic/Logic Tier)


The logic tier is pulled out from the presentation tier and, as its own layer; it controls an application’s
functionality by performing detailed processing.

Data Tier
This tier consists of Database Servers. Here information is stored and retrieved. This tier keeps data
neutral and independent from application servers or business logic. Giving data its own tier also improves
scalability and performance.

Comparison with the MVC architecture


At first glance, the three tiers may seem similar to the Model-view-controller (MVC) concept; however,
topologically they are different. A fundamental rule in three-tier architecture is the client tier never
communicates directly with the data tier; in a three-tier model all communication must pass through the
middleware tier. Conceptually the three-tier architecture is linear.

However, the MVC architecture is triangular: the View sends updates to the Controller, the Controller
updates the Model, and the View gets updated directly from the Model.

From a historical perspective the three-tier architecture concept emerged in the 1990's from observations
of distributed systems (e.g., web applications) where the client, middleware and data tiers ran on
physically separate platforms. Whereas MVC comes from the previous decade (by work at Xerox PARC
in the late 1970's and early 1980's) and is based on observations of applications that ran on a single
graphical workstation; MVC was applied to distributed applications much later in its history.

Web Development usage


In the Web development field, three-tier is often used to refer to Websites, commonly electronic
commerce websites, which are built using three tiers:
 A front end Web server serving static content.
 A middle dynamic content processing and generation level Application server, for example Java
EE platform.
 A back end Database, comprising both data sets and the Database management system or RDBMS
software that manages and provides access to the data.

DATABASE-CENTRIC ARCHITECTURE

Database-centric architecture or data-centric architecture has several distinct meanings, generally


relating to software architectures in which databases play a crucial role. Often this description is meant to
contrast the design to an alternative approach. For example, the characterization of architecture as
"database-centric" may mean any combination of the following:
 using a standard, general-purpose relational database management system, as opposed to
customized in-memory or file-based data structures and access methods. With the evolution of
sophisticated DBMS software, much of which is either free or included with the operating system,
application developers have become increasingly reliant on standard database tools, especially for
the sake of rapid application development.
 Using dynamic, table-driven logic, as opposed to logic embodied in previously compiled
programs. The use of table-driven logic, i.e. behavior that is heavily dictated by the contents of a
database, allows programs to be simpler and more flexible. This capability is a central feature of
dynamic programming languages.
 using stored procedures that run on database servers, as opposed to greater reliance on logic
running in middle-tier application servers in a multi-tier architecture. The extent to which business
logic should be placed at the back-end versus another tier is a subject of ongoing debate. For
example, Toon Koppelaars presents a detailed analysis of alternative Oracle-based architectures
that vary in the placement of business logic, concluding that a database-centric approach has
practical advantages from the standpoint of ease of development and maintainability.
 using a shared database as the basis for communicating between parallel processes in distributed
computing applications, as opposed to direct inter-process communication via message passing
functions and message-oriented middleware. A potential benefit of database-centric architecture in
distributed applications is that it simplifies the design by utilizing DBMS-provided transaction
processing and indexing to achieve a high degree of reliability, performance, and capacity. For
example, Base One describes a database-centric distributed computing architecture for grid and
cluster computing, and explains how this design provides enhanced security, fault-tolerance, and
scalability.

MULTI-TIER APPLICATIONS

N-tier (multi-tier) applications have become the norm for building enterprise software today. To
most people, an N-tier application is anything that is divided into discrete logical parts.
The most common choice is a three-part breakdown—presentation, business logic, and data—
although other possibilities exist. N-tier applications first emerged as a way of solving some of the
problems associated with traditional client/server applications, but with the arrival of the Web, this
architecture has come to dominate new development.

The Microsoft Windows® DNA technology has been a very successful foundation for N-tier
applications. The Microsoft .NET Framework also provides a solid platform for building N-tier
applications. Yet the changes .NET brings should make architects re-think some of the lessons they've
learned in the Windows DNA world about designing N-tier applications.

Even more important, the fundamental support for XML Web services built into the .NET
Framework allows building new kinds of applications that go beyond the traditional N-tier approach.
Understanding how best to architect applications for .NET requires knowing what changes in this new
world and how to exploit these changes. 
This article takes a look at these issues, beginning with a review of key architectural lessons learned in
building N-tier applications using Windows DNA. It next examines these same findings, in the same
order, as they apply to building applications using the .NET Framework. The final section provides some
advice on architecture for applications that use XML Web services.

CLIENT / SERVER APPLICATIONS

Typically, cooperating applications can be categorized as either a client or a server. The client application
requests services and data from the server, and the server application responds to client requests. Early
two-tier (client/server) applications were developed to access large databases, and incorporated the rules
used to manipulate the data with the user interface into the client application. The server's task was simply
to process as many requests for data storage and retrieval as possible.
Two-tier applications perform many of the functions of stand-alone systems: They present a user interface,
gather and process user input, perform the requested processing, and report the status of the request. This
sequence of commands can be repeated as many times as necessary. Because servers provide only access
to the data, the client uses its local resources to perform most of the processing.

One major advantage of the client/server model was that by allowing multiple users to simultaneously
access the same application data, updates from one computer were instantly made available to all
computers that had access to the server. However, as the number of clients increased, the server would
quickly become overwhelmed with client requests. Also, because much of the processing logic was tied to
a monolithic suite of applications, changes in business rules led to expensive and time-consuming
alterations to source code. Although the ease and flexibility of two-tier products continue to drive many
small-scale business applications, the need for faster data access and more rapid developmental timelines
has persuaded systems developers to seek out a new way of creating distributed applications.

Unit-3

XML document

 XML stands for EXtensible Markup Language


 XML is a markup language much like HTML
 XML was designed to describe data
 XML tags are not predefined. You must define your own tags
 XML uses a Document Type Definition (DTD) or an XML Schema to describe the data
 XML with a DTD or XML Schema is designed to be self-descriptive

The main difference between XML and HTML

XML was designed to carry data.


 XML is not a replacement for HTML. XML and HTML were designed with different goals:
 XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
 HTML is about displaying information, while XML is about describing information.

XML is free and extensible


XML tags are not predefined. You must "invent" your own tags.
 The tags used to mark up HTML documents and the structure of HTML documents is predefined.
The author of HTML documents can only use tags that are defined in the HTML standard (like
<p>, <h1>, etc.).
 XML allows the author to define his own tags and his own document structure.
 The tags in the example above (like <to> and <from>) are not defined in any XML standard. These
tags are "invented" by the author of the XML document.

How can XML be used?


It is important to understand that XML was designed to store, carry, and exchange data. XML was not
designed to display data.

XML can separate Data from HTML


With XML, your data is stored outside your HTML.
When HTML is used to display data, the data is stored inside your HTML. With XML, data can be stored
in separate XML files. This way you can concentrate on using HTML for data layout and display, and be
sure that changes in the underlying data will not require any changes to your HTML.
XML data can also be stored inside HTML pages as "Data Islands". You can still concentrate on using
HTML only for formatting and displaying the data.

XML is used to Exchange Data


With XML, data can be exchanged between incompatible systems.
In the real world, computer systems and databases contain data in incompatible formats. One of the most
time-consuming challenges for developers has been to exchange data between such systems over the
Internet.
Converting the data to XML can greatly reduce this complexity and create data that can be read by many
different types of applications.

XML can be used to Share Data


With XML, plain text files can be used to share data.
Since XML data is stored in plain text format, XML provides a software- and hardware-independent way
of sharing data.
This makes it much easier to create data that different applications can work with. It also makes it easier to
expand or upgrade a system to new operating systems, servers, applications, and new browsers. 

XML can be used to Store Data


With XML, plain text files can be used to store data.
XML can also be used to store data in files or in databases. Applications can be written to store and
retrieve information from the store, and generic applications can be used to display the data.

XML can make your Data more Useful


With XML, your data is available to more users.
Since XML is independent of hardware, software and application, you can make your data available to
other than only standard HTML browsers.
Other clients and applications can access your XML files as data sources, like they are accessing
databases. Your data can be made available to all kinds of "reading machines" (agents), and it is easier to
make your data available for blind people, or people with other disabilities.

XML can be used to create new Languages


XML is the mother of WAP and WML.
The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like
mobile phones, is written in XML.

XML MARKUP language

The syntax rules of XML are very simple and very strict. The rules are very easy to learn, and very easy to
use.
Because of this, creating software that can read and manipulate XML is very easy to do.
An example XML document
XML documents use a self-describing and simple syntax.

<? xml version="1.0" encoding="ISO-8859-1"?>


<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The first line in the document - the XML declaration - defines the XML version and the character
encoding used in the document. In this case the document conforms to the 1.0 specification of XML and
uses the ISO-8859-1 (Latin-1/West European) character set.

The next line describes the root element of the document (like it was saying: "this document is a note"):
<note>

The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> </note>
And finally the last line defines the end of the root element:

xml elements and attributes

XML ELEMENTS

All XML elements must have a closing tag


With XML, it is illegal to omit the closing tag.
In HTML some elements do not have to have a closing tag. The following code is legal in HTML:
<p>This is a paragraph
<p>This is another paragraph
In XML all elements must have a closing tag, like this:
<p>This is a paragraph</p>
<p>This is another paragraph</p> 

XML tags are case sensitive


Unlike HTML, XML tags are case sensitive.
With XML, the tag <Letter> is different from the tag <letter>.
Opening and closing tags must therefore be written with the same case:
<Message>This is incorrect</message>

<message>This is correct</message>

All XML elements must be properly nested


Improper nesting of tags makes no sense to XML.
In HTML some elements can be improperly nested within each other like this:
<b><i>This text is bold and italic</b></i>
In XML all elements must be properly nested within each other like this:
<b><i>This text is bold and italic</i></b>

All XML documents must have a root element


All XML documents must contain a single tag pair to define a root element.
All other elements must be within this root element.
All elements can have sub elements (child elements). Sub elements must be correctly nested within their
parent element:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>

Attribute values must always be quoted


With XML, it is illegal to omit quotation marks around attribute values. 
XML elements can have attributes in name/value pairs just like in HTML. In XML the attribute value
must always be quoted. Study the two XML documents below. The first one is incorrect, the second is
correct:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note date=12/11/2002>
<to>Tove</to>
<from>Jani</from>
</note>
<?xml version="1.0" encoding="ISO-8859-1"?>
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.
This is correct: date="12/11/2002". This is incorrect: date=12/11/2002.
With XML, white space is preserved
With XML, the white space in your document is not truncated.
This is unlike HTML. With HTML, a sentence like this:
Hello              my name is Tove,
will be displayed like this:
Hello my name is Tove,
because HTML strips off the white space.

With XML, CR / LF is converted to LF


With XML, a new line is always stored as LF.
Do you know what a typewriter is? Well, a typewriter is a mechanical device used in the previous century
to produce printed documents. :-) After you have typed one line of text on a typewriter, you have to
manually return the printing carriage to the left margin position and manually feed the paper up one line.
In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and
line feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line.
In Unix applications, a new line is normally stored as a LF character. Macintosh applications use only a
CR character to store a new line.

Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->

There is nothing special about XML


There is nothing special about XML. It is just plain text with the addition of some XML tags enclosed in
angle brackets.
Software that can handle plain text can also handle XML. In a simple text editor, the XML tags will be
visible and will not be handled specially.
In an XML-aware application however, the XML tags can be handled specially. The tags may or may not
be visible, or have a functional meaning, depending on the nature of the application.

XML ATTRIBUTES
XML elements can have attributes in the start tag, just like HTML.
Attributes are used to provide additional information about elements.

XML elements can have attributes.

From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides
additional information about the IMG element.
In HTML (and in XML) attributes provide additional information about elements:

<img src="computer.gif">
<a href="demo.asp">
Attributes often provide information that is not a part of the data. In the example below, the file type is
irrelevant to the data, but important to the software that wants to manipulate the element:

<file type="gif">computer.gif</file>
Take a look at these examples:

<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same
information.
There are no rules about when to use attributes, and when to use child elements. My experience is that
attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the
information feels like data.

Should you avoid using attributes?

Here are some of the problems using attributes:


 attributes cannot contain multiple values (child elements can)
 attributes are not easily expandable (for future changes)
 attributes cannot describe structures (child elements can)
 attributes are more difficult to manipulate by program code
 attribute values are not easy to test against a Document Type Definition (DTD) - which is used to
define the legal elements of an XML document
If you use attributes as containers for data, you end up with documents that are difficult to read and
maintain. Try to use elements to describe data. Use attributes only to provide information that is not
relevant to the data.

creating valid documents

XML with correct syntax is Well Formed XML.


XML validated against a DTD is Valid XML.
"Well Formed" XML documents
A "Well Formed" XML document has corrects XML syntax.
A "Well Formed" XML document is a document that conforms to the XML syntax rules that were
described in the previous chapters:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

A "Valid" XML document also conforms to a DTD.


A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a
Document Type Definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

XML DTD

A DTD defines the legal elements of an XML document.


The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document
structure with a list of legal elements. You can read more about DTD, and how to validate your XML
documents in our DTD tutorial.

XML Schema 
XML Schema is an XML based alternative to DTD.
W3C supports an alternative to DTD called XML Schema. You can read more about XML Schema in our
Schema tutorial.

Errors will stop you


Errors in XML documents will stop the XML program.

The W3C XML specification states that a program should not continue to process an XML document if it
finds a validation error. The reason is that XML software should be easy to write, and that all XML
documents should be compatible.
With HTML it was possible to create documents with lots of errors (like when you forget an end tag). One
of the main reasons that HTML browsers are so big and incompatible, is that they have their own ways to
figure out what a document should look like when they encounter an HTML error.
With XML this should not be possible.

A general XML Validator


To help you validate your xml files, we have created this link so that you can validate any XML file.

Viewing XML Files


Raw XML files can be viewed in IE 5.0 (and higher) and in Netscape 6, but to make it display like a
web page, you have to add some display information.

Viewing XML Files


To view an XML document in IE 5.0 (and higher) you can click on a link, type the URL in the address
bar, or double-click on the name of an XML file in a files folder. If you open an XML document in IE, it
will display the document with color coded root and child elements. A plus (+) or minus sign (-) to the left
of the elements can be clicked to expand or collapse the element structure. If you want to view the raw
XML source, you must select "View Source" from the browser menu.
To view an XML document in Netscape 6 you'll have to open the XML file and then right-click in XML
file and select "View Page Source". If you open an XML document in Netscape 6, it will display the
document with color coded root and child elements.

XML OBJECTS AND DOM

What is the DOM?

The DOM defines a standard for accessing documents like XML and HTML:

"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a document."

DOM, the so-called Document Object Model, is another name in this context for how XML can be read
into memory as a tree/hierarchy and manipulated by a script or program. When an HTML page is loaded,
the same is done. The browser is programmed to create an internal 'tree'/hierarchical representation, and
certain properties and methods are available to read or modify parts of this internal page hierarchy. Those
made available would be called the - application programming interface.

The DOM is separated into 3 different parts / levels:

 Core DOM - standard model for any structured document


 XML DOM - standard model for XML documents
 HTML DOM - standard model for HTML documents

The DOM defines the objects and properties of all document elements, and the methods (interface) to
access them.

XML DOM
 XML DOM definition
 XML DOM Nodes
 XML DOM - Properties and Methods
 XML DOM Parser
 XML Load functions

What is the XML DOM?

The XML DOM is:

 A standard object model for XML


 A standard programming interface for XML
 Platform- and language-independent
 A W3C standard

The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to
access them. In the DOM, everything in an XML document is a node.In other words: The XML DOM is a
standard for how to get, change, add, or delete XML elements.
DOM Nodes

According to the DOM, everything in an XML document is a node.

The DOM says:

 The entire document is a document node


 Every XML element is an element node
 The text in the XML elements are text nodes
 Every attribute is an attribute node
 Comments are comment nodes

DOM Example

<?xml version="1.0" encoding="ISO-8859-1"?>


<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>

  <book category="web" cover="paperback">


    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

The root node in the XML above is named <bookstore>. All other nodes in the document are contained
within <bookstore>.The root node <bookstore> holds four <book> nodes.

The first <book> node holds four nodes: <title>, <author>, <year>, and <price>, which contains one text
node each, "Everyday Italian", "Giada De Laurentiis", "2005", and "30.00". A common error in DOM
processing is to expect an element node to contain text. However, the text of an element node is stored in a
text node.In this example: <year>2005</year>, the element node <year>, holds a text node with the value
"2005".

XML DOM Tree Example

XML DOM - Properties and Methods

Programming Interface:

The DOM models XML as a set of node objects. The nodes can be accessed with JavaScript or other
programming languages. In this tutorial we use JavaScript.

The programming interface to the DOM is defined by a set standard properties and methods.Properties are
often referred to as something that is (i.e. nodename is "book").Methods are often referred to as something
that is done (i.e. delete "book").

XML DOM Properties

These are some typical DOM properties:

 x.nodeName - the name of x


 x.nodeValue - the value of x
 x.parentNode - the parent node of x
 x.childNodes - the child nodes of x
 x.attributes - the attributes nodes of x

Note: In the list above, x is a node object.

XML DOM Methods


 x.getElementsByTagName(name) - get all elements with a specified tag name
 x.appendChild(node) - insert a child node to x
 x.removeChild(node) - remove a child node from x

Note: In the list above, x is a node object.

Example

The JavaScript code to get the text from the first <title> element in books.xml:

txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue

After the execution of the statement, txt will hold the value "Everyday Italian"

Explained:

 xmlDoc - the XML DOM object created by the parser.


 getElementsByTagName("title")[0] - the first <title> element
 childNodes[0] - the first child of the <title> element (the text node)
 nodeValue - the value of the node (the text itself)

XML DOM Parser


Most browsers have a build-in XML parser to read and manipulate XML.

The parser converts XML into a JavaScript accessible object (the XML DOM).

XML Parser

The XML DOM contains methods (functions) to traverse XML trees, access, insert, and delete
nodes.However, before an XML document can be accessed and manipulated, it must be loaded into an
XML DOM object.An XML parser reads XML, and converts it into an XML DOM object that can be
accessed with JavaScript.

Most browsers have a build-in XML parser.

XML Accessing Nodes

You can access a node in three ways:

1. By using the getElementsByTagName() method

2. By looping through (traversing) the nodes tree.

3. By navigating the node tree, using the node relationships.

The getElementsByTagName() Method

getElementsByTagName() returns all elements with a specified tag name.


Syntax
node.getElementsByTagName("tagname");

Example

The following example returns all <title> elements under the x element:

x.getElementsByTagName("title");

Note that the example above only returns <title> elements under the x node. To return all <title> elements
in the XML document use:

xmlDoc.getElementsByTagName("title");

where xmlDoc is the document itself (document node).

DOM Node List

The getElementsByTagName() method returns a node list. A node list is an array of nodes.

The following code loads "books.xml" into xmlDoc using loadXMLDoc() and stores a list of <title> nodes
(a node list) in the variable x:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

The <title> elements in x can be accessed by index number. To access the third <title> you can write::

y=x[2];

DOM Node List Length

The length property defines the length of a node list (the number of nodes).

You can loop through a node list by using the length property: Example
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

for (i=0;i<x.length;i++)
  {
  document.write(x[i].childNodes[0].nodeValue);
  document.write("<br />");
  }

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()


2. Get all <title> element nodes
3. For each title element, output the value of its text node

Node Types

The documentElement property of the XML document is the root node.

The nodeName property of a node is the name of the node.

The nodeType property of a node is the type of the node.

NodeType Named Constant


1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE

Traversing Nodes

The following code loops through the child nodes, that are also element nodes, of the root node:

Example
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement.childNodes;

for (i=0;i<x.length;i++)
{
  if (x[i].nodeType==1)
  {//Process only element nodes (type 1)
  document.write(x[i].nodeName);
  document.write("<br />");
  }
}

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()


2. Get the child nodes of the root element
3. For each child node, check the node type of the node. If the node type is "1" it is an element node
4. Output the name of the node if it is an element node

Navigating Node Relationships

The following code navigates the node tree using the node relationships:

Example
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;

for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
  {//Process only element nodes (type 1)
  document.write(y.nodeName + "<br />");
  }
y=y.nextSibling;
}
1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Get the child nodes of the first book element
3. Set the "y" variable to be the first child node of the first book element

4. For each child node (starting with the first child node "y"):
5. Check the node type. If the node type is "1" it is an element node
6. Output the name of the node if it is an element node
7. Set the "y" variable to be the next sibling node, and run through the loop again.

XML DOM Load Functions


The code for loading XML documents can be stored in a function.

The loadXMLDoc() Function

To make the code from the previous page simpler to maintain (and check for older browsers), it should be
written as a function:
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
 }
else
 {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

The function above can be stored in the <head> section of an HTML page, and called from a script in the
page.

An External JavaScript for loadXMLDoc()

To make the code above even easier to maintain, and to make sure the same code is used in all pages, we
store the function in an external file.

The file is called "loadxmldoc.js", and will be loaded in the head section of an HTML page. Then, the
loadXMLDoc() function can be called from a script in the page.

The following example uses the loadXMLDoc() function to load books.xml:

Example
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");

code goes here.....

</script>

</body>
</html>

The loadXMLString() Function

To make the code from the previous page simpler to maintain (and check for older browsers), it should be
written as a function:
function loadXMLString(txt)
{
if (window.DOMParser)
 {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
 }
else // Internet Explorer
 {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  }
return xmlDoc;
}

The function above can be stored in the <head> section of an HTML page, and called from a script in the
page.

Access across Domains

For security reasons, modern browsers do not allow access across domains.This means, that both the web
page and the XML file it tries to load, must be located on the same server.The examples on W3Schools all
open XML files located on the W3Schools domain.If you want to use the example above on one of your
web pages, the XML files you load must be located on your own server.

XML DOM Clone Nodes

Examples

The examples below use the XML file books.xml.


A function, loadXMLDoc(), in an external JavaScript is used to load the XML file.

This example uses cloneNode() to copy a node and append it to the root node of the XML document.

Copy a Node

The cloneNode() method creates a copy of a specified node.

The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node
should include all attributes and child nodes of the original node.

The following code fragment copies the first <book> node and appends it to the root node of the
document:

Example
xmlDoc=loadXMLDoc("books.xml");
oldNode=xmlDoc.getElementsByTagName('book')[0];
newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);

//Output all titles


y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write("<br />");
}

OUTPUT:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()


2. Get the node to copy
3. Copy the node into "newNode" using the cloneNode method
4. Append the new node to the the root node of the XML document
5. Output all titles for all books in the document

Get the Value of an Element

The following code retrieves the text value of the first <title> element:

Example
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

txt=x.nodeValue;

Get the Value of an Attribute

The following code retrieves the text value of the "lang" attribute of the first <title> element:
Example
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Change the Value of an Element

The following code changes the text value of the first <title> element:

Example
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";

Change the Value of an Attribute

The setAttribute() method can be used to change the value of an existing attribute, or to create a new
attribute.

The following code adds a new attribute called "edition" (with the value "first") to each <book> element:

Example
x=xmlDoc.getElementsByTagName("book");

for(i=0;i<x.length;i++)
 {
  x[i].setAttribute("edition","first");
 }

Create an Element

 The createElement() method creates a new element node.


 The createTextNode() method creates a new text node.

 The appendChild() method adds a child node to a node (after the last child).
 To create a new element with text content, it is necessary to create both an element node and a text
node.

The following code creates an element (<edition>), and adds it to the first <book> element:

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");
x[0].appendChild(newel);

Example explained:
 Create an <edition> element
 Create a text node with value = "First"
 Append the text node to the <edition> element
 Append the <edition> element to the first <book> element

Remove an Element

The removeChild() method removes a specified node (or element).

The following code fragment will remove the first node in the first <book> element:

x=xmlDoc.getElementsByTagName("book")[0];

x.removeChild(x.childNodes[0]);

6. Explain briefly about ACTIVEX CONTROLS? (11 marks)

Dynamic Web Concept

 HTTP is a protocol that relies on TCP/IP. Understanding how this relationship affects your Web
documents is very important.
 Dynamic content can be generated in a number of ways including through the use of CGI
applications.
 The Common Gateway Interface (CGI) is a standard protocol for interfacing external application
software with an information server, commonly a web server.
 This allows the server to pass requests from a client web browser to the external application.
 The web server can then return the output from the application to the web browser.
 ActiveX and Java are the beginning of a new era in dynamic content over the Web.

 Dynamic documents in the Internet market space are clearly one of those key technological shifts.
 More specifically, ActiveX enables software developers to easily include dynamic, animated,
engaging content in Web pages without significant programming.
 Two basic problems plagued the static content model prior to the advent of Sun Microsystems'
Java and Microsoft's ActiveX, which are as follows:
 The computing power of client machine executing the Web browser is woefully underused in the
rendering of the content.

 The Web browser (or the user) had to know about and have available executable software capable
of rendering downloaded content.
 In the static content model, the Web browser, the content, and the content viewer are all separate
objects.
 In the dynamic model, the content knows how to execute itself. The Web browser understands
executable content in a generic sense.
 Since the downloaded object knows how to render itself, the browser has no need to worry about
helper applications and the like.
 Since the browser knows how to deal with executable objects such as Java applets and ActiveX
Controls, the computing power available on the client side of the client/server equation is exercised more
strenuously.
SHAPE \*

MERGEFORMAT

OVERVIEW OF ACTIVEX

 ActiveX is not a single technological thing.


 If you've browsed Microsoft's Web site, you've probably gathered that ActiveX is a centerpiece of
Microsoft's overall Internet strategy.
 ActiveX helps application developers and Web content authors build dynamic Internet tools,
content, and sites.
 That's a pretty tall order. To achieve this design goal, Microsoft needed to bring together a number
of different components.
 Add Internet functionality to your applications with these easy-to-use ActiveX Controls.
 High performance server side application for the Web is made possible with this framework.

ActiveX Controls for the Internet

 As the content you consume from the Internet becomes more and more dynamic, the Web browser
will be managing more and more of the computing tasks required to render that content.
 Sun Microsystems's Java takes early honors in the race for the development standard for
executable content.
 Netscape helped to push Java into prominence by releasing Navigator 2.0 with support for Java
applets.
 Java provides Web developers with a highly sophisticated tool set for interacting with the user and
the client machine.

 Microsoft, not to be outdone, announced support for Java in MSIE 3.0 and a new alternative called
ActiveX Controls.
 ActiveX Controls are based upon the OLE 2.0 standard.
 ActiveX Controls are to Web pages what the VBX and OCX are to MS-Windows GUI
development.
 ActiveX Controls have the following advantages over Java applets:
 They are built from tools you already know.
 They are easy to integrate with other applications.
 They are easy to employ in containers that are not Web pages
 ActiveX Controls are not constructed using Microsoft Foundation Classes. Instead, Microsoft has
made the ActiveX Template Library (ATL) available to help developers build ActiveX controls.
 Although stand-alone applications can be constructed using Java, Java does not really offer the
same potential interaction with other existing applications yet.
 Java applets are intended to execute in a Web browser and not as controls in other applications.
 The Internet ActiveX Control Pack demonstrates the value of ActiveX in other development
environments like Visual C++, Delphi, Visual Basic, and MS-Access.
 There are a couple of problems with the use of ActiveX Controls:
 They are not currently supported in Netscape.
 They are not currently supported on UNIX platforms.

Internet ActiveX Control Pack

 The Internet ActiveX Control Pack (ICP) is not really an ActiveX control. ICP is, instead, an
application of ActiveX.
 With ICP, it's easy to integrate ActiveX controls into your Visual Basic programs.
 The ICP contains controls for most of the major Internet services you'll want to integrate into your
own applications.

 Some of the controls are:


 The TCP Control
 The UDP Control
 The FTP Client Control
 The HTTP Client Control
 The HTML Client Control
 The SMTP Control
 The POP Client Control
 The NNTP Control
ActiveX Server Framework

 The ActiveX Server Framework provides an alternative to the Common Gateway Interface method
of executing applications on a Web server and interacting with the Web browser dynamically.
 There are two major types of applications that can be developed using the ActiveX Server
Framework:
 ISAPI applications
 ISAPI filters

ISAPI Applications
 The Internet Server Applications Programming Interface (ISAPI) specification provides an alternative
to CGI programs with potentially higher performance type capabilities for the Microsoft Internet
Information Server (IIS).
 ISAPI allows Web developers to build applications that execute in the same process space as the Web
server

 CGI and ISAPI have different approaches to use of process space.


 Applications using ISAPI are faster simply because the operating system does not have to duplicate
the environment and spawn a new process as is required by CGI.
 ISAPI applications are actually Dynamic Link Libraries (DLLs).
 Operating in the server's process space carries higher risk while providing somewhat higher
performance.
 Since the ISAPI application is loaded in the same process as the HTTP server, an access violation by
the ISAPI application may crash the HTTP server.

ISAPI Filters

 The Internet Survey Application Programming Interface (ISAPI) also provides a mechanism for
modifying the behavior of the server in specific ways.
 You'll employ ISAPI filters in cases where the default behavior of the server is inappropriate for the
application.

ISAPI filters can modify the behavior of the following server functions:
 Authentication
 Compression
 Encryption
 Logging
 Traffic Analysis

7. Explain briefly about building a basic OLE and ActiveX controls? (11 marks)

 OCX controls are the standard solution for Windows component software.
 They are implemented using OLE 2.0 technology and are designed for use on the desktop
environment.
 Most OCX controls today are built using the Control Development Kit, which is supplied as an
integrated part of Microsoft Visual C++ version 4.x.
 Controls developed with Visual C++ are built using the standard OLE 2.0 interfaces, some of which
were mandatory.
 This means they contain a lot of unnecessary code.
 They are also dependent on the Microsoft Foundation Classes (MFC) libraries which are several
megabytes in size.

 These controls are therefore relatively big in size which may limit their utility in the slow Internet
environment.
 Also, in order to use these controls on the Internet, the user must first have the MFC libraries on
their machine.
 A onetime download of these libraries for all controls to share may not be too much of a penalty,
but MFC is being revised and released approximately every three months.
 This means users of these controls must download these new libraries every three months or their
controls may not work.
 Management of data and properties is another potential problem. OCX controls on the desktop
operate synchronously.
 Function calls made to the OLE libraries or control containers do not return until they have
completed.
 While this is not a problem on the desktop, where data and properties are stored locally in files and
can be retrieved quickly.
 But it causes problems in a slow environment like the Internet, where large amounts of data have
to be retrieved from a remote site and loaded into the control.
 A 24 bit bitmap file for instance, can be several megabytes in size and take many minutes to
download.

ActiveX Control Pad

 The ActiveX Control Pad provides an integration environment for combining three major elements of
the basic World Wide Web dynamic document interface.
 You'll combine your basic HTML, ActiveX controls, as well as client-side scripts into a single
development tool with the ActiveX Control Pad.
 Managing your ActiveX controls and scripts is visual and easy when you use the ActiveX Control Pad.
 ActiveX objects can be inserted into your HTML documents using the ActiveX Control Pad.
 HTML layouts can be inserted into your HTML documents using the ActiveX Control Pad, as well.
Select and add controls using the control toolbox and properties.
 Adding scripts to your HTML Web pages is simple using the ActiveX Control Pad. Use the List View
and the Code View to manage VB scripts in your Web pages.
Using the ActiveX Control Pad

 Inserting ActiveX controls and adding client-side scripting like VBScript is straightforward.
 You'll recognize many of the elements of the user interface from other applications you use, like Lotus
1-2-3 or Microsoft Word are present here too.
 Most of the common items are there including, the File toolbar for saving, creating, and opening new
HTML files.
 However, there are also a couple of elements that you probably haven't seen before.
 The first of these two new items is found on the toolbar that uses a scroll as its icon. Pressing this
button brings up the script wizard.
 The second item is in the document window.

 You'll be inserting objects and scripts into the HTML documents in this window.
 The scroll icon indicates the start of a script. Pressing the scroll button in the margin bar opens the
Script Wizard, and loads the appropriate script.
 ActiveX objects are indicated in the margin bar by a small cube icon.
 This iconography indicates the start of an <OBJECT> declaration in the HTML.
 Pressing the ActiveX object button in the gray margin bar brings up a window containing the object,
and a window containing its property sheet.
 The Control Pad uses a special icon for insertions of HTML layouts.
 HTML layouts are indicated in the margin bar by a small icon containing the letter A, a circle, and a
square.
 Pressing this button brings up the HTML Layout editor and its associated tool bar.

ACTIVEX documents

 An existing application that doesn't need to be embedded in a Web page can be converted to an
ActiveX document.
 ActiveX documents are based upon a more general abstraction called DocObjects. The DocObjects
technology is a set of extensions to OLE documents, the compound document technology of OLE.

 As with OLE documents, the DocObjects standard requires a container to provide the display space for
a DocObject.
 This technology allows the browser to present documents from Office and Office Compatible
applications.
 Such functionality might allow any kind of document to be displayed within the Web browser.

Documents that conform to the ActiveX standard can be opened within other ActiveX document
containers including the following:

 Microsoft Internet Explorer 3.0


 Microsoft Office Binder
 Forthcoming new Windows shell

 Embedded objects do not, however, control the page on which they appear. These types of objects
are usually quite small and hold very little persistent data.
 ActiveX documents, on the other hand, provide a fully functional document space.
 ActiveX documents also control the page in which they appear (called a DocObject container).
 This means that ActiveX documents can be considerably more feature rich than the related
embedded object.
 Microsoft's Internet Explorer 3.0 is an excellent example of an ActiveX document hosting engine.

You might also like