You are on page 1of 338

INTERNET PROGRAMMING / WEB PROGRAMMING

UNIT 1 NOTES

Basic internet concepts-connecting to internet-Domain Name Space(DNS) -


Exchanging e-mail-sending and receiving file-Fighting spam-Avoiding email Viruses-Chatting
and conferencing on the internet-online chatting-messaging- Usenet newsgroup-Internet
Relay Chat(IRC)-Instant Messaging-Voice and Video Conferencing.

1.1 BASIC INTERNET CONCEPTS

What is the Internet?


The Internet is a global collection of computer networks that are linked together by
devices called routers and use a common set of protocols for data transmission known as
TCP/IP (transmission control protocol / Internet protocol). The primary purpose of the
Internet is to facilitate the sharing of information. There are many different tools used on the
Internet to make this possible. Some of the more common tools include email, listservs,
newsgroups, telnet, gopher, FTP, and the World Wide Web. Probably the most popular of
all Internet tools is the World Wide Web.
The Internet is a network of computer networks. It makes it possible for any computer
connected to it to send and receive data from any other computer connected to it.
America Online, Comcast, Earthlink, etc. are examples of Internet service providers.
They make it physically possible for you to send and access data from the Internet. They
allow you to send and receive data to and from their computers or routers which are
connected to the Internet.

World Wide Web is an example of an information protocol/service that can be used


to send and receive information over the Internet. It supports:
 Multimedia Information (text, movies, pictures, sound, programs . . . ).
 HyperText Information (information that contains links to other information
resources).
 Graphic User Interface (so users can point and click to request information instead
of typing in text commands).
The World Wide Web is an example of an information protocol/service that works
using a Client/Server software design. (Client/Server is an ADJECTIVE that describes the
software, not a noun!) A service that uses Client/Server design requires two pieces of
software to work: Client software which you use to request information, and Server software
which an Information Provider (like Fermilab or a museum) uses to answer requests and
provide their information. Most Internet information protocol/services are designed this
way.

1 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The Client/Server relationship is similar to the relationship between the TV in your


NOTES house and the TV stations you can select. Your TV acts as a client by tuning in (requesting
information) from a TV station which acts as a server by broadcasting (serving) the
information.

This means that if you encounter an error while using your Web browser, this may be
due to a problem with the Web server you are contacting for information. It does not
necessarily mean that your browser isn’t working, or that you installed something improperly.
Just like in the case of the TV and the TV station, sometimes problems are due to a bad
TV, and other times they are caused by a problem at the TV station.

The server software for the World Wide Web is called an HTTP server (or informally
a Web server). Examples are Apache and IIS. The client software for World Wide Web is
called a Web browser. Examples are: Netscape, Internet Explorer, Safari, Firefox, and
Mozilla. These examples are particular “brands” of software that have a similar function,
just like Lotus 123 and Excel are both spreadsheet software packages.

There are many different information protocols/services besides HTTP (Hyper Text
Transfer Protocol).

Some of these are:

 E-mail (SMTP) - for sending electronic mail messages.


 Usenet News (NNTP) - for having electronic group discussions.
 File Transfer Protocol (FTP) - for transferring files between computers.
 Telnet - for running programs on remote computers.
 Various Others . . .
Each of these works in a client/server manner by having a “language” defined that
allows the client and server to communicate with each other in order to give users the
information they request. These different languages have different purposes, capabilities,
and advantages.

One of the nicest things about the World Wide Web is that it provides “one-stop
shopping” for getting information over the Internet. In the past, you would have needed to
learn how to use many different software applications in order to use all these services.
Now you can use E-mail, News, FTP, Telnet, WAIS, Gopher, and HTTP services all
through your favourite Web browser.

1.2 CONNECT TO THE INTERNET

The Internet can turn your computer into a powerful tool for research, communications,
and sharing. If you have kids, the Internet is a great place for them to find homework help
and chat with friends.

2 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Connecting to the Internet is easy, and your service provider will do most of the
technical work for you. NOTES
To connect to the Internet
1. Find an Internet service provider, and choose a connection type.
2. If necessary, transfer an existing ISP account.
3. Set up your home network.
4. Install a network adapter.
5. Connect your computer to the network.

Find an Internet service provider


The first step in connecting to the Internet is to find an Internet service provider (ISP).
The most important services an ISP offers are:
Internet access: Access any Web site, send instant messages to your friends, play
online games, or use any other Internet service.
E-mail: You can access your e-mail with Microsoft Outlook Express or your Web
browser. Most ISPs offer multiple e-mail addresses, so everyone in your family can have
an account. ISPs typically provide spam filtering that reduces, but does not eliminate,
unwanted messages.
Depending on your location, you might have several different choices for Internet
access. Starting with the most attractive technologies for home Internet access, common
Internet connection types are:
Cable modems. The best performing and most affordable option available to
customers, most cable TV providers offer broadband Internet access.
DSL. An excellent choice for businesses, DSL typically offers better reliability than
cable modems. However, DSL tends to be more expensive than cable modems for similar
levels of service.
Dial-up. The slowest method of connecting to the Internet, dial-up enables you to
connect to the Internet using your existing phone lines. Dial-up is convenient because it is
available to any location with a phone. However, slow performance makes using the Internet
frustrating.
Satellite: Satellite broadband services provide high-speed Internet access to any
location with a clear view of the sky (currently available in North America and certain
other locations). Satellite services may be the only broadband option for people living in
rural areas. The cost of satellite services is significantly higher than other services. While
you can transfer large files quickly with satellite, browsing the Web or playing online games
can seem slower than with dial-up because of the delay caused by sending signals to and
from satellites.

3 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Additionally, ISPs are beginning to offer wireless or fiber broadband Internet access
NOTES in limited areas.

To find an ISP, you should contact your cable television provider for cable modem
service or your telephone company for DSL. Almost all cable and telephone companies
offer broadband Internet access, and they typically offer a discount if you purchase multiple
services from them.

Bandwidth

Internet bandwidth (the speed at which your computer can send and receive information)
is measured in either Kbps (kilobits per second) or Mbps (megabits per second). If you
are lucky enough to have multiple broadband options in your area, compare these factors:

Downstream bandwidth: This is the speed with which your computer can receive
information from the Internet. The higher the downstream bandwidth, the faster your
computer can display Web pages, transfer music, and download files. For most people,
downstream bandwidth is more important than upstream bandwidth, so the speeds tend to
be much higher. For example, a cable modem service might offer 6,000 Kbps downstream
and only 768 Kbps upstream.

Upstream bandwidth: This is the amount of data your computer can send to the Internet.
This isn’t important if you just plan to read e-mail and surf the Web, because your computer
only needs to send a small request in order to receive a large Web page or e-mail. However,
if you’re into online gaming or you want to send large files to people, then higher upstream
bandwidth is important, and you should choose the highest upstream bandwidth available.

Reliability and customer service: ISP reliability has increased significantly in recent
years; however, it is still not as reliable as your phone or television service. There is no
objective way to measure reliability and customer service, so you should talk to your
neighbors about their experiences and search the Web for reviews of ISPs in your area.

Transfer an existing account

No matter which ISP you use, you’ll connect to the same Internet. You’ll be able to
reach the same Web sites and send instant messages to all of your friends. However, if you
use the e-mail account provided by your current ISP, you will lose that when you switch
ISPs.

If possible, activate your new ISP account a week before you close your old ISP
account. Notify everyone you want to exchange e-mail with that you have a new e-mail
address, and ask them to update your address in their contact list. Then, if anyone sends an
e-mail to your old account before you close it, reply from your new address.

4 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Tip: Your new ISP will provide you with an e-mail address. However, you do not
have to use it. Consider using a free Hotmail account instead. If you use Hotmail, you can NOTES
keep the same e-mail address no matter which ISP you use.

If you’ve registered your e-mail account with Web sites that you want to receive e-
mail from, you’ll need to let those Web sites know that your address has changed. Examples
might include online stores, forums, communities, financial services, and Microsoft Passport.
It’s important to change your account while your old e-mail address is still available, because
Web sites often send a confirmation e-mail to your existing address before allowing you to
make a change.

Set up your home network

Your ISP brings the Internet to your home, but it’s your home network that connects
your computer to the Internet. If you have a single desktop computer and it’s in the same
room as your Internet connection, your home network will simply consist of a single cable
(provided by your ISP) that runs from your modem or router to your computer. Your ISP
will either set it up for you or provide you with instructions for connecting your computer.

If you have more than one computer, a portable computer, or devices such as an
Xbox 360 or Media Centre Extender that you want to connect to the network, you can
connect all the devices to the Internet simultaneously. For stationary devices, such as desktop
computers and Xbox 360, you can connect them to a wired network. For mobile devices,
such as a portable computer, you will need to install a wireless network. To help determine
which type of network to choose, read choosing the type of network to install.

1.3 CONNECT YOUR COMPUTER TO THE NETWORK

Once your network is set up, you need to connect your computers and other network
devices. For computers, first verify that they have a compatible network adapter, and
install one if necessary. Then follow these instructions for the devices you want to connect:

To connect to the Internet, follow these steps (which may vary slightly, depending on
the software required for your network adapter)
1. Shut down your computer and install your network adapter.Note: If you are installing
a USB (Universal Serial Bus), CardBus, or CF card network adapter, you do not
need to shut down your computer before installing the network adapter.

2. Start Windows, and log on as an administrator.


3. The Found New Hardware Wizard will appear. Click Yes, now and every time I
connect a device. Then click Next..

5 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

4. The Found New Hardware Wizard asks you how you would like to find your
driver. The driver is software Windows needs to communicate with your network
adapter. Typically, it is included on the CD-ROM that came with your network
adapter. Insert the floppy disk or CD-ROM, and then click Next.

5. If prompted, click Continue anyway. Software that does not have a digital signature
has not been officially tested by Microsoft. However, it’s usually okay.

6. Windows XP copies the needed files and installs the software for your network
adapter. On the final page, click Finish.

6 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

7. If prompted, click Yes to restart your computer. After your computer restarts, log
on normally.

If you are connecting to a wired network, you are ready to use the Internet.

1.4 DOMAIN NAME SYSTEM

Introduction

A key component of the Internet and how it works revolves around the Domain
Name System, otherwise known as DNS. The underlying technology behind the Internet,
is that when a computer needs to talk to another computer on the Internet, they communicate
via the computer’s IP Address. The IP Address is a unique set of numbers associated with
a particular machine, which will be discussed in a separate article. An example of an IP
Address is 216.213.19.27, which is the IP Address that corresponds to
www.bleepingcomputer.com.

As the predecessor of the Internet, ARPANET, grew larger, connecting to remote


machines by their IP Address grew cumbersome. It became more and more difficult for
people to remember the IP Addresses associated with the machines they were trying to
reach that a system was created to translate easier to remember symbolic names to their
equivalent numerical IP Address. Thus the Domain Name System was born.

Domain Name System

The Domain Name System is the system used on the Internet for the mapping of
names, such as www.google.com, to IP Addresses such as 216.239.51.99. Every time a
new domain, like bleepingcomputer.com, is registered, that domain is entered into one of
the 13 Root Servers spread throughout the world and overseen by an organization called
ICANN. Because your domain is in one of these servers, it can be reached and understood
by the rest of the users on the Internet.

7 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Another key element of the Domain Name System are DNS Servers run by Companies
NOTES and Internet Service Providers. Every time you connect to a site, you are asking your ISP’s
DNS Server to resolve, or convert, the hostname such as www.google.com to a an ip
address such as 216.239.51.99. If your ISP’s name server is not working or can not be
reached, then you will not be able to traverse the Internet using hostnames, but instead
would have to use their IP Address equivalent.

Any time you connect to a site, your ISP DNS Server must find out what name server
has the information about the domain for the site you are trying to reach. Your ISP’s DNS
Server will connect to a Root Server and ask it who the name server is that knows the
information about the site you are trying to reach. The Root Server will tell your ISP’s
DNS Server what server they should next contact for information. Next your ISP’s DNS
Server will then contact the server that the Root Server told it to contact, where it will be
given the IP Address associated with the site you are trying to reach.

Real Life Example

A lot of what has been discussed may be a bit confusing, so lets do a real life example.
In the flowchart below labelled Figure 1, you will see a computer trying to connect to
www.google.com and the steps it takes.

8 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

We will discuss these steps below:


NOTES
1. A User opens a web browser and tries to connect to www.google.com. The
operating system not knowing the IPAddress for www.google.com, asks the ISP’s
DNS Server for this information.

2. The ISP’s DNS Server does not know this information, so it connects to a Root
Server to find out what name server, running somewhere in the world, know the
information about google.com.

3. The Root Server tells the ISP’s DNS Server to contact a particular name server
that knows the information about google.com.
4. The ISP’s DNS Server connects to Google’s DNS server and asks for the IP
Address for www.google.com.
5. Google’s DNS Server responds to the ISP’s DNS server with the appropriate IP
Address.
6. The ISP’s DNS Server tells the User’s operating system the IP Address for
google.com.
7. The operating system tells the Web Browser the IP Address for www.google.com.
8. The web browser connects and starts communication with www.google.com.
Exchanging email’s

1.5 SMTP

The process of exchanging email is based upon a number of different protocols, but
at the heart of electronic email is the simple mail transport protocol (SMTP). This protocol
is based upon the connection-orientated TCP protocol and is certainly the most widely
used email transport in the Internet today. SMTP is situated at the top of the TCP/IP stack.

There are a number of commands a SMTP server understands. The commands


HELO, MAIL FROM, RCPT TO, DATA and “.” are all SMTP commands:

Instant messaging offers real-time communication and allows easy collaboration, which
might be considered more akin to genuine conversation than email’s “letter” format. In
contrast to e-mail, the parties know whether the peer is available. Most systems allow the
user to set an online status or away message so peers are notified when the user is available,
busy, or away from the computer. On the other hand, people are not forced to reply
immediately to incoming messages. For this reason, some people consider communication
via instant messaging to be less intrusive than communication via phone. However, some
systems allow the sending of messages to people not currently logged on (offline messages),
thus removing much of the difference between IM and email.

9 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Instant messaging allows instantaneous communication between a number of parties


NOTES simultaneously, by transmitting information quickly and efficiently, featuring immediate receipt
of acknowledgment or reply. In certain cases IM involves additional features, which make
it even more popular, i.e. to see the other party, e.g. by using web-cams, or to talk directly
for free over the internet.

It is possible to save a conversation for later reference. Instant messages are typically
logged in a local message history which closes the gap to the persistent nature of e-mails
and facilitates quick exchange of information like URLs or document snippets (which can
be unwieldy when communicated via telephone).

Instant messaging actually predates the internet, first appearing on multi-user operating
systems like CTSS and Multics in the mid-1960s. Initially, many of these systems, such as
CTSS’.SAVED, were used as notification systems for services like printing, but quickly
were used to facilitate communication with other users logged in to the same machine. As
networks developed, the protocols spread with the networks. Some of these used a peer-
to-peer protocol (eg talk, ntalk and ytalk), while others required peers to connect to a
server (see talker and IRC)

In the last half of the 1980s and into the early 1990s, the Quantum Link online service
for Commodore 64 computers offered user-to-user messages between currently connected
customers which they called “On-Line Messages” (or OLM for short). Quantum Link’s
better known later incarnation, America Online, offers a similar product under the name
“AOL Instant Messages” (AIM). While the Quantum Link service ran on a Commodore
64, using only the Commodore’s PETSCII text-graphics, the screen was visually divided
up into sections and OLMs would appear as a yellow bar saying “Message From:” and the
name of the sender along with the message across the top of whatever the user was already
doing, and presented a list of options for responding. As such, it could be considered a sort
of GUI , albeit much more primitive than the later Unix, Windows and Macintosh based
GUI IM programs. OLMs were what Q-Link called “Plus Services” meaning they charged
an extra per-minute fee on top of the monthly Q-Link access costs.

Modern, Internet-wide, GUI based messaging clients, as they are known today, began
to take off in the mid 1990s with ICQ (1996) being the first, followed by AOL Instant
Messenger (AOL Instant Messenger 1997). AOL later acquired Mirabilis, the creators of
ICQ. A few years later ICQ (by now owned by AOL) was awarded two patents for
instant messaging by the U.S. patent office. Meanwhile, other companies developed their
own applications (Excite, MSN, Ubique and Yahoo), each with its own proprietary protocol
and client; users therefore had to run multiple client applications if they wished to use more
than one of these networks. In 1998 IBM released IBM Lotus Sametime, a product based
on technology acquired when IBM bought Haifa-based Ubique and Lexington-based
Databeam.

10 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

In 2000, an open source application and open standard- based protocol called Jabber
was launched. Jabber servers could act as gateways to other IM protocols, reducing the NOTES
need to run multiple clients. Modern multi-protocol clients such as Pidgin, Trillian, Adium
and Miranda can use any of the popular IM protocols without the need for a server gateway.

Recently, many instant messaging services have begun to offer video conferencing
features, Voice Over IP (VoIP) and web conferencing services. Web conferencing services
integrate both video conferencing and instant messaging capabilities. Some newer instant
messaging companies are offering desktop sharing, IP radio, and IPTV to the voice and
video features.

The term “instant messenger” is a service mark of Time Warner and may not be used
in software not affiliated with AOL in the United states.. For this reason, the instant messaging
client formerly known as Gaim or gaim announced in April 2007 that they would be renamed
“Pidgin”.

1.6 MOBILE INSTANT MESSAGING

Mobile Instant Messaging (MIM) is a presence enabled messaging service that aims
to transpose the desktop messaging experience to the usage scenario of being on the
move. While several of the core ideas of the desktop experience on one hand apply to a
connected mobile device, others do not: Users usually only look at their phone’s screen —
presence status changes might occur under different circumstances as happens at the desktop,
and several functional limits exist based on the fact that the vast majority of mobile
communication devices are chosen by their users to fit into the palm of their hand.

Some of the form factor and mobility related differences need to be taken into account
in order to create a really adequate, powerful and yet convenient mobile experience: radio
bandwidth, memory size, availability of media formats, keypad based input, screen output,
CPU performance and battery power are core issues that desktop device users and even
nomadic users with connected notebooks are usually not exposed to.

Several formerly untackled issues have been identified and addressed within IMPS,
which was developed as part of an early mobile telephone industry initiative to kick off a
broader usage of mobile instant messaging. The Open Mobile Alliance has taken over this
standard, formerly called Wireless Village, as IMPS V1.0 in November 2002. Since then
this standards has been further developed to IMPS V1.3, the latest candidate for release,
and is expected to be released before the end of 2006.

There are downloadable mobile applications offered by different independent


developers that allow users to chat within public and corporate IM services from mobile
devices.

11 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Among the advantages of using such IM clients over SMS are: IM clients use data
NOTES instead of SMS text messages; IM-like chat mode, faster and quicker messaging. Some
IM software allows group communication.

Several large scale mobile telephone industry companies are planning to jointly deliver
a ubiquitous, interoperable presence enabled messaging service, built according to
interoperability recommendations developed in the GSM Association. Considering these
organisations are jointly representing approximately 1.5 billion active Short Text Messaging
(SMS) users, it remains to be seen if such an initiative may also help to drive the different
industry factions to agree on a truly interoperable approach at least for Mobile Instant
Messaging sometime in the not too far future.

In the meantime, other developments have proposed usage of downloadable


applications with the intention to create their own approach to IM that runs on most mobile
phones worldwide. Essentially, several of these clients are Java applications are instantly
downloaded and then connected to back-end servers through GPRS/3G Internet Channels.
Some of the implementations can connect to other IM services.

Friend-to-friend networks

Instant Messaging may be done in a friend-to-friend network, in which each node


connects to the friends on the friends list. This allows for communication with friends of
friends and for the building of chat rooms for instant messages with all friends on that
network.

Emotions are often expressed in shorthand. But a movement is currently underway to


be more accurate with the emotional expression. Real time reactions such as (chortle)
(snort) (guhfaw) or (eye-roll) are rapidly taking the place of acronyms.

Instant messaging has proven to be similar to personal computers, e-mail, and the
WWW, in that its adoption for use as a business communications medium was driven
primarily by individual employees using consumer software at work, rather than by formal
mandate or provisioning by corporate information technology departments. Tens of millions
of the consumer IM accounts in use are being used for business purposes by employees of
companies and other organizations.

In response to the demand for business-grade IM and the need to ensure security and
legal compliance, a new type of instant messaging, called “Enterprise Instant Messaging”
(“EIM”) was created when Lotus software launched IBM Lotus Sametime in 1998.
Microsoft followed suit shortly thereafter with Microsoft Exchange Instant Messaging, and
later created a new platform called Microsoft Office Live Communications Server. Since
then, both IBM Lotus and Microsoft have introduced federation between their EIM systems
and some of the public IM networks thus employees may use a single interface to both
their internal EIM system and their buddies on AOL, MSN, and Yahoo!. Current leading

12 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

EIM platforms include IBM Lotus Sametime, Microsoft Office Live Communications Server,
and Jabber XCP. NOTES
The adoption of IM across corporate networks outside of the control of IT
organizations creates many risks and liabilities for companies who do not effectively manage
and support IM use. Companies implement specialized IM archiving and security products
and services like those from Secure Computing, Akonix, Surfcontrol, and ScanSafe to
mitigate these risks and provide safe, secure, productive instant messaging capabilities to
their employees.

Risks and liabilities

Although instant messaging delivers many benefits, it also carries with it certain risks
and liabilities, particularly when used in workplaces. Among these risks and liabilities are:
 Security risks (e.g. IM used to infect computers with spyware, viruses, trojans,
worms)
 Compliance risks
 Inappropriate use
 Intellectual property leakage
Crackers’ (malicious “hacker”) use of instant messaging networks to deliver malicious
code has grown consistently from 2004 to the present, with the number of discrete attacks
listed by the IM Security Center[6] having grown 15% from 347 attacks in 2005 to 406 in
2006. Hackers use two methods of delivering malicious code through IM: delivery of
virus, trojan, or spyware within an infected file, and the use of “socially engineered” text
with a web address that entices the recipient to click on a URL that connects him or her to
a website that then downloads malicious code. Viruses, worms, and trojans typically
propagate by sending themselves rapidly through the infected user’s buddy list. An effective
attack using a “poison URL” may reach tens of thousands of people in minutes when each
person’s buddy list receives messages appearing to be from a trusted friend. The recipients
click on the web address, and the entire cycle starts again. Infections may range from
nuisance to criminal, and are becoming more sophisticated each year.

In addition to the malicious code threat, the use of instant messaging at work also
creates a risk of non-compliance to laws and regulations governing the use of electronic
communications in businesses. In the United States alone there are over 10,000 laws and
regulations related to electronic messaging and records retention. The more well-known
of these include the Sarbanes-Oxley Act, HIPAA, and SEC 17a-3. Recent changes to
Federal Rules of Civil Procedure, create a new category for electronic records which may
be requested during discovery (law) in legal proceedings. Most countries around the world
also regulate the use of electronic messaging and electronic records retention in similar
fashion to the United States. The most common regulations related to IM at work involve
the need to produce archived business communications to satisfy government or judicial

13 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

requests under law. Many instant messaging communications fall into the category of business
NOTES communications that must be archived and retrievable.

1.7 IRC
IRC is an open protocol that uses TCP and optionally SSL. An IRC server can
connect to other IRC servers to expand the IRC network. Users access IRC networks by
connecting a client to a server. There are many client and server implementations, such as
mIRC and the Bahamut IRCd. Most IRC servers do not require users to log in, but a user
will have to set a nickname before being connected.
IRC was originally a plain text protocol (although later extended), which on request
was assigned port 194/TCP by IANA. However, the de facto has always been to run IRC
on 6667/TCP and nearby port numbers to avoid having to run the IRCd software with root
privileges.. It is possible (though quite inconvenient) to use IRC via a basic byte-stream
client such as netcat or telnet.
The protocol specified that characters were 8 bit but did not specify the character
encoding the text was supposed to use. This can cause problems when users using different
clients and/or different platforms want to converse.
All client-to-server IRC protocols in use today are descended from the protocol
implemented in the irc2.8 version of the IRC2server, and documented in RFC 1459. Since
RFC 1459 was published, the new features in the irc2.10 implementation led to the
publication of several revised protocol documents; RFC 2810, RFC 2811, RFC 2812,
and RFC 2813, however these protocol changes have not been widely adopted among
other implementations.
Although many specifications on the IRC protocol have been published, there is no
official specification, as the protocol remains dynamic. Virtually no clients and very few
servers rely strictly on the above RFCs as a reference.
Microsoft made an extension for IRC in 1998 via the proprietary IRCX. They later
stopped distributing software supporting IRCX, instead developing the proprietary MSN
.NET Messenger service.

The standard structure of a network of IRC servers is a tree (acyclic graph). Messages
are routed along only necessary branches of the tree but network state is sent to every
server and there is generally a high degree of implicit trust between servers. This architecture
has a number of problems. A misbehaving or malicious server can cause major damage to
the network, and any changes in structure, whether intentional or a result of conditions on
the underlying network, require a net-split and net-join. This results in a lot of network
traffic and spurious quit/join messages to users, and temporary loss of communication.
Adding a server to a large network means a large background bandwidth load on the
network and a large memory load on the server.

14 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

1.8 SPAM
NOTES
This brings us onto the subject of open relays. In the past, SMTP servers were open
in that they permitted anybody, whether internally or externally, to send email through
them. This made life easier for users since they could simply send email using any SMTP
server.
That all changed with the rise of the scourge of the Internet “ SPAM!
The definition of spam is that it is unsolicited email - pretty much the equivalent of
junk mail in your post box.
People abusing the use of the Internet generate Spam in large volumes. They will
often use an SMTP server that is “open” to the public to relay their messages to multiple
recipients - hence the term “open relay”. When they do this, the recipients will receive
email’s from the spammer without a request for such email.

There are problems with spam.

1. Firstly, it clogs up valuable bandwidth with meaningless rubbish.


2. Secondly, and probably more detrimental to the “open relay”, is the fact that this
relay ends up being added to a “blacklist” of servers. Therefore, any “valid” email
sent from the open relay, will be bounced by the recipient who will be protecting
themselves from spammers. The fact that your email’s are valid since they originate
from people within your organization is immaterial - as far as the recipient mail
server is concerned, you are one of the spammers. It can take anywhere between
24 and 72 hours (and sometimes even longer) to get your mail server removed
from the blacklist. During this time however, any valid email’s sent by your mail
server will be rejected (bounced) back to your mail server. This can be frustrating
and cause a loss of productivity, but will be a real source of embarrassment to the
system administrator!
3. Finally, spam is just plain irritating.

1.9 RETRIEVING EMAIL

Having spent some time discussing how we send email, we now need to look at how
we, the client, receive the email that has been sent to us. Mail retrieval is slightly more
complex than sending mail purely because there are many different choices a system
administrator has when allowing clients to receive email - or when fetching email herself. In
effect there are three broad categories:
1. You are a user on the host that is also the SMTP server,
2. you are a user on another hosts on the same Intranet as the SMTP server,
3. you (probably) have very little in common with the SMTP server (perhaps the
SMTP server is maintained and belongs to your ISP). However, since you are a
client of the ISP, they host your email.

15 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

We’ll look at each of these in turn:


NOTES
Category 1: We are a user on the host that is also the SMTP server.

Note

I am not discussing how MS Exchange works as firstly I am not qualified to do so,


and secondly these courses have nothing to do with Microsoft products.

When the email is delivered from the sending mail server, it has to be stored somewhere
while time elapses until you, the user decides to retrieve it.

In general, most email servers store your email locally in a file or files on their hard
disk drives.

Assuming then you are the client that will be reading your email, and you already have
a login to the mail server, your email client will merely need to look inside the file(s) or
inside the directory that stores the files.

There is another whole issue of what mailbox type you are using to store email, but
we’ll leave that until the advanced networking course where we actually configure a mail
server.

Category 2: We are a user on another host on the same Intranet as the SMTP server

For both Category 2 and 3, we will want to implement a retrieval protocol. Categories
2 and 3 are similar in many respects, but differ in the fact that in Category 3, you, the client
have little to do with the organization offering the service.

A typical office environment (as is prevalent in many large corporate companies) is


that describing Category 2, while a user that dials into their ISP to retrieve their email
would be an example of Category 3. Of course these are not mutually exclusive, and one
could implement both Categories in an environment.

In the case of Category 2, we do not want to log onto a separate machine in order to
read our email.

Let ‘s make it a little more concrete. In the case of a large corporate, they may require
that all their clients keep all email on a central server for the purposes of security, backups
and the ability to roam around the office.

For this there is a protocol called the Internet Message Access Protocol or IMAP.
IMAP is used in this scenario (office environment) since it encourages email to be stored
on the server rather than an individual workstation client.

16 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Thus, IMAP is a central point of storage of the email. Naturally, given this type of
system, the system administrator will need to make the necessary arrangements to have NOTES
the email backed up and will need to provide a good level of service delivery. You will not
believe how fast users will moan if their email is not available.

Category 3: Your email resides at an ISP.

The other method of email retrieval is that offered by the majority of ISP’s.

ISP ‘s generally don’t want to store your email permanently (perhaps they have limited
storage capacity for all their clients to store email). As such, they are happy to store (for a
very limited time) the email that arrives at their mail servers addressed to you, but as soon
as you dial in to collect it, they expect you to take it away with you. They are usually not
responsible for your mail once it is accessed by you (contrast with this, a corporate client
who stored the email of everyone in the organization on a central email server).

Thus, in the ISP scenario, it makes little sense for the ISP to implement IMAP, since
this implies our email will be stored in a central repository at the ISP - pretty pointless
when you need to read something off-line.

Additionally, if the ISP has implemented IMAP, then when we do go online, all the
messages will need to be downloaded to the client (in fact only the headers are downloaded
with IMAP, the message body remains on the server until such time as it is accessed). This
is a costly process in both time (you will be waiting for the email to download) and money
(you will be paying phone charges while waiting - come to think of it, no wonder Telecoms
companies are so wealthy!!). In this scenario, it may be wise to have a protocol that has
the ability to pop the mail off the mail server and store it locally (on your machine) - enter
the Post Office Protocol version 3 (POP3).

POP3 was designed to retrieve email from a mail server in “batch” mode and save it
as files on the hard disk of the client machine. This system has the advantages in terms of
time and money saved, as well as the ability to read your email anywhere, any time. However,
the disadvantage is that if your hard disk fails (aka crashes), you loose all your email. But
of course, you are a good system administrator and you have done adequate backups!

POP3 and IMAP are often bundled together in the same package. In this way, when
you install a POP3 server, you get a free IMAP servers thrown in, or of course visa-versa.
Now who said Linux was not good value for money!!!! POP3 and IMAP serve different
purposes and it is wise to consider carefully what is required before offering one or the
other. In general, MS Exchange is a modified IMAP type service.

POP3 and IMAP serve different purposes and it is wise to consider carefully what is
required before offering one or the other. (In general, MS Exchange is a modified IMAP
type service.)

17 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

In much the same way as we were able to walk through a SMTP conversation, we
NOTES are able to walk through a POP3 and IMAP conversation. There are a variety of POP3
and IMAP commands, and we will look at some in the trouble shooting section later in this
course.
Finally, POP and IMAP are inherently insecure. They send passwords unencrypted
over the wire and are therefore prone to eavesdropping and packet sniffing. One method
of solving this is by means of using certificates and implementing the secure sockets layer
(SSL).
Thus in addition to POP3 and IMAP, there is POP3S and IMAPS and this will be
covered in the advanced networking course when you configure a POP3 and IMAP server.
In conclusion then, when setting up an email client like Ximian’s Evolution or Mutt,
you need to supply an outgoing mail server (the SMTP server) as well as an incoming mail
server (a POP3 or IMAP server). Once that is done, you should be able to send and
receive email.
It is also worth noting that, on the whole, outgoing SMTP servers do not usually
require that you authenticate in order to send email, while naturally incoming POP3 and
IMAP servers will require one form of authentication.
Fight Spam on the Internet!
You have probably seen an increase in the amount of “junk mail” which shows up in
your email box, or on your favorite newsgroup. The activities of a small number of people
are becoming a bigger problem for the Internet. We have been actively engaged in fighting
spam for years.
This document explains all options available in the Options window of Firefox.

Main Options

18 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Startup
NOTES
When Firefox starts

Here you are able to specify the action that Firefox will take when you launch it.
Choose Show my home page to make Firefox go to your home page, each time you
launch Firefox. Choose Show a blank page to make Firefox show a blank page, each time
you launch Firefox. Choose Show my windows and tabs from last time to make Firefox
open the webpages you had open when you last closed Firefox.

Home Page

Here you are able to specify the page (or tab group) that Firefox will show when you
press the Home button. Enter the address in the Location(s) field.

Click Use Current Page(s) to use the page you’re currently visiting. You can also use
multiple home pages. If more than one browser tab is currently opened, this button will set
the whole tab group as a start page.

To specify the home page(s) using a bookmark, click Use Bookmark.... You can
even select a whole bookmark folder to be used!

If you want to restore the default home page, click Restore to Default.

Downloads

Show the Downloads window when downloading a file

With this option selected, Firefox will open the Downloads window when you start
downloading a file.

Close it when all downloads are finished

Select this option to have Firefox close the Downloads window when all downloads
currently in progress finish.

Save files to

This is the default option. It allows you to specify a default folder where all downloads
will be saved to, such as the My Downloads folder. You can select a different folder by
clicking on the Browse... button, which will open the folder in the default file manager (e.g.
Windows Explorer or Nautilus).

Ask me where to save files

Firefox will ask you where you want a file to be saved when you download something.

19 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

System Defaults
NOTES
On Windows, you can check to see if Firefox as the default browser by clicking
Check Now. This will ensure Firefox is used whenever an application is trying to display a
web page or when you double-click a HTML file. This feature does not work in Linux or
Macintosh.

Tabs Options

Tabs Options Panel contains options related to the tabbed browsing feature.

New pages should be opened in:

a new window

With this option selected, any web pages opened by web links or other applications
will be displayed in a new window.

a new tab

With this option selected, any web pages opened by web links or other applications
will be displayed in a new browser tab.

Warn me when closing multiple tabs

If you’re trying to close a window with more than one tabs opened, Firefox will
display a confirmation dialog before the window is closed, to prevent accidental dataloss.

Warn me when opening multiple tabs might slow down Firefox

20 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

If you accidentally try to open a large set of tabs, which may slow down performance,
Firefox will display a confirmation dialog, giving you a chance stop it from opening so NOTES
many pages at once.

Always show the tab bar

If you’re only viewing one Web page in a browser window, the tab bar is not shown.
Check mark this option to show the tab bar all the time.

When I open a link in a new tab, switch to it immediately

When you middle-click on Web links (or hold down Ctrl while clicking with the left
mouse button), the links will be opened in a new tab. That tab will not be shown directly;
it will be loaded in a background tab. Check this option to load the link in a foreground tab
instead, which will show that tab directly.

1.10 CONTENT OPTIONS

Block pop-up windows

By default, Firefox blocks annoying pop-up windows on web sites. Unchecking this
option will disable pop-up blocking.

21 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Some sites make legitimate use of pop-up windows. Therefore, the pop-up blocking
NOTES feature has a simple whitelist mechanism to allow certain sites to open pop-ups anyway. To
add a site to the whitelist, click Exceptions and enter the address of the web site and click
Allow.

To remove a site from the whitelist, select that site from the list and then click Remove
Site. To clear the whitelist completely, click Remove All Sites.

Load images automatically

By default, images are loaded on web pages. Uncheck this option to disable images
completely. If images are loaded, you can still specify not to load images from certain sites
by clicking Exceptions.

Enable JavaScript

JavaScript is a scripting language commonly used to construct web pages. Programmers


use JavaScript to make web pages more interactive; for example, to display forms and
buttons. Disabling JavaScript may cause some sites not to work properly, including this
one.

While it is usually recommended to keep JavaScript enabled, there are some functions
that you may want to disable. Click the Advanced... button to display these options.

22 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Allow scripts to:


NOTES
Move or resize existing windows

Uncheck this option to disable moving and resizing windows using scripts.

Raise or lower windows

Uncheck this option to make sure scripts cannot raise (bring to the front) or lower
(send to the back) windows.

Disable or replace context menus

Uncheck this option to prevent web pages from disabling or changing the Firefox
context menu.

Hide the status bar

Check mark this option to allow scripts to hide the status bar in pop-up windows.

Change status bar text

Check mark this option to allow scripts to use status bar text scrolling and Web
address hiding.

The options displayed here are certainly not all of them. You can also make sure all
popup windows are resizable, minimizable, and that the menu and toolbar is always
displayed. All of these options are available on the Tips & Tricks page.

Fonts & Colors

Normally, web pages are displayed in the default font set by your browser or in a font
chosen by the web pages’ authors. Here, you can select the font and font size used when
web pages don’t specify one. For more advanced font options, click on Advanced..., or
for colors options, click on Colors....

23 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Fonts for
NOTES
You can actually set a specific default font for various types of fonts, and character
sets.

To change the default fonts by font type and character set:


1. From the Fonts for drop-down list, choose a character set. For instance, to set
default fonts for the Western (Roman) character set, choose Western.
2. Select whether proportional text should be serif (like “Times New Roman”) or
sans-serif (like “Arial”). Then specify the font size you want for proportional text.
3. Specify the font to use for Serif, Sans-serif and Monospace fonts. You can also
change the size for Monospace fonts.
Finally, you can set the minimum font size to be used on screen. This can be useful on
some sites that use very small fonts that are barely readable.

Allow pages to choose their own fonts, instead of my selections above

By default, Firefox uses the fonts specified by the Web page author. Uncheck this
option to force all sites to be using your default fonts instead.

Character Encoding

The character encoding selected here will be used to display pages that do not specify
which encoding to use.

Text and Background

Here you can change the default text and background color to be used on Web pages
that haven’t specified that information. Click on the color samples to select colors.

Use system colors

Check this option to use the colors defined in your OS settings instead of the colors
specified above.

24 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Link Colors
NOTES
Here you can change the default colors for Web links. Click on the color samples to
select colors.

Underline links

By default, links are underlined on Web pages. Uncheck this option to disable this.
Note that many sites specify their own styling rules (including this site) and this option has
no effect on those sites.

Allow pages to choose their own colors, instead of my selections above

By default, Firefox uses the colors specified by the Web page author. Disabling this
option will force all sites to be using your default colors instead.

File Types

This panel controls how Firefox handles different file types such as applications,
compressed files, multimedia, etc. Click on Manage... to change the action for each file
type.

Download Actions

This list contains file types that you have downloaded. You can choose what Firefox
should do when clicking on a specific file type. Select the file type you want to modify and
click the Change Action... button.

25 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

This will display the Change Action window, where you can choose to have the file
type being opened by an application or saved to disk. For example, if you view lots of
media files on Web pages, you might want to specify that it always open it in your media
player, instead of asking where you want the file to be saved.

Open them with the Default application

Select this option to open this file type in the default application for that file type
(determined by the Operating System).

Open them with this application

Select this option to specify another application that should handle this file type. You
will see a dialog asking you to specify the application to use. If not, click the Browse...
button.

Save them on my computer

This option will save the files to your computer. If you have the Save all files to option
selected, the files will be saved automatically.

Use this plugin

This option will make Firefox use a plugin to open the files.

26 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Feeds Options
NOTES

Web Feeds are specially formatted documents which summarize the content of web
sites. You can view the content of feeds in Firefox, create Live Bookmarks for them, or
add them to a feed reader on your computer or on the Web. The options in this pane
determine what Firefox does when you view a feed.

Show me a preview and ask me which Feed Reader to use

When you view a feed within Firefox, you will be shown a preview of its contents.
With this option selected, you are always given a choice of what you would like to use to
subscribe to the feed at the top of the preview page.

Subscribe to the feed using

Instead of displaying a preview of the feed when you view one, you can have the feed
be opened directly in a feed reader by selecting a reader from the list of available readers.
You can choose to use an application on your computer to subscribe to feeds by clicking
the Choose Application... button and finding the application on your computer. Alternately,
you can choose to automatically subscribe to feeds by saving them as Live Bookmarks.
To choose a subscription method, simply select it from the list.

27 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Privacy Options
NOTES

The Privacy panel contains options related to your privacy. As you browse the web,
information on where you have been, what pages you have visited, etc are stored here.

History

Remember visited pages for the last ... days.

Here you can specify for how many days you want the browser to remember what
pages you have visited. The default is 9 days.

Remember what I enter in forms and the search bar

When you enter information in web forms or the search bar, Firefox remembers what
you type and automatically makes suggestions when you enter information again. To stop
this behavior, uncheck this option.

Remember what I’ve downloaded

This option controls whether or not past downloads show up in the Downloads window.
The Downloads window (accessible from the Tools menu or by pressing Ctrl+J) displays
a list of your recent downloads. Downloads options are available in the Main panel.

Cookies

A cookie is a file created by a web site that stores information on your computer, such
as your preferences when visiting that site. For example, this site stores the navigation
menu behavior (locked or unlocked) in a cookie file.

28 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Accept cookies from sites


NOTES
By default, cookies are enabled. Uncheck this option to disable the use of cookies.
Note that some sites may not work properly when this feature is disabled.

To display the stored cookies, click Show Cookies. Click the Exceptions button to
allow or deny specific sites to store cookies on your computer, regardless of the global
settings.

You have three options of for how long Firefox should store cookies, as explained
below:
 until they expire
This is the default option. Cookies will be stored for as long as the site specifies.
 until I close Firefox
This will remove cookies when you close the browser.
 ask me every time

Select this option to gain maximum control of which cookies are allowed to be stored
on your computer. A confirmation dialog will be displayed each time a site tries to save a
cookie.

Private Data

Always clear my private data when I close Firefox

You can choose to have Firefox clear your private data when you close it. To configure
what data is cleared, click the Settings... button.

Ask me before clearing private data

With this option selected, Firefox will ask you before automatically clearing the data
specified by clicking Settings....

If you wish to clear your private data right now, you can click the Clear Now... button
to do so. To clear your private data from outside the options dialog, either press
Ctrl+Shift+Del or select Tools > Clear Private Data....

29 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Security Options
NOTES

Warn me when sites try to install add-ons


By default, Firefox allows some specific web sites to install add-ons for Firefox. To
view and edit this list of allowed sites, click Exceptions. This whitelist mechanism works
the same way as the Block Popup Windows feature explained above.
Tell me if the site I’m visiting is a suspected forgery
Check this option if you want Firefox to actively check whether the site you are
visiting may be an attempt to mislead you into providing personal information (this is often
referred to as phishing).
Check using a downloaded list of suspected sites
With this option selected, Firefox will check the current site against a frequently updated
list stored on your computer. No data about the sites you visit is transferred to third-party
anti-phishing providers during normal browsing. Since phishing techniques and sites evolve
quickly, this mode of protection may not be as effective as having an anti-phishing provider
check every site you visit.
Check by asking ... about each site I visit

With this option selected, Firefox will send the address of the current page over an
encrypted connection to the selected third-party anti-phishing provider in order to verify its
identity. This method offers the greatest protection, but information such as your IP address
and browser version may be transfered and stored by the selected provider. You will be
prompted to review and agree to the selected data provider’s privacy policy after selecting
this option.

30 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Passwords
NOTES
Remember passwords for sites

Firefox is capable of securely storing passwords you enter in web forms to make it
easier to log on to Web sites. You can manage the saved passwords and delete individual
passwords by clicking Show Passwords. To stop saving passwords altogether, uncheck
this option.

Even with this option checked, however, you’ll still be asked whether to save
passwords for a site when you first visit it. If you select Never for This Site, that site will be
added to an exceptions list. To access that list or to remove sites from it, click the
Exceptions... button.

Use a master password

Firefox can protect sensitive information such as saved passwords and certificates by
encrypting them using a master password. If you create a master password, each time you
start Firefox, it will ask you to enter the password the first time it needs to access a
certificate or stored password. You can set, change, or remove the master password by
by checking or unchecking this option or by clicking the Change Master Password...
button. If a master password is already set, you will need to enter it in order to change or
remove the master password.

Warning Messages

Click the Settings... button to configure the security warnings Firefox displays while
you browse the web.

I am about to view an encrypted page

When this option is enabled, Firefox will notify you every time you are about to view
an encrypted page.

I am about to view a page that uses low-grade encryption

31 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

With this option enabled, Firefox will warn you when you visit a page which uses low-
NOTES grade encryption.

I leave an encrypted page for one that isn’t encrypted

With this option enabled, Firefox will warn you every time you move from an encrypted
page to an unencrypted page either by selecting a link on the page, selecting a bookmark
or typing a new address into the location bar.

.I submit information that’s not encrypted

When this option is enabled, Firefox will warn you when you submit data via a form
that’s not encrypted.

I’m about to view an encrypted page that contains some unencrypted information

With this option enabled, Firefox will warn you when the page you’re viewing contains
a mixture of encrypted and unencrypted content. If an encrypted page contains unencrypted
data, you should verify the identity of the page you’re viewing prior to entering sensitive
data.

Advanced Options

ask me every time Accessibility

Always use the cursor keys to navigate within pages

32 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Specifies whether to move the system caret whenever the focus or selection changes.
Some accessibility aids, such as screen readers or screen magnifiers, use the system caret NOTES
to determine which area of the screen to read or magnify.

Search for text when I start typing

Find As You Type is a very useful feature in Firefox. It is a fast way of finding links or
text in a Web page without the need of displaying complex search dialogs. Read more
about Find As You Type at mozilla.org.

There are more options for Find As You Type covered in the Tips & Tricks page.

Browsing
Use autoscrolling

Autoscrolling is a useful feature which allows you to scroll the page by just holding
down the middle mouse button (usually the scroll wheel) and move the mouse up or down.
Some people find this annoying so here’s the option for it.

Use smooth scrolling

Smooth scrolling can be very useful if you read a lot of long pages. Normally, when
you press the Page Down key, the view jumps directly down one page. With Smooth
Scrolling, it slides down more smoothly, so you are actually able to see how much it
scrolls. This makes it easier to resume reading from the point you were before.

Check my spelling as I type

When this option is enabled, Firefox will check your spelling and offer possible
corrections as you type in web forms. Note that you may need to download a dictionary;
to do so, right-click on any text field, enable spellchecking if necessary, and then use the
provided Languages menu to download a dictionary.

Languages

Some web pages are offered in more than one language. Click the Choose... button
to specify your preferred languages.

33 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

Language selection

To add a language, press Select a language to add..., choose the language, and click
the Add button. Remove a language by selecting it in the list of active languages and clicking
the Remove button.

You can reorder languages to determine the preferred one in case a page is provided
in two or more of your selected languages. Do this using the Move Up and Move Down
buttons.

34 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Connection Settings
NOTES

Many organizations block access from the Internet to their networks. This prevents outside
parties from gaining access to sensitive information. The protection is called a firewall.

If your organization has a firewall, the browser may need to go through a proxy
server before connecting you to the Internet. The proxy server prevents outsiders from
breaking into your organization’s private network.

Configure Proxies to Access the Internet

Direct connection to the Internet

This is the default option. Choose this if you don’t want to use a proxy.

Auto-detect proxy settings for this network

Choose this if you want Firefox to automatically detect the proxy settings for your
network.

Manual proxy configuration

Choose this if you don’t have a proxy location (URL). Ask your system administrator
for the names and port numbers of the servers running proxy software for each network
service and enter the information in the appropriate fields.

Automatic proxy configuration URL

If there’s a proxy configuration file at your workplace, ask the system administrator
for its URL and enter it here. Press Reload to load the settings.

35 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Cache
NOTES
Pages you view are normally stored in a special cache folder for quicker viewing the
next time you visit the same page. Here you are able to specify the amount of disk space
the cache can use.

In addition to specifying the amount of disk space, you can also specify the cache
folder and the memory usage.

Clicking on Clear Now immediately clears the current contents of the cache, freeing
the disk space used by the cache.

Updates

Firefox can check whether a new version of your installed extensions or of Firefox
itself is available.

Firefox

By default, Firefox will periodically check and notify you when a new version is
available. Uncheck this option to disable the periodic check.

36 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Installed Add-ons
NOTES
By default, Firefox will periodically check and notify you when a new version for one
of your installed add-ons is available. Uncheck this option to disable the periodic check.

Search Engines

By default, Firefox will periodically check and notify you when a new version for one
of your installed search engines is available. Uncheck this option to disable the periodic
check.

When updates to Firefox are found:

Ask me what I want to do

By default, Firefox will periodically check and notify you when a new version is
available. Uncheck this option to disable the periodic check.

Automatically download and install the update

By default, Firefox will periodically check and notify you when a new version for one
of your installed add-ons is available. Uncheck this option to disable the periodic check.

Warn me if this will disable any of my add-ons

This option determines whether you will be warned before the installation of an update
which would require an incompatible add-on to be disabled because no newer, compatible
version exists. The warning will allow you to postpone installation of the update, though at
the expense of improvements included in it.

If all extensions and themes are compatible or can be updated to be compatible, the
Firefox update will be installed. Upon restart you will be asked to install any needed add-
on updates so that you can continue to use them.

Firefox automatically records the updates you have installed. You can view information
about these updates by clicking the Show Update History button.

You must be running Firefox as an administrator or as the user who originally installed
Firefox to install Firefox updates.

37 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

Protocols

1.11 USE SSL 3.0

Specifies whether you want to send and receive secured information through SSL3
(Secured Sockets Layer Level 3), a protocol that is intended to be more secure than
SSL2, standard protocol for communicating securely with web sites. Disabling it will prevent
you from visiting some sites.

Use TLS 1.0

Specifies whether you want to send and receive secured information through TLS
(Transport Layer Security), an open security standard similar to SSL3 (Secure Sockets
Layer). Note that some Web sites might not support this protocol.

Certificates

Certificates help perform encryption and decryption of connections to secure sites.

Select one automatically

When a web site requests a secure connection, Firefox will by default automatically
use an appropriate certificate.

Ask Every TimeIf you wish to manually choose a certificate (for example, if you
wish to use a certain type of encryption instead of what is automatically selected), select
this option and you’ll be in complete control of what certificates you use while browsing.

38 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Click the View Certificates button to view stored certificates, import new certificates,
and back up or delete old certificates in Firefox. NOTES
Firefox can use Certificate Revocation Lists (also known as CRLs) to ensure that
your certificates are always valid. Click the Revocation Lists button to manage the CRLs
installed on your computer.

Firefox can verify the validity of your certificates using OCSP (Online Certificate
Status Protocol) every time they are viewed or used. Firefox does not use OCSP by
default, but if you want to enable it, you can click on the Verification button. You will most
likely only need to change this if your Internet environment requires it.

Security devices can encrypt and decrypt connections and store certificates and
passwords. If you need to use a security device other than the one in Firefox, click the
Security Devices Security Devices button.

Instant messaging

It offers real-time communication and allows easy collaboration, which might be


considered more akin to genuine conversation than email’s “letter” format. In contrast to
e-mail, the parties know whether the peer is available. Most systems allow the user to set
an online status or away message so peers are notified when the user is available, busy, or
away from the computer. On the other hand, people are not forced to reply immediately to
incoming messages. For this reason, some people consider communication via instant
messaging to be less intrusive than communication via phone. However, some systems
allow the sending of messages to people not currently logged on (offline messages), thus
removing much of the difference between IM and email.

Instant messaging allows instantaneous communication between a number of parties


simultaneously, by transmitting information quickly and efficiently, featuring immediate receipt
of acknowledgment or reply. In certain cases IM involves additional features, which make
it even more popular, i.e. to see the other party, e.g. by using web-cams, or to talk directly
for free over the internet.

It is possible to save a conversation for later reference. Instant messages are typically
logged in a local message history which closes the gap to the persistent nature of e-mails
and facilitates quick exchange of information like URLs or document snippets (which can
be unwieldy when communicated via telephone).

Mobile Instant Messaging

Mobile Instant Messaging (MIM) is a presence enabled messaging service that aims
to transpose the desktop messaging experience to the usage scenario of being on the
move. While several of the core ideas of the desktop experience on one hand apply to a

39 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

connected mobile device, other do not. Users usually only look at their phone’s screen -
NOTES presence status changes might occus under different circumstances as happens at the desktop,
and several functional limits exist based on the fact that the vast majority of mobile
communication devices are chosen by their users to fit into the plam of their hand.

Some of the form factor and mobility related differences need to be taken into account
in order to create a really adequate, powerful and yet convenient mobile experience: radio
bandwidth, memory size, availability of media formats, keypad based input, screen output,
CPU performance and battery power are core issues that desktop device users and even
nomadic users with connected notebooks are usually not exposed to.

Several formerly untackled issues have been identified and addressed within IMPS,
which was developed as part of an early mobile telephone industry initiative to kick off a
broader usage of mobile instant messaging. The Open Mobile Alliance has taken over this
standard, formerly called Wireless Village, as IMPS V1.0 in November 2002. Since then
this standards has been further developed to IMPS V1.3, the latest candidate for release,
and is expected to be released before the end of 2006.

There are downloadable mobile applications offered by different independent


developers that allow users to chat within public (MSN, Yahoo!, Google Talk, AIM, ICQ)
and corporate (IBM Lotus Sametime, LCS, Reuters) IM services from mobile devices.

Among the advantages of using such IM clients over SMS are: IM clients use data
instead of SMS text messages; IM-like chat mode, faster and quicker messaging. Some
IM software allows group communication.

Several large scale mobile telephone industry companies are planning to jointly deliver
a ubiquitous, interoperable presence enabled messaging service, built according to
interoperability recommendations developed in the GSM Association.[5] Considering these
organisations are jointly representing approximately 1.5 billion active Short Text Messaging
(SMS) users, it remains to be seen[vague] if such an initiative may also help to drive the
different industry factions to agree on a truly interoperable approach at least for Mobile
Instant Messaging sometime in the not too far future.

In the meantime, other developments have proposed usage of downloadable


applications with the intention to create their own approach to IM that runs on most mobile
phones worldwide. Essentially, several of these clients are Java applications are instantly
downloaded and then connected to back-end servers through GPRS/3G Internet Channels.
Some of the implementations can connect to other IM services.

Online chat

Online chat can refer to any kind of communication over Internet, but is primarily
meant to refer to direct one-on-one chat or text-based group chat (formally also known as

40 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

synchronous conferencing), using tools such as instant messaging applications—computer


programs, Internet Relay Chat, talkers and possibly MUDs, MUCKs, MUSHes and NOTES
MOOes.

While many of the Internet’s well-known services offer online chat and messaging
services for free, an increasing number of providers are beginning to show strong revenue
streams from for-pay services. Again it is the adult service providers, profiting from the
advent of reliable and high-speed broadband, (notably across Eastern Europe) who are at
the forefront of the for-pay online chat revolution.

For every business traveller engaging in a video call or conference call rather than
braving the check-in queue, there are countless web users replacing traditional conversational
means with online chat and messaging. Like email, which has reduced the need for and
usage of letters, faxes, and memos, online chat is steadily replacing telephony as the means
of office and home communication. The early adopters in these areas are undoubtedly
teenage users of instant messaging. It might not be long before SMS text messaging usage
declines as mobile handsets provide the technology for online chat.

Reading and Sorting Messages

When you sign in to Web Mail, you will first see the contents of your Inbox. The
Inbox is a folder where all new incoming messages are stored. When you open a folder, a
list of its contents are displayed, this is called the message list.

In the message list, email messages are listed in the order they were received, with the
most recent at the top. You can change the order by clicking one of the column titles at the
top of the message list.

For each email message, the following information is displayed: who it is from, the
subject of the email, when it was received, and its size. (We will refer to this as the email’s
information line.)

At the top of the email list in the Inbox are the following clickable buttons: Delete,
This is spam, a pull-down menu, and Move. To use one of these functions on a particular
email message, select the email message by checking the box to the left of its information
line in the display list, then click the intended function button. You can select all the messages
on the page by checking the box at the very top of the pile of checkboxes.

 Delete—sends the selected messages to the Trash folder.


 This is spam—opens a page where you can report the checked messages as
spam, and block the addresses they were sent from.
 Move—choose a folder and click the Move button to send all the selected
messages to that folder.

41 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Note: You can select all the messages in a folder by checking the box above all the other
NOTES boxes, next to the column titles.

Checking for New Email Messages


Your new email messages will be displayed automatically each time you sign in to
Web Mail. To check for new email messages again, when the window is already open,
click the Check Mail button in the Folder List.
Opening a Message
To read a message, click its Subject in your message list. If you have Message
Preview turned on, the message will appear in the Preview Pane. Otherwise, the contents
of the message will replace the Message List.

When viewing a message, there are several options at the top of the message window,
beneath the buttons:
 Add to Address Book—This link adds the sender of the message to your Address
Book. If there is more than one “sending” address associated with a message,
Web Mail will let you choose which addresses you want to add. You can even add
the domain they came from, such as “earthlink.net”. Note: If you add a domain,
all email from that domain will be put in your Inbox, even if you have Suspect
Email Blocking turned on and the sender isn’t in your Address Book.
 Flag Message—This link puts a little flag icon next to the message in your message
list, so you can spot it in the list when you want to come back to it.
 Mark Unread—When you open a message, Web Mail marks it “read” in your
message list. If you accidentally opened the message and want it to look unread in
the list, click this link.
 This is spam—Web Mail lets you report messages that slip through spamBlocker’s
net. This link opens a page where you can report the message as spam, and block
its sender’s address.
Viewing Attachments

A paper-clip icon to the left of a message on its information line means that the email
message has an attachment.

To view an attachment:

1. Open the message in question.


2. Near the top of the message, beneath the Date, you will see a section for
Attachments.
3. Click the links to open the attachments. You can save the attachments to your
computer just as you would save any other file from a Web page. In Internet
Explorer, for example, you can right-click the link to the attachment and choose
Save Target As.

42 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Deleting a Message
NOTES
To delete a message, check the box next to its information line in the message list,
then click the Delete button.

Moving a Message

To move a message or group of messages to another folder:

1. Check the boxes next to each message to be moved.


2. Select the destination folder from the Move to Folder pull-down menu at the top
of the page.
3. Click the Move button.

Replying to a Message

To reply to an email message

1. To reply to the sender of the email message only, press the Reply button. To reply
to the sender of an email message, and to all other recipients, select the email
message and press the Reply All button.
2. Type your reply at the top of the page as you normally would and click Send.

When you reply to an email message, the message you’re replying to is normally
quoted at the bottom of your reply. If you don’t want to include originals when you reply,
you can turn off this function in the Web Mail Options.

To change the option to include the original email message in your reply
1. Click Preferences on the Web Mail default Web page.
2. Under Miscellaneous, click Web Mail Options.
3. Under Include Original When Replying, choose your preference.
4. Click Save.

Forwarding an Email Message

To forward an email message

1. Open the email message to be forwarded—select it in the email folder message


list, or open it a new window—then click Forward. The Forwarding window will
appear.
2. Type the email address you want to forward the message to in the Forward To
box and then type any additional message you want to include above the forwarded
text.
3. Click Send.
Printing An Email Message

To print an email, open it and click the Printable View link.

43 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Usenet
NOTES
Usenet (a contraction of user network) is a global, decentralized, distributed Internet
discussion system that evolved from a general purpose UUCP architecture of the same
name. It was conceived by Duke University graduate students Tom Truscott and Jim Ellis
in 1979. Users read and post public messages (called articles or posts, and collectively
termed news) to one or more categories, known as newsgroups. Usenet resembles bulletin
board systems (BBS) in most respects, and is the precursor to the various web forums
which are widely used today. Discussions are threaded, with modern news reader software,
as with web forums and BBSes, though posts are stored on the server sequentially.

One notable difference from a BBS or web forum is that there is no central server, nor
central system owner. Usenet is distributed among a large, constantly changing conglomeration
of servers which store and forward messages to one another. These servers are loosely
connected in a variable mesh.Individual users usually read from and post messages to a
local server operated by their ISP, university or employer. The servers then exchange the
messages between one another, so that they are available to readers beyond the original
server.

Usenet is one of the oldest computer network communications systems still in


widespread use. It was established in 1980, following experiments from the previous year,
over a decade before the World Wide Web was introduced and the general public got
access to the Internet. It was originally conceived as a “poor man’s ARPANET,” employing
UUCP to offer mail and file transfers, as well as announcements through the newly developed
news software. This system, developed at University of North Carolina at Chapel Hill and
Duke University, was called USENET to emphasize its creators’ hope that the USENIX
organization would take an active role in its operation (Daniel et al, 1980).

The articles that users post to Usenet are organized into topical categories called
newsgroups, which are themselves logically organized into hierarchies of subjects. For
instance, sci.math and sci.physics are within the sci hierarchy, for science. When a user
subscribes to a newsgroup, the news client software keeps track of which articles that user
has read.

In most newsgroups, the majority of the articles are responses to some other article.
The set of articles which can be traced to one single non-reply article is called a thread.
Most modern newsreaders display the articles arranged into threads and subthreads, making
it easy to follow a single discussion in a high-volume newsgroup.

When a user posts an article, it is initially only available on that user’s news server.
Each news server, however, talks to one or more other servers (its “newsfeeds”) and
exchanges articles with them. In this fashion, the article is copied from server to server and
(if all goes well) eventually reaches every server in the network. The later peer-to-peer

44 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

networks operate on a similar principle; but for Usenet it is normally the sender, rather than
the receiver, who initiates transfers. Some have noted that this seems an inefficient protocol NOTES
in the era of abundant high-speed network access. Usenet was designed for a time when
networks were much slower, and not always available. Many sites on the original Usenet
network would connect only once or twice a day to batch-transfer messages in and out.

Usenet has significant cultural importance in the networked world, having given rise
to, or popularized, many widely recognized concepts and terms such as “FAQ” and “spam.”

Today, almost all Usenet traffic is carried over the Internet. The current format and
transmission of Usenet articles is very similar to that of Internet email messages. However,
Usenet articles are posted for general consumption; any Usenet user has access to all
newsgroups, unlike email, which requires a list of known recipients.

Today, Usenet has diminished in importance with respect to Internet forums, blogs
and mailing lists. The difference, though, is that Usenet requires no personal registration
with the group concerned, that information need not be stored on a remote server, that
archives are always available, and that reading the messages requires not a mail or web
client, but a news client (included in many modern e-mail clients).

1.12 WEB SECURITY

You are offering your IP address to the entire world at this very moment.

Make sure you are not offering access to your private data at the same time.

YOUR IP ADDRESS IS PUBLIC

Accessing the Internet is a security risk.

When you are connected to the Internet, an IP address is used to identify your PC.
If you don’t protect yourself, this IP address can be used to access your computer from
the outside world.

A fixed IP address is a larger security risk.

If you’re using a modem with a dial-up connection, you will get a new IP address
every time you connect to Internet, but if you have a fixed Internet connection (cable,
ADSL, fixed line), your IP address will never change.

If you have a fixed IP address, you give potential Internet crackers all the time they
need to search for entrances to your computer, and to store and share (with other crackers)
information they might find about your unprotected private data.

45 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Your Network Shares


NOTES
Personal computers are often connected to a shared network. Personal computers in
large companies are connected to large corporate networks. Personal computers in small
companies are connected to a small local network, and computers in private homes often
share a network between family members.

Most often networks are used to share resources like printers, files and disk storage.

When you are connected to the Internet, your shared resources can be accessed by
the rest of the world.

A Common Windows Security Problem

Unfortunately, many Microsoft Windows users are unaware of a common security


leak in their network settings.

This is a common setup for network computers in Microsoft Windows


 Client for Microsoft Networks
 File and Printer Sharing for Microsoft Networks
 NetBEUI Protocol
 Internet Protocol TCP/IP

If your setup allows NetBIOS over TCP/IP, you have a security problem

 Your files can be shared all over the Internet


 Your logon-name, computer-name, and workgroup-name are visible to others.

If your setup allows File and Printer Sharing over TCP/IP, you also have a problem:

 Your files can be shared all over the Internet

Computers that are not connected to any network can also have dangerous network
settings because the network settings were changed when Internet was installed.

Solving the Problem

For Windows 2000 users:

You can solve your security problem by disabling NetBIOS over TCP/IP
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Right-click on Local Area Network
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties

46 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 Click on Advanced
 Select the WINS tab NOTES
 Select Disable NetBIOS over TCP/IP
 Click OK

If you get the message: “This connection has an empty......”, ignore the message and
click on YES to continue, and click OK to close the other setup windows.

You should restart your computer after the changes.

For Windows 95, 98, or ME users:

You can solve your security problem by disabling NetBIOS over TCP/IP:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties
 Select the NetBIOS tab
 Uncheck: Enable NetBIOS over TCP/IP
 Click OK

You must also disable the TCP/IP Bindings to Client for Microsoft Networks and
File and Printer Sharing:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties
 Select the Bindings tab
 Uncheck: Client for Microsoft Networks
 Uncheck: File and Printer Sharing
 Click OK

If you get a message with something like: “You must select a driver.........”, ignore the
message and click on YES to continue, and click OK to close the other setup windows.

If you still want to share your Files and Printer over the network, you must use the
NetBEUI protocol instead of the TCP/IP protocol. Make sure you have enabled it for
your local network:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Select: NetBEUI
 Click on Properties

47 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
 Select the Bindings tab
NOTES  Check: Client for Microsoft Networks
 Check: File and Printer Sharing
 Click OK

You should restart your computer after the changes.

Protect Your Server

iisPROTECT provides a complete range of password protection, authentication and


user management solutions:

iisPROTECTasp: Protect areas of your web site and require username and password.
Grant/deny any users/groups on a per resource basis. Extensive Web Interface for user/
group admin, use any DB backend, store custom data, set user start/end dates, email
users, audit logins.

iisPROTECT: Protect all web site files including images, databases,html,ASP etc.
Protect entire directories, users / groups independent from Windows accounts, complete
web administration, does not require cookies or any programming. Complete turn key
solution.

iisPROTECTquota: All of the features of iisPROTECT plus: prevent concurrent logins


and password cracking attempts, set quotas on hits, logins, kb per user.

Summary

In this unit, the basic concepts of internet, how to connect your computer to the
internet, domain name systems have been discussed. The concepts of SMTP, Mobile Instance
Messaging, IRC, e-mail concepts have also been discussed. The importance of security in
web is also highlighted.

Question
1. List out the services provided by interned.
2. Explain the steps involved in the process of connecting a computer with internet.
3. Explain the precess of sending and viewing an attachment.
4. What are the steps involved in sending an email.

5. What is SSL?

48 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
UNIT II

WWW

2.1 WORLD WIDE WEB

The World Wide Web (commonly shortened to the Web) is a system of interlinked,
hypertext documents accessed via the Internet. With a web browser, a user views web
page that may contain text, images, videos, and other multimedia and navigates between
them uses hyperlinks. The World Wide Web was created in 1989 by Sir Tim Berners-Lee,
working at CERN in Geneva, Switzerland. Since then, Berners-Lee has played an active
role in guiding the development of web standards (such as the markup languages in which
web pages are composed), and in recent years has advocated his vision of a Semantic
Web. Robert Cailliau, also at CERN, was an early evangelist for the project.

How the Web works

Viewing a web page on the World Wide Web normally begins either by typing the
URL of the page into a web browser, or by following a hyperlink to that page or resource.
The web browser then initiates a series of communication messages, behind the scenes, in
order to fetch and display it.

First, the server-name portion of the URL is resolved into an IP address using the
global, distributed Internet database known as the domain name system, or DNS. This IP
address is necessary to contact and send data packets to the web server.

The browser then requests the resource by sending an HTTP request to the web
server at that particular address. In the case of a typical web page, the HTML text of the
page is requested first and parsed immediately by the web browser, which will then make
additional requests for images and any other files that form a part of the page. Statistics
measuring a website’s popularity are usually based on the number of ‘page views’ or
associated server ‘hits’, or file requests, which take place.

49 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Having received the required files from the web server, the browser then renders the
NOTES page onto the screen as specified by its HTML, CSS, and other web languages. Any
images and other resources are incorporated to produce the on-screen web page that the
user sees.

Most web pages will themselves contain hyperlinks to other related pages and rhaps
to downloads, source documents, definitions and other web resources. Such a collection
of useful, related resources, interconnected via hypertext links, is what was dubbed a
“web” of information. Making it available on the Internet created what Tim Berners-Lee
first called the Worldwide Web (a term written in Camel Case, subsequently discarded) in
1990.

History

The underlying ideas of the Web can be traced as far back as 1980, when, at CERN
in Switzerland, Tim Berners-Lee built ENQUIRE (referring to Enquire within upon
everything, a book he recalled from his youth). While it was rather different from the system
in use today, it contained many of the same core ideas (and even some of the ideas of
Berners-Lee’s next project after the World Wide Web, the Semantic Web).

In March 1989, Tim Berners-Lee wrote a proposal, which referenced ENQUIRES


and described a more elaborate information management system. With help from Robert
Cailliau, he published a more formal proposal for the World Wide Web on November 12,
1990.The role model was provided by EBT’s (Electronic Book Technology, a spin-off
from the Institute for Research in Information and Scholarship at Brown University) Dynatext
SGML reader that CERN had licensed. The Dynatext system was considered, however
technically advanced (a key player in the extension of SGML ISO 8879:1986 to Hypermedia
within HyTime), too expensive and with an inappropriate licensing policy for general HEP
(High Energy Physics) community use: a fee for each document and each time a document
was charged.

A NeXTcube was used by Berners-Lee as the world’s first web server and also to
write the first web browser, Worldwide Web, in 1990. By Christmas 1990, Berners-Lee
had built all the tools necessary for a working Web: the first web browser (which was a
web editor as well), the first web server, and the first web pages which described the
project itself.

On August 6, 1991, he posted a short summary of the World Wide Web project on
the alt.hypertext newsgroup. This date also marked the debut of the Web as a publicly
available service on the Internet.

The crucial underlying concept of hypertext originated with older projects from the
1960s, such as the Hypertext Editing System (HES) at Brown University— among others
Ted Nelson and Andries van Dam— Ted Nelson’s Project Xanadu and Douglas Engelbart’s

50 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

oN-Line System (NLS). Both Nelson and Engelbart were in turn inspired by Vannevar
Bush’s microfilm-based “memex,” which was described in the 1945 essay “As We May NOTES
Think.”

Berners-Lee’s breakthrough was to marry hypertext to the Internet. In his book


weaving the Web, he explains that he had repeatedly suggested that a marriage between
the two technologies was possible to members of both technical communities, but when no
one took up his invitation, he finally tackled the project himself. In the process, he developed
a system of globally unique identifiers for resources on the Web and elsewhere: the Uniform
Resource Identifier.

The World Wide Web had a number of differences from other hypertext systems that
were then available. The Web required only unidirectional links rather than bidirectional
ones. This made it possible for someone to link to another resource without action by the
owner of that resource. It also significantly reduced the difficulty of implementing web
servers and browsers (in comparison to earlier systems), but in turn presented the chronic
problem of link rot. Unlike predecessors such as HyperCard, the World Wide Web was
non-proprietary, making it possible to develop servers and clients independently and to
add extensions without licensing restrictions.

On April 30, 1993, CERN announced that the World Wide Web would be free to
anyone, with no fees due. Coming two months after the announcement that the Gopher
protocol was no longer free to use, this produced a rapid shift away from Gopher and
towards the Web. An early popular web browser was ViolaWWW, which was based
upon HyperCard.

Scholars generally agree, however, that the turning point for the World Wide Web
began with the introduction[8] of the Mosaic web browser in 1993, a graphical browser
developed by a team at the National Center for Supercomputing Applications at the
University of Illinois at Urbana-Champaign (NCSA-UIUC), led by Marc Andreessen.
Funding for Mosaic came from the High-Performance Computing and Communications
Initiative, a funding program initiated by then-Senator Al Gore’s High Performance
Computing and Communication Act of 1991, also known as the Gore Bill. (See Al Gore’s
contributions to the Internet and technology for more information.) Prior to the release of
Mosaic, graphics were not commonly mixed with text in web pages, and its popularity was
less than older protocols in use over the Internet, such as Gopher and Wide Area Information
Servers (WAIS). Mosaic’s graphical user interface allowed the Web to become, by far,
the most popular Internet protocol.

istory in Literature

The concept of a home-based global information system goes back at least as far as
Isaac Asimov’s short story “Anniversary” (Amazing Stories, March 1959), in which the

51 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

characters look up information on a home computer called a “Multivac outlet” — which


NOTES was connected by a “planet wide network of circuits” to a mile-long “super-computer”
somewhere in the bowels of the Earth. One character is thinking of installing a Multivac, Jr.
Model for his kids.

The story was set in the far distant future when commercial space travel was
commonplace, and yet the machine “prints the answer on a slip of tape” that comes out a
slot — there is no video display — and the owner of the home computer says that he
doesn’t spend the kind of money to get a Multivac outlet that talks.

Standards

Many formal standards and other technical specifications define the operation of
different aspects of the World Wide Web, the Internet, and computer information exchange.
Many of the documents are the work of the World Wide Web Consortium (W3C), headed
by Berners-Lee, but some are produced by the Internet Engineering Task Force (IETF)
and other organizations.

Usually, when web standards are discussed, the following publications are seen as
foundational:

 Recommendations for markup languages, especially HTML and XHTML, from


the W3C. These define the structure and interpretation of hypertext documents.
 Recommendations for style sheets, especially CSS, from the W3C.
 Standards for ECMAScript, a.k.a. JavaScript, from Ecma International.
 Recommendations for the Document Object Model, from W3C.

Additional publications provide definitions of other essential technologies for the World
Wide Web, including, but not limited to, the following:

Uniform Resource Identifier (URI), which is a universal system for referencing resources
on the Internet, such as hypertext documents and images. URIs, often called URLs, are
defined by the IETF’s RFC 3986 / STD 66: Uniform Resource Identifier (URI): Generic
Syntax, as well as its predecessors and numerous URI scheme-defining RFCs;

Hypertext Transfer Protocol (HTTP), especially as defined by RFC 2616: HTTP/1.1


and RFC 2617: HTTP Authentication, which specify how the browser and server
communicate with each other.

Java and JavaScript

A significant advance in Web technology was Sun Microsystems’ Java platform. It


enables web pages to embed small programs (called applets) directly into the view. These
applets run on the end-user’s computer, providing a richer user interface than simple web
pages. Java client-side applets never gained the popularity that Sun had hoped for a variety

52 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

of reasons, including lack of integration with other content (applets were confined to small
boxes within the rendered page) and the fact that many computers at the time were supplied NOTES
to end users without a suitably installed Java Virtual Machine, and so required a download
by the user before applets would appear. Adobe Flash now performs many of the functions
that were originally envisioned for Java applets, including the playing of video content,
animation, and some rich UI features. Java itself has become more widely used as a platform
and language for server-side and other programming.

JavaScript, on the other hand, is a scripting language that was initially developed for
use within web pages. The standardized version is ECMAScript. While its name is similar
to Java, JavaScript was developed by Netscape and it has almost nothing to do with Java,
although, like Java, its syntax is derived from the C programming language. In conjunction
with a web page’s Document Object Model, JavaScript has become a much more powerful
technology than its creators originally envisioned. The manipulation of a page’s Document
Object Model after the page is delivered to the client has been called Dynamic HTML
(DHTML), to emphasize a shift away from static HTML displays.

In simple cases, all the optional information and actions available on a JavaScript-
enhanced web page will have been downloaded when the page was first delivered. Ajax
(“Asynchronous JavaScript and XML”) is a JavaScript-based technology that provides a
method whereby parts within a web page may be updated, using new information obtained
over the network at a later time in response to user actions. This allows the page to be
more responsive, interactive and interesting, without the user having to wait for whole-
page reloads. Ajax is seen as an important aspect of what is being called Web 2.0. Examples
of Ajax techniques currently in use can be seen in Gmail, Google Maps, and other dynamic
web applications.

Publishing web pages

Web page production is available to individuals outside the mass media. In order to
publish a web page, one does not have to go through a publisher or other media institution,
and potential readers could be found in all corners of the globe.

Many different kinds of information are available on the Web, and for those who wish
to know other societies, cultures, and peoples, it has become easier.

The increased opportunity to publish materials is observable in the countless personal


and social networking pages, as well as sites by families, small shops, etc., facilitated by
the emergence of free web hosting services.

Statistics

According to a 2001 study, there were more than 550 billion documents on the Web,
mostly in the “invisible web”, or deep web.[11] A 2002 survey of 2,024 million web

53 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

pages[12] determined that by far the most web content was in English: 56.4%; next were
NOTES pages in German (7.7%), French (5.6%), and Japanese (4.9%). A more recent study,
which used web searches in 75 different languages to sample the Web, determined that
there were over 11.5 billion web pages in the publicly index able web as of the end of
January 2005.

Speed issues

Frustration over congestion issues in the Internet infrastructure and the high latency
that results in slow browsing has led to an alternative, pejorative name for the World Wide
Web: the World Wide Wait. Speeding up the Internet is an ongoing discussion over the use
of peering and QoS technologies. Other solutions to reduce the World Wide Wait can be
found on W3C.

Standard guidelines for ideal web response times are:


 0.1 second (one tenth of a second). Ideal response time. The user doesn’t sense
any interruption.
 1 second. Highest acceptable response time. Download times above 1 second
interrupt the user experience.
 10 seconds. Unacceptable response time. The user experience is interrupted and
the user is likely to leave the site or system.
These numbers are useful for planning server capacity.

Caching

If a user revisits a web page after only a short interval, the page data may not need to
be re-obtained from the source web server. Almost all web browsers cache recently-
obtained data, usually on the local hard drive. HTTP requests sent by a browser will usually
only ask for data that has changed since the last download. If the locally-cached data is still
current, it will be reused.

Caching helps reduce the amount of web traffic on the Internet. The decision about
expiration is made independently for each downloaded file, whether image, style sheet,
JavaScript, HTML, or whatever other content the site may provide. Thus even on sites
with highly dynamic content, many of the basic resources only need to be refreshed
occasionally. Web site designers find it worthwhile to collate resources such as CSS data
and JavaScript into a few site-wide files so that they can be cached efficiently. This helps
reduce page download times and lowers demands on the web server.

There are other components of the Internet that can cache web content. Corporate
and academic firewalls often cache web resources requested by one user for the benefit of
all. (See also caching proxy server.) Some search engines, such as Google or Yahoo!, also
store cached content from web sites.

54 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Apart from the facilities built into web servers that can determine when files have
been updated and so need to be re-sent, designers of dynamically-generated web pages NOTES
can control the HTTP headers sent back to requesting users, so that transient or sensitive
pages are not cached. Internet banking and news sites frequently use this facility.

Data requested with an HTTP ‘GET’ is likely to be cached if other conditions are
met; data obtained in response to a ‘POST’ is assumed to depend on the data that was
posted and so is not cached.

Link rot and web archival

Over time, many web resources pointed to by hyperlinks disappear, relocate, or are
replaced with different content. This phenomenon is referred to in some circles as “link
rot” and the hyperlinks affected by it are often called “dead links”.

The ephemeral nature of the Web has prompted many efforts to archive web sites.
The Internet Archive is one of the most well-known efforts; it has been active since 1996.

Academic conferences

The major academic event covering the Web is the World Wide Web series of
conferences, promoted by IW3C2.

WWW prefix in web addresses

The letters “www” are commonly found at the beginning of web addresses because
of the long-standing practice of naming Internet hosts (servers) according to the services
they provide. So for example, the host name for a web server is often “www”; for an FTP
server, “ftp”; and for a USENET news server, “news” or “nntp” (after the news protocol
NNTP). These host names appear as DNS sub domain names, as in “www.example.com”.

This use of such prefixes is not required by any technical standard; indeed, the first
web server was at “nxoc01.cern.ch”, and even today many web sites exist without a
“www” prefix. The “www” prefix has no meaning in the way the main web site is shown.
The “www” prefix is simply one choice for a web site’s sub domain name.

Some web browsers will automatically try adding “www.” to the beginning, and
possibly “.com” to the end, of typed URLs if no host is found without them. Internet
Explorer, Mozilla Firefox, Safari, and Opera will also prefix “http: //www.” and append
“.com” to the address bar contents if the Control and Enter keys are pressed simultaneously.
For example, entering “example” in the address bar and then pressing just Enter or
Control+Enter will usually resolve to “http://www.example.com”, depending on the exact
browser version and its settings.

55 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Web Security
NOTES
You are offering your IP address to the entire world at this very moment. Make sure
you are not offering access to your private data at the same time.

Your IP Address is Public


Accessing the Internet is a security risk. When you are connected to the Internet, an
IP address is used to identify your PC. If you don’t protect yourself, this IP address can be
used to access your computer from the outside world. A fixed IP address is a larger security
risk.
If you’re using a modem with a dial-up connection, you will get a new IP address
every time you connect to Internet, but if you have a fixed Internet connection (cable,
ADSL, fixed line); your IP address will never change.
If you have a fixed IP address, you give potential Internet crackers all the time they
need to search for entrances to your computer, and to store and share (with other crackers)
information they might find about your unprotected private data.

You’re Network Shares


Personal computers are often connected to a shared network. Personal computers in
large companies are connected to large corporate networks. Personal computers in small
companies are connected to a small local network, and computers in private homes often
share a network between family members.
Most often networks are used to share resources like printers, files and disk storage.
When you are connected to the Internet, your shared resources can be accessed by
the rest of the world.
A Common Windows Security Problem
Unfortunately, many Microsoft Windows users are unaware of a common security
leak in their network settings.

This is a common setup for network computers in Microsoft Windows:

Client for Microsoft Networks


File and Printer Sharing for Microsoft Networks
NetBEUI Protocol
Internet Protocol TCP/IP

If your setup allows NetBIOS over TCP/IP, you have a security problem:

Your files can be shared all over the Internet


Your logon-name, computer-name, and workgroup-name are visible to others.

56 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

If your setup allows File and Printer Sharing over TCP/IP, you also have a problem:
NOTES
Your files can be shared all over the Internet

Computers that are not connected to any network can also have dangerous network
settings because the network settings were changed when Internet was installed.

Solving the Problem

For Windows 2000 users

You can solve your security problem by disabling NetBIOS over TCP/IP:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Right-click on Local Area Network
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties
 Click on Advanced
 Select the WINS tab
 Select Disable NetBIOS over TCP/IP
 Click OK
If you get the message: “This connection has an empty......” ignore the message and
click on YES to continue, and click OK to close the other setup windows.

You should restart your computer after the changes.

For Windows 95, 98, or ME users:

You can solve your security problem by disabling NetBIOS over TCP/IP:

 Open Windows Explorer


 Right-click on My Network Places
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties
 Select the NetBIOS tab
 Uncheck: Enable NetBIOS over TCP/IP
 Click OK

57 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

You must also disable the TCP/IP Bindings to Client for Microsoft Networks and File
NOTES and Printer Sharing:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Select: Internet Protocol TCP/IP
 Click on Properties
 Select the Bindings tab
 Uncheck: Client for Microsoft Networks
 Uncheck: File and Printer Sharing
 Click OK

If you get a message with something like: “You must select a driver.........”
ignore the message and click on YES to continue, and click OK to close the other
setup windows.

If you still want to share your Files and Printer over the network, you must use the
NetBEUI protocol instead of the TCP/IP protocol. Make sure you have enabled it for
your local network:
 Open Windows Explorer
 Right-click on My Network Places
 Select: Properties
 Select: NetBEUI
 Click on Properties
 Select the Bindings tab
 Check: Client for Microsoft Networks
 Check: File and Printer Sharing
 Click OK
 You should restart your computer after the changes.
Protect Your Server

iisPROTECT provides a complete range of password protection, authentication and


user management solutions:

iisPROTECTasp: Protect areas of your web site and require username and password.
Grant/deny any users/groups on a per resource basis. Extensive Web Interface for user/
group admin, use any DB backend, store custom data, set user start/end dates, email
users, audit logins.

iisPROTECT: Protect all web site files including images, databases,html,ASP etc.
Protect entire directories, users / groups independent from Windows accounts, complete
web administration, does not require cookies or any programming. Complete turn key
solution.

58 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

iisPROTECTquota: All of the features of iisPROTECT plus: prevent concurrent logins


and password cracking attempts, set quotas on hits, logins, kb per user. NOTES
2.2 CREATING A WEBSITE

Creating a website. Sounds complicated, doesn’t it? It isn’t. You don’t need to be
a techie or learn any type of complicated code either. If you are creating a website for the
first time, bookmark this site. It will be really useful to you. I tell you everything you need
to know and walk you through each step of the process - free!

There are three main steps to creating a web site:


1. Get a Domain Name - this page has everything you need to know about domain
names - what they are, why you need one, how to choose a good one and how to
get one.
2. Choose a Web Host - this page explains what a web host is, how to choose a
good one, and what you really need and what you don’t so you don’t get taken.
3. Creating a Web Site - this page explains how you actually build a website, what
you should and should not put on it and helps you figure out which website builder
is for you.

Creating a website is just the first step to having a successful website. I will also show
you add-ons you can put on your website like newsletters and counters for free or cheap,
ways you can make money from your website and how to get traffic to it.

Why I Created This Website

I started this website for my friends and family because I was tired of seeing them get
ripped off when they were creating a web site. They either paid way too much (and
thought it was a good deal!) or got suckered into buying stuff they didn’t need. This was
started for them, but I hope you find it helpful too.

Domain Name Information

The first thing you need to do when creating a website is to get your own domain
name. Don’t know what that is, why you need it or how to get one? This page contains all
of that domain name information and more.

 What is a Domain Name?


 How to Choose a Good Domain Name
 The Best Place to Get a Domain Name
 How to Register a Domain Name

59 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

What is a Domain Name?


NOTES
A domain name is your address on the web. It’s the information that customers type
in to find your website. It’s what comes after the www. in www.domain.com. The domain
name for this site is 4creatingawebsite.com.

A domain name can be any combination of letters and/or numbers up to 63 characters.


The official domain name extensions are .com, .net, .org, .biz, .info, .us and .ws. There are
also other extensions like .tv and .tk but don’t waste your time with those. Stick with a
recognized one.

How to Choose a Good Domain Name

Here is some information and tips to help you select the best domain name for you:

Keep it short. The shorter, the better. It’s best to keep your domain name less than
15 characters if possible. A shorter name is easier for people to remember and it also
reduces the likelihood that your visitor will mistype it or spell it wrong. For example,
BuyIt.com has a lot less chance of being mistyped than isellthispleasebuyitfrommetoday.com.

Keep it sweet. The more memorable the name, the easier it is for people to remember.
You can have the best site in the world, but if people can’t remember your domain name it
will cut down on your return visitors. It’s hard enough to get traffic on the web without
“losing” visitors that want to come back but can’t find you.

Keep it simple. Make sure the domain name is easy for people to spell. Don’t spell
words unusually. Unless you have incredible branding, people will type in the common
spelling of your domain and your competition will get visitors who were looking for you.
For example, BlueRight.com is much better than BluRite.com

Choose a domain name that reflects your business. Don’t just choose a random
name. For example, if your sell horse supplies, you should choose a name that has the
word “horse supplies” in it such as YourHorseSupplies.com. Or, your domain name should
refer to horses in some way, like whoa.com. The former will help you in the search engines.
The latter will be memorable.

When in doubt, choose a .com. If you are debating between several different domain
names, go with the .com. It’s what most people think of when they are typing in a domain
name. It’s true the best names are gone, but you can still find a good .com if you look.
Thousands of .coms expire and are released back in the market each month so there is
always a steady supply of new domains coming into the market.

Special tip: If you can afford it, you may also want to get your name followed by
.com along with whatever domain name you decide to get. I think the way the internet is
going it is smart to do and you will be very glad you have it later on.

60 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Best Place to Get a Domain Name


NOTES
Some people try to save money and get a freebie website and domain name. I’m all
for free whenever possible, but free websites and domain names just aren’t worth it. Click
here for information on why it’s better to pay.

I get my domain names from Act Now Domains. They are pretty cheap and they are
really reliable. I have over 200 domain names there.

I’ve tried a couple of other places but never could get help when I needed it with
those places. Act Now Domains has people available 24 hours a day by phone and email.
Plus, it’s just really easy to use.

Here’s a price comparison so you can see how their prices compare to others:

Registrar Price

Act Now Domains $12.75

TheComRegistry $19.95

Network Solutions $35.00

Register.com $35.00

Update: All domain registrars are now required to charge $0.20 for every domain
registration, transfer and renewal. This is something the organization that controls domain
registrations is making them do. This fee is added when you checkout so don’t be surprised
if you see it. The registrar isn’t trying to scam you or make you pay extra. It’s a fee
everyone has to pay and the registrars have to charge it.

How to Get a Domain Name

All you need to do to get a domain name is go to the registrar you want to use
(Domain name companies like Act Now Domains are called registrars.) They will have a
search box. Just type the domain name you want in the search box and it will tell you if the
domain is available.

If it is, just follow the steps and pay for it. If it isn’t, search for another name. You can
use their search tool to find variations of the domain name. It may take you a bit of time to
find the one you want, but keep looking you will.

Okay, now that you know the domain name information and know how to get a
domain name, let’s go to

61 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Choose a Web Host


NOTES
Once you have your domain name, you will need to choose a web host. It is very
important that you choose a good web host. Good doesn’t mean expensive. Good means
reliable with great support.

Some people try to save money and choose to go with a free web host. They usually
realize pretty quickly what a huge mistake that is. As I noted in the domain section, I’m all
for free whenever possible, but free web hosting will end up costing much more than you
save. Click here for information on why it’s better to pay.

This page contains the following information on how to choose a web host:
 What is a Web Host?
 How to Choose a Good Web Host
 Web Host Do’s and Don’ts
 Recommended Web Hosts

What is a Web Host?

A web host stores the web site you create on their servers and transmits it to the
internet so that when someone types in your domain name, your web site appears. A
server is just a fancy computer that “serves up” your web site to the internet.

How to Choose a Good Web Host

Again, I can’t stress how important it is that you choose a good web host. But, let’s
face it - how do you know if a web host is good or not? They all look pretty much the
same and they seem like they know what they are doing, right? Well, there is one thing you
can check on before you sign up and luckily it’s one of the most important things.

When you choose a web host, make sure they have excellent support. If you have a
problem with your web site you want help now, not in a day or two.

Personally, I refuse to choose a web host that I can’t get on the phone in an emergency.
Some web hosts are sneaky and they put a phone number on their web site, but it’s just for
show. They don’t always answer or they advertise 24/7 support but are actually only
around Monday-Friday. When I first started out, my site went down on a Friday afternoon
and I called ALL weekend and all I got was hold music. They put me on hold, but no one
was there! Needless to say that was it for that web host.

So, how can you tell how responsive the web host you are considering actually is?
You test them. If they have a phone number (and they should) call them late at night or on
weekends to see if they are really there.

62 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

You can also send an e-mail to their support e-mail address (not their sales e-mail)
and ask them a basic question like “How many e-mail addresses am I allowed?” or “Is NOTES
there a limit on the number of autoresponders I’m allowed?” and see how long it takes
them to get back to you. For a non-emergency email like that I think up to 12 hours is
acceptable. If you send the e-mail on Saturday and they don’t get back to you until
Monday, that isn’t good. If they take more than 12 hours and either don’t have phone
support or aren’t answering, choose another web host.

Web Host Do’s and Don’ts

Once you choose a web host, then you will have to decide what plan you want.
Most web hosts offer you several different plans and obviously want you to buy the more
expensive one.
1. Do get the least expensive plan. Most people buy the larger plans because they
aren’t sure how much space they need and the space in the smaller plans just
doesn’t sound like much. 10 mb is plenty for most first web sites. If you want to
be sure, then choose a plan around 50 mb. 50 mb is a HUGE amount of space.
If you get that you will be fine.
2. Don’t get caught up in what the various hosts offer like PHP, MySQL and a bunch
of other stuff like that. If you don’t know what those things are you don’t need
them.

Web Hosts I Like

As I mentioned earlier it doesn’t matter to me which company you go with. The


purpose of this site is to give you the tools you need to make the best decision for you.
Since so many people have asked me, the web hosts I recommend are:

Act Now Domains - they have incredible support and if you need a web site builder
I think their web site builder is really sharp and easy to use. They have a variety of hosting
plans and web builder plans available starting at $8.95/month. The price for the web
builder includes hosting and email. They have separate hosting accounts for people that
don’t require a web builder. If you want to sell products on your site get their online store
builder called Quick Shopping Cart.

Pro Website Group - this is also a favorite web host of mine. Each hosting account
comes with a really easy web site builder that makes great looking sites. You get lots of
free goodies with each account including a free domain name. The cost for an account is
only $6.95/month and they give you a 30 day money back guarantee. I really like the fact
that their support is so good. They offer toll-free support and they guarantee that all email
support questions are answered in one hour or less. I’ve put them to the test a couple of
times at odd hours day and night and they’ve come through each time within the hour limit.
The only real negative of this web host is that it doesn’t allow for month to month billing.

63 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Sign up for a year is required but they are a good group and give you a 30 day money back
NOTES guarantee. They have offices in both the US and the UK.

PowerWeb - this is a great web host. It gives you everything plus the kitchen sink at
a really cheap price and it comes with a 30 day money back guarantee. If you need a web
site builder it has one. It also provides unlimited e-mails, more space than you will ever use
and if you plan on using your site for ecommerce they have a free shopping cart. $9.95/
month if you pay by the month or $7.95/month if you pay a year in advance. If you need
a web site builder it’s included in the price.

Now that you know how to choose a web host, let’s go on to

Build a Website

Once you have your domain name and web host, the next step is to actually build
your website.
 Website Builder Software
 Do’s and Don’ts of Building a Website
 How to Upload your Website to your Web Host Account
 How to Add Newsletters and Counters to your Website
Website Builder Software

To build a website yourself you need some type of web building software. There are
two ways you can get this software:

1. Free from your web host. Some web hosts provide a web site builder that includes
free hosting and email. This is the best choice for beginners. All you need to do is
click on the design you want and then add your text. It’s really easy. As I mentioned
earlier, I recommend the web site builders at Act Now Domains, Pro Website
Group and iPowerweb. They each make good looking sites are easy to use and
best of all are really cheap.

If you want to sell products on your site and accept credit cards, you will need to sign
up with an outside credit card processor for this. Make sure to check out my make money
page for more info about the credit card processors so you don’t end up buying a lot of
stuff you don’t need or going with a bad one.

Click here for a step by step build a website tutorial. It’s one of the new tutorials I
created as a bonus with my Start your Internet Business ebook. I thought it might be
helpful to people here to.
2. From an outside vendor. The most popular build a website software is Microsoft’s
Front Page. It is a great program and will offer you much more flexibility than the
website builder that you get from your web host, but it can take a while to learn.
Plus it usually costs between$150-200. If you want to get it, the cheapest place

64 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

I’ve found for it is Amazon. Click here to get Microsoft FrontPage. They have a
special on now so it’s less than $120. NOTES
If this is your first website, I suggest you just go with the build a website software that
comes with your web hosting account. It is a lot easier, doesn’t cost very much and once
you get the hang of how to build a website then you can branch out to Front Page later.
Plus, most Front Page sites created by beginners don’t look too good.

Do’s and Don’ts of Building a Website

Regardless of what software you use to build a website, there are some basic do’s
and don’ts you need to keep in mind. It’s very easy to get carried away on your first site
and do things that send visitors away which is most definitely not what you want to do.

When you build a website:


 Do keep your web site simple. Don’t cram it full of clutter. Unless it’s a how-to
site like this one, the less text, the better.
 Do make it eye pleasing. No crazy fonts or colors. The standard fonts for web
sites are Arial, Verdana and Times New Roman. Whatever font you decide on,
use it throughout your site. Don’t alternate fonts.
 Don’t use a Flash introduction. Web designers love this and if you use a website
builder they can be really fun to create, but most people click off as soon as they
see it loading. The object is to get people to see your web site, not drive them
away.
 Don’t put music or sound files on your site. It startles people when they are surfing
and suddenly hear music blaring out. Their immediate reaction is to back track
immediately.
 Don’t put a lot of slow-loading graphics on your page. No matter how pretty the
graphics are, people aren’t going to stick around and wait for them to load. You
can optimize your images for free at NetMechanic.com. What this means is that
you load the image onto Net Mechanic’s web page and they will condense the
size of the image for you, then let you pick the image you like from the results.
Usually you can reduce the size by 50% or more without there being a noticeable
change in quality.
 Do make sure visitors to your site can tell at a glance what your site is about. For
example, if you sell widgets, make sure that’s evident right up front. Most people
will leave in the first two seconds if they don’t think your site offers them what they
are looking for. They aren’t going to take the time to scroll around to see if you
have what they need. Make it immediately clear to them what your site is about.
 Don’t use pop-ups on your web site. Visitors find these really annoying. The less
you annoy a visitor, the more likely he or she is to stay.

65 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

How to Upload your Website to your Web Host Account


NOTES
Once you build your website, you will need to get it to your web host’s server. This
process is called “uploading” your files. Some places call it “publishing your website.”
Either way it’s the same process.

If you are using the build a website software from your web host, all of your website
files will already be on your web host’s servers so all you will need to do is click a button.
The button is usually Publish or Submit.

If you are using Front Page to build a website, you will need to make sure that Front
Page extensions have been enabled on your web host account. Some web hosts require
you to ask for these, but most hosts have them already set up for you.

Once that is done, you will click the Publish to Web button, then follow the instructions
listed. At one point it will ask for your destination. This is the name of your domain. For
example, http://www.4creatingawebsite.com When it asks for username and password,
give the name and password your web host gave you or that you created yourself when
you signed up for your web host account.

If you are using another software program to build a website, you will need FTP
software installed on your computer. This will allow you to get your website files from your
computer to your hosting account. I use ws_ftp LE because it’s easy to use and it’s free.

Click here to download ws_ftp LE

Once you have installed ws_ftp le on your computer, click here for their tutorial on
how to transfer your files using ws_ftp.

How to Add Newsletters and Counters to your Web Site

Once you build a website you may want to add a little pizazz to your site by adding
some special content such as a newsletter sign up and/or a hit counter. If you are using a
web host’s build a website software they will have these options for you.

If you are using Front Page or another build a website program then you will need to
add these yourself. The best places I’ve found are:

Newsletters: Constant Contact - this is a super place that has everything you need to
collect email addresses and send out your newsletters. You can create newsletters here
which look fantastic. It’s who I use.

How it works is that after you sign up they give you code for a Sign Up for our
Newsletter box, you just copy that code and paste it into your html and you are all set. If
you have less than 50 people on your newsletter list it’s free and from 51-250 it’s only $10/

66 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

month. The more people you get the price goes up a bit, but it’s still a good deal. They
also give you a 60 day free trial. NOTES
Hit Counters: if you are using Front Page, you can add your own hit counter wherever
you like when you build your web site. This tracks how many people visit your pages.
Simply click on Insert, then click on Web Component, click on Hit Counter and choose
the style you want. That’s all there is to it.

Web Site Creation Concepts


 The purpose of a web site is to support and enhance the goals of your business or
organization - it is not an end in itself. A web site should be much more than just an
on-line brochure, and a well designed web site integrates the following key concepts.
 Good content is the most important aspect of successful web site design. To support
your content, you need the right combination of technology and graphics. In that
sense web site design is both an art and a science. It has to be functional and work
correctly, but should also be appealing to the eye.
 Good navigation and usability is the basis for a good design. It should always be
obvious to your visitors how to get where they want to go on your web site, and it
should be impossible to get “lost”.
 What makes the Web such a powerful medium is that it is interactive, and it
provides instant access to an incredible depth of information. Good web site
design should take full advantage of these capabilities, providing your visitors
with easy access to the information, products or service you provide, while
making their whole experience stimulating and satisfying.

Our Process
We begin with a free initial consultation via e-mail, by phone, or in person if you are
located in the Seattle area. At this stage we identify the objectives of your business
or organization, and then review the products, services, information or resources
you have to offer.
We will also evaluate the need for your own domain name and your web site
hosting options. Based on this we will provide you with a scope document that
summarizes the objectives of your web site and outlines the content and navigational
structure, free of charge.
At this point we will review with you your existing forms of advertising and the
methods you use currently to promote your products, services, or information.
We will also evaluate the source for the content of your web site: product and
price lists, newsletters, manuals, articles, forms, policies, charts, and your company
or organization information.

67 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
In order to make your web site dynamic we will also consider time sensitive
NOTES information that you want to publicize: announcements, schedules, events, meetings,
sales, and special activities or campaigns. You may also want to schedule periodic
content updating for certain portions of your web site.
Based on the scope document, we can proceed to the actual design and construction
of your web site. We can use graphic material you provide to us and we can create
graphics and a logo for you.
Included in the creation of all our web sites at Merit Web Design is the appropriate
use of HTML Meta tags and effectively presented text, which will help your web
site get high listings in the search engines. This is critical to getting traffic to your
web site, and can make the difference between success or failure. Many of our
clients have achieved #1 positions in the major search engines through these and
other techniques.

 The last step is to upload the initial version of the web site to your Internet Service
Provider (ISP) or to our server through one of our Hosting Plus accounts. We will
thoroughly test it in order to validate all hyperlinks and e-mail addresses, to make
sure that all forms, buttons, Java scripts, etc. work properly, and to test browser
compatibility, screen width, and colors.
At this point you can review your web site and request any changes you would like
us to make. We want you to be happy with our work.

Design Challenges

The same web page can look very different through different browsers. Even different
versions of the same browser can change the appearance of a web page. We will view
your web site through the most popular versions of Microsoft’s Internet Explorer (including
AOL’s version) and Netscape’s Navigator to make sure the layout is acceptable in all of
them.

Monitors display colors differently. Another challenge for web designers is to create
graphics and text which look good under an incredibly wide range of monitor color capacities.
A typical scanned photograph produces an image file with over 16 million colors.
Unfortunately, most monitors today can display 64,000 colors, and many older monitors
display 256 colors or in some cases only 16 colors.

An image that looks fantastic with 64,000 colors on a new monitor can look terrible
through an older monitor that displays only 256 colors, because the older monitor “dithers”
the colors it cannot display. If you have ever seen Images on a web site covered with dots
or bands of colors, this is the cause. Six common factors contribute to dithering and image
degradation:

68 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 The image’s color palette was not sufficiently reduced


 The image was not correctly compressed into the “GIF” or “JPG” format NOTES
 Lack of memory in the viewer’s computer
 Use of certain browsers, especially AOL’s older versions
 The viewer has set their monitor resolution incorrectly (i.e. someone with 17"
monitor set at 640 x 480 resolution—they should set it for 1024 x 768)
 The viewer has set their monitor colors incorrectly (i.e. someone with a new monitor
set at 256 colors which should be set at 16 million colors)

2.3 HTML EDITORS - WEB PAGE AUTHORING TOOLS


Web authoring tools and HTML editors range from simple text editors to high powered
graphical authoring tools and content management systems. These links will help you choose
the best one for you and learn how to use it.
Visual HTML EditorWYSIWYG .html, source/design view IE/FF preview -
Download free trialwww.myeclipseide.com
VisualSite Designer 5.7No Coding - No Hassle - Just Fun! and a very Cool looking
Website...www.CoffeeCup.com
WYSIWYG XML EditorVisual editor for XML documents WYSIWYG editing
based on CSSwww.oxygenxml.com
Web Design Software: Which One is Right for You?
If you don’t know what HTML editor or Web page editor would best suit your
needs, this questionnaire will help you. Answer the questions to find the HTML editor that
does what you need it to do in the price range that best suits your budget.
Before You Buy an HTML Editor
When building a Web page, you might think that it really isn’t important what editor
you use. You can write HTML in MS Word or you can use the tools on your hosting
provider. Think about what you need before you just buy the most expensive or cheapest
editor.

Common Myths and Facts About Modern WYSIWYG Editors

WYSIWYG editors have been much maligned but are they still as bad as they ever
were? I look at the common knowledge about WYSIWYG editors and determine which
ones apply and which ones don’t.

WYSIWYG vs. Hand Coding, the Great Debate

If you’re putting up Web pages, you might be using a special editor, or just writing
your pages in straight HTML in a text editor. But whichever you do, you’re still using
HTML. From Jennifer Kyrnin, your HTML Guide.

69 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

WYSIWYG Isn’t Really - What You See May Not Be What Your Customers Get
NOTES
Just because you use a WYSIWYG editor doesn’t guarantee that your pages will
look the same to your customers as what you see when you view your page in your editor.
WYSIWYG is actually a misnomer when it comes to Web editing software.

Optimizing Web Graphics

Which Color Palette?

The secret of shrinking graphic file size is reduction of bit depth, resolution, and
dimension while preserving image quality. This classic size-versus-quality tradeoff is the
key to the art and science of graphics compression.

Bit Depth Reduction

For palette-based indexed formats, such as GIF and PNG, the lower the bit depth,
the smaller the file. Lossy, full color RGB formats like JFIF, do not benefit from bit depth
reduction as file size is primarily related to quality.
Bit Depth = Color Depth
Number of colors = 2^bit depth
Where bit depth = number of bits/pixel

Example: An image with a bit depth of 8 can have up to 256 colors (2^8=256)

(640 x 480 pixels x 24 bits per pixel) / 8 bits per byte = 921,600 bytes or

raw file size = (total pixels x bit depth) / 8 bits per byte

A full-screen 640x480 24-bit color image requires 900K of disk space, enough to
discourage even the most bandwidth-enabled surfer. Even though the majority of Web
users support 16-bit or higher color depths (http://www.thecounter.com/stats/2000/February/
colors.html), older Macs and Windows PCs can display only 256 of these colors
simultaneously. Users viewing your full-color masterpiece on these 8-bit machines will see
your subtle color gradations as dithering bandits. One way to avoid these problems is to
deliberately work in a 256-color palette. At 256 colors this same graphic becomes a
300K TIFF, a factor of 1/3 (8/24 = 1/3).

Color Palettes

The two ways to store color raster images are indexed and RGB. Indexed formats
are indexed, or mapped to a 256-color (or less) lookup table (CLUT) or color palette.
RGB formats, also known as true color, use 8 bits (0 to 255) for each Red, Green, and
Blue value to form a 24-bit pixel (8+8+8=24) palette which can hold one of 16.7 million
colors (2^24=16,777,216 colors). Some formats support even higher bit depths, useful
for medical imaging or smooth transparencies.

70 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

A CLUT is a digital version of a paint store’s color-mixing chart: it contains 256


entries, each with its own formula for a specific color. Indexed images refer to each color NOTES
by its position in the palette. Finding the right color palette that provides the best image
quality on 8-bit machines is an art form in itself, and can dramatically affect the appearance
and size of your graphics.

Compression

Compression algorithms are used to re-encode data into more compact


representations of the same information. In other words, fewer words are used to say the
same thing. For GIFs and PNGs, the more linear change there is, the less efficient the
compression.

With judicious reduction of colors and constant color areas a GIF can compress this
raw 300K image from 150K (256 colors dithered) to 90K using HVS Color (64-128
colors), a factor of 6:1 to 10:1. GIFs retain edge and sharpness information if there is no
dithering. A JPEG can reduce this 900K 24-bit image to 45K (high quality) or 30K (medium
quality), a factor of 20:1 to 30:1. With JPEGs the more you compress, the more edge
definition and sharpness you lose. Humans are especially sensitive to edge sharpness,
possibly a holdover from ancient man spying snakes on the side of trees, so GIFs generally
appear sharper than JPEGs. Other compression methods can yield even higher compression
ratios, but are currently proprietary.

Dithering

To display a full-color image on a 256-color computer, it must simulate colors it can’t


actually display. The computer does this by dithering: combining pixels from its 256-color
palette into patterns that approximate other colors (Figure 1). At a distance the human eye
merges these dithered patterns into another color, but up close the image appears speckled.
Dithering makes it harder to losslessly compress images, as the compression algorithm
used in GIFs [LZ84] relies on horizontal areas of the same color. In order to avoid dithering,
create images that use the colors specified in the browser’s default color palette.

The main characteristic that separates the GIF plug-ins is the quality of color reduction
(quantization algorithm). To test this I reduced the full-color balloon (try it yourself, it’s
available as a stuffit file (57K), binhexed (79K) or TIFF/Zipped for PC users) to both 64
and 128 colors using an adaptive palette. The 64 color image is admittedly an extreme
example used to exaggerate the quantization quality. Only HVS ColorGIF and ImageVice
(see Compression for details on HVS ColorGIF 2.0 which replaces HVS PhotoGIF)
create an acceptable image at 64 colors. But add in some dithering, and the banding
smooths out (diffusion dithering works best here). Imageready excels at small yet high
quality reductions, check out the lossy/dithered 8.6K GIF below.

71 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Two new entries, Macromedia’s Fireworks, and Adobe’s Imageready created smaller
NOTES files at the same bit depth than the others, and better banding than all but HVS and ImageVice.
In addition to an adaptive palette, Imageready 2.0 has a perceptive palette, that works
similarly to HVS’s technology (though not as well). As you’d expect using Imageready’s
perceptive palette produced images with less noticeable banding than the adaptive palette.
To test color remapping quality I reduced the image to 128 colors of the Netscape 216-
color palette at the default dithering settings.

Figure 1 Color Reduction Test

Figure 1a 64 Color Balloon Comparison

72 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Imageready 2 is much improved over version 1. The GIFs are much smaller, and
show less discernible banding, especially at the percetual palette setting. Add in the NOTES
ImageVice-like “lossy” setting (which increases redundancy along scan lines) and I created
an 8.2K 64 color balloon. Add in some dithering to smooth out the banding, and IR
produces a svelte 8.6K GIF. Overall, Imageready creates the smallest yet highest quality
GIFs.

ImageVice 2, from BoxTop Software, is also much improved over version 1.


ImageVice, with the right settings, reduced our test balloon to nearly the same quality as
HVS ColorGIF. HVS ColorGIF, at the high shading setting, created slightly smoother,
less discernable bands than ImageVice (see the upper right green band for example).
ImageVice has some other tuning parameters similar to Imageready 2’s lossy feature that
allow even smaller images, at the expense of some image quality. However, Imageready
starts at an advantage, with smaller 64-color GIFs, and IR has a more effective (and easier
to use) lossy feature than ImageVice.

The quality of both the 64-color GIFs created by HVS ColorGIF and ImageVice are
amazing. With extreme reductions like this, setting the “smoothing” setting (which controls
how wide or noticeable the banding is) to the highest setting gives the best results in HVS
ColorGIF and ImageVice. The file sizes are similar to PhotoGIF’s at these settings, but
can be decreased by setting the smoothing to a lower setting at the expense of increased
banding.

Quality-wise, HVS ColorGIF created the best looking non-dithered images at the
high shading setting (which created the smoothest banding, see the upper right green and
blue bands). ImageVice came in a close second. Size-wise Imageready created the smallest
GIFs, especially with the lossy setting, at some loss of image quality.

There is a 216-color palette that will not dither on Mac and Windows PCs.
There is much discussion and confusion on the Web about color palettes. There is, in fact,
no one universal 8-bit palette that will not dither on all platforms.

There is, however, a 216-color palette that will not dither on most.

The problem is that different platforms use different color palettes. Netscape and
other applications use a 256-color palette on Macs and X-windows, a reduced palette on
Windows, and UNIX varies the palette according to how many colors are available. Note:
Netscape 4.0 Mac uses the 216-color “non-dithering” palette, not the Mac’s system palette.

The Windows Palette

Windows 3.x was designed around the ubiquitous 256-color VGA graphics board.
Windows computers reserve 20 of these colors for system use; the first and last ten colors.
Netscape reserves an additional 13 colors for its logo, which leaves 223 colors available

73 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

to the user. Netscape’s programmers reduced this figure to 216 colors (Figure 2), which is
NOTES the closest cube root below 223 [Ve95, NC95].

Figure 2 The Netscape 216-color palette

The Netscape Color Cube (PC/Mac)

Figure 3 The Netscape6x6x6 Color Cube (PC/Mac


The 6x6x6 color matrix (or “cube”) shown in Figure 3 is used by PC and Macintosh
versions of Netscape Navigator 1.1 on 8-bit (256-color) displays. Graphics using this
216-color palette will not dither on either platform. Netscape 2.x adds one refinement: a
GIF appearing as the only image on a page will be shown in its exact requested colors. If
there’s more than one image on a page, Netscape dithers to the 216-color cube.

74 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The cube consists of 216 colors, 6 colors for the red component, 6 for the green, and
6 for the blue. Each basic hue in this RGB color space ranges from 0 to 255 in decimal or NOTES
from 00 to ff in hexadecimal. The colors in the figure use RGB values of 00, 33, 66, 99, cc,
and ff (hex) or 0, 51, 102, 153, 204, and 255 (decimal). The increment is no coincidence,
51 is 1/5 of 255 which gives 6 evenly spaced points along each axis.

Thanks to Rick Levine of Sun Microsystems [Le96] for the use of his color cubes.The
UNIX Palette

Netscape for UNIX, however, automatically adjusts the size of its color cube depending
on how many colors are available at launch. It may use a 6x6x6, 5x5x5, or even a 4x4x4
color cube, depending on how many colors are free.

Figure 4 The Netscape

5x5x5 Color Cube (UNIX)

The 5x5x5 color matrix (or “cube”) shown in Figure 4 is most often used by
UNIX versions of Netscape Navigator on 8-bit displays. Graphics using this 125-color
palette will not dither on UNIX machines.

75 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The cube consists of 125 colors, 5 colors for the red component, 5 for the green, and
NOTES 5 for the blue. Each basic hue in this RGB color space ranges from 0 to 255 in decimal or
from 00 to ff in hexadecimal. The colors in the figure use RGB values of 00, 40, 80, bf, and
ff (hex) or 0, 64, 128, 191, and 255 (decimal).

On 8-bit PC and Macintosh platforms, all but eight colors in this table will be dithered
(the cube’s eight corners composed of 00s and ffs). So even when you use the 216-color
cube, most Unix users will still see most of your colors dither [Le96].

The general approach on the Web has been to ignore UNIX users and design for the
majority. Authors frequently cite the 216-color palette as a universal non-dithering palette
when it is in fact non-dithering for only Macs and Windows.

The 216-color non-dithering palette

You can download the Mac or PC non-dithering CLUT right here. To use it, unbinhex
and unstuff (Mac) it or unzip it (PC) and save to a convenient place. You’ll see a file called
216clut. This file can double as both a CLUT (color lookup table) or a swatch in Photoshop.
To avoid dithering on 8-bit machines you can either start out using only colors from this
special palette (picking colors from this color swatch) or map your images to it (using the
same file as a CLUT).Load non-dithering palette as a Swatch

Figure 5 216-color swatch

76 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

To make it easy to pick non-dithering colors for your art you can make this non-
dithering color table the default color swatch. First load the 216-color CLUT as a swatch NOTES
(Windows/Swatches/Load swatch) and then select save. This swatch becomes the default
and appears each time you start up Photoshop (Figure 5). Use this non-dithering swatch
when creating graphics from scratch.

Mapping an image to the 216-color CLU

To map the non-dithering CLUT to an existing image, first fill all of the flat-color areas
with colors from the 216-color palette (see above). Any other colors in the non-flat areas
of your image will dither on some systems, which is usually fine. Now convert the image
from RGB to indexed colors and load the special 216-color CLUT (Mode/Indexed Colors/
Custom Palette/Dither = None). Select load in the CLUT dialog box and select the special
216clut you just downloaded (Figure 6). Photoshop will reduce the image to 216 colors
and map the image to this palette. Of course, during this color mapping some posterization
may occur.

Figure 6 Custom 216-color CLUT

You can also create your own 216 color table from figure 3 (Netscape 216-color
cube). First save the image to a file on your computer. Open it in Photoshop, select Mode/
Color Table, then select Save (Figure 6). These days, look for a modern image editing
program (Imageready, Fireworks, etc) that includes a Web palette for you, to make this
step unnecessary.
Color Reduction
The Old Way
When to reduce colors

In general full-color images work best as JPEGs and require no color reduction.
Synthetic or flat-color images usually work best as GIFs or PNGs. The fewer the colors,
the smaller the GIF or PNG.

77 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Flat or Synthetic Images


NOTES
To map the non-dithering CLUT to an existing image, first fill all of the flat-color areas
with colors from the 216-color palette. Any other colors in the non-flat areas of your image
will dither on some systems, which is usually fine. Now convert the image from RGB to
indexed colors and load the special 216-color CLUT (Mode/Indexed Colors/Custom
Palette/Dither = None). Photoshop will reduce the image to 216 colors and map the image
to this palette. Of course, during this color mapping some posterization may occur.

Hint: Deliberately posterizing your image is a quick way to visualize your image with
reduced colors and flatten your image. In Photoshop select Image/Adjust/Posterize... and
try reducing the number of colors (Figure 7).

Figure 7 Posterize Dialog

Figure 8 Indexed Color Dialog

78 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Most full-color photographic-like images do not reduce well with the 216-color CLUT.
Using an adaptive palette with diffusion dithering generally gives the best results. Adaptive NOTES
palettes are tuned to the colors present in the image, and result in less dithering and a more
pleasing image [We96]. On 24-bit systems, 8-bit adaptive-palette images look nearly 24-
bit in quality. Perceptive palettes are even better. However, on 8-bit machines browsers
will remap the adaptive palette into the client’s system palette. Usually the results are
acceptable.

Caution:On older versions of Windows and Mac-platform Photoshop, the system


palette turns out to be the 256-color Mac system palette! Adobe says this is a “known
factor.” While the entire Mac system palette will be non-dithered on a Mac, Windows
users will find some colors dithered.

To reduce an image into an adaptive palette, select Image/Mode/Indexed Color/


Adaptive Palette/Dither = Diffusion or None in Photoshop. Select the minimum bit depth
that looks acceptable (Figure 8). Use Undo or Revert to tweak until you’re satisfied with
the image. Remember, your graphics will be viewed on screen at 72 dpi for an instant, not
printed on a reverse dye-sublimation printer so you can get away with less bits than you
may think.

Histogram Influencing

In Photoshop: Shift Happens

Photoshop is the most popular program for manipulating images for the Web, but it
has some quirks. Photoshop can shift colors slightly when reducing images using an adaptive
palette. An image that starts out with non-dithering colors can end up dithered after color
reduction. This is because Photoshop reduces colors from a 24-bit to a 15-bit color space
before it calculates the best palette. After reduction, the 15-bit palette gets padded with a
few extra bits to show the final palette colors as RGB. Those extra bits cause the color
shift, and the colors will dither on 8-bit machines.

The eight corners of the color cube, however, are not affected. Every other color will
be shifted slightly after adaptive color reduction. One solution is to select the color and
manually type in the non-dithering values. Another is to map to a fixed palette. Another
option* is to use the Curves feature after indexing an image to nudge the colors back
towards their intended non-dithering values. The centers of the curves only need to be
nudged one or two units in either direction and the operation usually must be done to each
channel separately. Or, you can use Debabelizer instead. Debabelizer does not shift colors,
and is a better choice for adaptive color reduction.

Color Correctors BoxTop Software has a free plug called WebClipper that fixes
this color shift problem. Bundled with their color blender plug-in ColorSafe, WebClipper
snaps your image back to the closest web-safe non-dithering colors. Adobe’s new

79 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Imageready has a new color reduction algorithm that doesn’t shift Web-safe colors, and
NOTES offers color locking and Web-shifting (snap a color to its closest Web-safe color) for more
control over the color reduction process.

http://www.boxtopsoft.com/ColorSafe/

*Thanks to Tricia McGillis tricia@cyborganic.net for this tip.

When you index to an adaptive palette Photoshop creates a histogram of the colors in
your image, ranked by popularity. Photoshop uses the histogram to determine how much
weight to give certain colors in the resulting palette. For simple images, this method works
well. For more complex images, Photoshop’s choice of color palette may be less than
optimal. A little known feature of Photoshop is that you can influence the histogram by
using selections.

Photoshop normally calculates the histogram using the entire image, but for more
control you can select the part(s) of the image that contain the colors you want included in
the palette. Photoshop weights the conversion towards the colors in the active selection(s).
This technique can be used to create the absolute minimum palette for a given image. For
example, the balloon image did not reduce well to 7-bits, but with careful use of selections
you can help minimize the damage (compare the blue and purple regions in Figure 9).

Figure 9 Histogram Influencing for 128-color GIF

The New Way

Adobe, BoxTop Software, and Spinwave all offer new ways to efficiently reduce
colors and minimize color banding. Adobe Photoshop 5.5 encapsulates the optimization
features of ImageReady 2 (bundled together) in the new “Save for Web” plug-in dialog.

Imageready

Imagready’s “lossy” feature increases redundancy along scan lines to allow LZW
compression to work more efficiently.

80 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 9a Photoshop 5.5’s Save for Web DialogImageVice

ImageVice

Imagevice 2, from BoxTop Software, is the power-user’s color reduction filter. It


offers more control than Imageready over its version of the lossy method, and is second
only to HVS ColorGIF in its minimization of banding. Unfortunately, the results are not as
smooth and small as Imageready’s.

Figure 9b ImageVice 2 Dialog

81 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

HVS
NOTES
Spinwave (http://www.spinwave.com) offers graphic reduction tools based on their
Human Visual System’s (HVS) technology. HVS Color, HVS ColorGIF, and HVS JPEG
are the first in a series of HVS-based products from Spinwave (formerly Digital Frontiers).
HVS is a patented, psychovisual algorithm that models the way the eye perceives and
masks color. HVS represents graphics in a color space that more closely matches how our
eye’s actually perceive color. The net effect is less discernible color banding at lower bit
depths, with no dithering.

Details - The HVS psychovisual color reduction (or quantization) algorithm take into
account the non-linearity of human visual perception. Humans are about four time more
sensitive to lightness or blackness than they are sensitive to color itself. HVS separates
colors in term of their lightness or blackness (i.e. luma) and each of the color/hue components
(i.e. chroma) and represents pictures in a color space which is much closer to the way we
perceive color. HVS also takes into account that our eyes are logarithmically sensitive to
perceived brightness or intensity. HVS discards little-used and “less-perceived” colors
and uses the more high frequency information (what we perceive most readily). The resulting
color space is optimized for how we perceive luma, croma, intensity, and color masking.

HVS ColorGIF

HVS ColorGIF is a Photoshop-compatible color-reduction plug-in (Export or Filter)


that reduces colors beyond conventional techniques. It mimics how the human eye perceives
and masks color and removes any unecessary colors. The effect is the same apparent
image quality using less colors. You can actually reduce some 24-bit images to 8- 7- or
even 6-bit GIFs and not notice the difference, except of course when you download it.

Using HVS ColorGIF

To use HVS Color you choose File/Export/HVS ColorGIF.... (or use the filter version
shown) The dialog box below appears (Figure 10). HVS guesses at the minimum acceptable
number of colors, but you can tweak the bit depth lower and preview interactively. For
continuous-tone images like the balloon photograph, you’ll want the set shading to more.
Dithering should be used only for extreme reductions, as this increases file size on the Web
(though you can trade off bit depth if you add some dithering). The threshholding option is
interesting, you can set what value of particular colors to shift to black or white, to further
reduce the color palette. The Gamma dialog allows easy tweaking of the image’s apparent
brightness on the screen, and makes a handy PC simulator for Mac users. Hit preview to
see what effect your settings have on the image. After some sophisticated calculations,
HVS Color displays the reduced image in the preview window.

82 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 10 HVS ColorGIF 2 Dialog

Digital Frontiers claims that resultant savings range from 10-70% depending on the
image. Every serious graphics designer should have HVS ColorGIF.
Now that you have reduced the color palette it’s time to save the image in the most
compressed way.

2.4 COMPRESSION METHODS

There are basically two types of compression methods: lossy and lossless. Lossy
compression creates smaller files by discarding some information about the original image.
It removes details and color changes it deems too small for the human eye to differentiate.
Lossless compression, on the other hand, never discards any information about the original
file.

Graphic File Formats

The graphic file formats most browsers support are GIF, JPEG and, more recently,
Progressive JPEG (p-jpeg). New formats are appearing as possible challengers, including
PNG, FIF, and those based on wavelet compression (see below). PNG, being an open
standard, promises to replace GIF. JPEG has no current non-proprietary competition,
though there has been a lot of hype, claims and discussion about the various proprietary
formats. The potential of improved compression ratios using one of the wavelet flavors is
showing scientific promise but will depend on the open standards issue for widespread
acceptance.

83 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

GIF
NOTES
The Graphics Interchange Format (GIF, pronounced jiff, though most people say
giff) was developed by Compuserve in 1987 to store multiple bitmap images into a single
file for easy exchange over computer networks. The GIF is the oldest graphic file format
on the Web, and nearly all browsers support it (except Lynx, of course). GIFs support up
to 8 bits per pixel, which means a maximum of 256 colors (2^8=256 colors), 4-pass
interlacing, transparency, and uses a variant of the Lempel-Ziv Welch compression algorithm
(LZW, [LZ84]).

LZW is a lossless compression algorithm and compression/decompression times are


symmetric. LZW is a repeated-string compressor, it uses a data dictionary (also called a
translation table or string table) to represent linear sequences of data in the uncompressed
input stream. The first time a sequence is encountered a code is added to the dictionary.
Any subsequent matching sequences are represented by this code.

Steps for Smaller GIFs

If you want the smallest possible GIFs, keep LZW’s row-oriented behavior in mind:
GIFs compress by removing horizontal redundancy. Try not to introduce extra vertical
detail or noise into GIF images. Horizontally oriented bands of color compress better than
vertically oriented bands. Avoid dithering, it breaks up those lovely minimizable rows of
color. These characteristics of the LZW compression algorithm are best shown by example
(Figure 11).

Figure 11 GIF vs. PNG Compression Test

GIF files can be saved in two ways: consecutive (top to bottom) and interlaced (8th
row, 4th row, 2nd...). Interlacing displays a low-resolution image quickly, which gradually
comes into focus, at the expense of additional file size.

84 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

GIF File Formats


NOTES
There are two types of GIFs:

 GIF87a: supports interlacing and storage of multiple files. It is named after the
year it was invented, and is the standard.

 GIF89a: extends the GIF87a specification and adds transparency, text comments,
and animation of text and graphics.

While the LZW compression algorithm used by GIFs is one of the better general
purpose compression algorithms, it wasn’t designed specifically for graphics. It doesn’t
work well with bilevel (black and white) or true color images.

The LZW algorithm used in GIFs, however, has been patented by Unisys [UN95].
Developers who distribute applications that create GIFs must obtain a license from Unisys.
Soon after CompuServe and Unisys decided to charge royalties for GIFs, PNG was
born.

PNG

The Portable Network Graphic (PNG, pronounced ping, [Bo96]) format was designed
to be a better, legally unencumbered replacement for GIF. Designed to losslessly store a
single bitmap image for transmitting over computer networks, PNG matches all of GIF’s
features except one (multiple images), improves on some (interlacing, compression), and
adds new features of its own (gamma storage, full alpha channel, true color support, error
detection). Support by Web browsers is mainly through plug-ins, but it should have built-
in support by popular browsers within the next year.

Better Compression

PNG uses the Deflate compression method, used in the popular pkzip file archiving
utility. Deflate is an improved version of the Lempel-Ziv compression algorithm [LZ77]. It
works similarly to the LZW algorithm, and looks for repeated horizontal patterns along
each scan line. To further enhance compression, PNG prefilters the image data using
predictor functions before it’s compressed. PNG uses four predictor functions, two of
which address vertical patterns.

So PNG behaves like GIF in that it compresses horizontal patterns, but PNG’s filters
also find vertical patterns, resulting in additional compression.

Improved Interlacing

PNG uses a 7-pass interlacing scheme which displays a recognizable image much
more quickly than an interlaced GIF. While GIFs simply rearrange the order in which rows
of pixels are stored, PNG uses the first six passes to gradually build up the even-numbered

85 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

scan lines (0, 2, 4, etc.) and the final pass to fill in the odd-numbered lines. Users see a 1/
NOTES 64-quality image almost immediately, following by 1/32, 1/16, and so on. The result is a
discernible image after only 20 to 30 percent of the image is received, compared to 50
percent for an interlaced GIF. This improved interlacing scheme adds about seven percent
to the file’s size.

True Color and Transparency

PNG supports up to 16 bits (gray scale) or 48 bits (true color) per pixel, and up to 16
bits of alpha data. PNG supports two methods of transparency, one-color masking like
GIF89a’s and an alpha channel. PNG’s full alpha channel allows up to 64K levels of
transparency for each pixel (2^16=65,536). This makes it possible to create beautiful
glows and drop shadows which layer over different-colored backgrounds perfectly.

Gamma Storage

PNGs can store gamma information. Gamma is a measure of how a display device
responds non-linearly to light intensity. By adjusting the gamma (gamma correction) you
can change the brightness values of the middle range of gray tones without dramatically
altering the shadows and highlights. Gamma values for different platforms and different
monitors vary, Macs have a gamma of about 1.8 and PCs a gamma of 2.2. Since there is
no “gamma standard” on the Web, graphics that look great on a Macintosh may look dark
on a PC. Since GIFs and JFIFs have no built-in gamma storage averaging these two
gammas is one approach. I adjust my gamma to 2.2 (Figure 11a) and tweak the white
point’s level (Figure 11b) to more closely simulate a PC’s gamma (see “Platform Crossing
Ahead” in the October 1996 issue of MacWORLD p. 159).

Figure 11a Gamma Control Panel

86 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 11b Photoshop’s Levels Dialog Box

PNG avoids these problems by allowing designers to store the gamma of the creation
device. When displayed, PNGs automatically adjust to the host monitor’s gamma.

Steps for Smaller PNGs

PNGs behave similar to GIFs horizontally, they both use similar compression methods
[LZ77, LZ78] based on changes along scan lines. However, PNGs also prefilter for vertical
patterns so solid colors generally work best. Avoid introducing detail or noise into PNG
images. Avoid dithering, it breaks up flat areas of color, and makes PNG’s compression
less efficient.

Figure 11 shows that PNGs are noticeably smaller than GIFs in most cases. The
“vertical” image is nearly identical in size to the “horizontal” one, which shows that PNGs
are not sensitive to vertical noise. PNGs compress dithered images better than GIFs, the
“dithered” PNG image is much smaller than the dithered GIF. PNGs typically compresses
8-bit files 10-30% better than GIFs.

PNG’s superior compression and new features provide a compelling alternative to


GIFs. More information can be found at the PNG home page http://www.cdrom.com/
pub/png/ and in a detailed summary of PNG by Lee Crocker in the July 1995 issue of Dr.
Dobb’s Journal [Cr95].

JPEG

JPEG (pronounced jay-peg, [JP95]) is designed for compressing either full-color or


gray-scale images of natural, real-world scenes. JPEGs work well on continuous tone
images like photographs or natural artwork; not so well on sharp-edged or flat-color art
like lettering, simple cartoons, or line drawings. JPEGs support 24-bits of color depth or
16.7 million colors (2^24=16,777,216 colors). Progressive JPEGs (p-JPEGs) are typically
a couple percent smaller than baseline JPEGs; but their main advantage is that they appear
in stages, similar to interlaced GIFs.

87 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

JPEG is a lossy compression algorithm. JPEG works by converting the spatial image
NOTES representation into a frequency map. A Discrete Cosine Transform (DCT) separates the
high- and low-frequency information present in the image. The high frequency information
is then selectively discarded, depending on the quality setting. The greater the compression,
the greater the degree of information loss. Compression and decompression times are
symmetric, although the International JPEG Group’s (IJG) decoder is more optimized and
noticeably faster than their encoder. However, when displayed at 8 bits, color quantization
slows decompression considerably. Many commercial vendors of JPEG offer enhancements
to speed, color quantization and quality over the IJG implementation. What separates the
various products are the enhancements they offer to the IJG or their own codecs:

JPEG Enhancements

 Huffman Code Optimization (most offer this feature) - Generates a custom “code
table” that works best to compress your individual image instead of using a standard
generic code table that works OK for most everything.
 Quantization Table Optimization (HVS JPEG)
 Improved Sub-sampling (sharper images at the expense of file size)
 Lossless edits/cropping/rotation
 Selective compression - Regions of interest

JPEG is designed to exploit certain properties of our eyes; namely, that we are more
sensitive to slow changes of brightness and color than we are to rapid changes over a short
distance. While JPEGs are usually the best choice for photographs, on 8-bit monitors they
are force-dithered into an 8-bit palette. JPEG compression takes place in Y/UV space and
therefore is treated as 24 bit data (8 bit for grey), regardless of the colors in the original
image. Therefore, if you reduce an image from 24bit to 8bit prior to JPEG compression,
the compression ratio will worsen as will the overall quality. JPEG compression introduces
noise into solid-color areas, which can distort and even blur flat-color graphics. This is why
JPEGs are not well suited to flat-color sharp-edged art or type.

JPEG is actually just a compression algorithm, not a file format. The files commonly
called JPEG on the Net are really in JFIF (JPEG File Interchange Format, [Ha92]). There
is work afoot to replace JFIF with a JPEG-based format called SPIFF, but since SPIFF is
compatible with JFIF, it’s likely that most users won’t even notice the changeover. Progressive
JPEG is now becoming a widely supported format by browsers and if implemented properly
gives the effect of the image fading in as opposed to painting from the top down. P-JPEG
may be converted to or from JFIF (standard JPEG) in a completely lossless manner.

All Web browsers support GIFs, most support JPEGs and a rapidly growing number
support progressive JPEGs. All three formats are supported by the latest versions of
Netscape and Internet Explorer.

88 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

JPEG itself has not been static. The very popular IJG code has gotten steadily more
efficient and effective in terms of compression ratio and quality. There are a few other NOTES
companies commercially shipping their own implementations of jpeg, including Pegasus
Imaging Corp. http://www.jpg.com and AutoGraph International (http://www.augrin.com).
Pegasus has provided even more optimized versions of the jpeg compressor, offering
slightly better compression ratios, but more importantly their decompressor removes many
of the blocking artifacts prevalent in highly compressed JPEG images and in smooth color
regions. Here are some typical compression ratios for graphics formats now use on the
Web (Figure 12).

Figure 12 Compression Ratios for Graphic File Formats

89 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Save As...
NOTES
Photoshop comes with its own file format plug-ins, but third party products can offer
tighter compression and additional features. New products from Spinwave and BoxTop
software (http://www.boxtopsoft.com) promise tighter compression and more control.

HVS PhotoGIF and HVS JPEG both incorporate Digital Frontier’s HVS technology.
BoxTop has updated their products to ProJPEG 4.

90 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

91 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

92 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 19 JPEG Program Comparison (med/hi/max optimized equivalents)

JPEGs

To compare the quality of JPEGs produced by the various programs I normalized the
results by creating equivalent size JPEGs from each program. I created three baseline
JPEGs with ImageReady 2 at approximately max, high, and medium quality (adjusted to
the closest K). I then created similar-sized JPEGs with the other programs. (22K, 14K,
and 9K approximately). The high and medium settings in ImageReady 2 correspond to
92% and 80% quality in the other programs (JPEG Wizard has their own scale, and I
manually edited the Luminance and Chrominance table to create the closest sized files for
comparison).

Debabelizer, ProJPEG, Fireworks, HVS and JPEG Cruncher all appear to share the
same IJG codec, so it’s not surprising that the non-optimized versions are nearly identical
in appearance and size. ImageReady 2’s max and high settings use selective subsampling
and images appear sharper than the other non-optimized high-quality JPEGs, especially in
the red-purple bands. JPEG Wizard creates uniformly sharp images, and the sharpest
medium quality JPEGs at some expense of artifacts.

JPEG Wizard (Windows only) also has a unique selective regional compression feature
that allows you to compress parts of the image less than the background, saving even more
space (useful for text or faces for example). Using an elliptical “Region” to surround the
balloon, I compressed the background sky even more to achieve 7.9K balloon above.
See Figure 20d below.

HVS JPEG2 has a Q-table optimization feature that creates slightly smaller files than
ProJPEG and the others at similar quality settings. HVS JPEG2 with the default “General”

93 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Q-table created the smallest files at equivalent quality settings, and had similar compression
NOTES artifacts as the other IJC products. JPEG Cruncher Desktop created identically-size JPEGs
that looked identical to HVS JPEG2. Turn on HVS JPEG’s Q-table optimization option
however, and the artifacts are smoothed out, and it eeks out Photoshop5.5/ImageReady 2
at the 80% setting. At the 92% high quality setting with an optimized Q-table, HVS JPEG2
created the smallest high quality image. However, JPEG Wizard and ImageReady 2 created
images that appear shaper at higher quality settings. I’d like to see HVS JPEG add a
selective sub-sampling similar to Photoshop’s.

ProJPEG 4.0 and Fireworks 3 created nearly identical images at the same settings
(not surprising as they both use the IJC codec). They both have an improved sub-sampling
option that improves the appearance and sharpness of JPEGs at the expense of 50%
larger file size. You can set the quality lower when using this sampling option to balance file
size and quality. ImageReady 2’s always-on sub-sampling is more efficient.

Conclusions

For typical Web use, at moderate to higher compression settings JPEG Wizard creates
the sharpest JPEGs. By utilizing its selective compression feature (regions) you can achieve
even smaller files. At the medium quality setting (47% for ImageReady, not the default 30%
quality), HVS JPEG 2 edges ImageReady 2 in image quality. HVS JPEG’s Q-table
optimization feature removes artifacts (especially at higher compression settings) at the
expense of little file size. At lower quality settings (50% or less) HVS JPEG 2 (with optimized
Q-table on) and ImageReady have less compression artifacts, which results in a smoother
look (see the purple-red bands in the 47% IR2 and the 80% opt q-table HVSJPEG 2
JPEGs). JPEG Wizard has sharper medium-quality JPEGs at the expense of some
smoothness.

At higher quality settings ImageReady’s and JPEG Wizard’s selective subsampling


are more efficient than the others (which increase file size by 50%), and the resulting image
appears sharper. So for typical Web compression levels, JPEG Wizard creates the sharpest
images, and HVS noses out ImageReady by a pixel. For the highest quality images
ImageReady 2 creates the sharpest yet smoothest JPEGs.

Fireworks vs. Imageready Of the two new all-in-one Web graphics programs,
Imageready makes the sharper JPEGs at the same file size. John Ahlquist, Macromedia’s
graphics optimization guru says “Fireworks JPEG always uses a more aggressive chroma
compression, which results in smaller files, but fuzzier colors for non-photographic data.”

ImageReady 2

Great JPEGs at all file sizes. Smoother with less artifacts, especially at the lower
quality settings. Third behind JPEG Wizard and HVS JPEG at typical compression levels
seen on the Web. Easy to use.

94 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 20a IR2

JPEG Wizard

Sharp JPEGs at common Web compression settings. Pegasus Software’s JPEG


Wizard (available as a Photoshop plugin, standalone product (both Win-only), or ASP
module) has its own JPEG codec, and a number of innovative features including lossless
cropping/decompression, hue/tint, etc. It is the first product to incorporate selective
compression with their region feature. Draw a rectagle/ellipse/freehand region around the
area(s) you want to remain legible, and you can selectively compress the background to
higher degrees of compression (8x8 blocks). See the figure below for an example of how
it works, and the 7.9K image above for an example image. The JPEGs created with JPEG
wizard appear sharper than the others, especially at higher compression settings.

Figure 20c JPEG Wizard Figure 20d JPEG Wizard Region

95 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

HVS JPEG
NOTES
Optimizing JPEGs is more straightforward than GIFs. HVS JPEG by Spinwave created
the sharpest yet smallest medium quality images (with Q-table optimization on), edging
Imageready by a pixel. HVS JPEG utilizes an improved JPEG codec which maximizes
both image quality and compression ratios. Like HVS PhotoGIF, HVS JPEG includes a
live preview feature so you can see exactly what your image will look like and about how
big it will be as you change settings. You can store your favorite settings and reuse them,
useful for production environments. One unique feature of HVS JPEG is the Q-table
optmization feature. These optimized quantization tables can create smaller files with nearly
the same quality.

Use the lowest quality setting that seems acceptable. Progressive JPEGs are a new
kind of JPEG that display the image in stages, similar to an interlaced GIF. P-JPEGs are
actually smaller, but are not supported by all browsers so you should provide alternatives
if you decide to use them.

Figure 20b HVS JPEG 2

ProJPEG 4

New targeted file size feature convenient for banner ads etc. that must be below 10K
etc. Input a file size and PJ4 iterates the quality to match that file size. Excellent results with
better image sampling on at the expense of 50% greater file size (unlike ImageReady).
Archival quality higher quality results.

96 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

Figure 20b PJ4

At equivalent quality settings HVS JPEG creates images that appear the same or
better than Photoshop, and are noticeably smaller (Figures 21-22). Maggie and her friend
looked the best using the quantization table optimized for skin tones.

Future Formats

JPEG 2000

The new JPEG 2000 spec, which should be ratified soon, includes wavelet, tiling,
progressive, regions of interest... and the kitchen sink. It is aimed primarily at large imagery.

“By offering functionalities such as multi-resolution representation, superior compression


efficiency at low bit rates, lossy to lossless progression, and embedded bit stream
architecture, the wavelet transform has been recognized as a preferred tool for image and
video compression and has been selected as the fundamental building block of the emerging
JPEG-2000 standard.” See http://www.jpeg.org/public/jpegnew.htm for more details.

97 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Exif
NOTES
This is an image format created by a Japanese camera manufacturers association. It is
essenially a JPEG image that contains metadata in the form of a jpeg app marker containing
a TIF IFD. It supports thumbnail image inclusion and the storage of lots of information
specific to photography and device description. For more information see: Exif File Format
unofficial spec.

PNP

The developers of PNG are currently working on formats for lossy image compression
and animation, using the overall design and technologies of PNG and applying them to
those tasks. A new format, tenatively called the Portable Network Photograph (PNP,
pronounced pinup) format will be the first result of their efforts. PNP will be designed to be
an open standard replacement for the aging JPEG format. PNP will use a lossy compression
algorithm, probably based on wavelets. For more information on this emerging standard,
consult the PNP home page at http://www.piclab.com/PNP/.

Other Formats

There have been many challengers to the GIF and JPEG monopoly in image
compression, but none so far has had much success. PNG, with its wide industry backing,
completely open standard, improved compression and expanded feature list seems destined
to succeed. People should be careful to substantiate any claims made about proprietary
formats, because over the years the hype has been constant and the actual results spotty.

ART

ART is a proprietary graphics format developed by Johnson-Grace (http://


www.jgc.com, where are they now?) and now owned by AOL (for a cool $72 million).
ART technology utilizes multiple compression algorithms within a single format. The
compressor employs a fuzzy logic engine to analyze the image — looking at factors such as
color depth, size, and whether the image is a photo or synthetic graphic — before selecting
the appropriate compression algorithms to use on the image. Where high compression is
needed such as online and Internet publishing, Johnson-Grace claims that ART-compressed
images are on average three times smaller than GIFs and JPEGs. (I doubt this) ART also
has a lossless mode for general use.

Johnson-Grace says that more algorithms are being developed to expand the format
to include audio, slideshows, and low bandwidth video. ART has built-in streaming, color
reduction, dither, and gamma correction.

98 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Wavelet
NOTES
Basically, wavelets are a better JPEG. Wavelets are a transformation of an image into
its component parts as a frequency representation (i.e., how fast things change in certain
repetitive patterns: waves). They use different basis functions that are claimed to be better
than the Discrete Cosine Transform (DCT) JPEGs use. DCT algorithms such as JPEG
and MPEG are all special cases of Wavelet transforms. Compression and decompression
times are symmetric, but three times slower than JPEGs. They are excellent for computer
graphics, stills, and video. All wavelet products are currently proprietary, and there are an
abundance of flavors and versions coming from both commercial and academic research
around the world.

Bravo
Adobe’s Bravo, the multiplatform 2-D imaging engine used in Adobe’s graphics
products, is being proposed as the next graphics standard for the Web. Bravo will be
provided as an API (Application Programming Interface), which will enable products that
display, print and manage high-fidelity graphics on the Web.
The API offers true color, transparency, and scaleable PostScript line art and text
that can be anti-aliased on-the-fly. Bravo can work interchangeably with Type1, TrueType,
and the new “OpenType(TM)” fonts announced by Microsoft. Adobe says this proposal
supersedes previous agreements with Netscape and Apple.

FlashPix

Developed collaboratively by Eastman Kodak Company, Hewlett-Packard Company,


Live Picture Inc. and Microsoft Corp. FlashPix is an attempt at a universal file format. It
supports multiple images and resolutions and two color spaces, a calibrated RGB and
Photo YCC (the color space used in PhotoCDs). Compression methods are JPEG, single
color, and uncompressed. Kodak published a specification for FlashPix in September.

2.5 WEB AUDIO FILES


Basic Information About Sound Files
The two most widely supported file formats for sound files are MIDI and WAV. As
with graphic files, you’ll want to keep the size of the file as small as possible while maintaining
quality.
The differences between WAV and MIDI are that MIDI files are generally a great
deal smaller, however, many types of audio, such as speech, don’t work well as MIDI
files. MIDI is a good file format for music. A one minute WAV file can be as large as 1MB
in size, while a 1 minute MIDI file can be less than 25K. Using software convertors, it may
be possible to resample WAV files to reduce their file size. See the bottom of this page for
links to helpful software sites.

99 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Create a Clickable Link to a Sound File


NOTES
The HTML to add a clickable link to a sound file is simple. You’ll use the A HREF tag
to point to the audio file. If you need help creating a path to the file, review our paths
tutorial. You’ll want to add a tag that looks like this:

<A HREF=”cat.wav”> Click here to hear my cat meow.</A>

The words “Click here to hear my cat meow” will become a link that will cause the
sound file to load when clicked. You can also create a clickable link with a graphic:

<A HREF=”cat.wav”><IMG SRC=”caticon.gif”></A> <BR> Click the cat! (6K File)

Which looks like this on a page:

Click the cat! (6K File)

The benefit of adding audio this way is that you give your web site visitors a choice of
hearing the sound file. Since audio over the Internet is less than perfect, and the file sizes
can be large, consider offering this choice to your web site visitors.

Create Sound Files That Play Automatically

Now things get sticky. You’ll need to use two different HTML tags to play a sound
automatically for both Internet Explorer and Netscape Navigator. (Big sigh). However,
before we jump in, take a moment to think: Do I really, really, really want to automatically
load an audio file?

If a web site visitor has to wait for an audio file to load, you’re giving them an opportunity
and an excuse to leave your web site. The longer the wait, the larger the chances are that
they will leave. So carefully consider your file sizes and placement of automatically playing
sound files.

Here are the tags you’ll need. The BGSOUND SRC tag is for Internet Explorer, the
Embed tag is for Netscape Navigator:

<BGSOUND SRC=”sound.wav” LOOP=INFINITE> <EMBED SRC=”sound.wav”


AUTOSTART=”true” HIDDEN=”true” LOOP=”true”>

This will create an infinitely looping sound for both IE and Netscape. To create a
sound that doesn’t loop:

100 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

<BGSOUND SRC=”sound.wav” LOOP=0>


NOTES
<EMBED SRC=”sound.wav” AUTOSTART=”True” HIDDEN=”True”
LOOP=”false”>

With the later versions of Internet Explorer, however, the above tag will give IE users
two sound controls. To avoid that, add the NOEMBED and closing /NOEMBED tag
around the BGSOUND SRC tag:

<NOEMBED>

<BGSOUND SRC=”sound.wav” LOOP=0>

</NOEMBED>

<EMBED SRC=”sound.wav” AUTOSTART=”True” HIDDEN=”True”

LOOP=”false”>

2.6 FORMS

Introduction to forms

An HTML form is a section of a document containing normal content, markup, special


elements called controls (checkboxes, radio buttons, menus, etc.), and labels on those
controls. Users generally “complete” a form by modifying its controls (entering text, selecting
menu items, etc.), before submitting the form to an agent for processing (e.g., to a Web
server, to a mail server, etc.)

Here’s a simple form that includes labels, radio buttons, and push buttons (reset the
form or submit it):

<FORM action=”http://somesite.com/prog/adduser” method=”post”>


<P>
<LABEL for=”firstname”>First name: </LABEL>
<INPUT type=”text” id=”firstname”><BR>
<LABEL for=”lastname”>Last name: </LABEL>
<INPUT type=”text” id=”lastname”><BR>
<LABEL for=”email”>email: </LABEL>
<INPUT type=”text” id=”email”><BR>
<INPUT type=”radio” name=”sex” value=”Male”> Male<BR>
<INPUT type=”radio” name=”sex” value=”Female”> Female<BR>
<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>
</P>
</FORM>

101 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Note. This specification includes more detailed information about forms in the
NOTES subsections on form display issues.

Controls

Users interact with forms through named controls.

A control’s “control name” is given by its name attribute. The scope of the name
attribute for a control within a FORM element is the FORM element.

Each control has both an initial value and a current value, both of which are character
strings. Please consult the definition of each control for information about initial values and
possible constraints on values imposed by the control. In general, a control’s “initial value”
may be specified with the control element’s value attribute. However, the initial value of a
TEXTAREA element is given by its contents, and the initial value of an OBJECT element in
a form is determined by the object implementation (i.e., it lies outside the scope of this
specification).

The control’s “current value” is first set to the initial value. Thereafter, the control’s
current value may be modified through user interaction and scripts.

A control’s initial value does not change. Thus, when a form is reset, each control’s
current value is reset to its initial value. If a control does not have an initial value, the effect
of a form reset on that control is undefined.

When a form is submitted for processing, some controls have their name paired with
their current value and these pairs are submitted with the form. Those controls for which
name/value pairs are submitted are called successful controls.

Control types

HTML defines the following control types:

buttons

Authors may create three types of buttons:

 submit buttons: When activated, a submit button submits a form. A form may contain
more than one submit button.

 reset buttons: When activated, a reset button resets all controls to their initial values.

 push buttons: Push buttons have no default behavior. Each push button may have
client-side scripts associated with the element’s event attributes. When an event
occurs (e.g., the user presses the button, releases it, etc.), the associated script is
triggered.

102 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Authors should specify the scripting language of a push button script through a default
script declaration (with the META element). NOTES
Authors create buttons with the BUTTON element or the INPUT element. Please
consult the definitions of these elements for details about specifying different button types.

Note. Authors should note that the BUTTON element offers richer rendering
capabilities than the INPUT element.

Checkboxes

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user.
A switch is “on” when the control element’s checked attribute is set. When a form is
submitted, only “on” checkbox controls can become successful.

Several checkboxes in a form may share the same control name. Thus, for example,
checkboxes allow users to select several values for the same property. The INPUT element
is used to create a checkbox control.

radio buttons

Radio buttons are like checkboxes except that when several share the same control
name, they are mutually exclusive: when one is switched “on”, all others with the same
name are switched “off”. The INPUT element is used to create a radio button control.

If no radio button in a set sharing the same control name is initially “on”, user agent
behavior for choosing which control is initially “on” is undefined. Note. Since existing
implementations handle this case differently, the current specification differs from RFC
1866 ([RFC1866] section 8.1.2.4), which states:

At all times, exactly one of the radio buttons in a set is checked. If none of the
<INPUT> elements of a set of radio buttons specifies ‘CHECKED’, then the user agent
must check the first radio button of the set initially.

Since user agent behavior differs, authors should ensure that in each set of radio
buttons that one is initially “on”.

menus

Menus offer users options from which to choose. The SELECT element creates a
menu, in combination with the OPTGROUP and OPTION elements.

text input

Authors may create two types of controls that allow users to input text. The INPUT
element creates a single-line input control and the TEXTAREA element creates a multi-line
input control. In both cases, the input text becomes the control’s current value.

103 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

file select
NOTES
This control type allows the user to select files so that their contents may be submitted
with a form. The INPUT element is used to create a file select control.

hidden controls

Authors may create controls that are not rendered but whose values are submitted
with a form. Authors generally use this control type to store information between client/
server exchanges that would otherwise be lost due to the stateless nature of HTTP (see
[RFC2616]). The INPUT element is used to create a hidden control.

object controls

Authors may insert generic objects in forms such that associated values are submitted
along with other controls. Authors create object controls with the OBJECT element.

The elements used to create controls generally appear inside a FORM element, but
may also appear outside of a FORM element declaration when they are used to build user
interfaces. This is discussed in the section on intrinsic events. Note that controls outside a
form cannot be successful controls.

The FORM element

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) — interactive form —>


<!ATTLIST FORM
%attrs; — %coreattrs, %i18n, %events —
action %URI; #REQUIRED — server-side form handler —
method (GET|POST) GET — HTTP method used to submit the form—
enctype %ContentType; “application/x-www-form-urlencoded”
accept %ContentTypes; #IMPLIED — list of MIME types for file upload —
name CDATA #IMPLIED — name of form for scripting —
onsubmit %Script; #IMPLIED — the form was submitted —
onreset %Script; #IMPLIED — the form was reset —
accept-charset %Charsets; #IMPLIED — list of supported charsets —
>
Start tag: required, End tag: required
Attribute definitions
action = uri [CT]

104 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

This attribute specifies a form processing agent. User agent behavior for a value other
than an HTTP URI is undefined. NOTES
method = get|post [CI]

This attribute specifies which HTTP method will be used to submit the form data set.
Possible (case-insensitive) values are “get” (the default) and “post”. See the section on
form submission for usage information.

enctype = content-type [CI]

This attribute specifies the content type used to submit the form to the server (when
the value of method is “post”). The default value for this attribute is “application/x-www-
form-urlencoded”. The value “multipart/form-data” should be used in combination with
the INPUT element, type=”file”.

accept-charset = charset list [CI]

This attribute specifies the list of character encodings for input data that is accepted
by the server processing this form. The value is a space- and/or comma-delimited list of
charset values. The client must interpret this list as an exclusive-or list, i.e., the server is
able to accept any single character encoding per entity received.

The default value for this attribute is the reserved string “UNKNOWN”. User agents
may interpret this value as the character encoding that was used to transmit the document
containing this FORM element.

accept = content-type-list [CI]

This attribute specifies a comma-separated list of content types that a server processing
this form will handle correctly. User agents may use this information to filter out non-
conforming files when prompting a user to select files to be sent to the server (cf. the
INPUT element when type=”file”).

name = cdata [CI]

This attribute names the element so that it may be referred to from style sheets or
scripts. Note. This attribute has been included for backwards compatibility. Applications
should use the id attribute to identify elements.

Attributes defined elsewhere

 id, class (document-wide identifiers)


 lang (language information), dir (text direction)
 style (inline style information)
 title (element title)

105 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

 target (target frame information)


NOTES  onsubmit, onreset, onclick, ondblclick, onmousedown, onmouseup, onmouseover,
onmousemove, onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

The FORM element acts as a container for controls. It specifies:


 The layout of the form (given by the contents of the element).

 The program that will handle the completed and submitted form (the action attribute).
The receiving program must be able to parse name/value pairs in order to make
use of them.

 The method by which user data will be sent to the server (the method attribute).

 A character encoding that must be accepted by the server in order to handle this
form (the accept-charset attribute). User agents may advise the user of the value
of the accept-charset attribute and/or restrict the user’s ability to enter unrecognized
characters.

A form can contain text and markup (paragraphs, lists, etc.) in addition to form controls.

The following example shows a form that is to be processed by the “adduser” program
when submitted. The form will be sent to the program using the HTTP “post” method.

<FORM action=”http://somesite.com/prog/adduser” method=”post”>

...form contents...

</FORM>

Please consult the section on form submission for information about how user agents
must prepare form data for servers and how user agents should handle expected responses.

Note. Further discussion on the behavior of servers that receive form data is beyond
the scope of this specification.

The INPUT element

<!ENTITY % InputType

“(TEXT | PASSWORD | CHECKBOX |

RADIO | SUBMIT | RESET |

FILE | HIDDEN | IMAGE | BUTTON)”

>

106 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

<!— attribute name required for all but submit and reset —>
NOTES
<!ELEMENT INPUT - O EMPTY — form control —>

<!ATTLIST INPUT

%attrs; — %coreattrs, %i18n, %events —

type %InputType; TEXT — what kind of widget is needed —

name CDATA #IMPLIED — submit as part of form —

value CDATA #IMPLIED — Specify for radio buttons and checkboxes —

checked (checked) #IMPLIED — for radio buttons and check boxes —

disabled (disabled) #IMPLIED — unavailable in this context —

readonly (readonly) #IMPLIED — for text and passwd —

size CDATA #IMPLIED — specific to each type of field —

maxlength NUMBER #IMPLIED — max chars for text fields —

src %URI; #IMPLIED — for fields with images —

alt CDATA #IMPLIED — short description —

usemap %URI; #IMPLIED — use client-side image map —

ismap (ismap) #IMPLIED — use server-side image map —

tabindex NUMBER #IMPLIED — position in tabbing order —

accesskey %Character; #IMPLIED — accessibility key character —

onfocus %Script; #IMPLIED — the element got the focus —

onblur %Script; #IMPLIED — the element lost the focus —

onselect %Script; #IMPLIED — some text was selected —

onchange %Script; #IMPLIED — the element value was changed —

accept %ContentTypes; #IMPLIED — list of MIME types for file upload —

>

107 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Start tag: required, End tag: forbidden


NOTES
Attribute definitions

type = text|password|checkbox|radio|submit|reset|file|hidden|image|button [CI]

This attribute specifies the type of control to create. The default value for this attribute
is “text”.

name = cdata [CI]

This attribute assigns the control name.

value = cdata [CA]

This attribute specifies the initial value of the control. It is optional except when the
type attribute has the value “radio” or “checkbox”.

size = cdata [CN]

This attribute tells the user agent the initial width of the control. The width is given in
pixels except when type attribute has the value “text” or “password”. In that case, its value
refers to the (integer) number of characters.

maxlength = number [CN]

When the type attribute has the value “text” or “password”, this attribute specifies the
maximum number of characters the user may enter. This number may exceed the specified
size, in which case the user agent should offer a scrolling mechanism. The default value for
this attribute is an unlimited number.

checked [CI]

When the type attribute has the value “radio” or “checkbox”, this boolean attribute
specifies that the button is on. User agents must ignore this attribute for other control types.

src = uri [CT]

When the type attribute has the value “image”, this attribute specifies the location of
the image to be used to decorate the graphical submit button.

108 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Attributes defined elsewhere


NOTES
 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 alt (alternate text)
 align (alignment)
 accept (legal content types for a server)
 readonly (read-only input controls)
 disabled (disabled input controls)
 tabindex (tabbing navigation)
 accesskey (access keys)
 usemap (client-side image maps)
 ismap (server-side image maps)
 onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown,
onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown,
onkeyup (intrinsic events)

Control types created with INPUT

The control type defined by the INPUT element depends on the value of the type
attribute text

Creates a single-line text input control.

password

Like “text”, but the input text is rendered in such a way as to hide the characters (e.g.,
a series of asterisks). This control type is often used for sensitive input such as passwords.
Note that the current value is the text entered by the user, not the text rendered by the user
agent.

Note. Application designers should note that this mechanism affords only light security
protection. Although the password is masked by user agents from casual observers, it is
transmitted to the server in clear text, and may be read by anyone with low-level access to
the network.

checkbox

Creates a checkbox.

109 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

radio
NOTES
Creates a radio button.

submit

Creates a submit button.

Image

Creates a graphical submit button. The value of the src attribute specifies the URI of
the image that will decorate the button. For accessibility reasons, authors should provide
alternate text for the image via the alt attribute.

When a pointing device is used to click on the image, the form is submitted and the
click coordinates passed to the server. The x value is measured in pixels from the left of the
image, and the y value in pixels from the top of the image. The submitted data includes
name.x=x-value and name.y=y-value where “name” is the value of the name attribute, and
x-value and y-value are the x and y coordinate values, respectively.

If the server takes different actions depending on the location clicked, users of non-
graphical browsers will be disadvantaged. For this reason, authors should consider alternate
approaches:

 Use multiple submit buttons (each with its own image) in place of a single graphical
submit button. Authors may use style sheets to control the positioning of these
buttons.

 Use a client-side image map together with scripting.

reset

Creates a reset button.

button

Creates a push button. User agents should use the value of the value attribute as the
button’s label.

hidden

Creates a hidden control.


file
Creates a file select control. User agents may use the value of the value attribute as
the initial file name.

110 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Examples of forms containing INPUT controls


NOTES
The following sample HTML fragment defines a simple form that allows the user to
enter a first name, last name, email address, and gender. When the submit button is
activated, the form will be sent to the program specified by the action attribute.

<FORM action=”http://somesite.com/prog/adduser” method=”post”>


<P>
First name: <INPUT type=”text” name=”firstname”><BR>
Last name: <INPUT type=”text” name=”lastname”><BR>
email: <INPUT type=”text” name=”email”><BR>
<INPUT type=”radio” name=”sex” value=”Male”> Male<BR>
INPUT type=”radio” name=”sex” value=”Female”> Female<BR>
<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>
</P> </FORM>

111 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES
<HEAD>

<META http-equiv=”Content-Script-Type” content=”text/javascript”>

</HEAD>

<BODY>

<FORM action=”...” method=”post”>

<P>

<INPUT type=”button” value=”Click Me” onclick=”verify()”>

</FORM>

</BODY>

Please consult the section on intrinsic events for more information about scripting and
events.

The following example shows how the contents of a user-specified file may be submitted
with a form. The user is prompted for his or her name and a list of file names whose
contents should be submitted with the form. By specifying the enctype value of “multipart/
form-data”, each file’s contents will be packaged for submission in a separate section of a
multipart document.

<FORM action=”http://server.dom/cgi/handle”

enctype=”multipart/form-data”

method=”post”>

<P>

What is your name? <INPUT type=”text” name=”name_of_sender”>

What files are you sending? <INPUT type=”file” name=”name_of_files”>

</P>

</FORM>

112 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

2.7 THE BUTTON ELEMENT


NOTES
<!ELEMENT BUTTON - -
(%flow;)* -(A|%formctrl;|FORM|FIELDSET)
— push button —>
<!ATTLIST BUTTON
%attrs; — %coreattrs, %i18n, %events —
name CDATA #IMPLIED
value CDATA #IMPLIED — sent to server when submitted —
type (button|submit|reset) submit — for use as form button —
disabled (disabled) #IMPLIED — unavailable in this context —
tabindex NUMBER #IMPLIED — position in tabbing order —
accesskey %Character; #IMPLIED — accessibility key character —
onfocus %Script; #IMPLIED — the element got the focus —
onblur %Script; #IMPLIED — the element lost the focus —
>
Start tag: required, End tag: required
Attribute definitions
name = cdata [CI]
This attribute assigns the control name.
value = cdata [CS]
This attribute assigns the initial value to the button.
type = submit|button|reset [CI]

This attribute declares the type of the button. Possible values:

 submit: Creates a submit button. This is the default value.


 reset: Creates a reset button.
 button: Creates a push button.
 Attributes defined elsewhere
 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 disabled (disabled input controls)
 accesskey (access keys)
 tabindex (tabbing navigation)
 onfocus, onblur, onclick, ondblclick, onmousedown, onmouseup, onmouseover,
onmousemove, onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

113 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Buttons created with the BUTTON element function just like buttons created with the
NOTES INPUT element, but they offer richer rendering possibilities: the BUTTON element may
have content. For example, a BUTTON element that contains an image functions like and
may resemble an INPUT element whose type is set to “image”, but the BUTTON element
type allows content.

Visual user agents may render BUTTON buttons with relief and an up/down motion
when clicked, while they may render INPUT buttons as “flat” images.

The following example expands a previous example, but creates submit and reset
buttons with BUTTON instead of INPUT. The buttons contain images by way of the IMG
element.
<FORM action=”http://somesite.com/prog/adduser” method=”post”>
<P>
First name: <INPUT type=”text” name=”firstname”><BR>
Last name: <INPUT type=”text” name=”lastname”><BR>
email: <INPUT type=”text” name=”email”><BR>
<INPUT type=”radio” name=”sex” value=”Male”> Male<BR>
<INPUT type=”radio” name=”sex” value=”Female”> Female<BR>
<BUTTON name=”submit” value=”submit” type=”submit”>
Send<IMG src=”/icons/wow.gif” alt=”wow”></BUTTON>
<BUTTON name=”reset” type=”reset”>
Reset<IMG src=”/icons/oops.gif” alt=”oops”></BUTTON>
</P>
</FORM>

Recall that authors must provide alternate text for an IMG element.

It is illegal to associate an image map with an IMG that appears as the contents of a
BUTTON element.

ILLEGAL EXAMPLE
The following is not legal HTML.
<BUTTON>
<IMG src=”foo.gif” usemap=”...”>
</BUTTON>

114 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

2.8 THE SELECT, OPTGROUP, AND OPTION ELEMENTS


NOTES
<!ELEMENT SELECT - - (OPTGROUP|OPTION)+ — option selector —>
ATTLIST SELECT
%attrs; — %coreattrs, %i18n, %events —
name CDATA #IMPLIED — field name —
size NUMBER #IMPLIED — rows visible —
multiple (multiple) #IMPLIED — default is single selection —
disabled (disabled) #IMPLIED — unavailable in this context —
tabindex NUMBER #IMPLIED — position in tabbing order —
onfocus %Script; #IMPLIED — the element got the focus —
onblur %Script; #IMPLIED — the element lost the focus —
onchange %Script; #IMPLIED — the element value was changed —
>

Start tag: required, End tag: required

SELECT Attribute definitions

name = cdata [CI]

This attribute assigns the control name.

size = number [CN]

If a SELECT element is presented as a scrolled list box, this attribute specifies the
number of rows in the list that should be visible at the same time. Visual user agents are not
required to present a SELECT element as a list box; they may use any other mechanism,
such as a drop-down menu.

multiple [CI]

If set, this boolean attribute allows multiple selections. If not set, the SELECT element
only permits single selections.

115 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Attributes defined elsewhere


NOTES
 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 disabled (disabled input controls)
 tabindex (tabbing navigation)
 onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove,
onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

The SELECT element creates a menu. Each choice offered by the menu is represented
by an OPTION element. A SELECT element must contain at least one OPTION element.

The OPTGROUP element allows authors to group choices logically. This is particularly
helpful when the user must choose from a long list of options; groups of related choices are
easier to grasp and remember than a single long list of options. In HTML 4, all OPTGROUP
elements must be specified directly within a SELECT element (i.e., groups may not be
nested).

Pre-selected options

Zero or more choices may be pre-selected for the user. User agents should determine
which choices are pre-selected as follows:

If no OPTION element has the selected attribute set, user agent behavior for
choosing which option is initially selected is undefined. Note. Since existing
implementations handle this case differently, the current specification differs from
RFC 1866 ([RFC1866] section 8.1.3), which states:

The initial state has the first option selected, unless a SELECTED attribute is present
on any of the <OPTION> elements.

Since user agent behavior differs, authors should ensure that each menu includes a
default pre-selected OPTION.
 If one OPTION element has the selected attribute set, it should be pre-selected.
 If the SELECT element has the multiple attribute set and more than one OPTION
element has the selected attribute set, they should all be pre-selected.
 It is considered an error if more than one OPTION element has the selected attribute
set and the SELECT element does not have the multiple attribute set. User agents
may vary in how they handle this error, but should not pre-select more than one
choice.

116 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

<!ELEMENT OPTGROUP - - (OPTION)+ — option group —>


NOTES
<!ATTLIST OPTGROUP

%attrs; — %coreattrs, %i18n, %events —

disabled (disabled) #IMPLIED — unavailable in this context —

label %Text; #REQUIRED — for use in hierarchical menus —

>

Start tag: required, End tag: required

OPTGROUP Attribute definitions

label = text [CS]

This attribute specifies the label for the option group.

Attributes defined elsewhere

 id, class (document-wide identifiers)


 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 disabled (disabled input controls)
 onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove,
onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

Note. Implementors are advised that future versions of HTML may extend the grouping
mechanism to allow for nested groups (i.e., OPTGROUP elements may nest). This will
allow authors to represent a richer hierarchy of choices.
<!ELEMENT OPTION - O (#PCDATA) — selectable choice —>
<!ATTLIST OPTION
%attrs; — %coreattrs, %i18n, %events —
selected (selected) #IMPLIED
disabled (disabled) #IMPLIED — unavailable in this context —
label %Text; #IMPLIED — for use in hierarchical menus —
value CDATA #IMPLIED — defaults to element content —
>

117 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Start tag: required, End tag: optional


NOTES
OPTION Attribute definitions

selected [CI]

When set, this boolean attribute specifies that this option is pre-selected.

value = cdata [CS]

This attribute specifies the initial value of the control. If this attribute is not set, the
initial value is set to the contents of the OPTION element.

label = text [CS]

This attribute allows authors to specify a shorter label for an option than the content of
the OPTION element. When specified, user agents should use the value of this attribute
rather than the content of the OPTION element as the option label.
Attributes defined elsewhere
 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 disabled (disabled input controls)
 onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove,
onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

When rendering a menu choice, user agents should use the value of the label attribute
of the OPTION element as the choice. If this attribute is not specified, user agents should
use the contents of the OPTION element.

The label attribute of the OPTGROUP element specifies the label for a group of
choices.

In this example, we create a menu that allows the user to select which of seven software
components to install. The first and second components are pre-selected but may be
deselected by the user. The remaining components are not pre-selected. The size attribute
states that the menu should only have 4 rows even though the user may select from among
7 options. The other options should be made available through a scrolling mechanism.

118 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The SELECT is followed by submit and reset buttons.


NOTES
<FORM action=”http://somesite.com/prog/component-select” method=”post”>
<P>
<SELECT multiple size=”4" name=”component-select”>
<OPTION selected value=”Component_1_a”>Component_1</OPTION>
<OPTION selected value=”Component_1_b”>Component_2</OPTION>
<OPTION>Component_3</OPTION>
<OPTION>Component_4</OPTION>
<OPTION>Component_5</OPTION>
<OPTION>Component_6</OPTION>
<OPTION>Component_7</OPTION>
</SELECT>
<INPUT type=”submit” value=”Send”><INPUT type=”reset”>
</P>
</FORM>

Only selected options will be successful (using the control name “component-select”).
When no options are selected, the control is not successful and neither the name nor any
values are submitted to the server when the form is submitted. Note that where the value
attribute is set, it determines the control’s initial value, otherwise it’s the element’s contents.

In this example we use the OPTGROUP element to group choices. The following
markup:
<FORM action=”http://somesite.com/prog/someprog” method=”post”>
<P>
<SELECT name=”ComOS”>
<OPTION selected label=”none” value=”none”>None</OPTION>
<OPTGROUP label=”PortMaster 3">
<OPTION label=”3.7.1" value=”pm3_3.7.1">PortMaster 3 with ComOS
3.7.1</OPTION>
<OPTION label=”3.7" value=”pm3_3.7">PortMaster 3 with
ComOS 3.7</ OPTION>

119 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

<OPTION label=”3.5" value=”pm3_3.5">PortMaster 3 with ComOS 3.5</


NOTES OPTION>
</OPTGROUP>
<OPTGROUP label=”PortMaster 2">
<OPTION label=”3.7" value=”pm2_3.7">PortMaster 2 with ComOS 3.7</
OPTION>
<OPTION label=”3.5" value=”pm2_3.5">PortMaster 2 with ComOS 3.5</
OPTION>
</OPTGROUP>
<OPTGROUP label=”IRX”>
<OPTION label=”3.7R” value=”IRX_3.7R”>IRX with ComOS 3.7R</
OPTION>
<OPTION label=”3.5R” value=”IRX_3.5R”>IRX with ComOS 3.5R</
OPTION>
</OPTGROUP>
</SELECT>
</FORM>

represents the following grouping:


None
PortMaster 3
3.7.1
3.7
3.5
PortMaster 2
3.7
3.5
IRX
3.7R
3.5R

Visual user agents may allow users to select from option groups through a hierarchical
menu or some other mechanism that reflects the structure of choices.

120 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

A graphical user agent might render this as:


NOTES
This image shows a SELECT element rendered as cascading menus. The top label of
the menu displays the currently selected value (PortMaster 3, 3.7.1). The user has unfurled
two cascading menus, but has not yet selected the new value (PortMaster 2, 3.7). Note
that each cascading menu displays the label of an OPTGROUP or OPTION element.

The TEXTAREA element


<!ELEMENT TEXTAREA - - (#PCDATA) — multi-line text field —>
<!ATTLIST TEXTAREA
%attrs; — %coreattrs, %i18n, %events —
name CDATA #IMPLIED
rows NUMBER #REQUIRED
cols NUMBER #REQUIRED
disabled (disabled) #IMPLIED — unavailable in this context —
readonly (readonly) #IMPLIED
tabindex NUMBER #IMPLIED — position in tabbing order —
accesskey %Character; #IMPLIED — accessibility key character —
onfocus %Script; #IMPLIED — the element got the focus —
onblur %Script; #IMPLIED — the element lost the focus —
onselect %Script; #IMPLIED — some text was selected —
onchange %Script; #IMPLIED — the element value was changed —
>
<!ELEMENT TEXTAREA - - (#PCDATA) — multi-line text field —>
<!ATTLIST TEXTAREA
%attrs; — %coreattrs, %i18n, %events —
name CDATA #IMPLIED
rows NUMBER #REQUIRED
cols NUMBER #REQUIRED

121 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

disabled (disabled) #IMPLIED — unavailable in this context —


NOTES readonly (readonly) #IMPLIED
tabindex NUMBER #IMPLIED — position in tabbing order —
accesskey %Character; #IMPLIED — accessibility key character —
onfocus %Script; #IMPLIED — the element got the focus —
onblur %Script; #IMPLIED — the element lost the focus —
onselect %Script; #IMPLIED — some text was selected —
onchange %Script; #IMPLIED — the element value was changed —
>

name = cdata [CI]

This attribute assigns the control name.

rows = number [CN]

This attribute specifies the number of visible text lines. Users should be able to enter
more lines than this, so user agents should provide some means to scroll through the contents
of the control when the contents extend beyond the visible area.

cols = number [CN]

This attribute specifies the visible width in average character widths. Users should be
able to enter longer lines than this, so user agents should provide some means to scroll
through the contents of the control when the contents extend beyond the visible area. User
agents may wrap visible text lines to keep long lines visible without the need for scrolling.

Attributes defined elsewhere


 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 readonly (read-only input controls)
 disabled (disabled input controls)
 tabindex (tabbing navigation)
 onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown,
onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown,
onkeyup (intrinsic events)
The TEXTAREA element creates a multi-line text input control. User agents should
use the contents of this element as the initial value of the control and should render this text
initially.

122 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

This example creates a TEXTAREA control that is 20 rows by 80 columns and


contains two lines of text initially. The TEXTAREA is followed by submit and reset buttons. NOTES
<FORM action=”http://somesite.com/prog/text-read” method=”post”>
<P>
<TEXTAREA name=”thetext” rows=”20" cols=”80">
First line of initial text.
Second line of initial text.
</TEXTAREA>
<INPUT type=”submit” value=”Send”><INPUT type=”reset”>
</P>
</FORM>

Setting the readonly attribute allows authors to display unmodifiable text in a


TEXTAREA. This differs from using standard marked-up text in a document because the
value of TEXTAREA is submitted with the form.

The ISINDEX element

ISINDEX is deprecated. This element creates a single-line text input control. Authors
should use the INPUT element to create text input controls.

See the Transitional DTD for the formal definition.

Attribute definitions

prompt = text [CS]

Deprecated. This attribute specifies a prompt string for the input field.

Attributes defined elsewhere

 id, class (document-wide identifiers)


 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)

The ISINDEX element creates a single-line text input control that allows any number
of characters. User agents may use the value of the prompt attribute as a title for the
prompt.

123 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

DEPRECATED EXAMPLE:
NOTES
The following ISINDEX declaration:

<ISINDEX prompt=”Enter your search phrase: “>

could be rewritten with INPUT as follows:

<FORM action=”...” method=”post”>

<P>Enter your search phrase: <INPUT type=”text”></P>

</FORM>

Semantics of ISINDEX. Currently, the semantics for ISINDEX are only well-defined
when the base URI for the enclosing document is an HTTP URI. In practice, the input
string is restricted to Latin-1 as there is no mechanism for the URI to specify a different
character set.

Labels

Some form controls automatically have labels associated with them (press buttons)
while most do not (text fields, checkboxes and radio buttons, and menus).

For those controls that have implicit labels, user agents should use the value of the
value attribute as the label string.

The LABEL element is used to specify labels for controls that do not have implicit
labels,

The LABEL element

<!ELEMENT LABEL - - (%inline;)* -(LABEL) — form field label text —>


<!ATTLIST LABEL
%attrs; — %coreattrs, %i18n, %events —
for IDREF #IMPLIED — matches field ID value —
accesskey %Character; #IMPLIED — accessibility key character —
onfocus %Script; #IMPLIED — the element got the focus —
onblur %Script; #IMPLIED — the element lost the focus —
>

Start tag: required, End tag: required

Attribute definitions

for = idref [CS]

124 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

This attribute explicitly associates the label being defined with another control. When
present, the value of this attribute must be the same as the value of the id attribute of some NOTES
other control in the same document. When absent, the label being defined is associated
with the element’s contents.

Attributes defined elsewhere

 id, class (document-wide identifiers)


 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 accesskey (access keys)
 onfocus, onblur, onclick, ondblclick, onmousedown, onmouseup, onmouseover,
onmousemove, onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)

The LABEL element may be used to attach information to controls. Each LABEL
element is associated with exactly one form control.

The for attribute associates a label with another control explicitly: the value of the for
attribute must be the same as the value of the id attribute of the associated control element.
More than one LABEL may be associated with the same control by creating multiple
references via the for attribute.

This example creates a table that is used to align two text input controls and their
associated labels. Each label is associated explicitly with one text input:
<FORM action=”...” method=”post”>
<TABLE>
<TR>
<TD><LABEL for=”fname”>First Name</LABEL>
<TD><INPUT type=”text” name=”firstname” id=”fname”>
<TR>
<TD><LABEL for=”lname”>Last Name</LABEL>
<TD><INPUT type=”text” name=”lastname” id=”lname”>
</TABLE>
</FORM>

125 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

This example extends a previous example form to include LABEL elements.


NOTES
<FORM action=”http://somesite.com/prog/adduser” method=”post”>
<P>
<LABEL for=”firstname”>First name: </LABEL>
<INPUT type=”text” id=”firstname”><BR>
<LABEL for=”lastname”>Last name: </LABEL>
<INPUT type=”text” id=”lastname”><BR>
<LABEL for=”email”>email: </LABEL>
<INPUT type=”text” id=”email”><BR>
<INPUT type=”radio” name=”sex” value=”Male”> Male<BR>
<INPUT type=”radio” name=”sex” value=”Female”> Female<BR>
<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>
</P>
</FORM>

To associate a label with another control implicitly, the control element must be within
the contents of the LABEL element. In this case, the LABEL may only contain one control
element. The label itself may be positioned before or after the associated control.

In this example, we implicitly associate two labels with two text input controls:
<FORM action=”...” method=”post”>
<P>
<LABEL>
First Name
<INPUT type=”text” name=”firstname”>
</LABEL>
<LABEL>
<INPUT type=”text” name=”lastname”>
Last Name
</LABEL>
</P>

</FORM>

126 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Note that this technique cannot be used when a table is being used for layout, with the
label in one cell and its associated control in another cell. NOTES
When a LABEL element receives focus, it passes the focus on to its associated
control. See the section below on access keys for examples.

Labels may be rendered by user agents in a number of ways (e.g., visually, read by
speech synthesizers, etc.)

Adding structure to forms: the FIELDSET and LEGEND elements


<!—
#PCDATA is to solve the mixed content problem,
per specification only whitespace is allowed there!
—>
<!ELEMENT FIELDSET - - (#PCDATA,LEGEND,(%flow;)*)
— form control group —>
<!ATTLIST FIELDSET
%attrs; — %coreattrs, %i18n, %events —
>
<!ELEMENT LEGEND - - (%inline;)* — fieldset legend —>
<!ATTLIST LEGEND
%attrs; — %coreattrs, %i18n, %events —
accesskey %Character; #IMPLIED — accessibility key character —
>

Start tag: required, End tag: required

LEGEND Attribute definitions

align = top|bottom|left|right [CI]

Deprecated. This attribute specifies the position of the legend with respect to the
fieldset. Possible values:

 top: The legend is at the top of the fieldset. This is the default value.
 bottom: The legend is at the bottom of the fieldset.
 left: The legend is at the left side of the fieldset.
 right: The legend is at the right side of the fieldset.

127 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Attributes defined elsewhere


NOTES
 id, class (document-wide identifiers)
 lang (language information), dir (text direction)
 title (element title)
 style (inline style information)
 accesskey (access keys)
 onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove,
onmouseout, onkeypress, onkeydown, onkeyup (intrinsic events)
The FIELDSET element allows authors to group thematically related controls and
labels. Grouping controls makes it easier for users to understand their purpose while
simultaneously facilitating tabbing navigation for visual user agents and speech navigation
for speech-oriented user agents. The proper use of this element makes documents more
accessible.

The LEGEND element allows authors to assign a caption to a FIELDSET. The legend
improves accessibility when the FIELDSET is rendered non-visually.

In this example, we create a form that one might fill out at the doctor’s office. It is
divided into three sections: personal information, medical history, and current medication.
Each section contains controls for inputting the appropriate information.
<FORM action=”...” method=”post”>
<P>
<FIELDSET>
<LEGEND>Personal Information</LEGEND>
Last Name: <INPUT name=”personal_lastname” type=”text” tabindex=”1">
First Name: <INPUT name=”personal_firstname” type=”text” tabindex=”2">
Address: <INPUT name=”personal_address” type=”text” tabindex=”3">
...more personal information...
</FIELDSET>
<FIELDSET>
<LEGEND>Medical History</LEGEND>

<INPUT name=”history_illness”
type=”checkbox”
value=”Smallpox” tabindex=”20"> Smallpox
<INPUT name=”history_illness”

128 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

type=”checkbox”
NOTES
value=”Mumps” tabindex=”21"> Mumps
<INPUT name=”history_illness”
type=”checkbox”
value=”Dizziness” tabindex=”22"> Dizziness
<INPUT name=”history_illness”
type=”checkbox”
value=”Sneezing” tabindex=”23"> Sneezing
...more medical history...
</FIELDSET>
<FIELDSET>
<LEGEND>Current Medication</LEGEND>
Are you currently taking any medication?
<INPUT name=”medication_now”
type=”radio”
value=”Yes” tabindex=”35">Yes
<INPUT name=”medication_now”
type=”radio”
value=”No” tabindex=”35">No
If you are currently taking medication, please indicate
it in the space below:
<TEXTAREA name=”current_medication”
rows=”20" cols=”50"
tabindex=”40">
</TEXTAREA>
</FIELDSET>

</FORM>

Note that in this example, we might improve the visual presentation of the form by
aligning elements within each FIELDSET (with style sheets), adding color and font
information (with style sheets), adding scripting (say, to only open the “current medication”
text area if the user indicates he or she is currently on medication), etc.

129 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Giving focus to an element


NOTES
In an HTML document, an element must receive focus from the user in order to
become active and perform its tasks. For example, users must activate a link specified by
the A element in order to follow the specified link. Similarly, users must give a TEXTAREA
focus in order to enter text into it.

There are several ways to give focus to an element:


 Designate the element with a pointing device.
 Navigate from one element to the next with the keyboard. The document’s author
may define a tabbing order that specifies the order in which elements will receive
focus if the user navigates the document with the keyboard (see tabbing navigation).
Once selected, an element may be activated by some other key sequence.
 Select an element through an access key (sometimes called “keyboard shortcut”
or “keyboard accelerator”).

Tabbing navigation

Attribute definitions

tabindex = number [CN]

This attribute specifies the position of the current element in the tabbing order for the
current document. This value must be a number between 0 and 32767. User agents should
ignore leading zeros.

The tabbing order defines the order in which elements will receive focus when navigated
by the user via the keyboard. The tabbing order may include elements nested within other
elements.

Elements that may receive focus should be navigated by user agents according to the
following rules:

1. Those elements that support the tabindex attribute and assign a positive value to it
are navigated first. Navigation proceeds from the element with the lowest tabindex
value to the element with the highest value. Values need not be sequential nor must
they begin with any particular value. Elements that have identical tabindex values
should be navigated in the order they appear in the character stream.
2. Those elements that do not support the tabindex attribute or support it and assign
it a value of “0” are navigated next. These elements are navigated in the order they
appear in the character stream.
3. Elements that are disabled do not participate in the tabbing order.

130 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The following elements support the tabindex attribute: A, AREA, BUTTON, INPUT,
OBJECT, SELECT, and TEXTAREA. NOTES
In this example, the tabbing order will be the BUTTON, the INPUT elements in
order (note that “field1” and the button share the same tabindex, but “field1” appears later
in the character stream), and finally the link created by the A element.
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>
<HTML>
<HEAD>
<TITLE>A document with FORM</TITLE>
</HEAD>
<BODY>
...some text...
<P>Go to the
<A tabindex=”10" href=”http://www.w3.org/”>W3C Web site.</A>
...some more...
<BUTTON type=”button” name=”get-database”
tabindex=”1" onclick=”get-database”>
Get the current database.
</BUTTON>
...some more...
<FORM action=”...” method=”post”>
<P>
<INPUT tabindex=”1" type=”text” name=”field1">
<INPUT tabindex=”2" type=”text” name=”field2">
<INPUT tabindex=”3" type=”submit” name=”submit”>
</P>
</FORM>
</BODY>
</HTML>

131 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Tabbing keys. The actual key sequence that causes tabbing navigation or element
NOTES activation depends on the configuration of the user agent (e.g., the “tab” key is used for
navigation and the “enter” key is used to activate a selected element).

User agents may also define key sequences to navigate the tabbing order in reverse.
When the end (or beginning) of the tabbing order is reached, user agents may circle back
to the beginning (or end).

Access keys

Attribute definitions

accesskey = character [CN]

This attribute assigns an access key to an element. An access key is a single character
from the document character set. Note. Authors should consider the input method of the
expected reader when specifying an accesskey.

Pressing an access key assigned to an element gives focus to the element. The action
that occurs when an element receives focus depends on the element. For example, when a
user activates a link defined by the A element, the user agent generally follows the link.
When a user activates a radio button, the user agent changes the value of the radio button.
When the user activates a text field, it allows input, etc.

The following elements support the accesskey attribute: A, AREA, BUTTON, INPUT,
LABEL, and LEGEND, and TEXTAREA.

This example assigns the access key “U” to a label associated with an INPUT control.
Typing the access key gives focus to the label which in turn gives it to the associated
control. The user may then enter text into the INPUT area.

<FORM action=”...” method=”post”>


<P>
<LABEL for=”fuser” accesskey=”U”>
User Name
</LABEL>
<INPUT type=”text” name=”user” id=”fuser”>
</P>
</FORM>

In this example, we assign an access key to a link defined by the A element. Typing
this access key takes the user to another document, in this case, a table of contents.

132 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

<P><A accesskey=”C”
NOTES
rel=”contents”

href=”http://someplace.com/specification/contents.html”>

Table of Contents</A>

The invocation of access keys depends on the underlying system. For instance, on
machines running MS Windows, one generally has to press the “alt” key in addition to the
access key. On Apple systems, one generally has to press the “cmd” key in addition to the
access key.

The rendering of access keys depends on the user agent. We recommend that authors
include the access key in label text or wherever the access key is to apply. User agents
should render the value of an access key in such a way as to emphasize its role and to
distinguish it from other characters (e.g., by underlining it).

Disabled and read-only controls

In contexts where user input is either undesirable or irrelevant, it is important to be


able to disable a control or render it read-only. For example, one may want to disable a
form’s submit button until the user has entered some required data. Similarly, an author
may want to include a piece of read-only text that must be submitted as a value along with
the form. The following sections describe disabled and read-only controls.

Disabled controls

Attribute definitions

disabled [CI]

When set for a form control, this boolean attribute disables the control for user input.

When set, the disabled attribute has the following effects on an element:
 Disabled controls do not receive focus.
 Disabled controls are skipped in tabbing navigation.
 Disabled controls cannot be successful.

The following elements support the disabled attribute: BUTTON, INPUT,


OPTGROUP, OPTION, SELECT, and TEXTAREA.

This attribute is inherited but local declarations override the inherited value.

How disabled elements are rendered depends on the user agent. For example, some
user agents “gray out” disabled menu items, button labels, etc.

133 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

In this example, the INPUT element is disabled. Therefore, it cannot receive user
NOTES input nor will its value be submitted with the form.

<INPUT disabled name=”fred” value=”stone”>

Note. The only way to modify dynamically the value of the disabled attribute is through
a script.

Read-only controls

Attribute definitions

readonly

When set for a form control, this boolean attribute prohibits changes to the control.

The readonly attribute specifies whether the control may be modified by the user.

When set, the readonly attribute has the following effects on an element:
 Read-only elements receive focus but cannot be modified by the user.
 Read-only elements are included in tabbing navigation.
 Read-only elements may be successful.

The following elements support the readonly attribute: INPUT and TEXTAREA.

How read-only elements are rendered depends on the user agent.

Note. The only way to modify dynamically the value of the readonly attribute is through
a script.

Form submission

The following sections explain how user agents submit form data to form processing
agents.

Form submission method

The method attribute of the FORM element specifies the HTTP method used to send
the form to the processing agent. This attribute may take two values:
 get: With the HTTP “get” method, the form data set is appended to the URI specified
by the action attribute (with a question-mark (“?”) as separator) and this new URI
is sent to the processing agent.
 post: With the HTTP “post” method, the form data set is included in the body of
the form and sent to the processing agent.
 The “get” method should be used when the form is idempotent (i.e., causes no
side-effects). Many database searches have no visible side-effects and make ideal
applications for the “get” method.

134 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

If the service associated with the processing of a form causes side effects (for example,
if the form modifies a database or subscription to a service), the “post” method should be NOTES
used.

Note. The “get” method restricts form data set values to ASCII characters. Only the
“post” method (with enctype=”multipart/form-data”) is specified to cover the entire
[ISO10646] character set.

Successful controls

A successful control is “valid” for submission. Every successful control has its control
name paired with its current value as part of the submitted form data set. A successful
control must be defined within a FORM element and must have a control name.

However
 Controls that are disabled cannot be successful.

 If a form contains more than one submit button, only the activated submit
button is successful.

 All “on” checkboxes may be successful.

 For radio buttons that share the same value of the name attribute, only the
“on” radio button may be successful.

 For menus, the control name is provided by a SELECT element and values
are provided by OPTION elements. Only selected options may be successful.
When no options are selected, the control is not successful and neither the
name nor any values are submitted to the server when the form is submitted.

 The current value of a file select is a list of one or more file names. Upon
submission of the form, the contents of each file are submitted with the rest of
the form data. The file contents are packaged according to the form’s content
type.

 The current value of an object control is determined by the object’s


implementation.

If a control doesn’t have a current value when the form is submitted, user agents are
not required to treat it as a successful control.

Furthermore, user agents should not consider the following controls successful:

 Reset buttons.

 OBJECT elements whose declare attribute has been set.

135 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Hidden controls and controls that are not rendered because of style sheet settings
NOTES may still be successful. For example:
<FORM action=”...” method=”post”>
<P>
<INPUT type=”password” style=”display:none”
name=”invisible-password”
value=”mypassword”>
</FORM>

will still cause a value to be paired with the name “invisible-password” and submitted
with the form.

Processing form data

When the user submits a form (e.g., by activating a submit button), the user agent
processes it as follows.

Step one: Identify the successful controls

Step two: Build a form data set

A form data set is a sequence of control-name/current-value pairs constructed from


successful controls

Step three: Encode the form data set

The form data set is then encoded according to the content type specified by the
enctype attribute of the FORM element.

Step four: Submit the encoded form data set

Finally, the encoded data is sent to the processing agent designated by the action
attribute using the protocol specified by the method attribute.

This specification does not specify all valid submission methods or content types that
may be used with forms. However, HTML 4 user agents must support the established
conventions in the following cases:

 If the method is “get” and the action is an HTTP URI, the user agent takes the
value of action, appends a `?’ to it, then appends the form data set, encoded using
the “application/x-www-form-urlencoded” content type. The user agent then
traverses the link to this URI. In this scenario, form data are restricted to ASCII
codes.

136 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 If the method is “post” and the action is an HTTP URI, the user agent conducts an
HTTP “post” transaction using the value of the action attribute and a message NOTES
created according to the content type specified by the enctype attribute.

For any other value of action or method, behavior is unspecified.

User agents should render the response from the HTTP “get” and “post” transactions.

Form content types

The enctype attribute of the FORM element specifies the content type used to encode
the form data set for submission to the server. User agents must support the content types
listed below. Behavior for other content types is unspecified.

Please also consult the section on escaping ampersands in URI attribute


values.application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type must be
encoded as follows:

1. Control names and values are escaped. Space characters are replaced by ‘+’,
and then reserved characters are escaped as described in [RFC1738], section
2.2: Non-alphanumeric characters are replaced by ‘%HH’, a percent sign and
two hexadecimal digits representing the ASCII code of the character. Line breaks
are represented as “CR LF” pairs (i.e., ‘%0D%0A’).

2. The control names/values are listed in the order they appear in the document. The
name is separated from the value by ‘=’ and name/value pairs are separated from
each other by ‘&’.

multipart/form-data

Note. Please consult [RFC2388] for additional information about file uploads, including
backwards compatibility issues, the relationship between “multipart/form-data” and other
content types, performance issues, etc.

Please consult the appendix for information about security issues for forms.

The content type “application/x-www-form-urlencoded” is inefficient for sending large


quantities of binary data or text containing non-ASCII characters. The content type “multipart/
form-data” should be used for submitting forms that contain files, non-ASCII data, and
binary data.

The content “multipart/form-data” follows the rules of all multipart MIME data streams
as outlined in [RFC2045]. The definition of “multipart/form-data” is available at the [IANA]
registry.

137 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

A “multipart/form-data” message contains a series of parts, each representing a


NOTES successful control. The parts are sent to the processing agent in the same order the
corresponding controls appear in the document stream. Part boundaries should not occur
in any of the data; how this is done lies outside the scope of this specification.

As with all multipart MIME types, each part has an optional “Content-Type” header
that defaults to “text/plain”. User agents should supply the “Content-Type” header,
accompanied by a “charset” parameter.

Each part is expected to contain:

1. a “Content-Disposition” header whose value is “form-data”.

2. a name attribute specifying the control name of the corresponding control. Control
names originally encoded in non-ASCII character sets may be encoded using the
method outlined in [RFC2045].

Thus, for example, for a control named “mycontrol”, the corresponding part would
be specified:

Content-Disposition: form-data; name=”mycontrol”

As with all MIME transmissions, “CR LF” (i.e., `%0D%0A’) is used to separate lines
of data.

Each part may be encoded and the “Content-Transfer-Encoding” header supplied if


the value of that part does not conform to the default (7BIT) encoding (see [RFC2045],
section 6)

If the contents of a file are submitted with a form, the file input should be identified by
the appropriate content type (e.g., “application/octet-stream”). If multiple files are to be
returned as the result of a single form entry, they should be returned as “multipart/mixed”
embedded within the “multipart/form-data”.

The user agent should attempt to supply a file name for each submitted file. The file
name may be specified with the “filename” parameter of the ‘Content-Disposition: form-
data’ header, or, in the case of multiple files, in a ‘Content-Disposition: file’ header of the
subpart. If the file name of the client’s operating system is not in US-ASCII, the file name
might be approximated or encoded using the method of [RFC2045]. This is convenient for
those cases where, for example, the uploaded files might contain references to each other
(e.g., a TeX file and its “.sty” auxiliary style description).

The following example illustrates “multipart/form-data” encoding. Suppose we have


the following form:

138 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
<FORM action=”http://server.com/cgi/handle”

enctype=”multipart/form-data”

method=”post”>

<P>

What is your name? <INPUT type=”text” name=”submit-name”><BR>

What files are you sending? <INPUT type=”file” name=”files”><BR>

<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>

</FORM>

If the user enters “Larry” in the text input, and selects the text file “file1.txt”, the user
agent might send back the following data:

Content-Type: multipart/form-data; boundary=AaB03x

—AaB03x

Content-Disposition: form-data; name=”submit-name”

Larry

—AaB03x

Content-Disposition: form-data; name=”files”; filename=”file1.txt”

Content-Type: text/plain

... contents of file1.txt ...

—AaB03x—

If the user selected a second (image) file “file2.gif”, the user agent might construct the
parts as follows:

Content-Type: multipart/form-data; boundary=AaB03x

—AaB03x

Content-Disposition: form-data; name=”submit-name”

Larry

—AaB03x

139 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Content-Disposition: form-data; name=”files”


NOTES
Content-Type: multipart/mixed; boundary=BbC04y

—BbC04y

Content-Disposition: file; filename=”file1.txt”

Content-Type: text/plain

... contents of file1.txt ...

—BbC04y

Content-Disposition: file; filename=”file2.gif”

Content-Type: image/gif

Content-Transfer-Encoding: binary

...contents of file2.gif...

—BbC04y—

—AaB03x—

Interactive Web Sites

The Internet is an interactive medium and successful websites are those that are built
on a foundation of interactivity. At NetLink India, we build sites that promote communications
through enhanced interactivity on your website. The websites we build open two-way
communications between your company and your users.

Throughout our website, we have demonstrated many different types of innovative


interactive elements of web design software to show that interaction can be more than just
filling out forms. Your company’s website could provide sophisticated entertainment like
games that match users against each other. Perhaps, your site can offer mini-spreadsheets
preconfigured with the latest mortgage or interest rate info so that users can try various
scenarios and see the benefits of doing business with your company. The possiblities are
endless, because at NetLink India, we offer revolutionary new ways of creating interactivity.

2.9 FORMS PROCESSING

As the Internet’s first real feedback mechanism, forms processing is still the most
widely used interactivity on websites. But as you’ve seen from our web design work so far,
we take the common and the ordinary and expand it into the extraordinary. Combined with
“smart” applications, your forms can have dynamic instant help pop-ups, have forms

140 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

checked for inconsistencies prior to submission and have forms that dynamically change
options based on other selections. NOTES
Common Gateway Interface (CGI) Scripting

One of the most powerful interactive elements on the Internet happens not on your
website, but on the server. CGI scripts account for probably over 90% of all interaction on
the Internet. Our skilled programmers can build sleek, highly efficient CGI programs and
combine them with “smart” embedded technologies like SmartSite to build high
performance interactive services! With CGI, your website can dynamically generate web
pages, embedded smart applications and even animation, all specific to a particular user.
Now that’s getting personal, and it’s the kingpin to generating loyalty with users.

Interactivity is the cornerstone of every strategy aimed at attracting, keeping and


motivating users to act. Yet, few designers consider user interaction as an integral part of
any corporate web site design. The cornerstone of our design strategy starts with interaction.
It’s here throughout our website and it’s on every website we design.

Interactive Websites

Interactive websites

By using:
 video
 audio
 javascript
 flash
 ajax

You can add user interactions such as to create rich learning experiences:
 quizzes
 dialog boxes
 list sort
 drag drop
 show hide
 tabbed content
 text input
 software demos
 interactive simulations

141 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Contact Helen Rysavy x 7779


NOTES
Technologies
Type Description
JavaScript is a client side language that runs on the client browser
and can be easily used to interact with HTML elements.
JAVASCRIPT

Asynchronous Javascript and XML - Ajax is a way of short-


ciruciting browser reloading. All it does is make a super secred
JAX
background HTTP request to your web application. Then you can
use JavaScript todisplay the results, if you want.
Flash is the industry's most advanced authoring environment for
FLASH creating interactive websites, digital experiences and mobile
content.
Captivate makes it easy to quickly create interactive simulations
and software demonstrations. No programming or multimedia
CAPTIVATE skills are required, content can be made engaging with
interactivity, audio and video, and the software integrates easily
with other Adobe products and eLearning applications.
Web 2.0 is about a rich user experience, community participated
content, quick turnaround on new features, "perpetual beta",
WEB 2.0
standard data interchange and instant gratification.
View a definition of web 2.0

142 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Web 2.0 meme map


NOTES

Tim O’Reilly’s Web 2.0 meme map

143 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Data Base Driven Web Sites


NOTES
One of the most common types of dynamic web pages is the database driven type.
This means that you have a web page that grabs information from a database (the web
page is connected to the database by programming,) and inserts that information into the
web page each time it is loaded.

If the information stored in the database changes, the web page connected to the
database will also change accordingly (and automatically,) without human intervention.

This is commonly seen on online banking sites where you can log in (by entering your
user name and password) and check out your bank account balance. Your bank account
information is stored in a database and has been connected to the web page with programming
thus enabling you to see your banking information.

Imagine if the web page holding your banking information had to be built traditionally
(that is by hand,) every time your bank balance changed! Even a thousand monkeys working
24/7 drinking 5 cups of coffee a day, would not be able to keep up!

Hopefully you are starting to see why you would want a database driven site: you
would want it if your information changes very often, just like in a banking site.

Database driven sites can be built using several competing technologies, each with it’s
own advantages. Some of those technologies/tools include:
PHP
JSP
ASP
PERL
Cold Fusion

I will be writing about these technologies in future articles and newsletters. You now
have them listed here for you to research on your own.

Database driven web site programming can also be called (or characterized as): ‘server
side programming’.

The reason it is so called is because the ‘action’ or magic that allows the web pages to
connect to the database is actually taking place on the server.

This is what happens: each time a dynamic web page is about to be sent to the
browser, the server automatically builds the page and sends a standard HTML page to the
browser.

The server ‘knows’ how to build the page by following the instructions provided by
the programmer. This is different from say JavaScript (think drop down menus or alert
boxes) that runs strictly in the web browser.
144 ANNA UNIVERSITY CHENNAI
INTERNET PROGRAMMING / WEB PROGRAMMING

At this point many people are getting very confused, the confusion lies in the difference
between server side programming (database driven web pages) versus client side NOTES
programming (JavaScript).

CLIENT SIDE PROGRAMMMING - The other type of dynamic web page.

Client side (that is to say: in the browser) or what is commonly called DHTML ...
dynamic HTML.

DHTML is basically taking HTML and JavaScript (sometimes VB script) to make


the web page change it’s own content (as far as the viewer is concerned) without having to
reload or load a new page.

Examples of DHTML would include drop down menu’s, ‘floating’ images that hover
over the rest of the page etc ... if you look around, you will find plenty on the web.

Note: The name DHTML has been replaced by the term ‘DOM SCRIPTING’. It’s
pretty much the same stuff (programming in the browser to make things happen dynamically,)
but DHTML has a little bit of a bad wrap from the late 90’s, so more people now refer to
it as ‘DOM scripting.’

Hopefully now you have basic conceptual understanding of dynamic web sites,
DHTML and database driven web sites. I tried to present the information in a non-geek
way so as to not confuse.

The downside of this simple approach is that I am not being 100% precise ... some
geeks out there may point out one or two items that are not dead on. But don’t worry, my
points are not in any way wrong.

2.10 FILE TRANSFER PROTOCOL

FTP or File Transfer Protocol is used to transfer data from one computer to another
over the Internet, or through a network. Specifically, FTP is a commonly used protocol for
exchanging files over any TCP/IP based network to manipulate files on another computer
on that network regardless of which operating systems are involved (if the computers
permit FTP access). There are many existing FTP client and server programs. FTP servers
can be set up anywhere between game servers, voice servers, internet hosts, and other
physical servers.

Connection Methods

FTP runs exclusively over TCP. FTP servers by default listen on port 21 for incoming
connections from FTP clients. A connection to this port from the FTP Client forms the
control stream on which commands are passed to the FTP server from the FTP client and
on occasion from the FTP server to the FTP client. FTP uses out-of-band control, which
means it uses a separate connection for control and data. Thus, for the actual file transfer

145 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

to take place, a different connection is required which is called the data stream. Depending
NOTES on the transfer mode, the process of setting up the data stream is different.

In active mode, the FTP client opens a random port (> 1023), sends the FTP server
the random port number on which it is listening over the control stream and waits for a
connection from the FTP server. When the FTP server initiates the data connection to the
FTP client it binds the source port to port 20 on the FTP server.

In order to use active mode, the client sends a PORT command, with the IP and port
as argument. The format for the IP and port is “h1,h2,h3,h4,p1,p2”. Each field is a decimal
representation of 8 bits of the host IP, followed by the chosen data port. For example, a
client with an IP of 192.168.0.1, listening on port 1025 for the data connection will send
the command “PORT 192,168,0,1,4,1”. The port fields should be interpreted as p1×256
+ p2 = port, or, in this example, 4×256 + 1 = 1025.

In passive mode, the FTP server opens a random port (> 1023), sends the FTP client
the server’s IP address to connect to and the port on which it is listening (a 16 bit value
broken into a high and low byte, like explained before) over the control stream and waits
for a connection from the FTP client. In this case the FTP client binds the source port of the
connection to a random port greater than 1023.

To use passive mode, the client sends the PASV command to which the server would
reply with something similar to “227 Entering Passive Mode (127,0,0,1,78,52)”. The syntax
of the IP address and port are the same as for the argument to the PORT command.

In extended passive mode, the FTP server operates exactly the same as passive
mode, however it only transmits the port number (not broken into high and low bytes) and
the client is to assume that it connects to the same IP address that was originally connected
to. Extended passive mode was added by RFC 2428 in September 1998.

While data is being transferred via the data stream, the control stream sits idle. This
can cause problems with large data transfers through firewalls which time out sessions after
lengthy periods of idleness. While the file may well be successfully transferred, the control
session can be disconnected by the firewall, causing an error to be generated.

The FTP protocol supports resuming of interrupted downloads using the REST
command. The client passes the number of bytes it has already received as argument to the
REST command and restarts the transfer. In some commandline clients for example, there
is an often-ignored but valuable command, “reget” (meaning “get again”) that will cause an
interrupted “get” command to be continued, hopefully to completion, after a communications
interruption.

Resuming uploads is not as easy. Although the FTP protocol supports the APPE
command to append data to a file on the server, the client does not know the exact position

146 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

at which a transfer got interrupted. It has to obtain the size of the file some other way, for
example over a directory listing or using the SIZE command. NOTES
In ASCII mode (see below), resuming transfers can be troublesome if client and
server use different end of line characters.

The objectives of FTP, as outlined by its RFC, are:


 To promote sharing of files (computer programs and/or data).

 To encourage indirect or implicit use of remote computers.

 To shield a user from variations in file storage systems among different hosts.

 To transfer data reliably, and efficiently.

 Criticisms of FTP

 Passwords and file contents are sent in clear text, which can be intercepted by
eavesdroppers. There are protocol enhancements that circumvent this, for instance
by using SSL or TLS.

 Multiple TCP/IP connections are used, one for the control connection, and one
for each download, upload, or directory listing. Firewalls may need additional
logic and or configuration changes to account for these connections.

 It is hard to filter active mode FTP traffic on the client side by using a firewall, since
the client must open an arbitrary port in order to receive the connection. This
problem is largely resolved by using passive mode FTP.

 It is possible to abuse the protocol’s built-in proxy features to tell a server to send
data to an arbitrary port of a third computer; see FXP.

 FTP is a high latency protocol due to the number of commands needed to initiate
a transfer.

 No integrity check on the receiver side. If a transfer is interrupted, the receiver has
no way to know if the received file is complete or not. Some servers support
extensions to calculate for example a file’s MD5 sum (e.g. using the SITE MD5
command) or CRC checksum, however even then the client has to make explicit
use of them. In the absence of such extensions, integrity checks have to be managed
externally.

 No date/timestamp attribute transfer. Uploaded files are given a new current


timestamp, unlike other file transfer protocols such as SFTP, which allow attributes
to be included. There is no way in the standard FTP protocol to set the time-last-
modified (or time-created) datestamp that most modern filesystems preserve. There

147 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

is a draft of a proposed extension that adds new commands for this, but as of yet,
NOTES most of the popular FTP servers do not support it.

Security problems

The original FTP specification is an inherently insecure method of transferring files


because there is no method specified for transferring data in an encrypted fashion. This
means that under most network configurations, user names, passwords, FTP commands
and transferred files can be “sniffed” or viewed by anyone on the same network using a
packet sniffer. This is a problem common to many Internet protocol specifications written
prior to the creation of SSL such as HTTP, SMTP and Telnet. The common solution to this
problem is to use either SFTP (SSH File Transfer Protocol), or FTPS (FTP over SSL),
which adds SSL or TLS encryption to FTP as specified in RFC 4217.

FTP return codes

FTP server return codes indicate their status by the digits within them. A brief explanation
of various digits’ meanings are given below:
 1xx: Positive Preliminary reply. The action requested is being initiated but there will
be another reply before it begins.
 2xx: Positive Completion reply. The action requested has been completed. The
client may now issue a new command.
 3xx: Positive Intermediate reply. The command was successful, but a further
command is required before the server can act upon the request.
 4xx: Transient Negative Completion reply. The command was not successful, but
the client is free to try the command again as the failure is only temporary.
 5xx: Permanent Negative Completion reply. The command was not successful and
the client should not attempt to repeat it again.
 x0x: The failure was due to a syntax error.
 x1x: This response is a reply to a request for information.
 x2x: This response is a reply relating to connection information.
 x3x: This response is a reply relating to accounting and authorization.
 x4x: Unspecified as yet
 x5x: These responses indicate the status of the Server file system vis-a-vis the
requested transfer or other file system action

Anonymous FTP

Many sites that run FTP servers enable anonymous ftp. Under this arrangement, users
do not need an account on the server. The user name for anonymous access is typically
‘anonymous’. This account does not need a password. Although users are commonly
asked to send their email addresses as their passwords for authentication, usually there is
trivial or no verification, depending on the FTP server and its configuration. As modern

148 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

FTP clients hide the login process from the user and usually don’t know the user’s email
address, they supply dummy passwords, for example: NOTES
 Mozilla Firefox (2.0) — mozilla@example.com
 KDE Konqueror (3.5) — anonymous@
 wget (1.10.2) — -wget@
 lftp (3.4.4) — lftp@

Internet Gopher has been suggested as an alternative to anonymous FTP, as well as


Trivial File Transfer Protocol and File Service Protocol.

Data format

While transferring data over the network, several data representations can be used.
The two most common transfer modes are:

 ASCII mode
 Binary mode: In “Binary mode”, the sending machine sends each file bit for bit and
as such the recipient stores the bitstream as it receives it.

In “ASCII mode”, any form of data that is not plain text will be corrupted. When a
file is sent using an ASCII-type transfer, the individual letters, numbers, and characters are
sent using their ASCII character codes. The receiving machine saves these in a text file in
the appropriate format (for example, a Unix machine saves it in a Unix format, a Windows
machine saves it in a Windows format). Hence if an ASCII transfer is used it can be
assumed plain text is sent, which is stored by the receiving computer in its own format.
Translating between text formats might entail substituting the end of line and end of file
characters used on the source platform with those on the destination platform, e.g. a Windows
machine receiving a file from a Unix machine will replace the line feeds with carriage return-
line feed pairs. It might also involve translating characters; for example, when transferring
from an IBM mainframe to a system using ASCII, EBCDIC characters used on the
mainframe will be translated to their ASCII equivalents, and when transferring from the
system using ASCII to the mainframe, ASCII characters will be translated to their EBCDIC
equivalents.

By default, most FTP clients use ASCII mode. Some clients try to determine the
required transfer-mode by inspecting the file’s name or contents, or by determining whether
the server is running an operating system with the same text file format.

The FTP specifications also list the following transfer modes:

 EBCDIC mode

 Local mode

149 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

In practice, these additional transfer modes are rarely used. They are however still
NOTES used by some legacy mainframe systems.

FTP and web browsers

Most recent web browsers and file managers can connect to FTP servers, although
they may lack the support for protocol extensions such as FTPS. This allows manipulation
of remote files over FTP through an interface similar to that used for local files. This is done
via an FTP URL, which takes the form ftp(s)://<ftpserveraddress> (e.g., [1]). A password
can optionally be given in the URL, e.g.:ftp(s):/login>:<password> @<ftpserveraddress>:
<port>. Most web-browsers require the use of passive mode FTP, which not all FTP
servers are capable of handling. Some browsers allow only the downloading of files, but
offer no way to upload files to the server.

FTP and NAT devices

FTP over SSH refers to the practice of tunneling a normal FTP session over an SSH
connection.

Because FTP uses multiple TCP connections (unusual for a TCP/IP protocol that is
still in use), it is particularly difficult to tunnel over SSH. With many SSH clients, attempting
to set up a tunnel for the control channel (the initial client-to-server connection on port 21)
will protect only that channel; when data is transferred, the FTP software at either end will
set up new TCP connections (data channels) which will bypass the SSH connection, and
thus have no confidentiality, integrity protection, etc.

If the FTP client is configured to use passive mode and to connect to a SOCKS
server interface that many SSH clients can present for tunneling, it is possible to run all the
FTP channels over the SSH connection.

Otherwise, it is necessary for the SSH client software to have specific knowledge of
the FTP protocol, and monitor and rewrite FTP control channel messages and autonomously
open new forwardings for FTP data channels. Version 3 of SSH Communications Security’s
software suite, and the GPL licensed FONC are two software packages that support this
mode.

FTP over SSH is sometimes referred to as secure FTP; this should not be confused
with other methods of securing FTP, such as with SSL/TLS (FTPS). Other methods of
transferring files using SSH that are not related to FTP include SFTP and SCP; in each of
these, the entire conversation (credentials and data) is always protected by the SSH protocol.

FTP-like protocols

 File Service Protocol (FSP)


 FTPS (FTP/SSL), FTP run over SSL

150 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 Gopher, a hyperlinking anonymous FTP-like protocol


 Secure copy (SCP), a protocol running over SSH NOTES
 Simple File Transfer Protocol, the historic protocol RFC 913
 SSH file transfer protocol (SFTP, SH-FTP, FTP/SSH), a protocol running over
SSH
 Trivial File Transfer Protocol (TFTP)
 WebDAV, widely used extension of HTTP

Peer-to-peer

A peer-to-peer (or “P2P”, or, rarely, “PtP”) computer network uses diverse
connectivity between participants in a network and the cumulative bandwidth of network
participants rather than conventional centralized resources where a relatively low number
of servers provide the core value to a service or application. Peer-to-peer networks are
typically used for connecting nodes via largely ad hoc connections. Such networks are
useful for many purposes. Sharing content files (see file sharing) containing audio, video,
data or anything in digital format is very common, and realtime data, such as telephony
traffic, is also passed using P2P technology.

A pure peer-to-peer network does not have the notion of clients or servers, but only
equal peer nodes that simultaneously function as both “clients” and “servers” to the other
nodes on the network. This model of network arrangement differs from the client-server
model where communication is usually to and from a central server. A typical example for
a non peer-to-peer file transfer is an FTP server where the client and server programs are
quite distinct, and the clients initiate the download/uploads and the servers react to and
satisfy these requests.

The earliest peer-to-peer network in widespread use was the Usenet news server
system, in which peers communicated with one another to propagate Usenet news articles
over the entire Usenet network. Particularly in the earlier days of Usenet, UUCP was used
to extend even beyond the Internet. However, the news server system also acted in a
client-server form when individual users accessed a local news server to read and post
articles. The same consideration applies to SMTP email in the sense that the core email
relaying network of Mail transfer agents is a peer-to-peer network while the periphery of
Mail user agents and their direct connections is client server.

Some networks and channels such as Napster, OpenNAP and IRC server channels
use a client-server structure for some tasks (e.g. searching) and a peer-to-peer structure
for others. Networks such as Gnutella or Freenet use a peer-to-peer structure for all
purposes, and are sometimes referred to as true peer-to-peer networks, although Gnutella
is greatly facilitated by directory servers that inform peers of the network addresses of
other peers.

151 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Peer-to-peer architecture embodies one of the key technical concepts of the internet,
NOTES described in the first internet Request for Comments, RFC 1, “Host Software” dated 7
April 1969. More recently, the concept has achieved recognition in the general public in
the context of the absence of central indexing servers in architectures used for exchanging
multimedia files.

The concept of peer to peer is increasingly evolving to an expanded usage as the


relational dynamic active in distributed networks, i.e. not just computer to computer, but
human to human. Yochai Benkler has coined the term “commons-based peer production”
to denote collaborative projects such as free software. Associated with peer production
are the concept of peer governance (referring to the manner in which peer production
projects are managed) and peer property (referring to the new type of licenses which
recognize individual authorship but not exclusive property rights, such as the GNU General
Public License and the Creative Commons licenses).

A server based network. (not peer-to-peer)

152 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

A peer-to-peer based network


NOTES
Classifications of peer-to-peer networks

Peer-to-peer networks can be classified by what they can be used for:

 file sharing
 telephony
 media streaming (radio, video)
 discussion forums

Other classification of peer-to-peer networks is according to their degree of


centralization.

In ‘pure’ peer-to-peer networks:

 Peers act as equals, merging the roles of clients and server


 There is no central server managing the network
 There is no central router

Some examples of pure peer-to-peer application layer networks designed for file
sharing are Gnutella and Freenet.

There also exist countless hybrid peer-to-peer systems:


 Has a central server that keeps information on peers and responds to requests for
that information.

 Peers are responsible for hosting available resources (as the central server does
not have them), for letting the central server know what resources they want to
share, and for making its shareable resources available to peers that request it.
 Route terminals are used addresses, which are referenced by a set of indices to
obtain an absolute address.
E.g.
 Centralized P2P network such as Napster
 Decentralized P2P network such as KaZaA
 Structured P2P network such as CAN
 Unstructured P2P network such as Gnutella
 Hybrid P2P network (Centralized and Decentralized) such as JXTA, GreenTea
and Shwup

Advantages of peer-to-peer networks

An important goal in peer-to-peer networks is that all clients provide resources,


including bandwidth, storage space, and computing power. Thus, as nodes arrive and
demand on the system increases, the total capacity of the system also increases. This is not

153 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

true of a client-server architecture with a fixed set of servers, in which adding more clients
NOTES could mean slower data transfer for all users.

The distributed nature of peer-to-peer networks also increases robustness in case of


failures by replicating data over multiple peers, and — in pure P2P systems — by enabling
peers to find the data without relying on a centralized index server. In the latter case, there
is no single point of failure in the system.

When the term peer-to-peer was used to describe the Napster network, it implied
that the peer protocol was important, but, in reality, the great achievement of Napster was
the empowerment of the peers (i.e., the fringes of the network) in association with a central
index, which made it fast and efficient to locate available content. The peer protocol was
just a common way to achieve this.

While the original Napster network was a P2P network, the newest version of Napster
has no connection to P2P networking at all. The modern day version of Napster is a
subscription based service which allows you to download music files legally.

Unstructured and structured P2P networks

The P2P overlay network consists of all the participating peers as network nodes.
There are links between any two nodes that know each other: i.e. if a participating peer
knows the location of another peer in the P2P network, then there is a directed edge from
the former node to the latter in the overlay network. Based on how the nodes in the overlay
network are linked to each other, we can classify the P2P networks as unstructured or
structured.

An unstructured P2P network is formed when the overlay links are established
arbitrarily. Such networks can be easily constructed as a new peer that wants to join the
network can copy existing links of another node and then form its own links over time. In
an unstructured P2P network, if a peer wants to find a desired piece of data in the network,
the query has to be flooded through the network to find as many peers as possible that
share the data. The main disadvantage with such networks is that the queries may not
always be resolved. Popular content is likely to be available at several peers and any peer
searching for it is likely to find the same thing, but if a peer is looking for rare data shared by
only a few other peers, then it is highly unlikely that search will be successful. Since there is
no correlation between a peer and the content managed by it, there is no guarantee that
flooding will find a peer that has the desired data. Flooding also causes a high amount of
signalling traffic in the network and hence such networks typically have very poor search
efficiency. Most of the popular P2P networks such as Gnutella and FastTrack are
unstructured.

Structured P2P network employ a globally consistent protocol to ensure that any
node can efficiently route a search to some peer that has the desired file, even if the file is

154 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

extremely rare. Such a guarantee necessitates a more structured pattern of overlay links.
By far the most common type of structured P2P network is the distributed hash table NOTES
(DHT), in which a variant of consistent hashing is used to assign ownership of each file to
a particular peer, in a way analogous to a traditional hash table’s assignment of each key to
a particular array slot. Some well known DHTs are Chord, Pastry, Tapestry, CAN, and
Tulip. Not a DHT-approach but a structured P2P network is HyperCuP.

Attacks on peer-to-peer networks

Many peer-to-peer networks are under constant attack by people with a variety of
motives.

Examples include

 poisoning attacks (e.g. providing files whose contents are different from the
description)
 polluting attacks (e.g. inserting “bad” chunks/packets into an otherwise valid file
on the network)
 freeloaders (Sometimes known as ‘Leechers’) (users or software that make use
of the network without contributing resources to it)
 insertion of viruses to carried data (e.g. downloaded or carried files may be infected
with viruses or other malware)
 malware in the peer-to-peer network software itself (e.g. distributed software
may contain spyware)
 denial of service attacks (attacks that may make the network run very slowly or
break completely)

 filtering (network operators may attempt to prevent peer-to-peer network data


from being carried)

 identity attacks (e.g. tracking down the users of the network and harassing or
legally attacking them)

 spamming (e.g. sending unsolicited information across the network- not necessarily
as a denial of service attack)

Most attacks can be defeated or controlled by careful design of the peer-to-peer


network and through the use of encryption. P2P network defense is in fact closely related
to the “Byzantine Generals Problem”. However, almost any network will fail when the
majority of the peers are trying to damage it, and many protocols may be rendered impotent
by far fewer numbers.

155 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Security
NOTES
Anonymity

Some peer-to-peer protocols (such as Freenet) attempt to hide the identity of network
users by passing all traffic through intermediate nodes.

Encryption

Some peer-to-peer networks encrypt the traffic flows between peers.

This may help to:


 make it harder for an ISP to detect that peer-to-peer technology is being used (as
some artificially limit bandwidth)
 hide the contents of the file from eavesdroppers
 impede efforts towards law enforcement or censorship of certain kinds of material
 authenticate users and prevent ‘man in the middle’ attacks on protocols
 aid in maintaining anonymity
 Other networks: Abacast, Alliance, ANts P2P, Applejuice, Audiogalaxy, Avalanche,
CAKE, Chord, The Circle, Coral, Dijjer, FileTopia, Groove, Hamachi, iFolder,
konspire2b, Madster/Aimster, MUTE, OpenFT, P-Grid, IRC, JXTA, MojoNation,
Mnet, Octoshape, Omemo, Overnet, Peersites, Perfect Dark, Scour, Skype,
Solipsis, soribada, Soulseek, SPIN, Swarmcast, WASTE, Winny

 An earlier generation of peer-to-peer systems were called “metacomputing” or


were classed as “middleware”. These include: Legion, Globus

 Introduction
 Zip files
 Exe files
 Warning!
 Downloading a file from the web
 Installing software
1. Dealing with *.exe files
2. Self-installing *.exe files
3. Dealing with *.exe files that do not self-install
4. Dealing with all *.zip files
5. Installing the program

156 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Networks and Protocol


NOTES
Network or
Use Applications
Protocol

Ares File sharing Ares Galaxy, Warez P2P, Filecroc

ABC, AllPeers, Azureus,


BitComet, BitLord, BitSpirit,
BitTornado, BitTorrent, Burst!,
File
Deluge FlashGet, G3 Torrent,
sharing/Software
BitTorrent Halite, KTorrent, Limewire,
distribution/Media
MLDonkey, Opera, QTorrent,
distribution
rTorrent, Shareaza, TorrentFlux,
Transmission, Tribler,
µTorrent,Thunder

Buzm Shared HTML wiki a peer-to-peer wiki platform

File sharing, text a peer-to-peer based


CSpace
chat, remote desktop communications system

DC++, NeoModus Direct Connect,


Direct Connect File sharing SababaDC, BCDC++, ApexDC++,
StrongDC++

Domain Name Internet information See Comparison of DNS server


System retrieval software

aMule, eDonkey2000
(discontinued), eMule, eMule Plus,
eDonkey File sharing Hydranode, Jubster, lMule, Lphant,
MLDonkey, Morpheus, Pruna,
Shareaza, xMule, iMesh

giFT, Grokster, iMesh (and its


variants stripped of adware
including iMesh Light), Kazaa (and
FastTrack File sharing
its variants stripped of adware such
as Kazaa Lite), KCeasy, Mammoth,
MLDonkey, Poisoned

Distributed data Entropy (on its own network),


Freenet
store Freenet

GNUnet File sharing, chat GNUnet, (GNUnet-gtk)

157 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES Acquisition, BearShare, Cabos,


Gnucleus, Grokster, iMesh, gtk-gnutella,
Gnutella File sharing Kiwi Alpha, LimeWire, FrostWire,
MLDonkey, Morpheus, Poisoned,
Swapper, Shareaza, XoloX

Adagio, Caribou, Gnucleus, iMesh, Kiwi


Gnutella2 File sharing Alpha, MLDonkey, Morpheus, Shareaza,
TrustyFiles

Kad Network File sharing aMule, eMule, MLDonkey

Peer WiredReach Platform, Collanos


JXTA
applications Workplace (Teamwork software)

Krawler Social network Krawler[x]

MANOLITO/MP2P File sharing Blubster, Piolet

MFPnet File sharing amiciPhone (no longer available)

Napster File sharing Napigator, Napster

File sharing,
MostFun Game Player, NeoARM game
NeoEdge peer
delivery
applications

WinMX, Utatane, XNap, Lopster,


OpenNap File sharing
WinLop, Napster

TVUPlayer, Joost, CoolStreaming,


Video stream
P2PTV Cybersky-TV, TVants, PPLive, Kontiki,
or file sharing
LiveStation

Streaming
PDTP media or file DistribuStream
sharing

Multicasting PeerCast, IceShare, FreeCast,


Peercasting
streams PeerStream, Rawflow

Retroshare serverless Filesharing with


Retroshare
Chat Messenger

158 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Tran che NOTES


Distributed
Usenet ex press Load. See list o f news clien ts
discussio n

Ad vanced Netwo rk ing Pack for Wind ows


Windo ws
XP,Wind ows XP SP2 , Wind ows Vista (Th is is a
Peer-to-
Wind ows comp onent that p ro vides a 'meta' p eer-to-
Peer
peer network that ap plication s can pigg yback)

WPNP File sharin g WinM X

Introduction

Most software available for downloading from the Web is usually contained in one or
more files into which all the files necessary to install the software have been packed. The
downloadable files will normally have the extension .zip or .exe.

To install the software on your PC, the files within these “containers” need to be
unpacked. To ensure that the individual files are easily identifiable always download software
into a new directory.

Zip files

Before installing, all of the files with the extension .zip will need unpacking using an
unpacking utility such as Power Zip. Power Zip is used in the following example for those
who do not already have an unpacking utility on their PCs.

Exe files

A file with the extension .exe is a program and its behaviour cannot be predicted
before running, i.e. double- clicking on the file. It may start a self-installing Wizard, or start
up an unpacking utility such as Power Zip. In the worst case, it could damage your PC.

Warning!

Installing new software is a high-risk activity. Malicious or badly written installation


programs can damage your PC.

To avoid this happening:

 Only install programs that have been recommended by IT Services or technical


support staff in your Unit.

159 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

 NEVER double-click on an *.exe file that has been sent to you via unsolicited
NOTES email.

There are many sites on the Web from which you can download software, however
you must ensure that the site is reputable and that you are fully aware of the type of license
that applies to the software that you wish to install on your PC.

Downloading a file from the web

Always download files from the web into their own directory within a temporary
directory on your hard drive. This will ensure that the files are easily identifiable.

As the first application you need to download may well be an unpacking utility, this
example uses PowerZip. However, the following instructions apply to any file that can be
saved from your browser.

Having decided on which piece of software you require:

 Open Windows Explorer


 Create a folder within the temp folder on your hard drive, called, for example,
download
 Find the correct page for downloading the piece of software - in this case a site
called Tucows which is at http://www.tucows.com/:

N.B. Ensure that the software is for your operating system - e.g Win XP or Win 2000
- and that you can comply with the licensing conditions. NEVER download software from
unknown sources.

 Print out and read any instructions provided


 Click on the appropriate link and save the file to the folder, c:\temp\download
 When the file has been successfully saved, close your browser.

160 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Installing software
NOTES
Having successfully saved the file, you are now ready to install the software onto your
PC.
1. Dealing with *.exe files
Some downloadable files are self-extracting - i.e. do not require a utility such as
PowerZip to unpack them. These files usually have the extension .exe. In addition, an
increasing number of this type of file automatically starts the installation process using a
Wizard. However, in most cases you will not know which type of file you have until you
double-click on it.

For all *.exe files, the first part of the process is the same:
 Close all open applications
 Open Windows Explorer
 Move to the Download sub-folder within the Temp folder
 Double-click on the file that you have just downloaded from the Internet

2. Self-installing *.exe files

In the example we are using the file is called PowerZip70_Tucows.exe. It is a well


behaved self-installing executable and so a set-up wizard starts up. [If double-clicking
does not automatically start the set-up process, please go to the next section, 3.Dealing
with *.exe files that do not self-install].
Continuing with our PowerZip example:
 The Setup Wizard automatically starts up
 Click on Next
 Read any licensing rules carefully - e.g. you must register PowerZip within 30 days
 Follow the remaining steps in the Wizard - ensuring that the program is installed in
its own folder under Program Files - the last step will often be to restart your PC.

161 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Once the installation process has been successfully completed:


NOTES
 Test the program
 If everything works successfully delete the downloaded file from your folder;
c:\temp\download

3. Dealing with *.exe files that do not self-install

Double-clicking on this type of file will not automatically start a set-up process. Instead,
the files contained within the *.exe file will be unpacked into the temporary folder that you
have created.

With Windows Explorer still open at the temporary folder that you have created:
 Press the |[F5]| to refresh the screen - this will ensure that any files that have been
unpacked are now visible in Windows Explorer

The remainder of the process is described in the section, 5. Installing the program
4. Dealing with all *.zip Files
All files with the extension .zip will need to be unpacked using an unpacking utility
such as PowerZip. After unpacking they will then need to be installed as described in the
section, 5. Installing the program.
Unpacking the files:
 Close all open applications including the Office Short-cut bar
 Open Windows Explorer
 Move to the Download folder within the Temp folder
 Right-click on the file that you have just downloaded from the Internet - this will
open a short-cut menu
 Click on the option: Extract to folder C:\TEMP\download\”filename”\

5. Installing the Program

As these types of compressed files do not self-install, you will now need to install the
software. In most cases this will involve double-clicking on one of the extracted files,
usually called setup.exe but occasionally it might be called install.exe.

 If there is no such file, the extracted files must be copied to the appropriate place
within your file structure, i.e. to their own sub-folder within Program Files on the
hard drive.

162 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Installing the Program from an Extracted File


 Ensure that all applications are closed
NOTES
 Open the sub-folder containing the unpacked files

 Print off and read any Readme files that have been unpacked as these often give
further instructions regarding installation

 Double-click on the file, setup.exe - This should start up the installation Wizard

 Follow the steps in the Wizard - ensuring that the program is installed in its own
sub-folder on the hard drive - the last step will often be to restart your PC.

 Warning!

Badly behaved setup programs will install without a wizard, i.e. they will not prompt
the user during the process. In these cases, you must check to see where it has installed
itself.
Once the installation process has been successfully completed:
 Test the program
 If everything works successfully delete the downloaded file from your sub-directory;
c:\temp\download
Summary

This unit discusses the basics of WWW, the concept of creating web pages, building web
sites and the authoring tools associated with the creation of web pages. The various
compression methods, file formats and including different file formats with web pages have
been highlighted. The different controls associated with HTML, scripting concepts are also
briefly discussed. The process of downloading and installing software for windows have also
been summarized.

Questions
1. Define www.
2. What is an IP address?
3. Why web security is important?
4. Explain the process of creating a webside.
5. What are authoring tools? Explain the authoring tools used for creating web pages.
6. List out the various files forward associated with HTML pages.
7. How would you incorporate audio files is web pages?
8. Design a HTML form to accept your bio-data details.
9. What is form processing? Explain the need for client side and serve side scripling.
10. Explain FTP.
11. Explain the process of downloding and installing softwares for windows.

163 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

164 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
UNIT III

JAVA FUNDAMENTALS
3.1 JAVA FEATURES

Necessity is the mother of invention – as the proverb says, computer language


innovation and development occurs,
 To adapt to changing environment.
 To implement refinements and improvements in the art of programming, in which
the existing language lag in.

Java is one such language which has been created to adapt to the changing needs of
the environment and to efficiently overcome the problems in Interned programming world.

What is Java?

Java is a programming language, which is related to C++, the direct descendant of C.


This doesn’t mean Java is an enhanced version of C++. The purpose of creation of Java is
different from C++, though both follows object oriented programming methodology. Java
is developed by James Gosling and his colleagues to overcome the problem of platform-
dependency. The exploration of James Gosling and others to produce a code that would
run on a variety of CPU’s in differing environments, led to the creation of Java. It was
initially named as “Oak” and later called “Java”.

Some features of Java which makes it different from other Programming languages
are:
 Secure
 Portable
 Object-oriented
 Robust
 Multithreaded
 Interpreted
 Distributed
 Dynamic

SECURE

The Java language has built-in capabilities to ensure that violations of security do not
occur. Consider a workstation connected to the Internet, which accesses data from a
remote machine. There are possibilities for some viruses, trojan horses or worms which
can invade the local computer system and wreak havoc on it.

165 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Java addresses these security issues by putting in place a very rigorous multilevel
NOTES system of security:

 At compile time, pointers and memory allocation are removed thereby eliminating
the tools that a system breaker could use to gain access to system resources.
Memory allocation is deferred until runtime.
 Even though the Java compiler produces only correct Java code, there is still the
possibility of the code being tampered between compilation and runtime. Java
guards against this by using the bytecode verifier to check the bytecode for language
compliance when the code first enters the interpreter, before it ever even gets the
chance to run. The bytecode verifier ensures that the code does not do any of the
following:
Forge pointers
 Violate access restrictions
 Incorrectly access classes
 Overflow or underflow operand stack
 Use incorrect parameters of bytecode instructions
 Use illegal data conversions
 At runtime, the Java interpreter further ensures that classes loaded do not
access the file system except in the manner permitted by the client or
the user.

PORTABLE

By porting an interpreter for the Java Virtual Machine (a hypothetical processor that
can run java code), to any computer hardware/operating system, one is assured that all
code compiled for it will run on that system. This forms the basis for Java’s portability.
Another feature which Java employs in order to guarantee portability is by creating a single
standard for data sizes irrespective of processor or operating system platforms.

OBJECT ORIENTED

Java is inherently object oriented (i.e.) java programs are completely made from
programming elements called objects. An object is a programming entity that represents
either some real world object or an abstract concept.

All objects have two basic characteristics

Objects have data, also known as state. For example, an object that represents a
car has data such as the car’s name, color etc.
 Objects also have behavior, which means that they can perform certain tasks. Java
names these tasks as methods. For example, an object that represents a car has
methods such as start, stop, drive etc.

166 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Classes are closely related to objects. Java’s basic execution unit is the class. A class
is the program code you write to create objects. The class describes the data and methods NOTES
that define the object’s state and behavior.

ROBUST

The three important features of java that contributes to the robustness of java are:
 Stong type-checking
 Automatic memory management
 Exception Handling

STRONG TYPE CHECKING

Type checking refers to how a language handles variables that store different types of
data. Some of the data types usually used in programming languages are numbers, strings,
integers, etc. Java requires you to declare every variable’s type and hence a type checking
can be done both at compile and runtime making sure that every data structure has been
clearly defined and typed.

The variables declared as the particular type is checked by the compiler to make
sure you use the variables correctly. Consider the following code snippet, that won’t compile:
int a = 10;
String b = “java”;
String c = a * b;

The above code snippet would give you an error message during compilation saying
that java can’t multiply an integer and a string.

AUTOMATIC MEMORY MANAGEMENT

In c++, you had to write codes explicitly to release the memory, which has been
allocated already by a program, so that other programs could access it.

Java manages memory automatically with the help of Java Virtual Machine (JVM),
which includes a special process called the garbage collector. When an object has no
more references, it is marked as a removal object by the garbage collector and is removed
immediately or on demand.

EXCEPTION HANDLING

Even though, you plan and test your programs, errors happen. Java has a unique
approach to handle these errors. In java, the Java Runtime Environment (JRE) intercepts
and folds errors of all types into a special type of object called an exception object. As a
programmer, you must anticipate errors, while the code is running, and any susceptible set

167 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

of statements has to be bracketed by code that can catch and handle the exception (i.e.)
NOTES those errors should be dealt properly to result in a more reliable program.

MULTITHREADED

Multithreading is the ability of an application to execute more than one task (thread)
at the same time e.g. a word processor can be carrying out spell check in one document
and printing a second document at the same time. The Multithreaded nature of java helps
in overcoming the performance problems of the processor.

INTERPRETED

The java source code once compiled creates an intermediate code known as Java
byte code which is understood by a Java Virtual Machine. The interpreter in the JVM then
converts the byte code to the target hardware and executes it.

DISTRIBUTED

Java is designed for the distributed environment of the Internet, since it handles TCP/
IP protocols. Java has features using which objects on two different computers can execute
procedures remotely. Java has packed these concepts inside a package called Remote
Method Invocation (RMI).

DYNAMIC

The linking of data and methods to where they are located is done at run-time. During
the execution of a program, java can dynamically load classes that it requires either from
the local hard drive, from another computer on the local area network or from a computer
somewhere on the Internet.

JAVA PLATFORM

PLATFORM INDEPENDENCE

The main purpose of creation of java is its platform independency. This is one of the
main reason for the popularity of Java.

168 ANNA UNIVERSITY CHENNAI


THE WAY JAVA WORKS
INTERNET PROGRAMMING / WEB PROGRAMMING

THE WAY JAVA WORKS


NOTES

Method Party()
Source
code for 0 aload 0
the
interactive 1 invokespecial #1
party <Method
invitation java.lang.Object()>

SOURCE COMPILER OUTPUT (CODE) VIRTUAL


MACHINES

A Java program runs on any computer with a Java Runtime Environment (JRE)
installed. The platform independence feature of Java is based on the concept of a virtual
machine. The Java virtual machine is a hypothetical computer platform and JRE is an
emulator that creates a JVM environment that can execute Java programs.

The Java compiler translates Java into the machine language of the JVM, called as
bytecode and not as a machine language of computer. Then the JRE runs the bytecode in
the JVM. That’s how Java provides platform independence. The programs you write run
in any version of windows, Macintosh, UNIX or Linux, or any other computer with a JRE
installed.

MACHINE INDEPENDENCY OF JAVA

169 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

HOW JVM WORKS?


NOTES
Java virtual machine comprises of :
Class Loader
Bytecode verifier
Just-In-Time compiler
Class loader

A class loader has a number of duties. Class loaders are the gatekeepers of the JVM,
controlling what bytecode may be loaded and what should be rejected. As such they have
two primary responsibilities:

1. To separate Java code from different sources, thus preventing malicious code from
corrupting known good code
2. To protect the boundaries of the core Java class packages (trusted classes) by
refusing to load classes into these restricted packages

Bytecode verifier

The Java Bytecode Verifier, which is the key component of Java Security serves to
make sure that the class files loaded into the JVM respect certain security-related properties
or constraints, which bear on:
 the class file’s syntax,
 the behavior of its code and
 the potential interdependencies that it has with other class files.
Just-In-time compiler

The just-in-time compiler comes with the virtual machine. It compiles the bytecode
into platform-specific executable code that is immediately executed.

Using the Java just-in-time compiler at the particular system platform compiles the bytecode
into the particular system code. After the code has been (re-)compiled by the JIT compiler,
it will usually run more quickly on the computer.

JAVA VERSIONS

You may need to be aware of version differences if you’re writing applications that
you want to be able to run on earlier versions of Java.
 JAVA 1.0 The original version of Java was released in 1996. It had
250classes and was slow.
 JAVA 1.1 This was the first upgrade to Java, released in 1997. It had
500 classes and a better support to GUI code, which the
previous version lagged in.

170 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 JAVA 2 Java versions 1.2-1.4 is been called Java 2.They had 2300
classes. They became the language of choice for new NOTES
enterprise and mobile applications.
 JAVA 5.0 Java versions 1.5 and above are called so. They have 3500
classes. This version has major changes to the language
itself, making it easier for programmers.

3.2 JAVA FUNDAMENTALS

THE JAVA KEYWORDS

There are 49 keywords currently defined in the Java language. These keywords
cannot be used as names for a variable, class or method.

JAVA RESERVED KEYWORDS

The keywords const and goto are reserved but not used. The assert keyword
was added in Java version 1.4.

A SAMPLE JAVA PROGRAM

Let us create the first java program, which prints the message “Welcome”.
1—public class first
2—{
3— public static void main(String args[])
4— {
5— System.out.println(“Welcome to Java”);
6— }
7—}

171 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

STEPS TO RUN THE PROGRAM


NOTES  Save the java program with the same name as that of the class name* and with the
extension .java Example- first.java
 Compile the program on the command prompt using the command javac
Example- javac first.java
 Run the program to see the output on the command prompt.

Example- java first

The Java program when compiled gives a .class file(bytecode) with the name of the
class we have created and hence when JVM runs the code, it searches for the .class file
created which holds the name of the Java program and hence we give same name to the
Java file as that of the class

The above code gives the following output:

In the above Java program,

Line-1

A class has been created, where

Public is an access specifier( so everyone can access it)

class - keyword to create the class

first - user-defined name of the class

Line- 2

Open braces for the class

Line – 3

Is the mandatory method in java, which a JVM searches for, to run the Java program
created.
public - is an access specifier (so everyone can access it).
static - we’ll cover this later.
void - the method main returns no value.
(String args[]) - Arguments to the method. This method must be given an array of strings
and the name of the array is args.

Line – 4

Open braces for the method.

172 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Line – 5
NOTES
The output statement which helps to print the message on the command prompt.

System. out. Println This prints the message to standard output and after
printing the output it leaves a line.
(“Welcome to Java”) Message to be printed.

Every statement must end with a semicolon.

Line – 6

Closing braces of the main method.

Line – 7

Closing braces of the class first.

Let us have a glance of different data types , literals, operators and control structures
in java that helps in complex programming.

DATA TYPES

Java defines eight simple types of data, which can be put in four groups:
 Integers includes byte, short, int and long which are for whole-
valued signed numbers
 Floating point includes float and double which represent numbers
with fractional precision.
 Characters includes char, which represents symbols in a
character set, like letters and numbers.
 Boolean includes Boolean, which is a special type for
representing true/false values.

The simple types form the basis for all other types of data you create. The simple
types have an explicit range and a mathematical behavior. In languages like C and C++,
the size of an integer is based on the execution environment. But in Java, all data types
have a strictly defined range which doesn’t vary from machine to machine.

173 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

In Java, the

DATA TYPES IN JAVA

In Java, the char data type stores characters in Unicode format unlike the 8-bit ASCII
format in C++. Unicode is a 16-bit character format that can store Asian language alphabets.

LITERALS

Literals are the values assigned to the variables of different data types.

The four types of literals are:

 Integer Literals

The values assigned to Integer data types like byte, short, int and long.

Example:
byte a =22;
short s=3;
int i=50;
long l=356214L;

Since java strongly does type checking, data types with wrong literal values should be
marked as mismatch error. But Java, performs implicit type casting if the literal value is
within the range of the target data type.

Floating-point Literals

Floating-point numbers represent decimal values with a fractional component. They


can be expressed in either standard or scientific notation.

174 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Example:
float f=23.141f; NOTES
float num=6.022E23f;
Floating-point literals in java default to double precision. To specify a float literal you
must append an F or f to the constant. A double literal can be denoted by appending D or
d to the floating-point literal.

Character Literals

Characters in Java are Unicode character set. They are 16-bit values that can be
converted into integers and manipulated with the integer operators, such as the addition
and subtraction operators. A character literal is represented inside a pair of single quotes.

Example:
char c = ‘a’;
char c=’\u0061’ //denotes the alphabet ‘a’.
Boolean Literals
The two logical values that a Boolean literal can take are true and false. The values of
true and false do not convert to any numerical representation.
Example:
boolean flag = true;
 String Literals
String literals in Java are specified by enclosing a sequence of characters between
a pair of double quotes. Java strings must begin and end on the same line.

Example:
String name=”James Gosling”;

Operators

Java possesses a rich set of operator environment. Most of the operators can be
divided into the following four groups:
 Arithmetic operators
 Logical operators
 Relational operators
 Bitwise operators

Arithmetic Operators
Arithmetic operators are the operators which helps in performing arithmetic operations
as that of algebra in maths. The expression that holds arithmetic operators are called as
arithmetic expressions. List of arithmetic operators are given below:
175 ANNA UNIVERSITY CHENNAI
DMC 1705 / DMC 1935

NOTES

The operands of the arithmetic operators must be of a numeric type and not of a
boolean type. Even char type is acceptable.

Example for basic arithmetic operators:

Example-1

public class basicmaths


{
public static void main(String args[])
{
int a=1 + 2;
int b= 5 - 1;
int c= a * b;
int d= c / 2;
int e = c % 2;
System.out.println (“The sum is “ +a); //Output statement in Java
System.out.println (“The difference is “ +b);
System.out.println (“The product is “ +c);
System.out.println (“The output of division is “ +d);
System.out.println (“The output of modulus is “ +e);
}
}

176 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The output would be:


NOTES
The sum is 3
The difference is 4
The product is 12
The ouput of division is 6
The ouput of modulus is 0
Example-2

To find the sum and average of 3 numbers.

public class sum_avg


{
public static void main(String args[])
{
int a=25,b=55,c=32,sum,avg;
sum=a+b+c;
avg=sum/3;
System.out.println(“The sum of the 3 numbers is “+sum);
System.out.println(“The average of 3 numbers is “+avg);

}
}

The output would be:

The sum of 3 numbers is 112

The average of 3 numbers is 37

Example for Assignment Operators:

Any statement of the form var = var op expression can be written as

var op=expression. They are efficientely executed by the Java Runtime system than their
equivalent long forms.

public class assignexample


{
public static void main(String args[])
{
int i=2,j=10,k=5,m=24;
i +=4;
j -=5;
k *=5;
m /=2;

177 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
n %=2;
NOTES System.out.println (“i =” +i);
System.out.println (“j = “ +j);
System.out.println (“k = “ +k);
System.out.println (“m = “ +m);
System.out.println(“n = “ +n);
}
}
The output would be:
i=6
j=5
k= 25
m=12
n=0

Example for Increment/Decrement Operators:

The increment and decrement operators increments and decrements the value of the
variable by 1, respectively.

The two types of increment and decrement operators are:


 Pre-increment/pre-decrement
 Post-increment/post-decrement
Increment operator:

Example 1:
int i=5;
i++;
System.out.println(“The value of i is “ + i);
Example - 2
int i=5;
++i;
System.out.println(“The value of i is “ + i);

In the above examples 1 and 2 , the value of i will be 6. If so,” what is the difference
between pre and post increment operators?” would be your question.

The pre and post increment/decrement operators differs only when the expression is
being assigned to one other variable.

178 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Post-increment Example:
//… NOTES
int i=5,j;
j=i++;
System.out.println(“The value of i is “ + i);
System.out.println(“The value of j is “ + j);
//…

In the above example, two operations occurs in the statement

j=i++;

First, the value of i is assigned to j.

Second, the value of i increments to 6.

The output would be

The value of i is 6
The value of j is 5

Pre-increment Example
//…
int i=5,j;
j=++i;
System.out.println(“The value of i is “ + i);
System.out.println(“The value of j is “ + j);
//…
In the above pre-increment example,
j=++i;
First, the value of i increments to 6.
Second, the value of i is assigned to j, so that both holds the same value.
The output would be:

The value of i is 6

The value of j is 6

The above concept holds good for the post/pre decrement operators.

Logical Operators

The logical operators help in combining two relational expressions.

179 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The Logical operators are given below:


NOTES

Example:
//…
if((a>10)&&(a<=20))
{
//…
}
//…

RELATIONAL OPERATORS

The relational operators determine the relationship that one operand has to the other.
The relational operators are given below:

RELATIONAL OPERATORS

The expression that uses relational operators is called as relational expression. The
outcome of a relational expression is a boolean value. The relational operators are frequently
used in the if and loop statements.

NB: In Java a comparison is made using == symbol and not using =, which is actually an
assignment operator.

180 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Difference between C/C++ and Java in using relational operators


NOTES
In C/C++,
int count;
//…
if(count) …
if(!count) …
are valid statements, but in Java they are invalid.

In Java,

if(count==0) …
if(count!=0) …

are valid statements.

BITWISE OPERATORS

Java defines several bitwise operators which can be applied to the integer types,
long, short, char and byte. These operators act upon the individual bits of their operands.
The following table would show you the different bitwise operators.

OPERATORS RESULT
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left

CONTROL STRUCTURES

Control structures provide the flow of control during the execution of the statements.
Any branching or looping can be done using the control structures. The control statements
can be put into three categories.
 Selection statements
 Iteration statements
 Jump statements

181 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

SELECTION STATEMENTS
NOTES
If statement

The if statement helps to check a condition and returns a boolean value either true or
false.

SYNTAX:
if(condition)
{
// statement-1
}
Example:
int a=45,b=35;
if(a>b)
{
System.out.println(“ a is greater than b”);
}

In the above example, the statement inside the block gets executed if and only if , the
condition returns true.

if…else statement

The if … else statement is similar to if statement, added to which an else part is


inserted, which gets executed if the condition specified fails.

SYNTAX:

if(condition)

{
// statement-1
}
else
{
//statement-2
}

Example:
int a=45,b=35;
if(a>b)
{
System.out.println(“ a is greater than b”);
}
else

182 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
System.out.println(“b is greater than a”); NOTES
}
In the above example, if the condition fails, the else part gets executed.

if…else if statement

The if … else if statement helps to check multiple conditions.

SYNTAX:
if(condition)
{
//statement-1
}
else if(condition)
{
//statement-2
}
// …
else if(condition)
{
//statement-m
}
else
{
//Default statement
}

Example-1:

Create a Java program to generate Electricity Bill. Given the starting metre
reading(SMR) and the End metre reading(EMR). According the unit the Electricity bill is
calculated with that specific rate.

public class bill


{
public static void main(String args[])
{
int smr=1200,emr=1900,unit;
float rate=0.0f;
unit=emr-smr;
if(unit<=100)
{
rate=unit * 0.80f;
}

183 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

else if((unit>100)&&(unit<=200))
NOTES {
rate=unit * 1.50f;

}
else if(unit>200)
{
rate = unit * 2.00f;
}
System.out.println(“The Electricity Bill “ +rate);
}
}

The output would be:

The Electricity Bill is 1400.00

switch … case:

The if … else if statement, even though helps in checking multiple conditions, it confuses
the programmer after a certain level and hence to overcome this dis-advantage, we have
switch..case statement.

SYNTAX:
switch(expression)
{
case value1:
//statement-1
break;
case value 2:
//statement-2
break;
// …
case value n:
//statement-n
break;
default:
//Default statement

}
Example-1:

To print the odd numbers and identify the even numbers using switch…case public
class numbers

184 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
public static void main(String args[]) NOTES
{
for(int i=1;i<=10;i++) //learn the Iteration statements
{
switch(i)
{

case 1:
System.out.println(“one”);
break;
case 3:
System.out.println(“two”);
break;

case 5:
System.out.println(“five”);
break;
case 7:
System.out.println(“seven”);
break;
case 9:
System.out.println(“nine”);
break;
default:
System.out.println(“The number “+i+” is an even
number”);
}
}
}
}

The output would be:

one

The number 2 is an even number

three

The number 4 is an even number

five

185 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The number 6 is an even number


NOTES
seven

The number 8 is an even number

nine

The number 10 is an even number

ITERATION STATEMENTS

Consider an example of getting the maths mark of all the 20 students in a class and to
find the average of the marks. In such a case, getting input one by one slows down the
efficiency of the program. Iteration statements helps in solving this problem.

The three types of Iteration statements are:


 for
 while
 do.. while
for . . . loop

SYNTAX:

for(initial value;condition;increment/decrement expression)


{
//statement to be iterated
}
Example:
public class marks
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
if((i % 2 )== 0)
{
System.out.println(“The number “+i+” is even”);

}
else
{
System.out.println(“The number “+i+” is odd”);
}

186 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

}
} NOTES
}

The output would be:


The number 1 is odd.
The number 2 is even.
The number 3 is odd.
The number 4 is even.
The number 5 is odd.
The number 6 is even.
The number 7 is odd.
The number 8 is even.
The number 9 is odd.
The number 10 is even.

The above program identifies and prints the odd and even numbers from 1 to 10. The
value of i is initially assigned to 1, then the condition is checked, since the condition is true
the control enters the loop and executes the if statement and prints the result accordingly.
Once the loop ends, the value of i is incremented by 1 and again the condition is checked.
The above steps are repeated till the condition fails.

while . . . loop

SYNTAX:
//initial value
while(condition)
{
//statement to be iterated
//increment or decrement operation
}

Example:
Just replace the for... loop in the above program by a while…loop.
int i =1;
while(i<=10)
{
if((i % 2 )== 0)
{
System.out.println(“The number “+i+” is even”);
}
else
{

187 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

System.out.println(“The number “+i+” is odd”);


NOTES }
i++;
}

The for … loop and while…loop are Entry control loops.

do…while loop

SYNTAX:

//initial value
while(condition)
{
//statement to be iterated
//increment or decrement operation
}

Example:

Just replace the while ... loop in the above program by a do … while loop.
int i =1;
do
{
if((i % 2 )== 0)
{
System.out.println(“The number “+i+” is even”);
}
else
{
System.out.println(“The number “+i+” is odd”);
}
i++;
} while(i<=10);

The do… while loop is an Exit control loop (i.e.) the statements inside the loop executes
atleast once, irrespective of whether the condition is true or false. If the condition is true,
the loop executes, else it terminates.

JUMP STATEMENTS
Java supports three jump statements. They are :
break
continue
return

188 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

break statement
using break statement, the loop can be terminated immediately and the control moves
NOTES
to the next statement of the loop.
Example:
for(int i=1;i<=2;i++)
{
System.out.println(“Student “+i);
for(int j=1;j<10;j++)
{
if(j==5)
break;
else
System.out.println(“Value “ +j);

}
}
In the above example, each time the j value reaches 5 , the inner loop terminates and
the outer loop executes.

The output would be:


Student 1
Value 1
Value 2
Value 3
Value 4
Student 2
Value 1
Value 2
Value 3
Value 4
continue statement
Any set of statements can be skipped by using the continue statement and hence the
control moves back to the loop again.
Example:
int num=0;
while(num<=10)
{
num++;
if((num % 2)!=0)
continue;
System.out.println(“The number is “ +num);
}

189 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output would be:


NOTES
The number is 2
The number is 4
The number is 6
The number is 8
The number is 10
In the above program, if the number is an odd number, the print statement is not
executed, but the control moves to the start of the loop.

return statement

The last control statement in a method is return. The return statement helps in returning
a value from a method. The return statement can be used to cause execution to branch
back to the caller of method at any time, by terminating the method immediately.

Arrays

Arrays give you fast random access by letting you use an index position to get to any
element in the array. An array can hold either primitives or object references(later chapters).

Declaring an array:

int[] nums;

Initializing an array:

nums = new int[5];

Assigning values to the array:


nums[0]= 20;
nums[1]= 25;
nums[2]= 30;
nums[3]= 35;

nums[4]= 40;

Remember, in an array of type int (int[]), each element can hold only an int variable,
similarly float, char and other arrays. Arrays are always objects, whether they are declared
to hold primitives or object references.

Example:

To find the maximum of an array.

public class maximum


{

190 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

public static void main(String args[])


{ NOTES
int[] nums ={34,2,23,5,76,41};
int max=nums[0];
for(int i=1;i<nums.length;i++)
{
if(nums[i] > max)
{
max=nums[i];
}
}
System.out.println(“The maximum of the given array is” +max);

}
}
The output would be:

The maximum of the given array is 76

In the above program, an integer array is created. The first number of the number is
taken as the maximum number. Using a for loop the subsequent numbers are checked with
the maximum number. If any of the number in the array is maximum than the current number,
the number is re-assigned, which ultimately gives the maximum number in the array.

Strings

As in C and C++, strings are not a collection of similar type of elements in Java,
whereas strings are implemented as objects of type String in Java. Java has methods to
compare two strings, search for a sub string, concatenate two strings, and change the case
of letters within a string. As the strings are built-in objects in Java, the above string handling
operations are convenient to accomplish.

A string once created in java cannot be changed (i.e.) they are immutable. The changes
made by the string handling functions create separate objects, thereby keeping the actual
string unaffected. For those cases, which require a modifiable string, there is a companion
class of String called StringBuffer.

To create and initialize a string object

To create objects for a class, the new operator is used and a call to the constructor
has to be made (learn constructors under Class). Since String is a class, object for String
is created using new operator. */
//Java is case-sensitive (Capital S for String)
String s=new String();

191 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

To initialize a String:
NOTES
Example-1:
String(char ch[]);
char ch[]={‘g’,’o’,’o’,’d’};
String s=new String(ch);
Now, the string ‘s’ is been initialized to “good”.
Example-2:
String(char ch[],int startindex,int numchars);
char ch[]={‘g’,’o’,’o’,’d’};
String s=new String(ch[],1,2);
Now, the string ‘s’ is initialized to “oo”.
Example-3:
String(Object obj);
public class MakeString
{
public static void main(String args[])
{
char c[] = {‘J’, ‘a’, ‘v’, ‘a’};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}

The output would be:

Java

Java

STRING HANDLING FUNCTIONS

String Length

To find the length of the number of characters that a string object holds, length() is used.

int length();

Example:

String name=”java”;
System.out.println(name.length()); //OUTPUT: 4

192 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

String Concatenation
NOTES
Arithmetic operations can’t be performed using string objects. The ‘+’ operator helps
in concatenating to strings.

Example:

System.out.println(“Hi”+”How are you”); //OUTPUT: Hi How are you

Character Extraction

To retrieve only certain characters from the string object, we go for functions like
charAt(), getChars(),etc.

charAt()

To extract one single character from the string.

char charAt(int where)

Example:
String name=”Java”;
char ch;
ch = name.charAt(1);
System.out.println(“ch= “+ch);

The output would be:


ch=a

getChars()

To extract more than one character, we go for getChars().

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)

Example-1
class getCharsDemo
{
public static void main(String args[])
{
String s = “This is a demo of the getChars method.”;
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}

193 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output would be:


NOTES
demo

String Comparison

The String class includes several methods that compare strings or substrings within

strings.

equals( ) and equalsIgnoreCase( )

To compare two strings for equality, use equals( ). It has this general form:

boolean equals(Object str) .

boolean equalsIgnoreCase(String str)

Example:
class equalsDemo
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = “Hello”;
String s3 = “Good-bye”;
String s4 = “HELLO”;
System.out.println(s1 + “ equals “ + s2 + “ -> “ +
s1.equals(s2));
System.out.println(s1 + “ equals “ + s3 + “ -> “ +
s1.equals(s3));
System.out.println(s1 + “ equals “ + s4 + “ -> “ +
s1.equals(s4));
System.out.println(s1 + “ equalsIgnoreCase “ + s4 + “ -> “ +
s1.equalsIgnoreCase(s4));
}
}
The output would be:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

regionMatches( )

The regionMatches( ) method compares a specific region inside a string with another
specific region in another string.

194 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

boolean regionMatches(int startIndex, String str2,


NOTES
int str2StartIndex, int numChars)

compareTo( )

To compare which string is lesser or greater than the next string, a comparison is
required which can be done using the following method.

int compareTo(String str)

Example:

class SortString
{
static String arr[] = {“Now”, “is”, “the”, “time”, “for”, “all”, “good”, “men”,
“to”, “come”, “to”, “the”, “aid”, “of”, “their”, “country”};

public static void main(String args[])


{
for(int j = 0; j < arr.length; j++) {
for(int i = j + 1; i < arr.length; i++) {
if(arr[i].compareTo(arr[j]) < 0)
{
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}

The output would be:


Now
aid
all
come
country
for
good
is
men
of

195 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
the
NOTES the
their
time
to
to
Modifying a String

substring( )

To retrieve a set of characters or a part of the string. It has two forms:

String substring(int startIndex);

String substring(int startIndex, int endIndex);

Example:

String s= “we are studying Java”;


String sub=s.subString(0,5);

The output would be:


we are

concat( )

You can concatenate two strings using concat( ).


String concat(String str)

Example:

String s1 = “one”;
String s2 = s1.concat(“two”);

replace( )

The replace( ) method replaces all occurrences of one character in the invoking string with
another character.

String replace(char original, char replacement)

Example:

String s = “Hello”.replace(‘l’, ‘w’);

The output would be:

Hewwo

196 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Changing case
NOTES
The method toLowerCase( ) converts all the characters in a string from uppercase
to lowercase. The toUpperCase( ) method converts all the characters in a string from
lowercase to uppercase.

String toLowerCase( )
String toUpperCase( )

Example:
String s = “This is a test.”;
System.out.println(“Original: “ + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println(“Uppercase: “ + upper);
System.out.println(“Lowercase: “ + lower);

The output would be:

Original: This is a test.


Uppercase: THIS IS A TEST.
Lowercase: this is a test.

STRINGBUFFER

StringBuffer class is similar to String class but is mutable.i.e. it accepts changes.


Some of the StringBuffer functions are:
length()
capacity()
ensureCapacity()
setLength()
append()
getChars()
charAt()
insert()
reverse()
replace()
delete()

Example:
class buffer_example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer(“Welcome to Java”);

197 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
System.out.println(“Length of the string buffer: “+sb.length());
NOTES System.out.println(“Capacity of the string buffer: “+sb.capacity());
System.out.println(“Appended text: “ + sb.append(“ Programs”));
System.out.println(“Character at index[1]: “+sb.charAt(1));
System.out.println(“Inserting the text: “+sb.insert(0,”Hi!!! “));
}
}
The output would be:
Length of the string buffer: 15
Capacity of the string buffer :31 //The capacity is 21 because room for 16 additional
characters is
automatically added.
Appended text: Welcome to JavaPrograms
Character at index[1]: e
Inserting the text: Hi!!! Welcome to JavaPrograms
3.3 CLASSES

The class forms the basis of object-oriented programming . Any concept has to be
embedded within the class. A class encloses the data members and the member functions
in it. Till now, you have created classes which has a method called main().

Lets have a glance of what all a class can enclose and how an object is created for the
class.

WHAT GOES IN THE CLASS BODY


 Fields: Defines the variable declarations (property) of a class.
 Methods: Defines the method declarations (behavior) of a class.
 Constructors: A constructor is a block of code that’s similar to a method but has
the same name as that of the class and doesn’t take a return type. Constructors
are invoked when an object for the specific class is created (i.e.) it runs to initialize
the object when an instance is created.
 Initializers: These are stand-alone blocks of code that are run only once, when
the class is initialized. There are two types of initializers: static and instance.
 Other classes and interfaces: A class can have one other class inside itself,
which is then called as an Inner class or a nested class. Classes can also have
interfaces in it.

The order in which they appear doesn’t matter, but its good to place them in order.

A General Form of the Class


Class classname
{

198 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

type instance-variable-1;
type instance-variable-2; NOTES
//…
type instance-variable-N;

type methodname1(parameter-list)
{
//body of the method
}
type methodname2(parameter-list)
{
//body of the method
}
//…
type methodnameN(parameter-list)
{
//body of the method
}
}

Access specifiers:

The variables and methods in a class have their own type of access to other classes,
which is specified using Access specifiers.

The four types of Access specifiers in Java are:

 Private

The members of a class, when declared private, can be accessed only w it h in


that class and those members can’t be accessed outside the class.

 Public

The members that are declared public in a class, can be accessed from any other
members inside and outside the class.

 Protected

The members that are declared protected in a class can be accessed by the members
inside the class and also to the class which derives this class(Inheritance), irrespective of
the package they are in.

 Default Access specifier

When any of the above three specifiers is not specified, the members of the class
takes a default specifier. Such members of a class can be accessed within the package.

199 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

FIELDS AND METHODS:


NOTES
The variables and methods declared inside the class are called as instance variables
and instance methods respectively. Apart from instance variables and instance methods,
there are also static variables and static methods, which play a major role in java.

Example(Fields):
public String name = “java”;
private int x-position = 20;
private int y-position = 40;

Example(Methods)-1:
public String message() //Methods with return value and no parameters
{
return “Welcome to Java”;
}

Example(Methods)-2:

public void add(int a,int b) // Methods with parameters and no return values
{
int c=a+b;
System.out.println(“the sum is :”+ c);

Overloading methods:

Two or more methods having same name but differ in their method’s signature other
than the name. A method’s signature is the combination of its name and the number and
type of parameters it accepts.

An overloaded method should have the same name but different:


 Number of parameters.
 Type of parameters.
 Order of the parameters.
Such methods are called as Overloaded methods, which is one of the key features
of the object-oriented programming paradigm that brings flexibility into your classes.

Two things that are not a part of a method’s signature are

 The method’s return type:

Any two methods can’t be coded with the same name and parameters but with
different return types.

200 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 The names of the parameters:


NOTES
All that matters to the method signature are the types of the parameters, the number
and the order in which they appear and not the name of the parameters.

Example-1:

The PrintWriter class which you access via System.out defines ten different versions
of println method that allow you to print different types of data. The following lines show
the overloaded methods:
void println()
void println(boolean x)
void println(char x)
void println(char[] x)
void println(double x)
void println(float x)
void println(int x)
void println(long x)
void println(Object x)
void println(String x)
Example-2:
//…
public void message()
{
System.out.println(“Welcome to overloaded message without a parameter “);
}
public void message(String msg)
{
System.out.println(“Welcome to overloaded message with a parameter “);
}
//…

STATIC VARIABLES AND STATIC METHODS:

A static variables and static methods are not associated with an instance of a class,
whereas the variable and the method belong to the class itself. As a result you can call the
method without first creating a class instance, mere using the class name.

static variables

A static variable (field) is a field that’s declared with the static keyword. A static
variable has the capability of retaining its value. A static variable defaultly gets initialized to
zero.

201 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Example:
NOTES
private static int count;
static private int count;//both are legal ways of declaring a static variable.

Static fields and methods are created and initialized when the class is first loaded.

static methods

A static method is a method declared with the static keyword. The best known
static method is main, which is called by the Java runtime to start an application. The main
method must be static, which means that applications are by default run in a static context.

One of the basic rules of working with static methods is that you can’t access a non-
static method or field from a static method. That’s because the static method doesn’t have
an instance of the class to use to reference instance methods or fields.

Example-1:
//…
static int x=45;
public static void printx()
{
System.out.println(“The value of x is : “+ x);
/* Will compile, because a static variable can be used in a static method.*/
}
//…
Example-2:
//…
int x=45;
public static void printx()
{
System.out.println(“The value of x is : “+ x);
/* won’t compile, because a non-static variable cannot be used in a static method.*/

}
//…

Example-3

To create a class called car.

public class car


{
static String color=”Color of the car is blue”;
public static void main(String args[])

202 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
car sobj=new car(); //learn constructors NOTES
System.out.println(color);
sobj.change();
}
public void change()
{
System.out.println(“Color of the car changed to Maroon”);
}
}

In the above example, the static variable color is accessed by the static method
main(). If the string color is not static, an error would be thrown saying “non-static variable
color cannot be referenced by a static context”

CONSTRUCTORS:

A constructor is a block of code which has the same name as that of the class and
gets invoked to initialize the fields of the class, when the instance of an object is created. In
many ways, a constructor is similar to a method, but with few differences:
 A constructor doesn’t take a return type.
 A constructor has the same name as that of the class.
 Constructors are not members of the class, as methods.
 A constructor is called when a new instance of an object is created. In fact, it’s the
new keyword that calls the constructor. The constructor can’t be invoked again
after creating the object.

Example:
public class book
{
public book()
{
//…
}
}

new keyword:

When you create a class, you create a new data type. To make use of the members
of the class instance of the class has to be created. Instance of the class is created using the
new keyword.

Example:
car ford=new car();
Obtaining objects to a class is a two-step process.

203 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

1. Declare a variable of the class type.


NOTES 2. Acquire the actual copy of the object and assign it to the variable.(done using new
operator).

The new operator dynamically allocates memory for an object during the run time
and returns a reference to it. The reference is the address in the memory of the object
allocated by new. This reference is then stored in the variable.

In the above example, the two-steps followed are:


car ford; // ford is the reference for the instance of the class.
ford=new car(); //makes a call to the constructor to initialize the class.
Now, add a statement to the above example:
car ford=new car();
car maruthi=ford;

Now, both ford and maruthi are references to the object created by the new operator
for the class car.(i.e) two references for the same object(same memory location).

Basic constructors:

The constructors are basically used to initialize the class fields.

Example-1
//…
String artistname;
int artistage;
public artist(String name,int age)
{
artistname=name;
artistage=age;
}

//…

The above constructor would be invoked as:

artist art = new artist(“Arnald “,35);

In the above example, the constructor helps in initializing the fields of the class.

Like methods, constructors can also be overloaded.

Consider the same example given above,

public class artist


{

204 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

String firstname;
String lastname; NOTES
int artistage;
public artist(String fname,int age)
{
firstname=fname;
artistage=age;
}

public artist(String fname,String lname,int age)


{
firstname=fname;
lastname=lname;
artistage=age;
}

public static void main(String args[])


{
artist art1 = new artist(“keshav “,25);
artist art2 = new artist(“John “, “Arnald “”,35);
System.out.println(art1.firstname);
System.out.println(art1.artistage);

System.out.println(art2.firstname);
System.out.println(art2.lastname);
System.out.println(art2.artistage);
}

The output would be:

Keshav
25
John
Arnald
35

Default constructors:

If the constructors are meant for initializing the class fields, then where were the
constructors in the programs whichever you wrote till now? is what your question would
be now.

205 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Even though, the programs don’t explicitly have a constructor, it implicitly had a default
NOTES constructor to initialize the class fields. The default constructor is the free constructor with
no parameters, which just allows your class to be instantiated.

Example:
1. public sample()
{
public sample()
{
}
}

2. public sample
{
}

The above classes are identical. In the first class, the class explicitly declares a
constructor. In the second class, Java creates a default constructor, as shown in the first
example.

NB: The default constructor is not created if you declare any constructors for the
class. (i.e.)If you declare a constructor that accepts parameters and still want to have an
empty constructor( with no parameters and no body), you must explicitly declare an empty
constructor for the class.

Example:

public class artist


{
String firstname;
String lastname;
int artistage;
public artist(String fname,int age)
{
firstname=fname;
artistage=age;
}

public artist (String fname, String lname, int age)


{
firstname=fname;
lastname=lname;
artistage=age;
}
public static void main(String args[])

206 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
artist art = new artist(); //error ! wont compile, since there is NOTES
no empty constructor.

artist art1 = new artist(“keshav”,25);


artist art2 = new artist(“John”,”Arnald”,35);
System.out.println(art1.firstname);
System.out.println(art1.artistage);

System.out.println(art2.firstname);
System.out.println(art2.lastname);
System.out.println(art2.artistage);

}
}

this keyword:

A constructor can call another constructor of the same class using the special keyword
this as a method call. When several constructors are built on each other, we use this
technique.

In such technique, this method call has to be the first statement of the method.
Consider the following example,

public class student


{
String firstname;
String regno;
public student(String fname)
{
firstname=fname;
}
public student(String fname,String rno)
{
this(fname);
regno=rno;
}
public static void main(String args[])
{
student s1=new student (“Bernard”,”200432156");
System.out.println (“Firstname: “+s1.firstname);
System.out.println (“Register No.: “+s1.regno);
}

207 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output would be:


NOTES
Firstname: Bernard

Register No.: 200432156

You can also use this keyword to refer to the current object—that is, the class instance
for which the constructor or method has been called.

Example-1:

//…
String firstname;
public student(String fname)
{
this.firstname=fname;
}

In the above example, this represents that firstname is the field of the current class,
which is actually not required, since the field name and the parameter name differs.

//…
String firstname;
public student(String firstname)
{
this.firstname=firstname;
}

In the above example, this represents that firstname is the field of the current class,
which is required, since the field name and the parameter name are the same.

INITIALIZERS

An initializer is a lonely block of code that’s placed outside of any method, constructor,
or other block of code. Initializers are executed whenever an instance of a class is created,
regardless of which constructor is used to create the instance.

Static initializers are invoked before an instance of the class is created.

3.4 INHERITANCE

Generally, a child inherits the features from its parents. Similarly, in Java, inheritance
refers to a feature of object-oriented programming that lets you create classes that are
derived from other classes. Consider an example of furniture. A chair, a table, a bookshelf
all are called as furniture. This proves that all the three have some common properties like
they are made of wood etc., and so they have a common name furniture. Also, all the three

208 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

may have one or two varying properties like the number of legs, the purpose of use etc.,
and hence they have their own unique names. NOTES
This can be simply defined as, the chair, the table and the bookshelf is derived from
its parent furniture, which shares some of the common properties.

Few important things about inheritance


 A derived class automatically takes on all the behavior and attributes of its base
class.
 A derived class can add features to the base class it inherits by defining its own
methods and fields.
 A derived class can also change the behavior provided by the base class.
 Inheritance is best used to implement is-a-type-of relationships. Example: A car is
a type of vehicle.

In Java, the derived (subclass) class is derived from the base(super class) class using
the keyword extends.

To derive a subclass from a superclass:

public class <superclass name>


{
//…
}
public class <subclass name> extends <superclass name>
{
//…
}

Example:

public class furniture


{
String wood_type;

209 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
Public void setType()
NOTES
{
wood_type=”bamboo”;
}
}
public class chair extends furniture
{
int no_of_legs;
Public void setLegs()
{
no_of_legs = 4;
}
public void print_msg()
{
System.out.println(“Type of wood: “ + wood_type);
System.out.println(“Number of legs: “ + no_of_legs);

}
public static void main(String args[])
{
chair chair_obj = new chair(); //instance and a reference of the object is
created.
chair_obj.setType(); //calls the setType() of furniture class.
chair_obj.setLegs(); //calls the setLegs() of chair class
chair_obj.print_msg();//calls the print_msg() of chair class

}
}

The output would be:

Type of wood: bamboo


Number of legs: 4

Overriding methods:

Two or more methods having the same member signature is called method
overriding.(i.e.) Two or more methods having the same name, same number of arguments,
same type of arguments in the same order creates overridden methods.

Method overriding is possible only in case of inheritance.

The above example of furniture can be re-written as:

210 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Example:
public class furniture NOTES
{
String wood_type;
Public void setValue()
{
wood_type=”bamboo”;
}
}
public class chair extends furniture
{
int no_of_legs;
Public void setValue()
{
no_of_legs = 4;
}
public void print_msg()
{
System.out.println(“Type of wood: “ + wood_type);
System.out.println(“Number of legs: “ + no_of_legs);

}
public static void main(String args[])
{
chair chair_obj = new chair(); //instance and a reference of the object is
created.
chair_obj.setType(); //calls the setType() of furniture class.
chair_obj.setLegs(); //calls the setLegs() of chair class
chair_obj.print_msg();//calls the print_msg() of chair class

}
}

The ouput would be:

Type of wood: null

Number of legs: 4

To invoke the setValue() of furniture class, super keyword can be used .

super keyword

Consider the furniture class to be one and the same.

class furniture

211 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

{
NOTES String wood_type;
public void setValue()
{
wood_type=”bamboo”;
}

}
public class chair extends furniture
{
int no_of_legs;
public void setValue()
{
super.setValue(); // calls the setValue() of furniture class.
no_of_legs = 4;
}
public void print_msg()
{
System.out.println(“Type of wood: “ + wood_type);
System.out.println(“Number of legs: “ + no_of_legs);
}
public static void main(String args[])
{
chair chair_obj = new chair(); //instance and a reference of the object is
created.
chair_obj.setValue(); //calls the setValue() of chair class.
chair_obj.print_msg();

}
}
The output would be:

Type of wood: bamboo

Number of legs: 4

3.5 INTERFACES

Java supports multiple inheritances through Interfaces. An Interface is a collection of


abstract methods, which has only the declarations of data member and member functions.
There are many built-in interfaces in Java, like that of a pre-defined classes. You can also
create you own interfaces depending on the needs.

NB: you cannot create an object for an interface.

212 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

To create your own interface:


NOTES
interface <interface name>
{
//set of abstract methods
}
Example-1:
interface university
{
void name();
String place();
}
Any interface can be inherited by a class (derived class) using the keyword
implements.

Few things to be remembered while using interfaces:


 All the abstract methods in the interface must be overridden in the derived class.
 All the methods declared in the interface are by default public and abstract.
 Methods in the interface can’t be declared final.

Example-2:

public class college implements university


{
public void name()
{
System.out.println(“Name of the University : Anna University”);
}
public String place()
{
return “Chennai”;
}
public void student_name(String n)
{
System.out.println(“Name of the student : “+n);
}
public static void main(String args[])
{
college abc=new college();
abc.student_name(args[0]);
abc.name();
System.out.println(“Place of the University: “+abc.place());
}
}

213 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

In the above example, the class college inherits the interface university and all the
NOTES methods of the interface are overridden. Command-line argument is also used in the
above program. The input to the program(Student name) is given during the execution time
as:

C:\> java college kiran

Where “kiran” is the command-line argument.


args[0]- is where the string is stored.

The output would be:


Name of the student: kiran
Name of the University: Anna University
Place of the University: Chennai
3.6 PACKAGES

The pre-defined classes and interfaces in Java (Java APIs) if unorganized, it would
have created a huge mess. The classes and interfaces in Java are organized under different
packages to make use of the classes and interfaces easily and mainly to avoid the name
collision among classes.

A package is a collection of related classes. The classes inside a package can be used
by a user-defined class by importing that specific package along with the class/classes in
the program. The following statement would show you this:

import java.util.*; //imports all the classes in the package.


import java.util.Date; //imports a specific class (Date) in the package.

The default package in Java is java.lang, where lang is the sub-package inside ava
package. The base class of all the classes in Java – the Object class belongs to this
package java.lang. Instead of importing a package, we can directly make use of the pre-
defined class in the following way:

java.util.Date now=new java.util.Date();

But, the above way would increase the work of the programmer and so it is good to
import the packages.

You can also create your own packages. The following are the steps to create your
package:

1. Create a package using the following syntax:

package <package name[.subpackage1.subpackage2…]>;

214 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Example:
NOTES
package samplepack;

2. Include the related classes under the package created.

package samplepack;

public class sampleA


{
public void Amsg()
{
System.out.println(“Inside package”);
}
}

3 Now create a folder to place the class files of the package. Let the name of the
folder created be “sample”.

4 Compile the class inside java using the following command:

javac –d <folder created> <classname to be compiled>.java

Example:

javac –d sample sampleA.java

5 Import the package in the required class that makes use of the class in the package.

Example:

import samplepack.*;
public class sampleB extends sampleA
{
public static void main(String args[])
{
sampleA aobj=new sampleA();
aobj.Amsg();
}
}

6 Compile the class which has imported the package. (the above file)

javac –classpath <name of the folder created> <name of the class that imports the
package>.java

215 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Example
NOTES
javac –classpath sample sampleB.java

7 Now, set the classpath of the folder created earlier.

set classpath=<classpath of the folder created>;

Example

Consider the programs are saved in the folder e:\java_examples.

set classpath=e:\java_examples\sample;

8 Execute the program using the following command.

java <name of the class file that imported the package>

Example

java sampleB

The output would be

Inside package

Access to the classes in different packages differs based on the access specifiers. The
table below would show the access level.

Default
Private Protected Public
Access
Same package No Yes Yes Yes
subclass
Same package No Yes No Yes
non-subclass
Different No No Yes Yes
package
subclass
Different No No No Yes
package non-
subclass

3.7 EXCEPTION HANDLING

An exception is an abnormal condition that arises in the code sequence during the
runtime. Whenever an exception occurs the program terminates abruptly. The abrupt
termination of the program can be avoided by handling the exceptions. An exception is an

216 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

object that’s created when an error occurs in a Java program and Java can’t automatically
fix the error. Exceptions thrown by Java relate to fundamental errors that violate the rules NOTES
of the Java language or the constraints of the Java execution environment.

Exception handling in java is done using five keywords. They are: try, catch, finally,
throw, throws. Handling exceptions doesn’t mean rectifying the error, instead it is the
technique to avoid the nasty exception message thrown by the java runtime environment,
when any error occurred.

How to handle the exceptions?

The code statements which are susceptible of throwing exceptions are enclosed within
the try block and the action that has to be taken is specified in the catch, which must follow
the try block immediately. i.e.

The general form of Exception Handling:

try
{
//statements that may throw exception
}
catch(ExceptionType1 exceptionObj)
{
//throw or handle the exception.
}
catch(ExceptionType2 exceptionObj)
{
//throw or handle the exception.
}

finally
{
//statements those are mandatory in a program
}

The finally block encloses the statements that are mandatory in a program (i.e.)
statements that must be executed irrespective of whether an exception occurs or not.
Example: closing the opened file, closing the opened socket connection, etc.

Exception Types

All exception types are subclasses of the built-in class Throwable. Thus, Throwable
is at the top of the exception class hierarchy. Immediately below Throwable are two
subclasses that partition exceptions into two distinct branches. One is the Exception class
and other Error class. java.lang is the package that has all the in-built exception classes.

217 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The two types of Exceptions are checked and unchecked Exceptions. The checked
NOTES Exceptions are the exceptions that the compiler checks to see if a method handles or
throws these exceptions. The table below shows some of the checked exceptions.

The exceptions that the compiler does not check to see if a method handles or throws
the exceptions are called unchecked exceptions. Few unchecked exceptions are shown
below:

Error defines exceptions that are not expected to be caught under normal circumstances
by your program. Errors are created in response to catastrophic failures that cannot usually
be handled by your program. Example: Stack overflow

218 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Example-1:
NOTES
class arith
{
public static void main(String args[])
{
int a=10,b=0,c=0;
c=a/b;
System.out.println(“The value of c: “+c);

}
}

The above program has some exceptional statements, which causes divide by zero
exception (unchecked exception). The program compiles successfully and throws the error
during runtime. The error would be:

Exception in thread “main” java.lang.ArithmeticException: / by zero

at arith.main(arith.java:6)

Example-2

The program in example-1 is re-written in the following way:

class arith
{
public static void main(String args[])
{
int a=10,b=0,c=0;
try
{
c=a/b;
}
catch(Exception obj)
{
System.out.println(“Exception caught: “+obj);
}
System.out.println(“The value of c: “+c);
}
}
The output would be:

Exception caught: java.lang.ArithmeticException: / by zero

The value of c: 0

219 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Multiple catch statements:


NOTES
In the above example, the exception is caught by an Exception class object, instead
the more specific exception (Arithmetic exception) can also be used.

Example-3:
class arith
{
public static void main(String args[])
{
int a=10,b=0,c=0;
try
{
c=a/b;
}
catch(ArithmeticException ae)
{
System.out.println(“ArithmeticException caught: “+ae);
}
catch(Exception obj)
{
System.out.println(“Exception caught: “+obj);
}
System.out.println(“The value of c: “+c);
}
}

The output would be:

ArithmeticException caught: java.lang.ArithmeticException: / by zero

The value of c: 0

finally statement:

Example-4:

class arith
{
public static void main(String args[])
{
int a=10,b=0,c=0;
try
{
c=a/b;
}
catch(ArithmeticException ae)

220 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
System.out.println(“ArithmeticException caught: “+ae); NOTES
}
catch(Exception obj)
{
System.out.println(“Exception caught: “+obj);
}
finally
{
System.out.println(“Finally statement”);
}
System.out.println(“The value of c: “+c);
}
}

The output would be:

ArithmeticException caught: java.lang.ArithmeticException: / by zero


Finally statement
The value of c: 0
throw and throws keyword:
The keyword throws is used whenever the function which has susceptible statements
doesn’t handle the exception, instead the exception would be handled by its calling function.
Example-5:
class arrayex
{
int[] arr = new int[2];
public arrayex()
{
arr[0]=45;
arr[1]=55;
}
public static void main(String args[])
{
arrayex aobj = new arrayex();
aobj.msg();
}
public void msg()
{
System.out.println(“1st number: “+arr[0]);
System.out.println(“2nd number: “+arr[1]);
System.out.println(“3rd number: “+arr[2]);
}
}

221 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output would be:


NOTES
1st number: 45
2nd number: 55

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 2

at arrayex.msg(arrayex.java:18)
at arrayex.main(arrayex.java:12)

In the above example, an integer array of size two is declared and initialized. But the
program tries to access the third element and hence an ArrayIndexOutOfBoundsException
occurs. The above example can be re-written as:

class arrayex
{
int[] arr = new int[2];
public arrayex()
{
arr[0]=45;
arr[1]=55;
}
public static void main(String args[])
{
arrayex aobj = new arrayex();
try
{
aobj.msg();
}
catch(ArrayIndexOutOfBoundsException ai)
{
System.out.println(“Exception caught: “+ai);
}
}
public void msg() throws ArrayIndexOutOfBoundsException
{

System.out.println(“1st number: “+arr[0]);

System.out.println(“2nd number: “+arr[1]);

System.out.println(“3rd number: “+arr[2]);

222 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The output would be:


NOTES
1st number: 45
2nd number: 55

Exception caught: java.lang.ArrayIndexOutOfBoundsException: 2

Throw keyword is mostly used in case of user-defined exceptions.

To create user-defined exceptions:

User-defined exception are created when any manual exception is to be thrown other
than the in-built exceptions. The following is an example to check whether the user is a
valid user or not(age >=18). If the user is not a valid user(age<18), an exception would
rise.
Example-6:
class userException extends Exception
{
public String toString()
{
return “Not a valid user!!!”;
}
}public class check
{
public static void main(String args[])
{
check obj=new check();
int age=Integer.parseInt(args[0]);
try
{
obj.checkage(age);
}
catch(userException ue)
{
System.out.println(“UserException caught: “+ue);
}
}
public void checkage(int a) throws userException
{
if(a<18)
throw new userException();
else
System.out.println(“Valid user”);
}
}

223 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output would be:


NOTES
C:>java check 10
UserException caught: Not a valid user!!!

In the above example, an user-defined Exception userException was created. Any


user-defined exception has to inherit the base class Excepion. The program check.java
makes use of the user-defined exception created. The age of the user is passed as the
command-line argument. If the user’s age is below 18, an exception object is created and
is thrown to the calling function. The calling function handles the exception by invoking the
toString(), which is overridden in the userException class.

Below are few methods that are defined in the Throwable class, which can be
overridden in the user-defined Exception class.

Summary

This unit discusses the basic features of Java, different versions, data types, operators,
various statements and simple programs have been discussed. The concept of classes, access
specifiers, and other class related features are also highlighted. The inheritance, interface and
packages are discussed briefly. The concept of exception handling is aiso explained.

224 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING
Questions
NOTES
1. Highlight the features of Java
2. List out the Keywords associated with Java
3. Write a java program to calculate payroll of an employee.
4. List out the operators supported in java
5. List out the conditional and iteration statement supported in java.
6. Write a java program to handle staring functions.
7. List the access specifiers associated with java.
8. Explain the use of ‘this’ keyword.
9. Explain the inheritance feature supported by java.
10. What is an interface? Explain its usage
11. What is a package?
12. Explain the exception handling mechanism supported in java.

225 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES

226 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
UNIT IV

AWT CLASSES

4.1 BASIES OF AWT

The AWT classes are contained in the java.awt package. It is one of Java’s largest
packages. Fortunately, because it is logically organized in a top-down, hierarchical fashion,
it is easier to understand and use than you might at first believe. Table 21-1 lists some of
the many AWT classes.

Class Description

AWTEvent Encapsulates AWT events.


AWTEventMulticaster Dispatches events to multiple listeners.
BorderLayout The border layout manager. Border layouts use five
components: North, South, East, West, and Center.
Button Creates a push button control.

The AWT defines windows according to a class hierarchy that adds functionality and
specificity with each level. The two most common windows are those derived from Panel,
which is used by applets, and those derived from Frame, which creates a standard window.
Much of the functionality of these windows is derived from their parent classes.

At the top of the AWT hierarchy is the Component class. Component is an abstract
class that encapsulates all of the attributes of a visual component. All user interface elements
that are displayed on the screen and that interact with the user are subclasses of Component.
It defines over a hundred public methods that are responsible for managing events, such as
mouse and keyboard input, positioning and sizing the window, and repainting.

The Container class is a subclass of Component. It has additional methods that


allow other Component objects to be nested within it. Other Container objects can be
stored inside of a Container (since they are themselves instances of Component). This
makes for a multileveled containment system. A container is responsible for laying out (that
is, positioning) any components that it contains. It does this through the use of various
layout managers

227 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The Panel class is a concrete subclass of Container. It doesn’t add any new methods;
NOTES it simply implements Container. A Panel may be thought of as a recursively nestable,
concrete screen component. Panel is the superclass for Applet. When screen output is
directed to an applet, it is drawn on the surface of a Panel object. In essence, a Panel is
a window that does not contain a title bar, menu bar, or border. This is why you don’t see
these items when an applet is run inside a browser. When you run an applet using an applet
viewer, the applet viewer provides the title and border. Other components can be added to
a Panel object by its add( ) method (inherited from Container). Once these components
have been added, you can position and resize them manually using the setLocation( ),
setSize( ), or setBounds( ) methods defined by Component.

The Window class creates a top-level window. A top-level window is not contained
within any other object; it sits directly on the desktop. Generally, you won’t create Window
objects directly. Instead, you will use a subclass of Window called Frame.

Frame encapsulates what is commonly thought of as a “window.” It is a subclass of


Window and has a title bar, menu bar, borders, and resizing corners. If you create a
Frame object from within an applet, it will contain a warning message, such as “Java
Applet Window,” to the user that an applet window has been created. This message warns
users that the window they see was started by an applet and not by software running on
their computer. When a Frame window is created by a program rather than an applet, a
normal window is created.

Although it is not part of the hierarchy for applet or frame windows, there is one other
type of window that you will find valuable: Canvas. Canvas encapsulates a blank window
upon which you can draw.

Creating a Frame Window in an Applet


// Create a child frame window from within an applet.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=”AppletFrame” width=300 height=50>
</applet>
*/
// Create a subclass of Frame.
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
// create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events

228 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

addWindowListener(adapter);
} NOTES
public void paint(Graphics g) {
g.drawString(“This is in frame window”, 10, 40);
}

}
class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}
}
// Create frame window.
public class AppletFrame extends Applet {
Frame f;
public void init() {
f = new SampleFrame(“A Frame Window”);
f.setSize(250, 250);
f.setVisible(true);
}
public void start() {
f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}
public void paint(Graphics g) {
g.drawString(“This is in applet window”, 10, 20);
}
}
b

// Create an AWT-based application.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// Create a frame window.
public class AppWindow extends Frame {
String keymsg = “This is a test.”;
String mousemsg = “”;
int mouseX=30, mouseY=30;

229 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
public AppWindow() {
NOTES addKeyListener(new MyKeyAdapter(this));
addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g) {
g.drawString(keymsg, 10, 40);
g.drawString(mousemsg, mouseX, mouseY);
}
// Create the window.
public static void main(String args[]) {
AppWindow appwin = new AppWindow();
appwin.setSize(new Dimension(300, 200));
appwin.setTitle(“An AWT-Based Application”);
appwin.setVisible(true);
}
}
class MyKeyAdapter extends KeyAdapter {
AppWindow appWindow;
public MyKeyAdapter(AppWindow appWindow) {
this.appWindow = appWindow;
}
public void keyTyped(KeyEvent ke) {
appWindow.keymsg += ke.getKeyChar();
appWindow.repaint();
};
}
class MyMouseAdapter extends MouseAdapter {
AppWindow appWindow;
public MyMouseAdapter(AppWindow appWindow) {
this.appWindow = appWindow;
}
public void mousePressed(MouseEvent me) {
appWindow.mouseX = me.getX();
appWindow.mouseY = me.getY();
appWindow.mousemsg = “Mouse Down at “ + appWindow.mouseX +
“, “ + appWindow.mouseY;
appWindow.repaint();
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
230 ANNA UNIVERSITY CHENNAI
INTERNET PROGRAMMING / WEB PROGRAMMING

4.2 WORKING WITH GRAPHICS


NOTES
The AWT supports a rich assortment of graphics methods. All graphics are drawn
relative to a window. This can be the main window of an applet, a child window of an
applet, or a stand-alone application window. The origin of each window is at the top-left
corner and is 0,0. Coordinates are specified in pixels. All output to a window takes place
through a graphics context. A graphics context is encapsulated by the Graphics class
and is obtained in two ways:

It is passed to an applet when one of its various methods, such as paint( ) or update(
), is called.

It is returned by the getGraphics( ) method of Component.

Drawing Lines

Lines are drawn by means of the drawLine( ) method, shown here:

void drawLine(int startX, int startY, int endX, int endY)

drawLine( ) displays a line in the current drawing color that begins at startX,startY and
ends at endX,endY.

The following applet draws several lines:


// Draw lines
import java.awt.*;
import java.applet.*;
/*
<applet code=”Lines” width=300 height=200>
</applet>
*/
public class Lines extends Applet {
public void paint(Graphics g) {
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);
g.drawLine(40, 25, 250, 180);
g.drawLine(75, 90, 400, 400);
g.drawLine(20, 150, 400, 40);
g.drawLine(5, 290, 80, 19);
}
}
Drawing Rectangles

The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle,
respectively. They are shown here:

231 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

void drawRect(int top, int left, int width, int height)


NOTES void fillRect(int top, int left, int width, int height)

The upper-left corner of the rectangle is at top,left. The dimensions of the rectangle
are specified by width and height. To draw a rounded rectangle, use drawRoundRect(
) or fillRoundRect( ), both shown here:

void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)

void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)

A rounded rectangle has rounded corners. The upper-left corner of the rectangle is at
top,left. The dimensions of the rectangle are specified by width and height. The diameter
of the rounding arc along the X axis is specified by xDiam. The diameter of the rounding
arc along the Y axis is specified by yDiam.

The following applet draws several rectangles:


// Draw rectangles
import java.awt.*;
import java.applet.*;
/*
<applet code=”Rectangles” width=300 height=200>
</applet>
*/
public class Rectangles extends Applet {
public void paint(Graphics g) {
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}
Drawing Ellipses and Circles

To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods
are shown here:

void drawOval(int top, int left, int width, int height)


void fillOval(int top, int left, int width, int height)

The ellipse is drawn within a bounding rectangle whose upper-left corner is specified
by top,left and whose width and height are specified by width and height. To draw a
circle, specify a square as the bounding rectangle.

The following program draws several ellipses:


// Draw Ellipses

232 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

import java.awt.*;
import java.applet.*; NOTES
/*
<applet code=”Ellipses” width=300 height=200>
</applet>
*/
public class Ellipses extends Applet {
public void paint(Graphics g) {
g.drawOval(10, 10, 50, 50);
g.fillOval(100, 10, 75, 50);
g.drawOval(190, 10, 90, 30);
g.fillOval(70, 90, 140, 100);
}
}

The Project Swing code that follows builds this simple application. The window on
the left appears when you start the application, and the window on the right appears when
you click the button. Click again and you are back to the original window on the left.

Import Statements

Here is the SwingUI.java code. At the top, you have four lines of import statements.
The lines indicate exactly which JavaTM API classes the program uses. You could replace
four of these lines with this one line: import java.awt.*;, to import the entire awt package,
but doing that increases compilation overhead than importing exactly the classes you need
and no others.

import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

Class Declaration

The class declaration comes next and indicates the top-level frame for the application’s
user interface is a JFrame that implements the ActionListener interface.

233 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

class SwingUI extends JFrame


NOTES implements ActionListener{

The JFrame class extends the Frame class that is part of the Abstract Window Toolkit
(AWT) APIs. Project Swing extends the AWT with a full set of GUI components and
services, pluggable look and feel capabilities, and assistive technology support.

The Java APIs provide classes and interfaces for you to use. An interface defines a
set of methods, but does not implement them. The rest of the SwingUI class declaration
indicates that this class will implement the ActionListener interface. This means the SwingUI
class must implement all methods defined in the ActionListener interface. Fortunately, there
is only one, actionPerformed, which is discussed below.

Instance Variables

These next lines declare the Project Swing component classes the SwingUI class
uses. These are instance variables that can be accessed by any method in the instantiated
class. In this example, they are built in the SwingUI constructor and accessed in the
actionPerformed method implementation. The private boolean instance variable is visible
only to the SwingUI class and is used in the actionPerformedmethod to find out whether or
not the button has been clicked.

JLabel text, clicked;

JButton button, clickButton;

JPanel panel;

private boolean _clickMeMode = true;

Constructor

The constructor (shown below) creates the user interface components and JPanel
object, adds the components to the JPanel object, adds the panel to the frame, and makes
the JButton components event listeners. The JFrame object is created in the main method
when the program starts.

SwingUI(){

text = new JLabel(“I’m a Simple Program”);

clicked = new JLabel(“Button Clicked”);

button = new JButton(“Click Me”);


//Add button as an event listener
button.addActionListener(this);

234 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

clickButton = new JButton(“Click Again”);


//Add button as an event listener NOTES
clickButton.addActionListener(this);
//Create panel
panel = new JPanel();
//Specify layout manager and background color
panel.setLayout(new BorderLayout(1,1));
panel.setBackground(Color.white);
//Add label and button to panel
getContentPane().add(panel);
panel.add(BorderLayout.CENTER, text);
panel.add(BorderLayout.SOUTH, button);
}

When the JPanel object is created, the layout manager and background color are
specified. The layout manager in use determines how user interface components are
arranged on the display area.

The code uses the BorderLayout layout manager, which arranges user interface
components in the five areas shown at left. To add a component, specify the area (north,
south, east, west, or center).

//Create panel
panel = new JPanel();
//Specify layout manager and background color
panel.setLayout(new BorderLayout(1,1));
panel.setBackground(Color.white);
//Add label and button to panel
getContentPane().add(panel);
panel.add(BorderLayout.CENTER, text);
panel.add(BorderLayout.SOUTH, button);
}

235 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The call to the getContentPane method of the JFrame class is for adding the Panel to
NOTES the JFrame. Components are not added directly to a JFrame, but to its content pane.
Because the layout manager controls the layout of components, it is set on the content pane
where the components reside. A content pane provides functionality that allows different
types of components to work together in Project Swing.

Action Listening

In addition to implementing the ActionListener interface, you have to add the event
listener to the JButton components. An action listener is the SwingUI object because it
implements the ActionListener interface. In this example, when the end user clicks the
button, the underlying Java platform services pass the action (or event) to the
actionPerformed method. In your code, you implement the actionPerformed method to
take the appropriate action based on which button is clicked..

The component classes have the appropriate add methods to add action listeners to
them. In the code the JButton class has an addActionListener method. The parameter
passed to addActionListener is this, which means the SwingUI action listener is added to
the button so button-generated actions are passed to the actionPerformed method in the
SwingUI object.

button = new JButton(“Click Me”);


//Add button as an event listener
button.addActionListener(this);

4.3 EVENT HANDLING

The actionPerformed method is passed an event object that represents the action
event that occurred. Next, it uses an if statement to find out which component had the
event, and takes action according to its findings.

public void actionPerformed(ActionEvent event){


Object source = event.getSource();
if (_clickMeMode) {
text.setText(“Button Clicked”);
button.setText(“Click Again”);
_clickMeMode = false;
} else {
text.setText(“I’m a Simple Program”);
button.setText(“Click Me”);
_clickMeMode = true;
}
}

236 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Main Method
NOTES
The main method creates the top-level frame, sets the title, and includes code that lets
the end user close the window using the frame menu.

public static void main(String[] args){


//Create top-level frame
SwingUI frame = new SwingUI();
frame.setTitle(“Example”);
//This code lets you close the window
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
//This code lets you see the frame
frame.pack();
frame.setVisible(true);
}
}

The code for closing the window shows an easy way to add event handling functionality
to a program. If the event listener interface you need provides more functionality than the
program actually uses, use an adapter class. The Java APIs provide adapter classes for all
listener interfaces with more than one method. This way, you can use the adapter class
instead of the listener interface and implement only the methods you need. In the example,
the WindowListener interface has 7 methods and this program needs only the windowClosing
method so it makes sense to use the WindowAdapter class instead.

This code extends the WindowAdapter class and overrides the windowClosing
method. The new keyword creates an anonymous instance of the extended inner class. It
is anonymous because you are not assigning a name to the class and you cannot create
another instance of the class without executing the code again. It is an inner class because
the extended class definition is nested within the SwingUI class.

This approach takes only a few lines of code, while implementing the WindowListener
interface would require 6 empty method implementations. Be sure to add the
WindowAdapter object to the frame object so the frame object will listen for window
events.

WindowListener l = new WindowAdapter() {


//The instantiation of object l is extended to
//include this code:
public void windowClosing(WindowEvent e){

237 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
System.exit(0);
NOTES }
};
frame.addWindowListener(l);
Garbage collection

The JVM’s heap stores all objects created by an executing Java program. Objects
are created by Java’s “new” operator, and memory for new objects is allocated on the
heap at run time. Garbage collection is the process of automatically freeing objects that are
no longer referenced by the program. This frees the programmer from having to keep track
of when to free allocated memory, thereby preventing many potential bugs and headaches.

The name “garbage collection” implies that objects that are no longer needed by the
program are “garbage” and can be thrown away. A more accurate and up-to-date metaphor
might be “memory recycling.” When an object is no longer referenced by the program, the
heap space it occupies must be recycled so that the space is available for subsequent new
objects. The garbage collector must somehow determine which objects are no longer
referenced by the program and make available the heap space occupied by such unreferenced
objects. In the process of freeing unreferenced objects, the garbage collector must run any
finalizers of objects being freed.

In addition to freeing unreferenced objects, a garbage collector may also combat


heap fragmentation. Heap fragmentation occurs through the course of normal program
execution. New objects are allocated, and unreferenced objects are freed such that free
blocks of heap memory are left in between blocks occupied by live objects. Requests to
allocate new objects may have to be filled by extending the size of the heap even though
there is enough total unused space in the existing heap. This will happen if there is not
enough contiguous free heap space available into which the new object will fit. On a virtual
memory system, the extra paging required to service an ever growing heap can degrade
the performance of the executing program.

This article does not describe an official Java garbage-collected heap, because none
exists. The JVM specification says only that the heap of the Java virtual machine must be
garbage collected. The specification does not define how the garbage collector must work.
The designer of each JVM must decide how to implement the garbage-collected heap.
This article describes various garbage collection techniques that have been developed and
demonstrates a particular garbage collection technique in an applet.

Advantages of Garbage collection

1. Garbage collection relieves programmers from the burden of freeing allocated


memory. Knowing when to explicitly free allocated memory can be very tricky.
Giving this job to the JVM has several advantages. First, it can make programmers

238 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

more productive. When programming in non-garbage-collected languages the


programmer can spend many late hours (or days or weeks) chasing down an NOTES
elusive memory problem. When programming in Java the programmer can use
that time more advantageously by getting ahead of schedule or simply going home
to have a life.
2. A second advantage of garbage collection is that it helps ensure program integrity.
Garbage collection is an important part of Java’s security strategy. Java programmers
are unable to accidentally (or purposely) crash the JVM by incorrectly freeing
memory.

Disadvantage of Garbage collection

1. A potential disadvantage of a garbage-collected heap is that it adds an overhead


that can affect program performance. The JVM has to keep track of which objects
are being referenced by the executing program, and finalize and free unreferenced
objects on the fly. This activity will likely require more CPU time than would have
been required if the program explicitly freed unnecessary memory. In addition,
programmers in a garbage-collected environment have less control over the
scheduling of CPU time devoted to freeing objects that are no longer needed.

To overcome disadvantage of Garbage collection

Fortunately, very good garbage collection algorithms have been developed, and
adequate performance can be achieved for all but the most demanding of applications.
Because Java’s garbage collector runs in its own thread, it will, in most cases, run
transparently alongside the execution of the program. Plus, if a programmer really wants to
explicitly request a garbage collection at some point, System.gc() or Runtime.gc()
can be invoked, which will fire off a garbage collection at that time.

The Java programmer must keep in mind that it is the garbage collector that runs
finalizers on objects. Because it is not generally possible to predict exactly when unreferenced
objects will be garbage collected, it is not possible to predict when object finalizers will be
run. Java programmers, therefore, should avoid writing code for which program correctness
depends upon the timely finalization of objects. For example, if a finalizer of an unreferenced
object releases a resource that is needed again later by the program, the resource will not
be made available until after the garbage collector has run the object finalizer. If the program
needs the resource before the garbage collector has gotten around to finalizing the
unreferenced object, the program is out of luck.

Garbage collection algorithms

A great deal of work has been done in the area of garbage collection algorithms.
Many different techniques have been developed that could be applied to a JVM. The

239 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

garbage-collected heap is one area in which JVM designers can strive to make their JVM
NOTES better than the competition’s.

Any garbage collection algorithm must do two basic things. First, it must detect garbage
objects. Second, it must reclaim the heap space used by the garbage objects and make it
available to the program. Garbage detection is ordinarily accomplished by defining a set of
roots and determining reachability from the roots. An object is reachable if there is some
path of references from the roots by which the executing program can access the object.
The roots are always accessible to the program. Any objects that are reachable from the
roots are considered live. Objects that are not reachable are considered garbage, because
they can no longer affect the future course of program execution.

n a JVM the root set is implementation dependent but would always include any
object references in the local variables. In the JVM, all objects reside on the heap. The
local variables reside on the Java stack, and each thread of execution has its own stack.
Each local variable is either an object reference or a primitive type, such as int, char, or
float. Therefore the roots of any JVM garbage-collected heap will include every object
reference on every thread’s stack. Another source of roots are any object references, such
as strings, in the constant pool of loaded classes. The constant pool of a loaded class may
refer to strings stored on the heap, such as the class name, superclass name, superinterface
names, field names, field signatures, method names, and method signatures.

Any object referred to by a root is reachable and is therefore a live object. Additionally,
any objects referred to by a live object are also reachable. The program is able to access
any reachable objects, so these objects must remain on the heap. Any objects that are not
reachable can be garbage collected because there is no way for the program to access
them.

The JVM can be implemented such that the garbage collector knows the difference
between a genuine object reference and a primitive type (for example, an int) that appears
to be a valid object reference. However, some garbage collectors may choose not to
distinguish between genuine object references and look-alikes. Such garbage collectors
are called conservative because they may not always free every unreferenced object.
Sometimes a garbage object will be wrongly considered to be live by a conservative
collector, because an object reference look-alike refered to it. Conservative collectors
trade off an increase in garbage collection speed for occasionally not freeing some actual
garbage.

Two basic approaches to distinguishing live objects from garbage are reference
counting and tracing. Reference counting garbage collectors distinguish live objects from
garbage objects by keeping a count for each object on the heap. The count keeps track of
the number of references to that object. Tracing garbage collectors, on the other hand,
actually trace out the graph of references starting with the root nodes. Objects that are

240 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

encountered during the trace are marked in some way. After the trace is complete, unmarked
objects are known to be unreachable and can be garbage collected. NOTES
4.4 MULTITHREADING IN JAVA

Thread

A thread executes a series of instructions. Every line of code that is executed is done
so by a thread. Some threads can run for the entire life of the applet, while others are alive
for only a few milliseconds. A thread also creates a new thread or kills an existing one.
Threads run in methods or constructors. The methods and constructors themselves are
lifeless. The threads go into the methods and follow their instructions. Methods and
constructors reside in the computer’s memory. Figure 16.1 shows threads, constructors,
and methods in a typical applet.

The Thread Class

The class java.lang.Thread is used to create and control threads. To create a thread,
a new instance of this class must be created. However, the thread does not start running
right away. Thread.start() must be called to actually make the thread run. When Thread.start()
is called, the thread begins executing in the run() method of the target class. A new Thread
class always starts running the public void run() method of a class. There are two ways to
create a thread:
 Extend the Thread class. With this technique the new class inherits from the class
Thread. The thread can start running in the class’s run method.
 Implement the Runnable interface. This technique is probably more common
than extending the Thread class. It is not necessary to define a new class to run
the thread. If a thread is to start running in the applet, it must use the Runnable
interface. The applet cannot inherit from both the Thread and Applet classes.
An applet with the Runnable interface must have a run() method for the
thread to start.

There isn’t much difference between the two approaches. Both extending the Thread
class and implementing the Runnable interface have the same functionality. The interface
approach must be used if the thread is to actually start in the applet class. But if the
thread is going to be running in its own class, it may be more convenient to extend the
Thread class. Examples of both approaches are in this chapter.

The Thread class has seven constructors. All of them create a new thread. The thread
does not start running until Thread.start() is called. When Thread.start() is called, the new
thread starts running in the run() method of an object. The constructors are the following:
Thread()
Thread(Runnable)

241 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
Thread(ThreadGroup)
NOTES Thread(String)
Thread(ThreadGroup,String)
Thread(Runnable,String)
Thread(ThreadGroup,Runnable,String)

The constructors can use three possible parameters:


 String The name of the new thread is the parameter String. A thread can get its
name by calling Thread.getName().
 ThreadGroup The new thread will belong to the group specified by the parameter
ThreadGroup. A ThreadGroup can be used to organize a thread.
 Runnable The Runnable parameter is an object that has implemented the Runnable
interface. The thread will start executing in the run() method of the Runnable
parameter when Thread.start() has been called.
 There are many methods in the Thread class. Some of them, such as destroy(),
don’t seem to have been implemented yet, and may never be. Some of the methods
that control the thread execution are the following:
 start() This method starts the thread. It starts executing in the run() method of its
Runnable target that was set when the constructor was called. This method can be
called only once.
 suspend() This method suspends the execution of the thread. It remains suspended
until resume() is called.
 resume() This method resumes the execution of a suspended thread. It has no
effect on a thread that is not suspended.
 stop() This method stops and kills a running thread. Currently, the thread does not
stop unless it is running. If it is suspended, it does not die until it starts running
again. However, this may be fixed someday.
 sleep(int m)/sleep(int m,int n) The thread sleeps for m milliseconds, plus n
nanoseconds

Simple Thread Examples

How to start, stop, suspend, and resume threads is shown below. It uses the
Runnable interface. Threads like this are useful for things like controlling animation
sequences or repeatedly playing audio samples. This example uses a thread that counts
and prints a string every second. The thread starts when the applet is initialized. It continues
to run until the user leaves the page. If the user returns to the page (and all is well), the
thread continues from where it left off. This allows applets to retain their states while the
user is away.

242 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Thread examples.
NOTES
import java.lang.Thread;
import java.applet.Applet;
public class InfiniteThreadExample extends Applet implements Runnable
{
Thread myThread;

public void init()


{
System.out.println(“in init() — starting thread.”);
myThread= new Thread(this);
myThread.start();
}

public void start()


{
System.out.println(“in start() — resuming thread.”);
myThread.resume();
}

public void stop()


{
System.out.println(“in stop() — suspending thread.”);
myThread.suspend();

}
public void destroy()
{
System.out.println(“in destroy() — stoping thread.”);
myThread.resume();
myThread.stop();
}
public void run()
{
int i=0;
for(;;)
{
i++;
System.out.println(“At “ + i + “ and counting!”);
try {myThread.sleep(1000);}
catch (InterruptedException e ) {}
}
}
}

243 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

SimpleThreadExample Output
NOTES
The output of InfiniteThreadExample is shown here. The applet ran for nine
seconds until it was stopped.
in init() — starting thread.
At 1 and counting!
in start() — resuming thread.
At 2 and counting!
At 3 and counting!
At 4 and counting!
At 5 and counting!
At 6 and counting!
At 7 and counting!
At 8 and counting!
At 9 and counting!
in stop() — suspending thread.
in destroy() — stoping thread.

The applet has only five methods:


 public void init() The thread is initialized and is started in this method. In this example,
the constructor Thread takes the argument this. When the Thread.start() method
is called, the thread looks for a public void run() method in the this object.
 public void start() When this method is called by the system, the thread resumes
execution. If the thread is already running, this method has no effect.
 public void stop() This method suspends the thread.
 public void destroy() This method stops the thread. Thread.stop() stops and kills
the thread. However, it only kills a thread that is running, so Thread.resume() is
called beforehand.
 public void run() This is where the thread actually starts running. This example has
an infinite loop that prints a string and then sleeps for a second. Long running
threads should sleep every once in a while to give other threads a chance to run. If
not, the system may not even get a chance to paint the applet.

When Are the Methods in InfiniteThreadExample Called?

Unfortunately, its not always possible to know exactly when or if the methods are
called. It can vary from browser to browser, or even by how the user has the browser
configured. Netscape 3.0 calls init() and then calls start() the first time the applet
is loaded. If the user leaves the page with the applet, stop() is called. Returning to the
applet calls start(), but it is possible that init() may be the first called. It depends

244 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

on whether or not the applet is still residing in memory. If it is, then only start() is
called; otherwise, both init() and start() are called again. NOTES
The method destroy() is called before the applet is removed from memory. All
threads should be destroyed at this time so that its resources can be used by other applets.
The browsers can only handle so many threads. If the user visits too many applets that
don’t clean up after themselves, the browser may crash. Generally, threads should be
suspended when the user leaves the page and killed when the applet is destroyed. It is
possible to leave threads running while the user is visiting other pages, but the user may not
appreciate it.

When an event that existing threads shouldn’t take the time to handle happens in the
applet, a new thread is spawned to handle that event. After the event is handled, the new
thread quietly dies.Each thread draws a blue target.Methods such as mouseDown()
or mouseUp() are called by threads external to the applet. While the thread is running
in the applet, no other mouse movements are detected. Keeping the external thread may
not just affect the applet, but possibly the whole browser. Generally, these methods should
be returned as soon as possible. If it is necessary to do a long computation or wait for
something else, a new thread should be started. By starting a new thread, the external
thread can return almost immediately.

Handling an event with threads.


import java.applet.Applet;
import java.awt.*;

public class FiniteThreadExample extends Applet


{
Image offImage; /* off screen image */
Graphics offGraphics; /* Graphics for offImage */

public void init()


{
offImage=createImage(400,300);
offGraphics=offImage.getGraphics();
}

public void paint(Graphics g)


{
g.drawImage(offImage,0,0,null);
}

public void update(Graphics g)


{
paint(g);

245 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
}
NOTES
public boolean mouseDown(Event e, int x, int y)
{
new DrawTarget(this,x,y,offGraphics);
return true;
}
}

class DrawTarget extends Thread


{
int xPos,yPos; /* position of the target */
Applet myMaster; /* who to repaint */
Graphics offGraphics; /* Graphics to draw on */

public DrawTarget(Applet a, int x, int y, Graphics g)


{
xPos=x; yPos=y;
myMaster=a;
offGraphics=g;
start();
}

public void run()


{
int i; /* i is direction the circles are moving */
int r; /* r is the radius of the current circle */

offGraphics.setColor(Color.white);
offGraphics.setXORMode(Color.blue);
for(r=0,i=10;i>-20;i-=20) /* i=(10,-10) */
for(r+=i;(r<90)&&(r>0);r+=i) /* r=(10,20...80,80,70...10) */
{
offGraphics.fillOval(xPos-r,yPos-r,2*r,2*r);
myMaster.repaint();
try {sleep(200);}
catch (InterruptedException e) {}
}
}
}

The class FiniteThreadExample is used to paint the applet, to catch the mouse clicks,
but not to start the threads. The applet uses a class that extends the Thread class to start
new threads. The class FiniteThreadExample has four methods shown below that sets up
things, handles the painting, and catches the mouse clicks:

246 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 public void init() This method creates an image and gets a Graphics context for
that image. NOTES
 public void paint(Graphics) This method paints the Image offImage on the screen.
 public void update(Graphics) This method isn’t really necessary. It overrides
update(Graphics) in java.awt.Component, and is used to reduce flickering.
 public boolean mouseDown(Event, int, int) This method is called when there is a
mouse click in the applet. It creates a new instance of the class DrawTarget.
DrawTarget is defined in the next class.
 DrawTarget is where the threads are created to draw the targets. DrawTarget
inherits properties from java.lang.Thread and has a single constructor and method,
which are listed below:
 public DrawTarget(Applet a, int x, int y, Graphics g) The constructor copies the
parameters to instance variables and starts the thread. Applet is needed so the run
method can tell the applet to repaint. The integers x and y are the coordinates of
target. Graphics is the graphics context on which the targets are drawn. The thread
is started by simply calling start().
 public void run() This method is where the thread starts and draws the targets. It
is called sometime after start() is called in the constructor. The method first sets
offGraphics to XOR-Mode. In this mode, if something is drawn on something
previously drawn, it reverts back to its original color. Next, the thread enters the
nested for loops. Each iteration draws a circle, asks the applet to repaint, and
sleeps for 200ms. The radius of the circle is varied from 10 to 80, and then from
80 back to 10. The thread dies on its own after it exits the loops, so there is no
need to call stop().

Prolems with Multithreading

If more than one thread manipulates shared variables or objects at the same time,
corruption may result. Instance variables are shared between threads. If one is modified
by a thread, the change affects the other threads. Method variables are unique for each
threads. Each Thread has its own copy.

How to get corrupted data.

import java.lang.*;
import java.applet.Applet;

public class ProblemThreadExample extends Applet


{
CorruptedDataExample CDE;

public void start()

247 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
{
NOTES int i;

CDE=new CorruptedDataExample();
for(i=0;i<20;i++) /* start 20 threads */
new Thread(CDE,new String(“booThread”+i)).start();
}

public void stop()


{
CDE.stopThreads();
}
}

class CorruptedDataExample extends Object implements Runnable


{
int num=0; /* num will be corrupted */

public void run()


{
int i=0;
for(;;)
{
for(i=0;i<1000;i++)
{
num=num+10;
num=num-10;
}

try {Thread.sleep(10000);}
catch (InterruptedException e ) {}
System.out.println(Thread.currentThread().getName()+
” sees the number: “ + num);
}
}

void stopThreads()
{
Thread tArray[];
int numThreads;

numThreads=Thread.activeCount();
tArray=new Thread[numThreads];
Thread.enumerate(tArray);

248 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

for(int i=0;i<numThreads;i++)
if(tArray[i].getName().startsWith(“booThread”)) NOTES
tArray[i].stop();
}
}
 public void start() This method gets a new instance of the CorruptedDataExample
class and starts 20 threads. Each thread has an individual name that is from
“booThread0” t o “booThread19”. The threads st art running in
CorruptedDataExample’s run method.
 public void stop() This method calls stopThreads() in CorruptedDataExample.
The class CorruptedDataExample does not guard against corruption of its instance
variable num. The class has two methods, which are as follows:
 public void run() This method has an infinite loop that shows how data can be
corrupted by multiple threads. Twenty threads are executed in this loop at the
same time. The loop does the following:
 Adds 10 then -10 to num 1000 times
 Sleeps
 Prints a string that contains num
 The first step is the cause of the corruption. It serves no purpose other than illustrating
how data is corrupted.
 void stopThreads() All of the booThreads are stopped in this method. A list of
threads is fetched. All of the threads that have names that begin with booThread
are stopped.

The following is the ProblemThreadExample output:

booThread0 sees the number: 0


booThread1 sees the number: 0
booThread2 sees the number: 0
booThread3 sees the number: 0
booThread4 sees the number: 10
booThread6 sees the number: 10
booThread7 sees the number: 10
booThread8 sees the number: 10
booThread9 sees the number: 10
booThread10 sees the number: 0
booThread11 sees the number: 0
booThread12 sees the number: 0
booThread13 sees the number: 0
booThread5 sees the number: 0
booThread14 sees the number: 0
booThread15 sees the number: 0
booThread16 sees the number: 0
booThread17 sees the number: 0
booThread18 sees the number: 0

249 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
booThread19 sees the number: 0
NOTES booThread0 sees the number: 0
booThread1 sees the number: 0
booThread3 sees the number: 0
booThread4 sees the number: 0
booThread6 sees the number: 0
booThread8 sees the number: 0
booThread9 sees the number: 0
booThread2 sees the number: 0
booThread7 sees the number: 0
booThread10 sees the number: 10
booThread11 sees the number: 0
booThread12 sees the number: -10
booThread13 sees the number: -10
booThread5 sees the number: -10
booThread14 sees the number: -10
booThread16 sees the number: -10
booThread17 sees the number: -10
booThread18 sees the number: -10
booThread19 sees the number: -10

What Goes Wrong?

The first step in the infinite loop would have no ill effect in a single-threaded environment.
It simply adds and then subtracts 10 to a variable with the net result being no change.
However, in a multithreaded environment, the operations can interfere with each other. You
can see one scenario in the following steps in which two threads try to add 10 at the same
time.

Step Thread A Thread B num

1. Atmp num 0

2. Atmp Atmp+10 0

3. Btmp num 0

4. Btmp Btmp+10 0

5. num Btmp 10

. . . .

. . . .

. . . .

10. num Atmp 10

250 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Two num=num+10; operations have been executed, but the value of num has
only increased by 10. The problem here is that Thread A was interrupted in the middle of NOTES
its operation before it could save its results. Threads A and B have added 10 to the same
number.

This type of problem is somewhat rare, but should not be ignored. In the previous
example, 10 is added and subtracted 1000 times in 20 threads, and the problem still did
not occur that often. The bad thing about these types of bugs is that they can be extremely
difficult to find. Imagine that num is the index of an array. The problem may not show up
until long after num has been corrupted. Generally, these bugs are not reproducible, so
they are hard to catch. In some ways the problem is amplified in Java because Java is
expected to run on so many platforms. On some systems, num=num+10 may be an
atomic operation (cannot be interrupted). In this case, everything works fine. A developer
may create an applet on such a system thinking that everything is fine, but it may not work
when someone from a different system views the applet. Data corruption can also be more
common with other data types. Integer operations are simple compared to many others.
Arrays or other data structures can take much longer to process so it is much more likely
to be corrupted.

Thread Names and Current Threads

In the above example. They are named booThread0 through booThread19 when
they are created by the constructor Thread(Runnable,String). The names can be used to
identify the threads. The thread names are printed along with num in the run() method of
CorruptedDataExample. The Thread method current.Thread() is called to get a reference
to the currently running thread.

The names are also used to stop the threads. A reference is needed for each thread
so that stop() can be called to kill it. Thread.enumerate(Thread[]) gets a reference to every
thread in this group. Some of these threads are the booThreads, but they may be others.
The other threads should not be killed. Before each thread is killed it is checked to see if its
name starts with booThread.

Java’s synchronized

A way to prevent data from being corrupted by multiple threads is to prevent the
interruption of critical regions. Critical regions are places like num=num+10 above, where
only one thread should be running at once. Java’s synchronized can be used to ensure that
only one thread is in a critical region at once. When the thread enters a synchronized code
block, it tries to get a lock on that region. While the thread is in the critical region, no other
thread can enter the critical region. If a thread tries to enter and the code is already locked,
the thread has to wait for the other thread to leave the critical region

251 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The run() method in CorruptedDataExample can be modified to the following:


NOTES public void run()
{
int i=0;
int tmp; /*new*/
for(;;)
{
for(i=0;i<1000;i++)
{
synchronized (this) /*new*/
{
num=num+10;
num=num-10;
}
}

try {Thread.sleep(10000);}
catch (InterruptedException e ) {}
synchronized (this) /*new*/
{tmp=num;} /*new*/
System.out.println(Thread.currentThread().getName()+
” sees the number: “ + tmp); /*new*/
}
}

The following lines make up a protected critical region:

synchronized (this)
{
num=num+10;
num=num-10;
}

synchronized (this) ensures that only one thread can be in the following code block.
The argument this tells the thread to use the lock for this this object.

The variable num is also referenced when the string is printed. The new code is as
follows:
synchronized (this) /*new*/
{tmp=num;} /*new*/
System.out.println(currentThread().getName()+
” sees the number: “ + tmp); /*new*/

A critical region is used to copy num to temporary storage. The string is then printed
using the temporary storage. It would have been possible to synchronize the print line

252 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

directly, but it would cut performance because the print line does many other things that
have nothing to do with referencing num. All the threads waiting to enter the critical region NOTES
will needlessly wait longer while the print line is executed. Generally, the synchronized
blocks should be as small as possible while still protecting the critical region.

You may also think that the other variables in the run method, i and tmp, also
need to be synchronized, but it’s not necessary. Both i and tmp are method variables so
each running thread has its own private copy. There are 20 i’s and tmp’s but there is only
one num.

Synchronizing threads

This example shows how synchronized can be used to control critical regions. There
are two synchronized methods: drawRoundTarget() and drawSquareTarget(). If a thread
is in a synchronized method, no other thread can be in any synchronized method that uses
the same lock. This example draws only one square or circle at a time. The seven methods
of the SynchronizedThreadExample applet are shown below.

Using synchronized.
import java.applet.Applet;
import java.awt.*;

public class SynchronizedThreadExample extends Applet


implements Runnable
{
Image offImage; /* off screen image */
Graphics offGraphics; /* Graphics for offImage */
Thread Thr1, Thr2; /* threads */

public void start()


{
offImage=createImage(400,300);
offGraphics=offImage.getGraphics();
offGraphics.setColor(Color.white);
offGraphics.setXORMode(Color.blue);
Thr1=new Thread(this); Thr1.start();
Thr2=new Thread(this); Thr2.start();
}

public void stop()


{
Thr1.stop();
Thr2.stop();
}

253 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
public void paint(Graphics g)
NOTES {
g.drawImage(offImage,0,0,null);
}

public void update(Graphics g)


{
paint(g);
}

public void run()


{
for(;;)
{
drawRoundTarget();
drawSquareTarget();
}
}

synchronized void drawRoundTarget()


{
for(int r=0,i=10;i>-20;i-=20) /* i=(10,-10) */
for(r+=i;(r<90)&&(r>0);r+=i) /* r=(10,20...80,80,70...10) */
{
offGraphics.fillOval(200-r,150-r,2*r,2*r);
repaint();
try {Thread.currentThread().sleep(200);}
catch (InterruptedException e) {}
}
}

synchronized void drawSquareTarget()


{
int i,r;
for(r=0,i=10;i>-20;i-=20) /* i=(10,-10) */
for(r+=i;(r<90)&&(r>0);r+=i) /* r=(10,20...80,80,70...10) */
{
offGraphics.fillRect (200-r,150-r,2*r,2*r);
repaint();
try {Thread.currentThread().sleep(250);}
catch (InterruptedException e) {}
}
}

254 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The methods of the SynchronizedThreadExample applet are as follows:


 public void start()/stop() The threads are started and stopped in these methods.
NOTES
 public void paint/update(Graphics) These methods paint the applet.
 public void run() Both threads start executing in this method. It is an infinite loop
that draws round and then square targets.
 synchronized void drawRoundTarget() This method is synchronized. Only one
thread can be inside drawing circles.
 synchronized void drawSquareTarget() This method is like drawRoundTarget,
but draws squares instead of circles.

Multiple Locks

What if two locks are needed?

The current applet only allows one target to be drawn at a time, be it round or square.
Suppose that you want the applet to draw a round and a square target at the same time;
you would need two locks for two independent critical regions. The problem is that each
object has only one lock. Creating separate classes for drawRoundTarget() and
drawSquareTarget() could solve the problem, but it may not be convenient. A better way
is to create new objects, and to use their locks to control the methods.

This is done by modifying previous example as follows:


Object RoundSync,SquareSync; /*new*/

public void start()


{
. .
. .
. .
RoundSync=new Object(); /*new*/
SquareSync=new Object(); /*new*/
Thr1=new Thread(this); Thr1.start();

Thr2=new Thread(this); Thr2.start();

void drawRoundTarget()
{
synchronized (RoundSync) /*new*/

255 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
{
NOTES for(int r=0,i=10;i>-20;i-=20)
. .
. .
. .
}
}

void drawSquareTarget()
{
synchronized (SquareSync) /*new*/
{
for(r=0,i=10;i>-20;i-=20)
. .
. .
. .
}
}
}

Two new Objects are created: RoundSync and SquareSync. The Objects don’t actually
do anything themselves, but their locks are used when drawing the targets. The instances of
Object are obtained in the start method. synchronized (RoundSync) is put in front of the
for loops in the drawRoundTarget() method and drawSquareTarget() is modified similarly.
When a thread tries to execute the body of the target drawing methods, it first has to get a
lock. drawRoundTarget() gets a lock from the object RoundSync and drawSquareTarget()
gets a lock from SquareSync(). After these modifications have been made, the
drawRoundTarget() and drawSquareTargets() methods do not block each other. The applet
draws round and square targets at the same time. But it is not able to draw two round
targets or two square targets at the same time.

The Dining philosopher problem

The Dining Philosophers problem is a classic concurrent programming problem. In


the problem, a philosopher only does two things, think and eat. A philosopher thinks for a
while, and then gets hungry. Then it eats until it gets full, and then starts thinking again. Each
philosopher is so involved in its own thinking or eating that it’s oblivious to anything else.
The problem has five philosophers that are sitting at a table. Between neighboring
philosophers there is a single chop stick. A philosopher must have both the stick on its right
and the stick on its left in order for it to eat. Obviously, no two neighboring philosophers
can be eating at the same time. The philosophers cannot disturb each other. If a philosopher
wants to start eating and its neighbor has one of the sticks, the philosopher must wait until

256 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

the stick becomes available. To solve the problem, an algorithm must be designed to let all
of the philosophers eat. NOTES
A simple algorithm for the philosophers could be:
Think Get right chopstick
Get left chopstick
Eat
Drop left chopstick
Drop right chopstick
Repeat

There is one very serious problem with this algorithm. Suppose that each philosopher
picks up the right chopstick at the same time. When they try to get the left stick, it won’t be
there. The neighbor to the left has it. All of the philosophers starve to death with a chopstick
in one hand and food on the table. This is known as a deadlock.

Deadlocks

Deadlocks are always a danger in multithreaded environments. A deadlock has


occurred because

 Each thread needed exclusive use of the chopsticks.


 One thread is not allowed to take a chopstick from its neighbor.
 Each thread is waiting while holding a chopstick that another thread is waiting for.

All deadlocks in any problem have the same reasons for deadlocking. Instead of
waiting for chopsticks, they are waiting for some other resource or resources. If only one
of the conditions can be broken, a deadlock will not occur.

The first condition is usually hard to break. In the previous algorithm this could done
by allowing philosophers to share a chopstick, but that isn’t really possible. Sometimes
threads need exclusive use of a resource. Things like the instance of a class or a socket
may require that only one thread may use it at once.

The second condition can sometimes be used to avoid deadlocks. If a philosopher


was allowed to take a chopstick from its neighbor, there would not be a deadlock. However,
if the philosophers keep taking sticks from each other, they may never get a chance to take
a bite. Some problems can be solved by allowing resource stealing. It depends on the
resource and the problem.

If the deadlock cannot be avoided with the other conditions, it should be avoided by
breaking the third condition. The philosophers can avoid a deadlock if there is a special
chopstick that they aren’t allowed to hold while they are waiting for the second chopstick.
They are allowed to eat with the special stick, but they can’t just hold it. An algorithm
should not be too strict, otherwise the resources may be underused. For example, an

257 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

algorithm could be made that only allows one philosopher to eat at once. Obviously, there
NOTES would be no deadlocks, but a philosopher may have to wait longer before it can eat.

A Solution to the Dining Philosophers Problem

There are many solutions to the Dining Philosophers problem. One solution follows.

One of the chopsticks is marked as gold, while the rest are wood. No philosopher is
allowed to hold the gold stick without eating. This prevents the philosophers from
deadlocking. The philosophers picks up one chopstick then the other. If the gold stick is
picked up first, it is put down and the other stick is picked up. It is possible that in the time
between putting down the gold chopstick and picking up the other chopstick, all the other
philosophers will have eaten and moved the gold chopstick all the way around the table, so
when the philosopher picks up the other chopstick, it too is the gold chopstick. If this
happens, the philosopher starts over. The solution is as follows:

Think
Pick up right chopstick
If right chopstick is gold
Drop right chopstick
Pick up left chopstick
If left chopstick is gold
Start over
Pick up right chopstick
Else
Pick up left chopstick
Eat
Switch chopsticks in hands
Drop right chopstick
Drop left chopstick

Repeat

The chopsticks are switched when the philosopher puts down the chopsticks. This
allows the philosophers equal chances to eat. Otherwise, the philosopher to the left of the
gold chopstick would be disadvantaged.

The philosophers may interfere with each other when they try to pick up a chopstick.
A critical region is used to ensure that one philosopher can pick up or put down a chopstick.
If one philosopher is picking up or putting down a stick, its neighbor is not allowed to touch
the stick. What if a philosopher enters the critical section to get a chopstick, but the chopstick
is not there? The philosopher must wait until the stick returns. This is done by a special
wait in the critical section. The philosopher releases its lock and waits in the critical
section. This allows the other philosopher to enter the critical section to return the chopstick.
After the chopstick is returned, the waiting philosopher is woken up. After awakening, the

258 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

philosopher reacquires the lock and continues executing. At any one time there can be only
one philosopher running in the critical section, but it is OK if another philosopher is also NOTES
sleeping in the critical section.

Java’s wait() and notify()

Java has three wait() and two notify() methods that aid in synchronizing threads. The
wait() methods cause the thread to pause in the critical region. While paused, the thread
releases its lock. It must get the lock again before it starts executing again. The notify()
methods wake up threads that have called wait(). Calling notify() when no wait() has been
called has no effect. The methods shown below are in the Object class and can only be
called in a synchronized block or method.
 public final void wait() This method causes the thread to wait forever until a notify()
or notifyAll() is called. The thread releases its lock on the critical regions so that
other threads may enter.
 public final void wait(long m) This method causes the thread to wait m milliseconds
for a notify() or notifyAll() to be called. After the time is expired, the thread tries to
resume execution. However, it must first reobtain the lock for the critical region.
Another thread may have entered the critical section while the thread was waiting.
 public final void wait(long m, int n) This method is similar to the previous one
except that it waits for m milliseconds plus n nanoseconds.
 public final void notify() This method wakes up a thread that has previously called
wait(). The thread that was waiting has to get the lock before it can resume
execution. It has to wait until the current thread leaves the critical region. Only one
thread that has called wait() is woken up. It is not guaranteed that the first thread
that called wait() is the first one woken up.
 public final void notifyAll() This method wakes up all the threads that have called
wait(). Each waiting thread has to get the lock for the critical region before it
resumes. There can still be only one thread running in the critical section at once.

Dining Philosophers Example

The Dining Philosophers applet uses four classes: the ones shown below. The first
class, DiningPhilosophers, extends the Applet class. The structure of this class is similar to
the first example. Philosopher threads are created when the applet is initialized. They are
suspended if the user leaves the page and resumed if the user returns. However, unlike the
first example, no threads are created in this class.

259 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

1.The class DiningPhilosophers.


NOTES
public class DiningPhilosophers extends Applet
{
final int numPhils=5;

Image offImage; /* off screen image */


Graphics offGraphics; /* Graphics for offImage */
Philosopher Phil[] = new Philosopher[numPhils];
Chopstick Stick[] = new Chopstick[numPhils];
ScenePainter painter;

public void init()


{
int i;

offImage=createImage(400,300);
offGraphics=offImage.getGraphics();
painter=new ScenePainter(offGraphics,this,numPhils);

for(i=0;i<numPhils;i++)
Stick[i]=new Chopstick (i==0 ? Chopstick.gold :
Chopstick.wood,
painter,i);
for(i=0;i<numPhils;i++)
Phil[i]= new Philosopher(Stick[i],Stick[(i+1)%numPhils],
painter,i);
}

public void start()


{
int i;
for(i=0;i<numPhils;i++)
Phil[i].resume();
}

public void stop()


{
int i;
for(i=0;i<numPhils;i++)
Phil[i].suspend();
}

public void destroy()


{

260 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

int i;
for(i=0;i<numPhils;i++) NOTES
{
Phil[i].resume();
Phil[i].stop();
}
}

public void paint(Graphics g)


{
g.drawImage(offImage,0,0,null);
}

public void update(Graphics Dijkstra)


{
paint(Dijkstra);
}
}

The class DiningPhilosophers has six methods and is similar to the


InfiniteThreadExample.
· public void init() This method initializes the applet. It creates five instances of the
classes Chopstick and Philosopher. One of the Chopstick classes is created as a
gold chopstick, the rest are wood. Each Philosopher can reach two Chopsticks.
On the right is Chopstick i, and on the left is Chopstick i+1 mod 5.
· public void start() This method resumes philosopher execution.
· public void stop() This method suspends philosopher execution.
· public void destroy() This method kills the philosophers.
· public void paint()/update() These two methods paint the state of the philosophers.
Each philosopher at the table is its own thread. The thread is created in the class
Philosopher by extending Thread. The methods in the class control the philosopher’s life
of thinking and eating. Initially, the philosopher is thinking. After some time, the philosopher
picks up the two chopsticks next to it and starts eating. It calls methods in the Chopstick
class to get the chopsticks. The philosopher also paints its state by calling methods in the
ScenePainter class.

2.The class Philosopher.


class Philosopher extends Thread
{
final int ThinkTime=5000, EatTime=3000;
Chopstick rightStick,leftStick;
ScenePainter painter;
int rightHand,leftHand;

261 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
int myNum;
NOTES public Philosopher(Chopstick right, Chopstick left,
ScenePainter p, int n)
{
painter=p;
myNum=n;
rightStick=right;
leftStick=left;
start();
}

public void run()


{
for(;;)
{
think();
PickUpSticks();
eat();
PutDownSticks();
}
}

void think()
{
painter.drawThink(myNum);
try {sleep((int)(ThinkTime*Math.random()));}
catch (InterruptedException e) {}
painter.drawThink(myNum);
}

void PickUpSticks()
{
for(boolean gotem=false;!gotem;)
{
painter.drawFirstGrab(myNum);
rightHand=rightStick.grabStick();

painter.drawSecondGrab(myNum);
if(rightHand==Chopstick.gold)
{
painter.drawGoldGrab(myNum);

rightStick.dropStick(rightHand);
leftHand=leftStick.grabStick();
if(leftHand==Chopstick.gold)

262 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
leftStick.dropStick(leftHand); NOTES
continue; /* gold stick went around table */
}
rightHand=rightStick.grabStick();
painter.drawGoldGrab(myNum);
}
else leftHand=leftStick.grabStick();

painter.drawSecondGrab(myNum);
painter.drawFirstGrab(myNum);
gotem=true;
}
}
void eat()
{
painter.drawEat(myNum);
try {sleep((int)(EatTime*Math.random()));}
catch (InterruptedException e) {}
painter.drawEat(myNum);
}
void PutDownSticks()
{/* swap sticks and put them down */
rightStick.dropStick(leftHand);
leftStick.dropStick(rightHand);
}
}

The class Philosopher is used to start the threads for the Dining Philosophers
applet. The class has the following five methods:
 public void run() This method defines the philosophers actions. Each philosopher
thinks, waits to pick up its chopsticks, eats, returns the chopsticks, and repeats
the cycle.
 void think() This method is where the philosopher thinks. The thinking image is
drawn and the philosopher sleeps for a random amount of time.
 void PickUpSticks() In this method, the philosopher picks up the chopsticks in a
way that is fair and avoids deadlocks.
 void eat() This method is where the philosopher eats. The eating image is drawn
and the philosopher sleeps for a random amount of time.
 void PutDownSticks() In this method, the philosopher returns the sticks. The
chopsticks are switched when they are put down so that the gold stick is not
always in the same place.

263 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
 All of the synchronization is done in the Chopstick class. There is one instance of
NOTES this class for each chopstick on the table. The class is used by the philosophers
when they want to pick up or return a chopstick. The three states of the chopstick
are represented by the variable stickHolder: noStick means the chopstick is gone,
wood means this is the wooden stick, and gold means this is the golden stick.
stickHolder is an instance variable. More than one philosopher may be trying to
get/drop it at once, so there is a danger of data corruption. The methods of this
class are synchronized to ensure that stickHolder does not get corrupted.

3.The class Chopstick.


class Chopstick extends Object
{
final static int noStick=0;
final static int wood=1;
final static int gold=2;
ScenePainter painter;
int stickHolder;
int myNum;

public Chopstick(int stick, ScenePainter p, int n)


{
painter=p;
myNum=n;
dropStick(stick);
}
synchronized int grabStick()
{
int Thuy;

if(stickHolder==noStick)
try {wait();} catch (InterruptedException e) {}
painter.drawStick(myNum,stickHolder);
Thuy=stickHolder;
stickHolder=noStick;
return Thuy;
}

synchronized void dropStick(int stick)


{
stickHolder=stick;
painter.drawStick(myNum,stickHolder);
notify();
}
}

264 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The class Chopstick is used to synchronize the threads in the Dining Philosophers
applet. The class has the following two methods: NOTES
 synchronized int grabStick() Philosophers (threads) will come into this method
when they attempt to get a stick. If the stick is there, the method gives the stick to
the philosopher. If the stick has already been taken by its neighbor, the philosopher
waits for the stick to be returned.
 synchronized void dropStick() The philosophers use this method to return the
sticks. If the other philosopher is waiting for this chopstick, it is woken.

The class ScenePainter is used by the philosophers to paint their state. If a philosopher
starts eating, puts down a stick, or does anything else, it calls methods in this class. The
states of philosophers (for example, eating or waiting for a stick) are represented by different
shapes. When a philosopher starts thinking, it calls drawThink(myNum) to draw the
philosopher in its thinking state. Then, when it is done thinking, it calls drawThink(myNum)
again to erase the philosopher. All of the methods in this class are called in pairs. The first
call draws a state and the second erases it.

4. The class ScenePainter.


class ScenePainter extends Object
{
int sX1[], sY1[], sX2[], sY2[], pX[], pY[];
final int xOffset=150, yOffset=150, Tscale=70;
final int rg=2, rEating=15, rThinking=8, rFirst=7;
final int rSecond=3, rGold=5;
Graphics G;
Component C;

public ScenePainter(Graphics g, Component c, int numPhils)


{
int i;
pX = new int[numPhils]; pY = new int[numPhils];
sX1 = new int[numPhils]; sY1 = new int[numPhils];
sX2 = new int[numPhils]; sY2 = new int[numPhils];
double arc=Math.PI/numPhils;

G=g; C=c;

for(i=0;i<numPhils;i++)
{
pX[i]= (int)(xOffset+ Tscale*Math.cos(i*2*arc));
pY[i]= (int)(yOffset+ Tscale*Math.sin(i*2*arc));
sX1[i]=(int)(xOffset+ Tscale*Math.cos(i*2*arc+arc));

265 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
sY1[i]=(int)(yOffset+ Tscale*Math.sin(i*2*arc+arc));
NOTES sX2[i]=(int)(xOffset+.7*Tscale*Math.cos(i*2*arc+arc));
sY2[i]=(int)(yOffset+.7*Tscale*Math. sin(i*2*arc+arc));
}
G.setColor(Color.white);
G.setXORMode(Color.blue);
}

void drawStick(int num, int stick)


{
G.drawLine(sX1[num],sY1[num],sX2[num],sY2[num]);
if(stick==Chopstick.gold)
G.fillOval(sX1[num]-rg,sY1[num]-rg,rg+rg,rg+rg);
C.repaint();
}
void drawEat(int num)
{
fillCircle(num,rEating);
}

void drawThink(int num)


{
fillCircle(num,rThinking);
}
void drawFirstGrab(int num)
{
fillSquare(num,rFirst);
}

The class ScenePainter is used to draw the state of all the philosophers in the Dining
Philosophers applet.
 public ScenePainter(Graphics, Component, int) The constructor for this class
calculates the position of each philosopher and chopstick.
 void drawStick(num, stick) This method draws/erases the chopstick specified by
num. A stick is represented by drawing a line between the two philosophers that
may use it. If stick is the gold chopstick, a small circle is added to the end of the
chopstick.
 void drawEat(num) This method draws/erases the philosopher specified by num
in its eating state. A large circle represents the eating state.
 void drawThink(num) This method draws/erases a small circle that represents a
philosopher in its thinking state.

266 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 void drawFirstGrab(num) This method draws/erases a small square. The method


is called when the philosopher tries to grab the first stick. NOTES
 void drawSecondGrab(num) This method erases/draws a smaller square inside
the square drawn by drawFirstGrab(num). The method is called after the philosopher
already has one chopstick and is trying to grab the second. A philosopher in this
state is represented by a small hollow square.
 void drawGoldGrab(num) This method is called if the first stick the philosopher
picked up is the gold stick. It is only called after both drawFirstGrab(num) and
drawSecondGrab(num) have been called. A philosopher in this state is represented
by a small hollow square with a tiny circle in the middle.

4.5 LANGUAGE PACKAGE

java.lang
java.lang is automatically imported into all programs. It contains classes
and interfaces that are fundamental to virtually all of Java programming. It is
Java’s most widely used package.
java.lang includes the following classes:
Boolean
Long
StackTraceElement (Java 2,1.4)
Byte
Math
StrictMath (Java 2,1.3)
Character
Number
String
Class
Object
StringBuffer
ClassLoader
Package (Java 2)
System
Compiler
Process
Thread
Double
Runtime
ThreadGroup
Float
RuntimePermission (Java 2)
ThreadLocal (Java 2)
InheritableThreadLocal (Java 2)
SecurityManager

267 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
Throwable
NOTES Integer
Short
Void

In addition, there are two classes defined by Character: Character.Subset and

Character.UnicodeBlock. These were added by Java 2.

java.lang also defines the following interfaces:


 Cloneable
 Comparable
 Runnable
 CharSequence

Simple Type Wrappers

As we know earlier, Java uses simple types, such as int and char, for performance
reasons. These data types are not part of the object hierarchy. They are passed by value to
methods and cannot be directly passed by reference. Also, there is no way for two methods
to refer to the same instance of an int. At times, you will need to create an object
representation for one of these simple types. For example, there are enumeration classes
discussed in Chapter 15 that deal only with objects; to store a simple type in one of these
classes, you need to wrap the simple type in a class. To address this need, Java provides
classes that correspond to each of the simple types. In essence, these classes encapsulate,
or wrap, the simple types within a class. Thus, they are commonly referred to as type
wrappers.

Number

The abstract class Number defines a superclass that is implemented by the classes
that
wrap the numeric types byte, short, int, long, float, and double. Number has abstract
methods that return the value of the object in each of the different number formats.

That is, doubleValue( ) returns the value as a double, floatValue( ) returns the value

as a float, and so on. These methods are shown here:

byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )

268 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The values returned by these methods can be rounded.


NOTES
Number has six concrete subclasses that hold explicit values of each numeric type:

Double, Float, Byte, Short, Integer, and Long.

Double and Float

Double and Float are wrappers for floating-point values of type double and float,

respectively.
The constructors for Float are shown here:
Float(double num)
Float(float num)
Float(String str) throws NumberFormatException

As you can see, Float objects can be constructed with values of type float or double.

They can also be constructed from the string representation of a floating-point number.

The constructors for Double are shown here:


Double(double num)
Double(String str) throws NumberFormatException

Double objects can be constructed with a double value or a string containing a

floating-point value.

The methods defined by Float are shown in Table below. The methods defined by

Double are shown below. Both Float and Double define the following constants:

MAX_VALUE Maximum positive value


MIN_VALUE Minimum positive value
NaN Not a number
POSITIVE_INFINITY Positive infinity
NEGATIVE_INFINITY Negative infinity
TYPE The Class object for float or double

Example

The following example creates two Double objects—one by using a double value
and the other by passing a string that can be parsed as a double:
class DoubleDemo {
public static void main(String args[]) {
Double d1 = new Double(3.14159);
Double d2 = new Double(“314159E-5”);

269 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

System.out.println(d1 + “ = “ + d2 + “ -> “ + d1.equals(d2));


NOTES }
}

As you can see from the following output, both constructors created identical

Double

instances, as shown by the equals( ) method returning true:

3.14159 = 3.14159 –> true

Understanding isInfinite( ) and isNaN( )

Float and Double provide the methods isInfinite( ) and isNaN( ), which help when

manipulating two special double and float values. These methods test for two unique

values defined by the IEEE floating-point specification: infinity and NaN (not a

number). isInfinite( ) returns true if the value being tested is infinitely large or small

in magnitude. isNaN( ) returns true if the value being tested is not a number.

The following example creates two Double objects; one is infinite, and the other is

not a number:\

// Demonstrate isInfinite() and isNaN()

class InfNaN {
public static void main(String args[]) {
Double d1 = new Double(1/0.);
Double d2 = new Double(0/0.);
System.out.println(d1 + “: “ + d1.isInfinite() + “, “ + d1.isNaN());
System.out.println(d2 + “: “ + d2.isInfinite() + “, “ + d2.isNaN());
}
}
This program generates the following output:
Infinity: true, false
NaN: false, true

Byte, Short, Integer, and Long

The Byte, Short, Integer, and Long classes are wrappers for byte, short, int, and long
integer types, respectively. Their constructors are shown here:
Byte(byte num)
Byte(String str) throws NumberFormatException

270 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Short(short num)
Short(String str) throws NumberFormatException NOTES
Integer(int num)
Integer(String str) throws NumberFormatException
Long(long num)
Long(String str) throws NumberFormatException
As you can see, these objects can be constructed from numeric values or from strings

that contain valid whole number values.

The methods defined by these classes are shown in Tables 14-3 through 14-6. As
you can see, they define methods for parsing integers from strings and converting strings
back into integers. Variants of these methods allow you to specify the radix, or numeric
base, for conversion. Common radixes are 2 for binary, 8 for octal, 10 for decimal, and 16
for hexadecimal.

The following constants are defined:


MIN_VALUE Minimum value
MAX_VALUE Maximum value
TYPE The Class object for byte, short, int, or long

Converting Numbers to and from Strings

One of the most common programming chores is converting the string representation
of a number into its internal, binary format. Fortunately, Java provides an easy way to
accomplish this. The Byte, Short, Integer, and Long classes provide the parseByte( ),
parseShort( ), parseInt( ), and parseLong( ) methods, respectively. These methods
return the byte, short, int, or long equivalent of the numeric string with which they are
called. (Similar methods also exist for the Float and Double classes.)

The following program demonstrates parseInt( ). It sums a list of integers entered


by the user. It reads the integers using readLine( ) and uses parseInt( ) to convert these

strings into their int equivalents.

/* This program sums a list of numbers entered by the user. It converts the string
representation of each number into an int using parseInt().
*/
import java.io.*;
class ParseDemo {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new

271 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
BufferedReader(new InputStreamReader(System.in));
NOTES String str;
int i;
int sum=0;
System.out.println(“Enter numbers, 0 to quit.”);
do {
str = br.readLine();
try {
i = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.println(“Invalid format”);
i = 0;
}
sum += i;
System.out.println(“Current sum is: “ + sum);
} while(i != 0);
}
}

To convert a whole number into a decimal string, use the versions of toString( )
defined in the Byte, Short, Integer, or Long classes. The Integer and Long classes also
provide the methods toBinaryString( ), toHexString( ), and toOctalString( ), which
convert a value into a binary, hexadecimal, or octal string, respectively.

The following program demonstrates binary, hexadecimal, and octal conversion:

/* Convert an integer into binary, hexadecimal, and octal. */


class StringConversions {
public static void main(String args[]) {
int num = 19648;
System.out.println(num + “ in binary: “ +
Integer.toBinaryString(num));
System.out.println(num + “ in octal: “ +
Integer.toOctalString(num));
System.out.println(num + “ in hexadecimal: “ +
Integer.toHexString(num));
}

The output of this program is shown here:


19648 in binary: 100110011000000
19648 in octal: 46300
19648 in hexadecimal: 4cc0

272 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Character
NOTES
Character is a simple wrapper around a char. The constructor for Character is
Character(char ch)

Here, ch specifies the character that will be wrapped by the Character object

being created.

To obtain the char value contained in a Character object, call charValue( ),


shown here:
char charValue( )
It returns the character.
The Character class defines several constants, including the following:
MAX_RADIX The largest radix
MIN_RADIX The smallest radix
MAX_VALUE The largest character value
MIN_VALUE The smallest character value
TYPE The Class object for char

Character includes several static methods that categorize characters and alter their

case.. The following example demonstrates several of these methods.


// Demonstrate several Is... methods.
class IsDemo {
public static void main(String args[]) {
char a[] = {‘a’, ‘b’, ‘5’, ‘?’, ‘A’, ‘ ‘};
for(int i=0; i<a.length; i++) {
if(Character.isDigit(a[i]))
System.out.println(a[i] + “ is a digit.”);
if(Character.isLetter(a[i]))
System.out.println(a[i] + “ is a letter.”);
if(Character.isWhitespace(a[i]))
System.out.println(a[i] + “ is whitespace.”);
if(Character.isUpperCase(a[i]))
System.out.println(a[i] + “ is uppercase.”);
if(Character.isLowerCase(a[i]))
System.out.println(a[i] + “ is lowercase.”);
}
}
}
The output from this program is shown here:
a is a letter.
a is lowercase.
b is a letter.
b is lowercase.

273 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
5 is a digit.
NOTES A is a letter.
A is uppercase.
is whitespace.

Character defines the forDigit( ) and digit( ) methods, shown here:

static char forDigit(int num, int radix)


static int digit(char digit, int radix)

forDigit( ) returns the digit character associated with the value of num. The radix of the

conversion is specified by radix. digit( ) returns the integer value associated with the

specified character (which is presumably a digit) according to the specified radix.

Another method defined by Character is compareTo( ), which has the following two
forms:

int compareTo(Character c)
int compareTo(Object obj)

The first form returns 0 if the invoking object and c have the same value. It returns a

negative value if the invoking object has a lower value. Otherwise, it returns a positive

value. The second form works just like the first if obj is a reference to a Character.

Otherwise, a ClassCastException is thrown. These methods were added by Java 2.


Method Description
static boolean isTitleCase(char ch) Returns true if ch is a Unicode
titlecase character. Otherwise,
it returns false.
static boolean isUnicodeIdentifierPart(char ch) Returns true if ch is allowed as
part of a Unicode identifier (other
than the first character).
Otherwise, it returns false.
static boolean isUnicodeIdentifierStart(char ch) Returns true if ch is allowed
as the first character of a Unicode
identifier. Otherwise,
it returns false.

static boolean isUpperCase(char ch) Returns true if ch is an uppercase letter. Otherwise,


it returns false. static boolean isWhitespace(char ch) Returns true if ch is whitespace.
Otherwise, it returns false.
static char toLowerCase(char ch) Returns lowercase equivalent of ch.

274 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

static char toTitleCase(char ch) Returns titlecase equivalent of ch.


static char toUpperCase(char ch) Returns uppercase equivalent of ch. NOTES
Java 2, version 1.4 adds a method called getDirectionality( ) which can be used to
determine the direction of a character. Several new constants have been added which
describe directionality. Most programs will not need to use character directionality.

Character also defines the equals( ) and hashCode( ) methods.

Two other character-related classes are Character.Subset, used to describe a subset of


Unicode, and Character.UnicodeBlock, which contains Unicode character blocks.

Boolean

Boolean is a very thin wrapper around boolean values, which is useful mostly when you
want to pass a boolean variable by reference. It contains the constants TRUE and FALSE,
which define true and false Boolean objects. Boolean also defines the TYPE field, which
is the Class object for boolean. Boolean defines these constructors:

Boolean(boolean boolValue)
Boolean(String boolString)

In the first version, boolValue must be either true or false. In the second version, if
boolString contains the string “true” (in uppercase or lowercase), then the new Boolean

object will be true. Otherwise, it will be false.

Boolean defines the methods shown in Table 14-8.

Void

The Void class has one field, TYPE, which holds a reference to the Class object for type

void. You do not create instances of this class.

Process
The abstract Process class encapsulates a process—that is, an executing program.
It is used primarily as a superclass for the type of objects created by exec( ) in the Runtime
class described in the next section.

Runtime

The Runtime class encapsulates the run-time environment. You cannot instantiate a
Runtime object. However, you can get a reference to the current Runtime object by
calling the static method Runtime.getRuntime( ). Once you obtain a reference to the
current Runtime object, you can call several methods that control the state and behavior
of the Java Virtual Machine. Applets and other untrusted code typically cannot call any of

275 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

the Runtime methods without raising a SecurityException. The methods defined by


NOTES Runtime are shown in Table 14-10. Java 2 deprecates the method runFinalizersOnExit(
). This method was added by Java 1.1 but was deemed unstable.

Memory Management

Although Java provides automatic garbage collection, sometimes you will want to
know how large the object heap is and how much of it is left. You can use this information,
for example, to check your code for efficiency or to approximate how many more objects
of a certain type can be instantiated. To obtain these values, use the totalMemory( ) and
freeMemory( ) methods.

As we mentioned in Part I, Java’s garbage collector runs periodically to recycle unused


objects. However, sometimes you will want to collect discarded objects prior to the
collector’s next appointed rounds. You can run the garbage collector on demand by calling
the gc( ) method. A good thing to try is to call gc( ) and then call freeMemory( ) to get a
baseline memory usage. Next, execute your code and call freeMemory( ) again to see
how much memory it is allocating. The following program illustrates this idea:

// Demonstrate totalMemory(), freeMemory() and gc().


class MemoryDemo {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
long mem1, mem2;
Integer someints[] = new Integer[1000];
System.out.println(“Total memory is: “ +
r.totalMemory());
mem1 = r.freeMemory();
System.out.println(“Initial free memory: “ + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println(“Free memory after garbage collection: “ + mem1);
for(int i=0; i<1000; i++)
someints[i] = new Integer(i); // allocate integers
mem2 = r.freeMemory();
System.out.println(“Free memory after allocation: “ + mem2);
System.out.println(“Memory used by allocation: “ + (mem1-mem2));
// discard Integers
for(int i=0; i<1000; i++) someints[i] = null;
r.gc(); // request garbage collection
mem2 = r.freeMemory();
System.out.println(“Free memory after collecting” + “ discarded Integers: “ + mem2);
}
}

276 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Sample output from this program is shown here (of course, your actual results may vary):
NOTES
Total memory is: 1048568
Initial free memory: 751392
Free memory after garbage collection: 841424
Free memory after allocation: 824000
Memory used by allocation: 17424
Free memory after collecting discarded Integers: 842640
Executing Other Programs
In safe environments, you can use Java to execute other heavyweight processes (that
is, programs) on your multitasking operating system. Several forms of the exec( ) method
allow you to name the program you want to run as well as its input parameters. The exec(
) method returns a Process object, which can then be used to control how your Java
program interacts with this new running process. Because Java can run on a variety of
platforms and under a variety of operating systems, exec( ) is inherently
environment-dependent.
The following example uses exec( ) to launch notepad, Windows’ simple text editor.
Obviously, this example must be run under the Windows operating system.

// Demonstrate exec().
class ExecDemo {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(“notepad”);
} catch (Exception e) {
System.out.println(“Error executing notepad.”);
}
}
}
There are several alternate forms of exec( ), but the one shown in the example is the
most common. The Process object returned by exec( ) can be manipulated by Process’
methods after the new program starts running. You can kill the subprocess with the destroy(
) method. The waitFor( ) method causes your program to wait until the subprocess finishes.
The exitValue( ) method returns the value returned by the subprocess when it is finished.
This is typically 0 if no problems occur. Here is the preceding exec( ) example modified to
wait for the running process to exit:

// Wait until notepad is terminated.


class ExecDemoFini {
public static void main(String args[]) {
277 ANNA UNIVERSITY CHENNAI
DMC 1705 / DMC 1935
Runtime r = Runtime.getRuntime();
NOTES Process p = null;
try {
p = r.exec(“notepad”);
p.waitFor();
} catch (Exception e) {
System.out.println(“Error executing notepad.”);
}
System.out.println(“Notepad returned “ + p.exitValue());
}
}
While a subprocess is running, you can write to and read from its standard input and
output. The getOutputStream( ) and getInputStream( ) methods return the handles to
standard in and out of the subprocess. (I/O is examined in detail in Chapter 17.)
System
The System class holds a collection of static methods and variables. The standard
input, output, and error output of the Java run time are stored in the in, out, and err
variables. The methods defined by System are shown in Table 14-11. Many of the methods
throw a THE JAVA LIBRARY
SecurityException if the operation is not permitted by the security manager. One
other point: Java 2 deprecated the method runFinalizersOnExit( ). This method was
added by Java 1.1, but was determined to be unstable.

Summary

The basic concept of AWT, basics of graphics, different layout managers and event
handling are the highlights of this unit. The concept of garbage collection, and multithreading
and thread related concepts are also discussed in this unit. Some of important packages of
Java are also highlighted.

Questions

1. Explain the process of creating user interface in a form based application.


2. What is the difference between an applet and an application?
3. Write a java program to draw a circle and a rectangle.
4. List out the different layout managers supported in java.
5. Explain the lifecycle of thread.
6. List out the classes associated with language package and highlight their features.

278 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
UNIT V

ADVANCED JAVA PROGRAMMING


5.1 UTILITY PACKAGES

The utility package is one of java’s most widely used packages. The java.util classes
are listed here.
AbstractCollection (Java 2)
EventObject
PropertyResourceBundle
AbstractList (Java 2)
GregorianCalendar
Random
AbstractMap (Java 2)
HashMap (Java 2)
ResourceBundle
AbstractSequentialList (Java 2)
HashSet (Java 2)
SimpleTimeZone
AbstractSet (Java 2)
Hashtable
Stack
ArrayList (Java 2)
IdentityHashMap (Java 2, v1.4)
StringTokenizer
Arrays (Java 2)
LinkedHashMap (Java 2, v1.4)
Timer (Java 2, v1.3)
BitSet
LinkedHashSet (Java 2, v1.4)
TimerTask (Java 2, v1.3)
Calendar
LinkedList (Java 2)
TimeZone
Collections (Java 2)
ListResourceBundle
TreeMap (Java 2)

279 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
Currency (Java 2, v1.4)
NOTES Locale
TreeSet (Java 2)
Date
Observable
Vector
Dictionary
Properties
WeakHashMap (Java 2)
EventListenerProxy (Java 2, v1.4)

String Tokenizers:

The processing of text often consists of parsing a formatted input string. Parsing is
the division of text into a set of discrete parts, or tokens, which in a certain sequence can
convey a semantic meaning. The StringTokenizer class provides the first step in this parsing
process, often called the lexer (lexical analyzer) or scanner. StringTokenizer implements
the Enumeration interface. Therefore, given an input string, you can enumerate the individual
tokens contained in it using StringTokenizer.

The StringTokenizer constructors are shown here:

 StringTokenizer(String str)
 StringTokenizer(String str, String delimiters)
 StringTokenizer(String str, String delimiters, boolean delimAsToken)

In all versions, str is the string that will be tokenized. In the first version, the default
delimiters are used. In the second and third versions, delimiters is a string that specifies the
delimiters. In the third version, if delimAsToken is true, then the delimiters are also returned
as tokens when the string is parsed. Otherwise, the delimiters are not returned.

Delimiters are not returned as tokens by the first two forms.

Once you have created a StringTokenizer object, the nextToken( ) method is used
to extract consecutive tokens.

One simple example for string tokenizer is


import java.util.StringTokenizer;
THE JAVA LIBRARY
class STDemo {
static String in =”
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(in, “=;”);
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();

280 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

System.out.println(key + “\t” + val);


} NOTES
}
}

BitSet

A BitSet class creates a special type of array that holds bit values. This array can
increase in size as needed. This makes it similar to a vector of bits. The BitSet constructors
are shown here:

BitSet( )

BitSet(int size)

The first version creates a default object. The second version allows you to specify its
initial size (that is, the number of bits that it can hold). All bits are initialized to zero.

Date

The Date class encapsulates the current date and time. Before beginning our
examination of Date, it is important to point out that it has changed substantially from its
original version defined by Java 1.0. When Java 1.1 was released, many of the functions
carried out by the original Date class were moved into the Calendar and DateFormat
classes, and as a result, many of the original 1.0 Date methods were deprecated. Java 2
added a few new methods to the time and date classes, but otherwise implemented them
in the same form as did 1.1. Since the deprecated 1.0 methods should not be used for new
code, they are not described here.

Date supports the following constructors:

Date( )
Date(long millisec)

The first constructor initializes the object with the current date and time. The second
constructor accepts one argument that equals the number of milliseconds.
// Show date and time using only Date methods.
import java.util.Date;
class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date);
// Display number of milliseconds since midnight, January 1, 1970GMT
long msec = date.getTime();

281 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
System.out.println(“Milliseconds since Jan. 1, 1970 GMT = “ + msec);
NOTES }
}

Sample output is shown here:


Mon Apr 22 09:51:52 CDT 2002
Milliseconds since Jan. 1, 1970 GMT = 1019487112894

Date Comparison

There are three ways to compare two Date objects. First, you can use getTime( ) to
obtain the number of milliseconds that have elapsed since midnight, January 1, 1970,for
both objects and then compare these two values. Second, you can use the methods before(
), after(), and equals( ). Because the 12th of the month comes before the 18th,for example,
new Date(99, 2, 12).before(new Date (99, 2, 18)) returns true. Finally, youcan use the
compareTo( ) method, which is defined by the Comparable interface and implemented
by Date.

Calendar

The abstract Calendar class provides a set of methods that allows you to convert a
time in milliseconds to a number of useful components. Some examples of the type of
information that can be provided are: year, month, day, hour, minute, and second. It is
intended that subclasses of Calendar will provide the specific functionality to interpret
time information according to their own rules. This is one aspect of the Java class library
that enables you to write programs that can operate in several international environments.
An example of such a subclass is GregorianCalendar.

Calendar provides no public constructors.

Calendar defines several protected instance variables. areFieldsSet is a Boolean that


indicates if the time components have been set. fields is an array of ints that holds the
components of the time. isSet is a boolean array that indicates if a specific time component
has been set. time is a long that holds the current time for this object. isTimeSet is a
boolean that indicates if the current time has been set.

Calendar defines the following int constants, which are used when you get or set

components of the calendar:


AM FRIDAY PM
AM_PM HOUR SATURDAY
APRIL HOUR_OF_DAY SECOND
AUGUST JANUARY SEPTEMBER
DATE JULY SUNDAY
DAY_OF_MONTH JUNE THURSDAY

282 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

DAY_OF_WEEK MARCH TUESDAY


DAY_OF_WEEK_IN_MONTH MAY UNDECIMBER NOTES
DAY_OF_YEAR MILLISECOND WEDNESDAY
DECEMBER MINUTE WEEK_OF_MONTH
DST_OFFSET MONDAY WEEK_OF_YEAR
ERA MONTH YEAR
FEBRUARY NOVEMBER ZONE_OFFSET
FIELD_COUNT OCTOBER

SimpleTimeZone

The SimpleTimeZone class is a convenient subclass of TimeZone. It implements


TimeZone’s abstract methods and allows you to work with time zones for a Gregorian
calendar. It also computes daylight saving time.

SimpleTimeZone defines four constructors. One is SimpleTimeZone(int timeDelta, String


tzName)

This constructor creates a SimpleTimeZone object. The offset relative to Greenwich


mean time (GMT) is timeDelta. The time zone is named tzName.
The second SimpleTimeZone constructor is
SimpleTimeZone(int timeDelta, String tzId, int dstMonth0,
int dstDayInMonth0, int dstDay0, int time0,
int dstMonth1, int dstDayInMonth1, int dstDay1,
int time1)

Here, the offset relative to GMT is specified in timeDelta. The time zone name is
passed in tzId. The start of daylight saving time is indicated by the parameters
dstMonth0,dstDayInMonth0, dstDay0, and time0. The end of daylight saving time is
indicated by the parameters dstMonth1, dstDayInMonth1, dstDay1, and time1.

The third SimpleTimeZone constructor is


SimpleTimeZone(int timeDelta, String tzId, int dstMonth0,
int dstDayInMonth0, int dstDay0, int time0,
int dstMonth1, int dstDayInMonth1, int dstDay1,
int time1, int dstDelta)
Here, dstDelta is the number of milliseconds saved during daylight saving time.

The fourth SimpleTimeZone constructor is:


SimpleTimeZone(int timeDelta, String tzId, int dstMonth0,
int dstDayInMonth0, int dstDay0, int time0,
int time0mode, int dstMonth1, int dstDayInMonth1,
int dstDay1, int time1, int time1mode, int dstDelta)

283 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Here, time0mode specifies the mode of the starting time, and time1mode specifies the
NOTES mode of the ending time. Valid mode values include

STANDARD_TIME WALL_TIME UTC_TIME

Locale

The Locale class is instantiated to produce objects that each describe a geographical
or cultural region. It is one of several classes that provide you with the ability to write
programs that can execute in several different international environments. For example, the
formats used to display dates, times, and numbers are different in various
regions.Internationalization is a large topic that is beyond the scope of this book.However,
most programs will only need to deal with its basics, which include setting the current
locale.

The Locale class defines the following constants that are useful for dealing with the
most common locales:
CANADA GERMAN KOREAN
CANADA_FRENCH GERMANY PRC
CHINA ITALIAN SIMPLIFIED_CHINESE
CHINESE ITALY TAIWAN
ENGLISH JAPAN TRADITIONAL_CHINESE
FRANCE JAPANESE UK
FRENCH KOREA US

For example, the expression Locale.CANADA represents the Locale object for
Canada.The constructors for Locale are
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String data)

These constructors build a Locale object to represent a specific language and in the
case of the last two, country. These values must contain ISO-standard language and country
codes. Auxiliary browser and vendor-specific information can be provided in data. The
first constructor was added by Java 2, version 1.4.

Locale defines several methods. One of the most important is setDefault( ),shown here:
static void setDefault(Locale localeObj)
This sets the default locale to that specified by localeObj.
Some other interesting methods are the following:
final String getDisplayCountry( )
final String getDisplayLanguage( )
final String getDisplayName( )

284 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

These return human-readable strings that can be used to display the name of the
country, the name of the language, and the complete description of the locale. NOTES
The default locale can be obtained using getDefault( ), shown here:

static Locale getDefault( ) Calendar and GregorianCalendar are examples of classes


that operate in a localesensitive manner. DateFormat and SimpleDateFormat also depend
on the locale.

Random

The Random class is a generator of pseudorandom numbers. These are called


pseudorandom numbers because they are simply uniformly distributed sequences.

Random defines the following constructors:

Random( )
Random(long seed)

The first version creates a number generator that uses the current time as the starting,or
seed, value. The second form allows you to specify a seed value manually. If you initialize
a Random object with a seed, you define the starting point for the random sequence. If you
use the same seed to initialize another Random object, you will extract the same random
sequence. If you want to generate different sequences, specify different seed values. The
easiest way to do this is to use the current time to seed a Random object. This approach
reduces the possibility of getting repeated sequences.

As you can see, there are seven types of random numbers that you can extract from
a Random object. Random Boolean values are available from nextBoolean( ). Random
bytes can be obtained by calling nextBytes( ). Integers can be extracted via the nextInt( )
method. Long integers, uniformly distributed over their range, can be obtained with
nextLong( ). The nextFloat( ) and nextDouble( ) methods return a uniformly distributed
float and double, respectively, between 0.0 and 1.0. Finally,nextGaussian( ) returns a double
value centered at 0.0 with a standard deviation of 1.0.

Method Description
boolean nextBoolean( ) Returns the next boolean random number.
void nextBytes(byte vals[ ]) Fills vals with randomly generated values.
double nextDouble( ) Returns the next double random number.
float nextFloat( ) Returns the next float random number.
double nextGaussian( ) Returns the next Gaussian random number.
int nextInt( ) Returns the next int random number.
int nextInt(int n) Returns the next int random number within
the range zero to n.
long nextLong( ) Returns the next long random number.

285 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
void setSeed(long newSeed) Sets the seed value (that is, the starting
NOTES point for the random number generator) to that specified by newSeed.

Here is an example that demonstrates the sequence produced by nextGaussian( ).It


obtains 100 random Gaussian values and averages these values. The program also counts
the number of values that fall within two standard deviations, plus or minus,using increments
of 0.5 for each category.
// Demonstrate random Gaussian values.
import java.util.Random;
class RandDemo {
public static void main(String args[]) {
Random r = new Random();
double val;
double sum = 0;
int bell[] = new int[10];
for(int i=0; i<100; i++) {
val = r.nextGaussian();
sum += val;
double t = -2;
for(int x=0; x<10; x++, t += 0.5)
if(val < t) {
bell[x]++;
break;
}
}
System.out.println(“Average of values: “ +
(sum/100));
// display bell curve, sideways
for(int i=0; i<10; i++) {
for(int x=bell[i]; x>0; x—)
System.out.print(“*”);
System.out.println();
}
}
}

Here is a sample program run. As you can see, a bell-like distribution of numbers

is obtained.

Average of values: 0.0702235271133344


**
*******
******
***************

286 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

******************
***************** NOTES
*************
BufferedInputStream FileWriter PipedInputStream
BufferedOutputStream FilterInputStream PipedOutputStream
BufferedReader FilterOutputStream PipedReader
BufferedWriter FilterReader PipedWriter
ByteArrayInputStream FilterWriter PrintStream
ByteArrayOutputStream InputStream PrintWriter
CharArrayReader InputStreamReader PushbackInputStream
CharArrayWriter LineNumberReader PushbackReader
DataInputStream ObjectInputStream RandomAccessFile
DataOutputStream ObjectInputStream. GetField Reader
File ObjectOutputStream SequenceInputStream
FileDescriptor ObjectOutputStream. PutField SerializablePermission
FileInputStream ObjectStreamClass StreamTokenizer
FileOutputStream ObjectStreamField StringReader
FilePermission OutputStream StringWriter
FileReader OutputStreamWriter Writer

5.2 FILE

Although most of the classes defined by java.io operate on streams, the File class
does not. It deals directly with files and the file system. That is, the File class does not
specify how information is retrieved from or stored in files; it describes the properties of a
file itself. A File object is used to obtain or manipulate the information associatedwith a
disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory
hierarchies.

Files are a primary source and destination for data within many programs. Although
there are severe restrictions on their use within applets for security reasons,files are still a
central resource for storing persistent and shared information. A directory in Java is treated
simply as a File with one additional property—a list of filenames that can be examined by
the list( ) method.
The following constructors can be used to create File objects:
File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File(URI uriObj)

Here, directoryPath is the path name of the file, filename is the name of the file,
dirObj is a File object that specifies a directory, and uriObj is a URI object that describes
a file. The fourth constructor was added by Java 2, version 1.4.

287 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The following example creates three files: f1, f2, and f3. The first File object is
NOTES constructed with a directory path as the only argument. The second includes two arguments—
the path and the filename. The third includes the file path assigned to f1 and a filename; f3
refers to the same file as f2.
File f1 = new File(“/”);
File f2 = new File(“/”,”autoexec.bat”);
File f3 = new File(f1,”autoexec.bat”);
The following example demonstrates several of the File methods:
// Demonstrate File.
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File(“/java/COPYRIGHT”);
p(“File Name: “ + f1.getName());
p(“Path: “ + f1.getPath());
p(“Abs Path: “ + f1.getAbsolutePath());
p(“Parent: “ + f1.getParent());
p(f1.exists() ? “exists” : “does not exist”);
p(f1.canWrite() ? “is writeable” : “is not writeable”);
p(f1.canRead() ? “is readable” : “is not readable”);
p(“is “ + (f1.isDirectory() ? “” : “not” + “ a directory”));
p(f1.isFile() ? “is normal file” : “might be a named pipe”);
p(f1.isAbsolute() ? “is absolute” : “is not absolute”);
p(“File last modified: “ + f1.lastModified());
p(“File size: “ + f1.length() + “ Bytes”);
}
}
When you run this program, you will see something similar to the following:
File Name: COPYRIGHT
Path: /java/COPYRIGHT
Abs Path: /java/COPYRIGHT
Parent: /java
exists
is writeable
is readable
is not a directory
is normal file
is absolute
File last modified: 812465204000
File size: 695 Bytes

288 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES
Method Description
void deleteOnExit( ) Removes the file associated with the
invoking object when the Java Virtual
Machine terminates.
boolean isHidden( ) Returns true if the invoking file is
hidden. Returns false otherwise
boolean setLastModified Sets the time stamp on the invoking
(long millisec) file to that specified by millisec, which
Is the number of milliseconds from
January 1, 1970, Coordinated
Universal Time (UTC).

boolean setReadOnly( ) Sets the invoking file to read-only

Directories

A directory is a File that contains a list of other files and directories. When you create
a File object and it is a directory, the isDirectory( ) method will return true. In this
case,you can call list( ) on that object to extract the list of other files and directories inside.

It has two forms. The first is shown here:

String[ ] list( )

The list of files is returned in an array of String objects.The program shown here
illustrates how to use list( ) to examine the contents of
a directory:
// Using directories.
import java.io.File;
class DirList {
public static void main(String args[]) {
String dirname = “/java”;
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println(“Directory of “ + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++) {
File f = new File(dirname + “/” + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + “ is a directory”);
} else {
System.out.println(s[i] + “ is a file”);
}
}

289 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
} else {
NOTES System.out.println(dirname + “ is not a directory”);
}
}
}
Here is sample output from the program. (Of course, the output you see will be different,
based on what is in the directory.)
Directory of /java
bin is a directory
lib is a directory
demo is a directory
COPYRIGHT is a file
README is a file
index.html is a file
include is a directory
src.zip is a file
.hotjava is a directory
src is a directory

Using FilenameFilter

You will often want to limit the number of files returned by the list( ) method to include
only those files that match a certain filename pattern, or filter. To do this, you must use a
second form of list( ), shown here:

String[ ] list(FilenameFilter FFObj)

In this form, FFObj is an object of a class that implements the FilenameFilter interface.
FilenameFilter defines only a single method, accept( ), which is called once for each file
in a list. Its general form is given here:

boolean accept(File directory, String filename)

The accept( ) method returns true for files in the directory specified by directory that
should be included in the list (that is, those that match the filename argument), and returns
false for those files that should be excluded.

The OnlyExt class, shown next, implements FilenameFilter. It will be used to modify
the preceding program so that it restricts the visibility of the filenames returned by list( ) to
files with names that end in the file extension specified when the object is constructed.
import java.io.*;
public class OnlyExt implements FilenameFilter {
String ext;
public OnlyExt(String ext) {
this.ext = “.” + ext;
}

290 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

public boolean accept(File dir, String name) {


return name.endsWith(ext); NOTES
}
}
The modified directory listing program is shown here. Now it will only display files
that use the .html extension.
// Directory of .HTML files.
import java.io.*;
class DirListOnly {
public static void main(String args[]) {
String dirname = “/java”;
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt(“html”);
String s[] = f1.list(only);
for (int i=0; i < s.length; i++) {
System.out.println(s[i]);
}
}
}

Creating Directories

Another two useful File utility methods are mkdir( ) and mkdirs( ). The mkdir( )
method creates a directory, returning true on success and false on failure. Failure indicates
that the path specified in the File object already exists, or that the directory cannot be
created because the entire path does not exist yet. To create a directory for which no path
exists, use the mkdirs( ) method. It creates both a directory and all the parents of the
directory.

The Byte Streams

The byte stream classes provide a rich environment for handling byte-oriented I/O. A
byte stream can be used with any type of object, including binary data. This versatility
makes byte streams important to many types of programs. Since the byte stream classes
are topped by InputStream and OutputStream, our discussion will begin with them.

InputStream

InputStream is an abstract class that defines Java’s model of streaming byte input. All of
the methods in this class will throw an IOException on error conditions.

291 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES Method Description


int available( ) Returns the number of bytes of input
currentlyavailable for reading
void close( ) Closes the input source. Further read attempts
will generate an IOException
void mark(int numBytes) Places a mark at the current point in the input
stream that will remain valid until numBytes bytes
are read.
boolean markSupported( ) Returns true if mark( )/reset( ) are supported
by the invoking stream
int read( ) Returns an integer representation of the next
available byte of input. –1 is returned when the
end of the file is encountered
int read(byte buffer[ ]) Attempts to read up to buffer.length bytes into
buffer and returns the actual number of bytes
that were successfully read. –1 is returned
when the end of the file is encountered
int read(byte buffer[ ],

int offset,

int numBytes) Attempts to read up to numBytes bytes into buffer


starting at buffer[offset], returning the number of
bytes successfully read. –1 is returned when the end of the file is encountered.

OutputStream

OutputStream is an abstract class that defines streaming byte output. All of the methods in
this class return a void value and throw an IOException in the case of errors.

Method Description
void close( ) Closes the output stream. Further write
attempts will generate an IOException
void flush( ) Finalizes the output state so that any
buffers are cleared. That is, it flushes
the output buffers.
void write(int b) Writes a single byte to an output
stream. Note that the parameter is an
int, which allows you to call write( )
with expressions without having to ast
them back to byte
void write(byte buffer[ ]) Writes a complete array of bytes to an
output stream

292 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

void write(byte buffer[ ],


NOTES
int offset,

int numBytes) Writes a subrange of numBytes bytes from the array


buffer, beginning at buffer[offset].

FileInputStream

The FileInputStream class creates an InputStream that you can use to read bytes
from a file. Its two most common constructors are shown here:

FileInputStream(String filepath)

FileInputStream(File fileObj)

Either can throw a FileNotFoundException. Here, filepath is the full path name of a
file,and fileObj is a File object that describes the file.

The following example creates two FileInputStreams that use the same disk file and
each of the two constructors:

FileInputStream f0 = new FileInputStream(“/autoexec.bat”)


File f = new File(“/autoexec.bat”);
FileInputStream f1 = new FileInputStream(f);

The example shows how to read a single byte, an array of bytes, and a subrange
array of bytes. It also illustrates how to use available( ) to determine the number of bytes
remaining, and how to use the skip( ) method to skip over unwanted bytes. The program
reads its own source file, which must be in the current directory.

// Demonstrate FileInputStream.
import java.io.*;
class FileInputStreamDemo {
public static void main(String args[]) throws Exception {
int size;
InputStream f =
new FileInputStream(“FileInputStreamDemo.java”);
System.out.println(“Total Available Bytes: “ +
(size = f.available()));
int n = size/40;
System.out.println(“First “ + n +
“ bytes of the file one read() at a time”);
for (int i=0; i < n; i++) {
System.out.print((char) f.read());
}
System.out.println(“\nStill Available: “ + f.available());

293 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
System.out.println(“Reading the next “ + n +
NOTES “ with one read(b[])”);
byte b[] = new byte[n];
if (f.read(b) != n) {
System.err.println(“couldn’t read “ + n + “ bytes.”);
}
System.out.println(new String(b, 0, n));
System.out.println(“\nStill Available: “ + (size = f.available()));
System.out.println(“Skipping half of remaining bytes with skip()”);
f.skip(size/2);
System.out.println(“Still Available: “ + f.available());
System.out.println(“Reading “ + n/2 + “ into the end of array”);
if (f.read(b, n/2, n/2) != n/2) {
System.err.println(“couldn’t read “ + n/2 + “ bytes.”);
}
System.out.println(new String(b, 0, b.length));
System.out.println(“\nStill Available: “ + f.available());
f.close();
}
}
Here is the output produced by this program:
Total Available Bytes: 1433
First 35 bytes of the file one read() at a time
// Demonstrate FileInputStream.
im
Still Available: 1398
Reading the next 35 with one read(b[])
port java.io.*;
class FileInputS
Still Available: 1363
Skipping half of remaining bytes with skip()
Still Available: 682
Reading 17 into the end of array
port java.io.*;
read(b) != n) {
S
Still Available: 665

FileOutputStream

FileOutputStream creates an OutputStream that you can use to write bytes to a file. Its
most commonly used constructors are shown here:
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)

294 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

FileOutputStream(File fileObj, boolean append)


NOTES
They can throw a FileNotFoundException or a SecurityException. Here, filePath
is the full path name of a file, and fileObj is a File object that describes the file. If append
is true, the file is opened in append mode. The fourth constructor was added by Java 2,
version 1.4.

Creation of a FileOutputStream is not dependent on the file already


existing.FileOutputStream will create the file before opening it for output when you create
the object. In the case where you attempt to open a read-only file, an IOException will
be thrown.

// Demonstrate FileOutputStream.
import java.io.*;
class FileOutputStreamDemo {
public static void main(String args[]) throws Exception {
String source = “Now is the time for all good men\n”
+ “ to come to the aid of their country\n”
+ “ and pay their due taxes.”;
byte buf[] = source.getBytes();
OutputStream f0 = new FileOutputStream(“file1.txt”);
for (int i=0; i < buf.length; i += 2) {
f0.write(buf[i]);
}
f0.close();
OutputStream f1 = new FileOutputStream(“file2.txt”);
f1.write(buf);
f1.close();
OutputStream f2 = new FileOutputStream(“file3.txt”);
f2.write(buf,buf.length-buf.length/4,buf.length/4);
f2.close();
}
}

Here are the contents of each file after running this program. First, file1.txt:
Nwi h iefralgo e
t oet h i ftercuty n a hi u ae.
Next, file2.txt:
Now is the time for all good men
to come to the aid of their country
and pay their due taxes.
Finally, file3.txt:
nd pay their due taxes.

295 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

ByteArrayInputStream
NOTES
ByteArrayInputStream is an implementation of an input stream that uses a byte
array as the source. This class has two constructors, each of which requires a byte array to
provide the data source:

ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)

Here, array is the input source. The second constructor creates an InputStream from a
subset of your byte array that begins with the character at the index specified by start and
is numBytes long.

ByteArrayOutputStream

ByteArrayOutputStream is an implementation of an output stream that uses a byte array


as the destination. ByteArrayOutputStream has two constructors, shown here:

ByteArrayOutputStream( )
ByteArrayOutputStream(int numBytes)

In the first form, a buffer of 32 bytes is created. In the second, a buffer is created with
a size equal to that specified by numBytes. The buffer is held in the protected buf field of
ByteArrayOutputStream. The buffer size will be increased automatically, if needed. The
number of bytes held by the buffer is contained in the protected count field of
ByteArrayOutputStream.

Buffered Byte Streams

For the byte-oriented streams, a buffered stream extends a filtered stream class by
attaching a memory buffer to the I/O streams. This buffer allows Java to do I/O operations
on more than a byte at a time, hence increasing performance. Because the buffer is
available,skipping, marking, and resetting of the stream becomes possible. The buffered
byte stream classes are BufferedInputStream and BufferedOutputStream.
PushbackInputStream also implements a buffered stream.

BufferedInputStream

Buffering I/O is a very co mmon performance optimization. Java’s


BufferedInputStream class allows you to “wrap” any InputStream into a buffered stream
and achieve this performance improvement.

BufferedInputStream has two constructors:


BufferedInputStream(InputStream inputStream)
BufferedInputStream(InputStream inputStream, int bufSize)

296 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

The first form creates a buffered stream using a default buffer size. In the second, the
size of the buffer is passed in bufSize. Use of sizes that are multiples of memory page,disk NOTES
block, and so on can have a significant positive impact on performance.

BufferedOutputStream

A BufferedOutputStream is similar to any OutputStream with the exception of an


added flush( ) method that is used to ensure that data buffers are physically written to the
actual output device. Since the point of a BufferedOutputStream is to improve
performance by reducing the number of times the system actually writes data, you may
need to call flush( ) to cause any data that is in the buffer to get written.Unlike buffered
input, buffering output does not provide additional functionality.Buffers for output in Java
are there to increase performance. Here are the two available constructors:
BufferedOutputStream(OutputStream outputStream)
BufferedOutputStream(OutputStream outputStream, int bufSize)
The first form creates a buffered stream using a buffer of 512 bytes. In the second form,the
size of the buffer is passed in bufSize.

The Character Streams

While the byte stream classes provide sufficient functionality to handle any type of I/
O operation, they cannot work directly with Unicode characters. Since one of the main
purposes of Java is to support the “write once, run anywhere” philosophy, it was necessary
to include direct I/O support for characters. In this section, several of the character I/O
classes are discussed. As explained earlier, at the top of the character stream hierarchies
are the Reader and Writer abstract classes. We will begin with them.

Reader

Reader is an abstract class that defines Java’s model of streaming character input.
All of the methods in this class will throw an IOException on error conditions.

Writer

Writer is an abstract class that defines streaming character output. All of the methods
in this class return a void value and throw an IOException in the case of errors.

FileReader

The FileReader class creates a Reader that you can use to read the contents of a
file. Its two most commonly used constructors are shown here:

FileReader(String filePath)
FileReader(File fileObj)

297 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

FileWriter
NOTES
FileWriter creates a Writer that you can use to write to a file. Its most commonly used
constructors are shown here:

FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)

They can throw an IOException. Here, filePath is the full path name of a file, and
fileObj is a File object that describes the file. If append is true, then output is appended
to the end of the file. The fourth constructor was added by Java 2, version 1.4. Creation of
a FileWriter is not dependent on the file already existing. FileWriter will create the file
before opening it for output when you create the object. In the case where you attempt to
open a read-only file, an IOException will be thrown.

BufferedReader

BufferedReader improves performance by buffering input. It has two constructors:

BufferedReader(Reader inputStream)
BufferedReader(Reader inputStream, int bufSize)

The first form creates a buffered character stream using a default buffer size. In the
second, the size of the buffer is passed in bufSize. As is the case with the byte-oriented
stream, buffering an input character stream also provides the foundation required to support
moving backward in the stream within the available buffer. To support this, BufferedReader
implements the mark( ) and reset( ) methods, and BufferedReader.markSupported( )
returns true.

BufferedWriter

A BufferedWriter is a Writer that adds a flush( ) method that can be used to


ensure that data buffers are physically written to the actual output stream. Using a
BufferedWriter can increase performance by reducing the number of times data is actually
physically written to the output stream.

A BufferedWriter has these two constructors:


BufferedWriter(Writer outputStream)
BufferedWriter(Writer outputStream, int bufSize)

The first form creates a buffered stream using a buffer with a default size. In the
second, the size of the buffer is passed in bufSize.

298 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

5.3 INNER CLASSES


NOTES
Inner Class is a class which is defined within another class. The code in the inner class
has access to the members of the enclosing (outer) class. The inner class instance has
access to all the members of the outer class even though marked private.
The general definition for inner class is
class MyOuter{
class MyInner{}
}
An example for inner class is
class MyOuter
{
private int x=7;
class MyInner{
public void seeOuter(){
System.out.println(“Outer is “+x);
}
}//close inner class definition
}//close outer class definition

The seeOuter() function of the inner class can access the outer variable x since MyInner
is also a member of outer class.

Instantiating an Inner Class:

To create the instance of an inner class, one must create the instance of the outer class
to tie to the inner class.

For the above example the instance creation is given below.


public static void main(String args[])
{
MyOuter m=new MyOuter();
MyOuter.MyInner inner=m.new MyInner();
Inner.seeOuter();
}
The other way for creating instance to the inner class is
public static void main(String args[])
{
MyOuter.MyInner inner=new MyOuter().new MyInner();
Inner.seeOuter();
}
The output of this program is :
Outer x is 7

299 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
Instantiating an inner class from within the outer class:
NOTES
class MyOuter{
private int x=7;
public void makeInner()
{
MyInner in=new MyInner();
in.seeOuter();
}
class MyInner{
public void seeOuter(){
System.out.println(“outer is”+x);
}
}
public static void main(String args[])
{
MyOuter m=new MyOuter();
m.makeInner();
}
}
The output of this program is:
Outer is 7

The control flow for this program is:

 Instance “m” for MyOuter class is created.


 With this instance, the member function “makeInner()” of MyOuter class is called.
 The member unction “makeInner()” creates the instance “in” for MyInner class
 With this instance the member function “seeOuter()” is called.

MyOuter class considers the MyInner class member function “seeOuter()” as its
member function. Due to this, the member variable “x” is accessed by the function
“seeOuter()”.

An inner class has access to all of the members of its enclosing (outer) class, but the
reverse is not true. Members of the inner class are known only within the scope of the inner
class and may not be used by the outer class.
class Outer{
private int x=100;
void test()
{
Inner in=new Inner();
in.display();
}
class Inner

300 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

{
private int y=10 //y is local to Inner NOTES
void display()
{
System.out.println(“value is”+x);
}
}
Void showy()
{
System.out.println(y); // error , y not known here!
}
}
class InnerClassDemo
{
public static void main(String args[]){
Outer outer=new Outer();
Outer.test();
}
}

Member modifiers applied to inner classes:


 final
 abstract
 public
 private
 protected
 static

5.4 JAVA DATABASE CONNECTIVITY

JDBC provides connectivity to databases through java. The overview is:

APPLICATION  JDBC  DRIVER  DATABASE

 Java code calls JDBC library


 JDBC loads a driver
 Driver talks to a particular database
 Can have more than one driver ,so more than one database
 Ideal: can change database engines without changing any application code

JDBC DRIVERS:

 Type I: “Bridge”
 Type II: “Native”
 Type III: “Middleware”
 Type IV: “Pure”

301 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Type1:
NOTES
 Use bridging technology
 Requires installation/configuration on client machines
 Not good for Web
 e.g. ODBC Bridge

Type 2:

 Native API drivers


 Requires installation/configuration on client machines
 Used to leverage existing CLI libraries
 Usually not thread-safe
 Mostly obsolete now
 e.g. Intersolv Oracle Driver, WebLogic drivers

Type 3:

 Calls middleware server, usually on database host


 Very flexible — allows access to multiple databases using one driver
 Only need to download one driver
 But it’s another server application to install and maintain
 e.g. Symantec DBAnywhere

Type 4:

 100% Pure Java — the Holy Grail


 Use Java networking libraries to talk directly to database engines
 Only disadvantage: need to download a new driver for each database engine, e.g.
Oracle, mSQL

JDBC APIs:

java.sql – JDBC is implemented via the classes in java.sql package.

JDBC object classes:

 DriverManager
o Loads, chooses drivers
 Driver
o connects to actual database
 Connection
o a series of SQL statements to and from the DB
 Statement
o a single SQL statement
 ResultSet
o the records returned from a Statement

302 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Overview:
NOTES

DriverManager:

Connection getConnection(String url, String user, String password)


 Connects to given JDBC URL with given user name and password
 Throws java.sql.SQLException
 returns a Connection object
Connection:
 A Connection represents a session with a specific database.
 Within the context of a Connection, SQL statements are executed and results are
returned.
 Can have multiple connections to a database
 Also provides “metadata” — information about the database, tables, and fields
 Also methods to deal with transactions

Obtaining a connection:
String url = “jdbc:odbc:Northwind”;
try{
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection(url);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}

303 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Connection methods:
NOTES Statement createStatement()
returns a new Statement object
PreparedStatement prepareStatement(String sql)
returns a new PreparedStatement object
CallableStatement prepareCall(String sql)
returns a new CallableStatement object

Statement:

A Statement object is used for executing a static SQL statement and obtaining the
results produced by it

Statement methods:
ResultSet executeQuery(String)
Executes a SQL statement that returns a single ResultSet.
int executeUpdate(String)
Executes a SQL INSERT, UPDATE or DELETE
statement. Returns the number of rows changed.
boolean execute(String)
Executes a SQL statement that may return multiple results.

ResultSet:

 A ResultSet provides access to a table of data generated by executing a Statement.


 Only one ResultSet per Statement can be open at once.
 The table rows are retrieved in sequence.
 A ResultSet maintains a cursor pointing to its current row of data.
 The ‘next’ method moves the cursor to the next row.
ResultSet Methods:
 boolean next()
o activates the next row
o the first call to next() activates the first row
o returns false if there are no more rows
 void close()
o disposes of the ResultSet
o allows you to re-use the Statement that created it
o automatically called by most Statement methods
 Type getType(int columnIndex)
o returns the given field as the given type
o fields indexed starting at 1 (not 0)

304 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 Type getType(String columnName)


o same, but uses name of field NOTES
o less efficient
 int findColumn(String columnName)
o looks up column index given column name
getType() methods:
 String getString(int columnIndex)
 boolean getBoolean(int columnIndex)
 byte getByte(int columnIndex)
 short getShort(int columnIndex)
 int getInt(int columnIndex)
 long getLong(int columnIndex)
 float getFloat(int columnIndex)
 double getDouble(int columnIndex)
 Date getDate(int columnIndex)
 Time getTime(int columnIndex)
 Timestamp getTimestamp(int columnIndex)
Example:
Consider the following database:

Employee ID Last Name First Name


1 Dev Nancy
2 Fuller Andrew
3 Lever Janet
4 Peacock Margaret
5 Buchanan Steven

Select example:

Connection con = DriverManager.getConnection(url, “user”, “passwd”);


Statement st = con.createStatement();
ResultSet results = st.executeQuery(“SELECT EmployeeID, LastName,
FirstName FROM Employees”);
while (results.next()) {
int id = results.getInt(1);
String last = results.getString(2);
String first = results.getString(3);
System.out.println(“” + id + “: “ + first + “ “ + last);
}
st.close();
con.close();

305 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Mapping java types to SQL types:


NOTES
SQL type Java Type
CHAR, VARCHAR, LONGVARCHAR String
NUMERIC, DECIMAL java.math.BigDecimal
BIT boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT, DOUBLE double
BINARY, VARBINARY, LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp

Modifying database:
 use executeUpdate if the SQL contains “INSERT” or “UPDATE”
 executeUpdate returns the number of rows modified
 executeUpdate also used for “CREATE TABLE” etc. (DDL)
5.5 SERVLETS

Introduction:

Java networking capabilities:


(1) Socket-based and packet-based communications
Package java.net
(2) Remote Method Invocation (RMI)
Package java.rmi
(3) Servlets and Java Server Pages (JSP)
- Request-response model
- Packages javax.servlet
javax.servlet.http
javax.servlet.jsp
javax.servlet.tagext
(4) Form the Web tier of J2EE

Definition:
 A servlet is a generic server extension
o A Java class that can be loaded dynamically to expand the
functionality of a server.
o Commonly used with web servers,

306 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

o Taking the place of CGI scripts


 Similar to a proprietary server extension, except: NOTES
o it runs inside a Java Virtual Machine (JVM) on the server
o so it is safe and portable.
 Handled by separate threads within the web server process
o efficient and scalable
 Operate solely within the domain of the server
o Unlike applets, they do not require support for Java in the web browser.

Comparison with CGI:


 CGI:
o use multiple processes to handle separate programs and/or separate
requests
o Outside the server
 Servlets:
o Handled by separate threads within the web server process.
 Thus more efficient and scalable.
o Can interact very closely with the server to do things that are not possible with
CGI scripts.
o Java Servlets are portable:
 Across operating systems (java)
 Across web servers, since all major web servers support servlets.
 Thus, Java servlets offer a good platform for web-application development

Servlet needs the following:


 Servlet container (servlet engine)
o Server that executes a servlet
o There are three types : standalone,add-on,embedded.
 Standalone servlet engines
 built-in support for servlets
 hard to keep on with the latest version of servlet
 Add-on servlet server engines
 plug-in to an existing server
 Embeddable servlet engines
 lightweight servlet-deployment platform that can be embedded in another
application
 Web servers and application servers
 Examples:
 Netscape iPlanet Application Server
 Microsoft’s Internet Information Server (IIS)
 Apache HTTP Server
 BEA’s WebLogic Application Server
 IBM’s WebSphere Application Server
 World Wide Web Consortium’s Jigsaw Web Server

307 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Servlet Interface and the servlet lifecycle:


NOTES
 Servlet interface
oAll servlets must implement this interface
oAll methods of interface Servlet are invoked automatically

Servlet interface
{
void init(ServletConfig sc)
throws ServletException;
void service(ServletRequest req,ServletResponse res);
throws ServletException,IOException;
void destroy();
}
 Servlet life cycle
oServlet container invokes the Servlet’s init method
oServlet’s service method handles requests
oServlet’s destroy method releases servlet resources when the servlet
container terminates the servlet
 Servlet implementation
oUsing generic API
oUsing protocol-specific API

Structure of a servlet
 A servlet does not have a main() method.
 Certain methods of a servlet are invoked by the server in the process of handling
requests.
 Each time the server dispatches a request to a servlet, it invokes the servlet’s
service() method.
 The service() method accepts two parameters:
o a request object: tells the servlet about the request
o a response object: the response object is used to return a response

308 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Servlet API:
NOTES
The servlet API provides the following facilities:

(1) Callback methods for initializations and servlet processing


(2) Methods by which the servlet can get configuration and environment information
(3) Access to protocol-specific resources such as client’s session object

javax.servlet.Servlet:

Servlet is an interface that defines the basic structure of a Servlet. It’s notable methods
are:
ovoid init(ServletConfig)
ovoid destroy()
oServletConfig getServletConfig()
 A ServletConfig object passes configuration information from the engine to the
Servlet.
 Houses the initialization parameters for the Servlet in an Enumeration
oString getServletInfo()
ovoid service(ServletRequest req, ServletResponse res)
 This method is called by the Servlet engine for each request

javax.servlet.GenericServlet:

 GenericServlet implements Servlet, ServletConfig and java.io.Serializable interfaces


 Used to create a protocol-independent (non-HTTP) Servlet, such as:
 a telnet, ftp server, a remote mortgage calculator, a game server, almost anything
else conceivable
 Includes default implementations for init() and destroy()
 Keeps the service() method abstract, so service is the only method you have to
override to implement a GenericServlet

HttpServlet: (javax.servlet.http.HttpServlet)

HttpServlet is an abstract class that defines a number of methods to handle common


HTTP requests, such as GET, POST, etc.
 These methods are:
o void doGet(HttpServletRequest req, HttpServletResponse resp)
o void doPost(HttpServletRequest req, HttpServletResponse resp)
o void doPut(HttpServletRequest req, HttpServletResponse resp)
o void doOptions(HttpServletRequest req, HttpServletResponse resp)
o void doDelete(HttpServletRequest req, HttpServletResponse resp)
o void doTrace(HttpServletRequest req, HttpServletResponse resp)
 Provides a default implementation for the service() method which calls the
appropriate derived doXXX() method based on the incoming HTTP request

309 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Processing requests:
NOTES  Interface HttpServletRequest extends javax.servlet.ServletRequest
 HttpServletRequest is passed into the service and getXXX() methods
 HttpServletRequest provides methods that facilitate access to information related
to an HTTP request, such as:

o getParameter, getParameterNames, getParameterValues


o String getHeader(): gets the header
o Enumeration getHeaderNames(): gets the header names
o Enumeration getHeader(): get all header values
o String getAuthType(): gets the authorization type, if one
o Cookie[] getCookies(): gets the cookies from the request
o String getMethod(): returns the HTTP request, i.e., GET, POST,
o String getRemoteUser(): returns the remote user id
o HttpSession getSession(): returns the current HttpSession object
 Derives getInputStream() from javax.servlet.ServletRequest

Building response:
 Interface HttpServletResponse extends javax.servlet.ServletResponse
 HttpServletResponse is passed into the service and getXXX() methods, and
represents the response path from the Servlet in the communication
 HttpServletResponse provides methods that facilitate access to information related
to an HTTP request, such as:
o void setHeader(String name, int value)
o Void addHeader (int value)
o void sendError(int sc)
o void setRedirect(String url)
o void setStatus(int sc)
 Derives getOutputStream() from javax.servlet. ServletResponse

Example:

//WelcomeServlet.java

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;
public class WelcomeServlet extends HttpServlet {
// process “get” requests from clients
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException,IOException
{
response.setContentType( “text/html” );
PrintWriter out = response.getWriter();

310 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

// start XHTML document


out.println( “<?xml version = \”1.0\”?>” ); NOTES
out.println( “<!DOCTYPE html PUBLIC \”-//W3C//DTD “ +
“XHTML 1.0 Strict//EN\” \”http://www.w3.org” +
“/TR/xhtml1/DTD/xhtml1-strict.dtd\”>” );
out.println( “<html xmlns = \”http://www.w3.org/1999/xhtml\”>” );

// head section of document


out.println( “<head>” );
out.println( “<title>A Simple Servlet Example</title>” );
out.println( “</head>” );
// body section of document
out.println( “<body>” );
out.println( “<h1>Welcome to Servlets!</h1>” );
out.println( “</body>” );
// end XHTML document
out.println( “</html>” );
out.close(); // close stream to complete the page
}
}
WelcomeServlet.html

<?xml version = “1.0”?>


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<!— Fig. 24.6: WelcomeServlet.html —>

<html xmlns = “http://www.w3.org/1999/xhtml”>


<head>
<title>Handling an HTTP Get Request</title>
</head>

<body>
<form action = “/jhtp5/welcome1” method = “get”>

<p><label>Click the button to invoke the servlet


<input type = “submit” value = “Get HTML Document” />
</label></p>

</form>
</body>
</html>

311 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

The output is shown below:


NOTES

Servlet Session Management

 By default, Servlet session management is based on cookies, but the engine will
resort to URL rewriting if cookies are unavailable
o note that URL rewriting can become a major performance bottleneck
 The important note is that session handling with cookies is done for you automatically
by the Servlet engine, servers vary on URL rewriting methods, may require some
work on your part
 HttpServletRequest methods for session management:
o HttpSession getSession(): gets the HttpSession object attached to this
request
o HttpSession getSession(boolean):
o boolean isRequestedSessionIdFromCookie(): is it derived from a cookie?
o boolean isRequestedSessionIdFromURL(): is it from a URL?
o boolean isRequestedSessionIdValid(): true if valid, false if invalid (i.e.,
expired)

javax.servlet.http.HttpSession:

 Object getAttribute (String)


 Enumeration getAttributeNames()
 removeAttribute(String)
 setAttribute(String, Object)
 String getId(): gets the session ID
 long getCreationTime(): gets the time the session was created
 long getLastAccessedTime(): gets the time of the client’s last request
 boolean isNew(): returns true if this is a new session (first client access with this
ID)

312 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 void setMaxInactiveInterval(int interval): sets the max number of seconds that a


session is guaranteed to be held valid before it is expired by the Servlet engine. NOTES
 void invalidate(): expires the current session

5.6 REMOTE METHOD INVOCATION

Introduction:
 Java’s implementation of RPC
 Handle the marshaling of data across the network
 Transfer Java objects
 No IDL is required
To create a distributed system:

o Define a remote interface


o Define the remove object implementation
o Define the client application
o Compile and execute the remove object and the client

RMI architecture layers:


 Stub & Skeleton Layer
o intercepts method calls made by the client to the interface reference and
redirects them to a remote RMI service.
 Remote Reference Layer
o interprets and manages references made from clients to remote service
objects
 Transport Layer
o provides basic connectivity via stream-based TCP/IP connections between
networked machines

Finding remote objects:

RMI provides a Naming Service through the RMI Registry that simplifies how
programs specify the location of remote objects. This naming service is a JDK utility called
rmiregistry that runs at a well known address (by default).

Object serialization:
 One of RMI’s key features is its ability to pass and return objects remotely between
networked computers.
 This is achieved by Java’s object serialization mechanism.
 This allows an object to be converted into a stream of data that can be written and
read to files, sockets on remote machines etc.
 Objects gain this capability by implementing the java.io.Serializable interface.
 This interface has no methods (no need to override or implement any method
signatures)

313 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Marshalling
NOTES
 Marshaling is process of encoding object to put them on the wire (network)
 Unmarshaling is the process of decoding from the wire and placing object in the
address space
 RMI uses serialization (deserialization) to perform marshaling (Unmarshaling)
 All parameters passed by value between client and server are marshaled/
unmarshaled

Serialization

It allows objects to be converted to a sequence of bytes


The sequence of bytes can be stored in a file, database, sent to a remote machine etc.
The sequence of bytes can be used to recreate the original object
Serializing
Creating the sequence of bytes from an object

Deserializing

Recreating the object from the above generated bytes

Actually creates a new object with the fields containing the same values as the original
object

If the fields are also serializable objects then they are also “recreated”

Steps in creating an RMI application:

Defining remote interface:

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{ String sayHello() throws RemoteException; }

Implement server-side class:


public class HelloImpl extends UnicastRemoteObject implements Hello
{
public HelloImpl() throws RemoteException
{
super();
}

public String sayHello()


{
return “Hello World!”;
}

314 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Instantiate and register the server:


public static void main (String args[]) NOTES
{
try
{
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name HelloServer” Naming.rebind(“rmi://localhost/svce”,
obj);
System.out.println(“svce bound in registry”);
}
catch (Exception e) {
System.out.println(“HelloImpl err: “ + e.getMessage()); e.printStackTrace();
}

Generating stubs and skeleton:


 Stubs and skeletons are generated by calling the RMI compiler rmic on the server
implementation class.
 This generates 2 class files:
o HelloImp_Skel.class
server skeleton class
o HelloImpl_Stub.class
client stub class

Stubs and skeletons:

 A stub for a remote object acts as a client’s local representative or proxy for the
remote object.
o The caller invokes a method on the local stub which is responsible for
carrying out the method call on the remote object.
o In RMI, a stub for a remote object implements the same set of remote
interfaces that a remote object implements.
 When a stub’s method is invoked, it does the following:
o initiates a connection with the remote JVM containing the remote object,
o marshals (writes and transmits) the parameters to the remote JVM,
o waits for the result of the method invocation,
o unmarshals (reads) the return value or exception returned, and
o returns the value to the caller
 The stub hides the serialization of parameters and the network-level communication
in order to present a simple invocation mechanism to the caller.
o Good for the developer
 In the remote JVM, each remote object may have a corresponding skeleton (in
Java 2 platform-only environments, skeletons are not required).

315 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
o The skeleton is responsible for dispatching the call to the actual remote
NOTES object implementation.
o When a skeleton receives an incoming method invocation it does the
following:
o unmarshals (reads) the parameters for the remote method,
o invokes the method on the actual remote object implementation, and
o marshals (writes and transmits) the result (return value or exception) to the
caller.

Building clients:

A simple client will typically do the following:


o Set a client side security manager
o Retrieve a reference to a RemoteObject from the RMI registry
o Make calls to the remote object.
public class HelloApplet extends Applet
{ String message = “blank”;
// “obj” is the identifier that we’ll use to refer
// to the remote object that implements the “Hello” interface
Hello obj = null;
public void init() {
try {
obj = (Hello)Naming.lookup(“ rmi://localhost/svce”);
message = obj.sayHello(); }
catch (Exception e) {
System.out.println(“HelloApplet exception: “ + e.getMessage());
e.printStackTrace(); }
}
public void paint(Graphics g)
{ g.drawString(message, 25, 50); }
}

Setting up the server:


• Start an RMI registry for the server implementation to be registered with (Solaris)
d:\> rmiregistry &
• Start the server
java -Djava.security.policy=$HOME/mysrc/policy
- D ja va . r mi. s er v e r. c o d eb a s e = h t t p : / / myh o st / ~ myu s r na me / myc la s s e s
examples.hello.HelloImpl

316 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

5.7 JAVA BEANS


NOTES
Introduction:

“A Java Bean is a reusable software component that can be manipulated visually in a


builder tool.”
 JavaBeans takes Java’s “Write Once, Run Anywhere” capability.
 Extends it to include “reuse everywhere”.
 “JavaBeans is a portable, platform-independent component model, written in Java.”
 With it, you create small, reusable, software components.
 A visual “builder” program combines components from disparate sources to create
applications quickly and easily.

Bean:

 A Bean is a JavaBeans component.


 Beans are independent, reusable software modules.
 Beans may be visible objects, like AWT components, or invisible objects, like
queues and stacks.
 A builder/integration tool manipulates Beans to create applets and applications.

Usage:

The bean developer:


 defines the component
 defines design-time tools to view or configure
the bean

The bean user (application developer):


 obtains the packaged bean
 loads it into the builder tool
 specifies (usually by drag-and-drop) the
instances of the bean
 customizes each instance
 hooks them to other beans
 generates the application
The Beans Architecture:

1. Requirements
Threadsafe
Serializable or Externalizable
Naming conventions
Component

317 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

2. Properties
NOTES
get/set
Indexed
Bound
Constrained

3. Methods

public

4. Events

EventListener
Unicast/Multicast

5. Introspection / Design Info vs runtime / “Design patterns” and introspection

BeanInfo
Customizer

6. Packaging

JAR file

Beans consist of three things:

Events

Properties

Methods

Also, since Beans rely on their state, they need to be able to be persistent over time.

Events:
 Java events are used heavily in JavaBeans
 An event in Java is an object
o Events are instances of (subclasses of) java.util.EventObject
 An event is sent by an object to a specific object
o The identity of the sender is associated with the event
 An event is sent as an argument in a method call
 An object that generates events must keep track of its own listeners
 Events are for notifying others when something is happening. The delegation-event
model of AWT, introduced with Java 1.1, demonstrates the Beans event model.

318 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

It has three parts:


NOTES
 EventObjects:
AWTEvent in the AWT world.
 EventListeners
ActionListener, ItemListener, ...
 Event Sources (Beans)
The different AWT Components

Properties:
 Properties define the characteristics of the Bean.
 For instance, when examining an AWT TextField for its properties, you will see
properties for the caret position, current text, and the echo character, among others.
 public void setPropertyName(PropertyType value); public PropertyType
getPropertyName();
 Where PropertyName is the name of the property, and PropertyType is its
datatype. If only one method is present, the property is read-only (set missing) or
write-only (get missing).

 Read-only properties have only a Get method


 Write-only properties have only a Set method
 Read/Write properties have both
Methods:
 Bean methods are available for anyone to call by just making each public.
 However, you can restrict which methods are visible to the Bean builder/integration
tool by providing a getMethodDescriptors method along with your Bean’s BeanInfo.
 Every Bean can provide a supporting BeanInfo class to customize a Bean’s
appearance to an integration tool.

Persistence:
 Persistence is the ability of an object to store its state, for recreation later.
 Beans use Java’s object serialization capabilities for persistence.
 The ObjectInput and ObjectOutput interfaces are the basis for serialization within
Java.
 The ObjectInputStream and ObjectOutputStream classes, respectively, implement
the interfaces

319 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

Steps to create a bean:


NOTES
 Create a bean Program. Save the program with .java extension.
 Compile the source file.
 Create a Manifest File using any Text Editor.
 Generate a Java Archive (JAR) file.
 Loading the Bean into the ToolBox.
 Dropping a Bean instance into the BeanBox.
 Inspecting the Bean’s properties, methods and events.
Starting and Using the BeanBox
This section gives you a brief, introductory look at the BeanBox and it’s basic operation.
The beans/beanbox directory contains Windows (run.bat) and Unix (run.sh) scripts that
start the BeanBox. You can use these commands to start the BeanBox, or use a make
utility:

See the BDK files beans/doc/makefiles.html and beans/doc/gnu.txt for information


about getting copies of gnumake and nmake.

When started, the BeanBo displays three windows:


 The ToolBox,
 the BeanBox window, and
 the Properties sheet.
 The ToolBox contains the Beans available for use by the BeanBox. To work on a
Bean, you choose it from the ToolBox and drop it on the BeanBox window.We
can create our Bean and store it in the toolbox.
 The BeanBox window is the area where you visually wire Beans together, defining
how Beans appear, and interact with other Beans. The BeanBox window is itself
an instance of a BeanBox Bean. The above screenshot shows the BeanBox window
with a Juggler Bean instance dropped in it. Later you’ll see how to wire the
Juggler to two button Beans that start and stop him juggling.
 The Properties sheet displays the properties for the Bean currently selected
within the BeanBox window. In the above screenshot, the Properties sheet displays
the Juggler Bean’s properties. If you drop another Bean in the BeanBox
window, the properties sheet will display that Bean’s properties.
 Here are brief descriptions of each window.

320 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES

You select among Beans in the BeanBox window simply by clicking on the Bean. The
selected Bean will have a hatched border, as the Juggler Bean does in the above
screenshot. Which Bean is selected has significance for the Properties sheet.

Adding a Bean to the ToolBox

When the BeanBox is started, it automatically loads the ToolBox with all the Beans it
finds within the JAR files contained in the beans/jars directory. Move your JAR files
into that directory to have them automatically loaded at BeanBox startup. You can load
Beans from JAR files located elsewhere by using the File|LoadJar... BeanBox menu item.
 Dropping a Bean on the BeanBox

Clicking on a Bean name within the ToolBox chooses that Bean for placement within
the BeanBox. To drop a JellyBean instance onto the BeanBox
1) Click on the word JellyBean in the ToolBox. The cursor will change to a crosshair
when flying over the BeanBox windows.
2) Click within the BeanBox. The JellyBean instance will appear, and will be selected.

Note the change in the Properties sheet when you put the JellyBean in the BeanBox.
Before you placed the JellyBean in the BeanBox, the BeanBox’s properties were displayed.
After placing the JellyBean in the BeanBox, the JellyBean properties are displayed. If you
missed the change, click within the BeanBox, away from the JellyBean. This will select the
BeanBox rather than the JellyBean. The Properties sheet will then display the BeanBox’s
properties.

321 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

After dropping a JellyBean instance on the BeanBox, the Properties sheet displays
NOTES the JellyBean properties: color, foreground, priceInCents, background, and font.

 Editing Bean Properties

The Properties sheet displays each property’s name and its current value. Values are
displayed in an editable text field (strings and numbers), a choice menu (booleans), or as
painted values (colors and fonts). Each property has an associated property editor. Clicking
on a property within the Properties sheet activates the property’s editor. Properties displayed
in text fields or choice menus are edited within the Properties sheet. Because editing their
values requires a more sophisticated user interface, Color and Font property types use a
custom property editor. When you click on a color or font property a separate panel will
pop up to do the editing. Try clicking on each of the JellyBean properties.

 Saving and Restoring Beans

You can save the state of a Bean that your are customizing, and restore the Bean and
its saved state at a later time. The BeanBox uses Java Object Serialization to save and
restore Beans and their state. The following steps demonstrate how to save and restore a
Bean:
1. Drop a JellyBean on the BeanBox.
2. Change the color property to anything you want.
3. Select the File|Save menu item. A file browser will pop up; use it to save the Bean
to a file.
4. Select the File|Clear menu item.
5. Select the File|Load menu item. The file browser will again pop up; use it to retrieve
the serialized Bean. The JellyBean will be the color you chose.

Writing a Simple Bean

In this section you will learn more about Beans and the BeanBox by

 Creating a simple Bean


 Compiling and saving the Bean into a Java Archive (JAR) file
 Loading the Bean into the ToolBox
 Dropping a Bean instance into the BeanBox
 Inspecting the Bean’s properties, methods, and events
 Generating an introspection report
 Your Bean will be named SimpleBean. Here are the steps to create it and
view it in the BeanBox:

322 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

1. Write the SimpleBean code. Put it in a file named SimpleBean.java, in the direc
tory of your choice. Here's the code: NOTES
2.
3. import java.awt.*;
4. import java.io.Serializable;
5.
6. public class SimpleBean extends Canvas
7. implements Serializable
8. {
9. //Constructor sets inherited properties
10. public SimpleBean(){
11. setSize(60,40);
12. setBackground(Color.red);
13. }
14. }

SimpleBean extends the java.awt.Canvas component. SimpleBean also implements


the java.io.Serializable interface, a requirement for all Beans. SimpleBean sets the back-
ground color and component size.
15. Make sure the CLASSPATH environment variable is set to point to all
needed .class (or .jar) files. Here are some URLs that will help you to set
CLASSPATH correctly:
 The Managing Source and Class Files lesson gives good advice on how
and when to set your CLASSPATH.
 The JDK Tool Reference Page provides complete CLASSPATH infor
mation for both Windows and Solaris platforms.
16. Compile the Bean:
17. javac SimpleBean.java
18. This produces the class file SimpleBean.class Create a manifest file. Use your
favorite text editor to create a file, we'll call it manifest.tmp, that contains the fol
lowing text:

19. Name: SimpleBean.class


20. Java-Bean: True

21. Create the JAR file. The JAR file will contain the manifest and the SimpleBean class
file:

22. jar cfm SimpleBean.jar manifest.tmp SimpleBean.class

See the JAR Files trail, and the JDK JAR file documentation for complete information
on JAR files.
23. Load the JAR file into the ToolBox. Select the File|LoadJar... menu item. This

323 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
will bring up a file browser. Navigate to the SimpleBean.jar location and select it.
NOTES SimpleBean will appear at the bottom of the ToolBox. (Note that when the BeanBox
is started, all Beans in JAR files in the beans/jars directory are automatically loaded
into the ToolBox).
24. Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean
in the ToolBox. The cursor will change to a crosshair. Move the cursor to a spot
within the BeanBox and click. SimpleBean will appear as a painted rectangle with
a hatched border. This border means that SimpleBean is selected. The SimpleBean
properties will appear in the Properties sheet.

You can resize SimpleBean, because it inherits from Canvas, by dragging a corner.
You will see the cursor change to a right angle when over a corner. You can also reposition
SimpleBean within the BeanBox by dragging on any non-corner portion of the hatched
border. You will see the cursor change to crossed arrows when in position to move the
Bean.

Manipulating Events in the BeanBox

Using Introspection to Discover the Events A Bean Fires

JavaBeans provides event-oriented design patterns to give introspecting tools the


ability to discover what events a Bean can fire. For a Bean to be the source of an event, it
must implement methods that add and remove listener objects for that type of event. The
design patterns for these methods are

public void add<EventListenerType>(<EventListenerType> a)


public void remove<EventListenerType>(<EventListenerType> a)

These methods let a source Bean know where to fire events. The source Bean then
fires events at those listener Beans using the methods for those particular interfaces. For
example, if a source Bean registers ActionListener objects, it will fire events at those objects
by calling the actionPerformed method on those listeners.

To see events discovered using design patterns, drop an instance of OurButton into
the BeanBox and pull down the Edit|Events menu. This displays a list of event interfaces to
which OurButton can fire events. Note that OurButton itself only adds and removes two of
these interfaces; the rest are inherited from the base class.

Using BeanInfo to Define the Events a Bean Fires

You can explicitly “publish” the events a Bean fires by using a class that implements
the BeanInfo interface. The ExplicitButton demo Bean subclasses OurButton, and provides
an associated ExplicitButtonBeanInfo class. ExplicitButtonBeanInfo implements the following
method to explicitly define interfaces to which ExplicitButton fires events.
public EventSetDescriptor[] getEventSetDescriptors() {

324 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

try {
EventSetDescriptor push = new EventSetDescriptor(beanClass, NOTES
“actionPerformed”,
java.awt.event.ActionListener.class,
“actionPerformed”);

EventSetDescriptor changed = new EventSetDescriptor(beanClass,


“propertyChange”,
java.beans.PropertyChangeListener.class,
“propertyChange”);

push.setDisplayName(“button push”);
changed.setDisplayName(“bound property change”);
EventSetDescriptor[] rv = { push, changed};
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}

Drop an ExplicitButton instance in the BeanBox, and pull down the Edit|Events menu.
Notice that only those interfaces explicitly exposed in the ExplicitButtonBeanInfo class are
listed. No inherited capabilities are exposed.

Viewing a Bean’s Events in the BeanBox

If you select an OurButton Bean in the BeanBox, then pull down the Edit|Events
menu, you will see a list of interfaces that OurButton can fire events at. Each interface item
will, when selected, display the methods that fire different events at those interfaces. These
correspond to all the events that OurButton can fire.

Hooking Up Events in the BeanBox

In this example you will use two OurButton Bean instances to stop and start an
instance of the animated Juggler Bean. You will label the buttons “start” and “stop”; make
the start button, when pressed, invoke the Juggler Bean’s startJuggling method; and make
the stop button, when pressed, invoke the Juggler Bean’s stopJuggling method.
1. Start the BeanBox.
2. Drop a Juggler Bean and two OurButton bean instances into the BeanBox.
3. Select an OurButton instance. In the Properties sheet, change the label property
to “start”. Select the second OurButton instance and change its label to “stop”.

325 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
4. Select the start button. Choose the Edit|Events|action|actionPerformed menu item.
NOTES This causes a rubber band line to track between the start button and the cursor.
Click on the Juggler instance. This brings up the EventTargetDialog:

This list contains Juggler methods that take no arguments, or arguments of type
actionPerformed.
5. Select the startJuggling method and press OK. You will see a message that the
BeanBox is generating adapter classes.
6. Do the above two steps on the stop button, except choose the stopJuggling method
in the EventTargetDialog.

Clicking on the start and stop buttons will now start and stop the Juggler. Here is a
general description of what happened:
 The start and stop buttons, are event sources. Event sources fire events at event
targets. In this example the Juggler Bean is the event target.
 When you select the start button and choose an event method (via the Edit|Event
menu item), you are choosing the type of event the event source will fire.
 When you connect the rubber band line to another Bean, you are selecting the
event target Bean.
 The Event TargetDialog lists methods that can accept that type of event or that
take no parameters. When you choose a method in the EventTargetDialog, you
are specifying the method that will receive the fired event, and act on it. Use the
File|Save menu item to save this example to a file of your choice.

Event Adapter Classes

The BeanBox generates an adapter class that interposes between the event source
and target. The adapter class implements the appropriate event listener interface (and so is
the actual listener), catches the event fired by the button, and then calls the selected target
Bean method. Here is the BeanBox-generated adapter class that interposes between the
start button and the JugglerBean:

// Automatically generated event hookup file.


package tmp.sunw.beanbox;
import sunw.demo.juggler.Juggler;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ___Hookup_1474c0159e implements
java.awt.event.ActionListener, java.io.Serializable {
public void setTarget(sunw.demo.juggler.Juggler t) {
target = t;

326 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

}
public void actionPerformed(java.awt.event.ActionEvent arg0) { NOTES
target.startJuggling(arg0);
}
private sunw.demo.juggler.Juggler target;
}

The adapter implements the ActionListener interface that you selected in the BeanBox’s
Edit|Events menu. ActionListener declares one method, actionPerformed(), which is
implemented by the adapter to call the target Bean method (startJuggling()) that you selected.
The adapter’s setTarget() method is called by the BeanBox to set the actual target Bean,
in this case Juggler. See section 6.7 of the JavaBeans specification for a complete discussion
of event adapters.

The EventMonitor Demo Bean

The EventMonitor Bean (beans/demo/sunw/demo/encapsulatedEvents) prints out


source Bean event reports, as they occur, in a scrolling listbox. To see how this works,
take the following steps:
1. Drop OurButton and EventMonitor instances in the BeanBox. You might want to
resize the EventMonitor (and the BeanBox) to accommodate viewing the event
reports.
2. Select the OurButton instance, and choose any event method in the Edit|Events
menu.
3. Connect the rubber band line to the EventMonitor, and choose its
initiateEventSourcMonitoring in the EventTargetDialog.
4. Select the OurButton Bean. You will begin seeing event reports in the EventMonitor.

When the first event is delivered, EventMonitor analyzes the source Bean to discover
all the events it fires, creates and registers an event listener for each event type, and then
reports whenever any event is fired. This is useful for debugging. Try connecting other
demo Beans to EventMonitor to observer their events.

Bean Persistence

A Bean persists by having its properties, fields, and state information saved and restored
to and from storage. The mechanism that makes persistence possible is called serialization.
When a Bean instance is serialized, it is converted into a data stream and written to storage.
Any applet, application, or tool that uses that Bean can then “reconstitute” it by
deserialization. JavaBeans uses the JDK’s object serialization API for its serialization
needs.

327 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

All Beans must persist. To persist, your Beans must support serialization by implementing
NOTES either the java.io.Serializable interface, or the java.io.Externalizable interface. These
interfaces offer you the choice between automatic serialization, and “roll your own”. As
long as one class in a class’s inheritance hierarchy implements Serializable or Externalizable,
that class is serializable.

 Controlling Serialization
You can control the level of serialization that your Beans undergo:
 Automatic: implement Serializable. Everything gets serialized by the Java serialization
software.
 Selectively exclude fields you do not want serialized by marking with the transient
(or static) modifier.

 Writing Beans to a specific file format: implement Externalizable, and its two methods.

Default Serialization: The Serializable Interface

The Serializable interface provides automatic serialization by using the Java Object
Serialization tools. Serializable declares no methods; it acts as a marker, telling the Object
Serialization tools that your Bean class is serializable. Marking your class with Serializable
means you are telling the Java Virtual Machine (JVM) that you have made sure your class
will work with default serialization. Here are some important points about working with the
Serializable interface:
 Classes that implement Serializable must have a no-argument constructor. This
constructor will be called when an object is “reconstituted” from a .ser file.
 You don’t need to implement Serializable in your class if if it is already implemented
in a superclass (but you do need to make sure works correctly and as you expect
with default serialization).
 All fields but static and transient are serialized. Use the transient modifier to specify
fields you do not want serialized, and to specify classes that are not serializable.

The BeanBox writes serialized Beans to a file with a .ser extension.

The OurButton demo Bean uses default serialization to make its properties persist.
OurButton only added Serializable to its class definition to make use of default serialization:

public class OurButton extends Component implements Serializable,...

If you drop an OurButton instance into the BeanBox, the properties sheet displays
OurButton’s properties. To ascertain that serialization is working
1. Change some OurButton properties. For example change the font size and colors.

328 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

2. Serialize the changed OurButton instance by selecting the File|SerializeComponent...


BeanBox menu item. A file browser will pop up. NOTES
3. Put the .ser file in a JAR file with a suitable manifest.
4. Clear the BeanBox form by selecting the File|Clear menu item.

5. Reload the serialized instance by selecting the File|LoadJar menu item.

The OurButton instance will appear in the BeanBox with your property changes
intact. By implementing Serializable in your class, simple, primitive properties and fields
can be serialized. For more complex class members, different techniques must be used, as
described in the following sections.

Selective Serialization Using the transient Keyword

To exclude fields from serialization in a Serializable object from serialization, mark


the fields with the transient modifier.

transient int Status;

Default serialization will not serialize transient and static fields.

Selective Serialization: writeObject and readObject

If your serializable class contains either of the following two methods (the signatures
must be exact), then the default serialization will not take place.

private void writeObject(java.io.ObjectOutputStream out)

throws IOException;

private void readObject(java.io.ObjectInputStream in)

throws IOException, ClassNotFoundException;

You can control how more complex objects are serialized, by writing your own
implementations of the writeObject and readObject methods. Implement writeObject when
you need to exercise greater control over what gets serialized, when you need to serialize
objects that default serialization cannot handle, or when you need to add data to the
serialization stream that is not an object data member. Implement readObject to reconstruct
the data stream you wrote with writeObject.

Example: The Molecule Demo Bean

The Molecule demo keeps a version number in a static field. Since static fields are
not serialized by default, writeObject and readObject are implemented to serialize this
field. Here is the writeObject and readObject implementations in Molecule.java:

329 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

private void writeObject(java.io.ObjectOutputStream s)


NOTES
throws java.io.IOException {

s.writeInt(ourVersion);
s.writeObject(moleculeName);
}
private void readObject(java.io.ObjectInputStream s)
throws java.lang.ClassNotFoundException,
java.io.IOException {
// Compensate for missing constructor.
reset();
if (s.readInt() != ourVersion) {
throw new IOException(“Molecule.readObject: version mismatch”);
}
moleculeName = (String) s.readObject();
}

These implementations limit the fields serialized to ourVersion and moleculeName.


Any other data in the class will not be serialized.

It is best to use the ObjectInputStream’s defaultWriteObject and defaultReadObject


before doing your own specific stream writing. For example:
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
//First write out defaults
s.defaultWriteObject();
//...
}

private void readObject(java.io.ObjectInputStream s)


throws java.lang.ClassNotFoundException,
java.io.IOException {
//First read in defaults
s.defaultReadObject();
//...
}

The Externalizable Interface

Use the Externalizable interface when you need complete control over your Bean’s
serialization (for example, when writing and reading a specific file format). You need to

330 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

implement two methods: readExternal and writeExternal. Externalizable classes must also
have a no-argument constructor. NOTES
Example: The BlueButton and OrangeButton Demo Beans

When you run the BeanBox, you will see two Beans named BlueButton and
OrangeButton in the ToolBox. These two Beans are actually serialized instances of the
ExternalizableButton class.

ExternalizableButton implements the Externalizable interface. This means it does all


its own serializatio n, by implementing Externalizable.readExternal and
Externalizable.writeExternal. The BlueButtonWriter program is used by the buttons makefile
to create an ExternalizableButton instance, change its background property to blue, and
write the Bean out to the file BlueButton.ser. Another button, OrangeButton, is created the
same way using OrangeButtonWriter. The button makefile then puts these .ser files in
buttons.jar, where the ToolBox can find and reconstitute them.

Bean Customization

A Bean’s appearance and behavior can be customized at design time within Beans-
compliant builder tools. Typically there are two ways to customize a Bean:
 By using a property editor. Each Bean property has its own property editor. A
builder tool usually displays a Bean’s property editors in a property sheet. A
property editor is associated with, and edits a particular property type.
 By using customizers. Customizers give you complete GUI control over Bean
customization. Customizers are used where property editors are not practical or
applicable. Unlike a property editor, which is associated with a property, a
customizer is associated with a Bean.

Property Editors

A property editor is a tool for customizing a particular property type. Property editors
are displayed in, or activated from property sheets. A property sheet will determine a
property’s type, search for a relevant property editor, and display the property’s current
value in a relevant way.

Property editors must implement the PropertyEditor interface. PropertyEditor provides


methods that specify how a property should be displayed in a property sheet.

Here is the BeanBox’s Properties sheet containing OurButton properties:

You begin the process of editing these properties by clicking on the property entry in
the sheet.

331 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935
 The label and fontSize properties are displayed in an editable text box. Changes
NOTES can be made in place.
 The largeFont and debug properties are selection boxes with discrete choices.
 Clicking on the foreground, background, and font entries brings up separate panels.

How each of these is displayed depends on which PropertyEditor methods you


implement to return non-null (or equivalent) values.

For example, the int property editor implements the setAsText method. This indicates
to the property sheet that the property can be displayed as a String, hence an editable text
box will be used.

The Color and Font property editors use a separate panel, and merely use the property
sheet to display the current property value. The editor is displayed by clicking on that
value. To display the current property value “sample” within the property sheet you need to
override isPaintable to return true, and override paintValue to paint the current property
value in a rectangle in the property sheet. Here’s how ColorEditor implements paintValue:

public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {


Color oldColor = gfx.getColor();
gfx.setColor(Color.black);
gfx.drawRect(box.x, box.y, box.width-3, box.height-3);
gfx.setColor(color);
gfx.fillRect(box.x+1, box.y+1, box.width-4, box.height-4);
gfx.setColor(oldColor);

To support the custom property editor, you need to override two more methods:
Override supportsCustomEditor to return true, and override getCustomEditor to return a
custom editor instance. ColorEditor.getCustomEditor returns this.

Additionally, the PropertyEditorSupport class maintains a PropertyChangeListener


list, and fires property change event notifications to those listeners when a bound property
is changed.

How Property Editors are Associated with Properties

Property editors are discovered and associated with a given property by


 Explicit association via a BeanInfo object. The Molecule demo Bean uses this
technique. Within the MoleculeBeanInfo class, the Molecule Bean’s property editor
is set with the following line of code:
 pd.setPropertyEditorClass(MoleculeNameEditor.class);

332 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

 Explicit registration via java.Beans.PropertyEditorManager.registerEditor. This


method takes a pair of arguments: The class type, and the editor to be associated NOTES
with that type.
 Name search. If a class has no explicitly associated property editor, then the
PropertyEditorManager searchs for that class’s property editor by:
o Appending “Editor” to the fully qualified class name. For example, for the
java.beans.ComplexNumber class, the property editor manager would
search for the java.beans.ComplexNumberEditor class.
o Appending “Editor” to the class name and searching a class search path.
The default class path for the BeanBox is sun.beans.editors.

The BDK Property Editors

The BDK provides property editors for the primitive data types like int, boolean, and
float, and Color and Font class types. The source code for these property editors is in
beans/apis/sun/beans/editors. These sources make a good starting point for writing your
own property editors. Some things to note about the BDK property editors:

 All the “number” properties are represented as String objects. The IntEditor
overrides PropertyEditorSupport.setAsText.
 The boolean property editor is a menu of discrete choices that overrides the
PropertyEditorSupport.getTags method to return a String[] containing “True” and
“False”:
 public String[] getTags() {
 String result[] = { “True”, “False” };
 return result;
 }
 The Color and Font property editors implement custom property editors. Because
these objects require a more sophisticated interface to be easily edited a separate
component pops up to do the property editing. Overriding supportsCustomEditor
to return true signals the property sheet that this property’s editor is a custom
component. The isPaintable and paintValue methods are also overridden to provide
color and font painting in the editors property sheet sample areas.

Note that if no property editor is found for a property, the BeanBox will not display
that property in the Properties sheet.

Customizers

When you use a Bean Customizer, you get complete control over how to configure
or edit a Bean. A Customizer is like an application that specifically targets a Bean’s
customization. Sometimes properties are insufficient for representing a Bean’s configurable

333 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

attributes. Customizers are used where sophisticated instructions would be needed to change
NOTES a Bean, and where property editors are too primitive to achieve Bean customization.

All customizers must


 Extend java.awt.Component or one of its subclasses.
 Implement the java.beans.Customizer interface This means implementing methods
to register PropertyChangeListener objects, and firing property change events at
those listeners when a change to the target Bean has occurred.
 Implement a default constructor.
 Associate the customizer with its target class via BeanInfo.getBeanDescriptor.
If a Bean that has an associated Customizer is dropped into the BeanBox, you will
notice a “Customize...” item on the Edit menu.

BDK Customizers

The OurButtonCustomizer serves as an example that demonstrates the mechanics of


building a customizer. OurButtonCustomizer:
 Extends java.awt.Panel (a Component subclass).
 Implements the Customizer interface, and uses a PropertyChangeSupport object
to manage PropertyChangeListener registration and notification. See the bound
property section for a PropertyChangeSupport description.
 Implements a default constructor:
 public OurButtonCustomizer() {
 setLayout(null);
 }
 Is associated with its target class, ExplicitButton, by the following
ExplicitButtonBeanInfo code:
 public BeanDescriptor getBeanDescriptor() {
 return new BeanDescriptor(beanClass, customizerClass);
 }
 ...
 private final static Class customizerClass =
 OurButtonCustomizer.class ;

Summary

This unit discusses the features of utility package, basic File classes and the stream related
operations. The database connectivity, different types of connections are also discussed.
Some of the advanced concepts of Java such as servlets, RMI and Java beans are also the
highlights of this unit.

334 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

Questions
NOTES
1. Highlight the features of utility packages.
2. List out the classes associated with file stream.
3. Write a java program to write a set of sentence in to a text file and to read the
same from the file
4. What are inner classes?
5. Explain the process of connecting to a database through java program.
6. What is a servlets? List the steps involved in using servlets
7. What is RM I? Explain the steps involved in RMI.

335 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES NOTES

336 ANNA UNIVERSITY CHENNAI


INTERNET PROGRAMMING / WEB PROGRAMMING

NOTES NOTES

337 ANNA UNIVERSITY CHENNAI


DMC 1705 / DMC 1935

NOTES NOTES

338 ANNA UNIVERSITY CHENNAI

You might also like