Professional Documents
Culture Documents
UNIT 1 NOTES
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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”.
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.
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.
Friend-to-friend networks
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
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.
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
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.
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.
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.
Note
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.
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.
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.
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.)
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
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
With this option selected, Firefox will open the Downloads window when you start
downloading a file.
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).
Firefox will ask you where you want a file to be saved when you download something.
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.
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.
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.
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.
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 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.
By default, Firefox blocks annoying pop-up windows on web sites. Unchecking this
option will disable pop-up blocking.
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.
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
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.
Uncheck this option to disable moving and resizing windows using scripts.
Uncheck this option to make sure scripts cannot raise (bring to the front) or lower
(send to the back) windows.
Uncheck this option to prevent web pages from disabling or changing the Firefox
context menu.
Check mark this option to allow scripts to hide the status bar in pop-up windows.
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.
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....
Fonts for
NOTES
You can actually set a specific default font for various types of fonts, and character
sets.
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.
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.
Check this option to use the colors defined in your OS settings instead of the colors
specified above.
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.
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.
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.
Select this option to open this file type in the default application for that file type
(determined by the Operating System).
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.
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.
This option will make Firefox use a plugin to open the files.
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.
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.
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.
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
Here you can specify for how many days you want the browser to remember what
pages you have visited. The default is 9 days.
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.
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.
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
You can choose to have Firefox clear your private data when you close it. To configure
what data is cleared, click the Settings... button.
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....
Security Options
NOTES
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.
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.
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.
When this option is enabled, Firefox will notify you every time you are about to view
an encrypted page.
With this option enabled, Firefox will warn you when you visit a page which uses low-
NOTES grade encryption.
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.
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
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.
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.
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.
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.
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.
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.
This is the default option. Choose this if you don’t want to use a proxy.
Choose this if you want Firefox to automatically detect the proxy settings for your
network.
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.
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.
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.
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.
By default, Firefox will periodically check and notify you when a new version is
available. Uncheck this option to disable the periodic check.
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.
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.
NOTES
Protocols
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.
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
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.
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 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 (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, 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.
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.
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
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.
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.
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.
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:
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
Replying to a 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.
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.
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
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).
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.
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.
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.
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.
If your setup allows NetBIOS over TCP/IP, you have a security problem
If your setup allows File and Printer Sharing over TCP/IP, you also have a problem:
Computers that are not connected to any network can also have dangerous network
settings because the network settings were changed when Internet was installed.
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 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 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
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.
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?
NOTES
UNIT II
WWW
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.
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.
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).
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
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.”
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
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:
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;
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.
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.
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
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.
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.
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.
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.
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.
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.
If your setup allows NetBIOS over TCP/IP, you have a security problem:
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.
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 can solve your security problem by disabling NetBIOS over TCP/IP:
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
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.
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!
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.
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.
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.
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.
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
TheComRegistry $19.95
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.
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
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
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
Once you have installed ws_ftp le on your computer, click here for their tutorial on
how to transfer your files using ws_ftp.
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/
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.
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.
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:
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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
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].
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.
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.
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.
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
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.
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.
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.
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).
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.
Histogram Influencing
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
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/
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).
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.
NOTES
ImageVice
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
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.
NOTES
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.
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.
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.
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]).
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).
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.
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
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.
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).
NOTES
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.
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.
JPEG
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.
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).
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.
NOTES
NOTES
NOTES
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”
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.
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.
NOTES
JPEG Wizard
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.
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.
NOTES
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.
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
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.
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
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:
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.
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:
This will create an infinitely looping sound for both IE and Netscape. To create a
sound that doesn’t loop:
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>
</NOEMBED>
LOOP=”false”>
2.6 FORMS
Introduction to forms
Here’s a simple form that includes labels, radio buttons, and push buttons (reset the
form or submit it):
Note. This specification includes more detailed information about forms in the
NOTES subsections on form display issues.
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
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.
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.
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.
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.
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”.
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.
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”).
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.
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 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.
<!ENTITY % InputType
>
<!— attribute name required for all but submit and reset —>
NOTES
<!ELEMENT INPUT - O EMPTY — form control —>
<!ATTLIST INPUT
>
This attribute specifies the type of control to create. The default value for this attribute
is “text”.
This attribute specifies the initial value of the control. It is optional except when the
type attribute has the value “radio” or “checkbox”.
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.
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.
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.
The control type defined by the INPUT element depends on the value of the type
attribute text
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.
radio
NOTES
Creates a radio button.
submit
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.
reset
button
Creates a push button. User agents should use the value of the value attribute as the
button’s label.
hidden
NOTES
<HEAD>
</HEAD>
<BODY>
<P>
</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>
</P>
</FORM>
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>
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.
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.
>
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 —
>
selected [CI]
When set, this boolean attribute specifies that this option is pre-selected.
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.
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.
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>
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.
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.
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.
ISINDEX is deprecated. This element creates a single-line text input control. Authors
should use the INPUT element to create text input controls.
Attribute definitions
Deprecated. This attribute specifies a prompt string for the input field.
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.
DEPRECATED EXAMPLE:
NOTES
The following ISINDEX declaration:
</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,
Attribute definitions
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.
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>
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>
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.)
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.
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”
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.
Tabbing navigation
Attribute definitions
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.
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>
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
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.
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.
<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 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.
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.
In this example, the INPUT element is disabled. Therefore, it cannot receive user
NOTES input nor will its value be submitted with the form.
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.
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.
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.
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.
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.
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.
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.
When the user submits a form (e.g., by activating a submit button), the user agent
processes it as follows.
The form data set is then encoded according to the content type specified by the
enctype attribute of the FORM element.
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.
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.
User agents should render the response from the HTTP “get” and “post” transactions.
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.
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 “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.
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.
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:
As with all MIME transmissions, “CR LF” (i.e., `%0D%0A’) is used to separate lines
of data.
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).
NOTES
<FORM action=”http://server.com/cgi/handle”
enctype=”multipart/form-data”
method=”post”>
<P>
</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:
—AaB03x
Larry
—AaB03x
Content-Type: text/plain
—AaB03x—
If the user selected a second (image) file “file2.gif”, the user agent might construct the
parts as follows:
—AaB03x
Larry
—AaB03x
—BbC04y
Content-Type: text/plain
—BbC04y
Content-Type: image/gif
Content-Transfer-Encoding: binary
...contents of file2.gif...
—BbC04y—
—AaB03x—
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.
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
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.
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
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 (that is to say: in the browser) or what is commonly called DHTML ...
dynamic HTML.
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.
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
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
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.
To shield a user from variations in file storage systems among different hosts.
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.
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
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
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@
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.
EBCDIC mode
Local mode
In practice, these additional transfer modes are rarely used. They are however still
NOTES used by some legacy mainframe systems.
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 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
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.
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.
file sharing
telephony
media streaming (radio, video)
discussion forums
Some examples of pure peer-to-peer application layer networks designed for file
sharing are Gnutella and Freenet.
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
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.
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.
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
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.
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)
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)
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
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
aMule, eDonkey2000
(discontinued), eMule, eMule Plus,
eDonkey File sharing Hydranode, Jubster, lMule, Lphant,
MLDonkey, Morpheus, Pruna,
Shareaza, xMule, iMesh
File sharing,
MostFun Game Player, NeoARM game
NeoEdge peer
delivery
applications
Streaming
PDTP media or file DistribuStream
sharing
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!
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.
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.
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.
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
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”\
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.
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.
NOTES
NOTES
UNIT III
JAVA FUNDAMENTALS
3.1 JAVA FEATURES
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?
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.
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.
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.
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
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.
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
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.
Method Party()
Source
code for 0 aload 0
the
interactive 1 invokespecial #1
party <Method
invitation java.lang.Object()>
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.
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.
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.
There are 49 keywords currently defined in the Java language. These keywords
cannot be used as names for a variable, class or method.
The keywords const and goto are reserved but not used. The assert keyword
was added in Java version 1.4.
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—}
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
Line-1
Line- 2
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
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.
Line – 6
Line – 7
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.
NOTES
In Java, the
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 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
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-1
}
}
var op=expression. They are efficientely executed by the Java Runtime system than their
equivalent long forms.
The increment and decrement operators increments and decrements the value of the
variable by 1, respectively.
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.
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);
//…
j=i++;
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
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.
In Java,
if(count==0) …
if(count!=0) …
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
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
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
{
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
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.
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);
}
}
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
{
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”);
}
}
}
}
one
three
five
nine
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.
SYNTAX:
}
else
{
System.out.println(“The number “+i+” is odd”);
}
}
} NOTES
}
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
{
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
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.
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[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:
}
}
The output would be:
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 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();
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);
}
}
Java
Java
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
String Concatenation
NOTES
Arithmetic operations can’t be performed using string objects. The ‘+’ operator helps
in concatenating to strings.
Example:
Character Extraction
To retrieve only certain characters from the string object, we go for functions like
charAt(), getChars(),etc.
charAt()
Example:
String name=”Java”;
char ch;
ch = name.charAt(1);
System.out.println(“ch= “+ch);
getChars()
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);
}
}
String Comparison
The String class includes several methods that compare strings or substrings within
strings.
To compare two strings for equality, use equals( ). It has this general form:
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.
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.
Example:
class SortString
{
static String arr[] = {“Now”, “is”, “the”, “time”, “for”, “all”, “good”, “men”,
“to”, “come”, “to”, “the”, “aid”, “of”, “their”, “country”};
substring( )
Example:
concat( )
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.
Example:
Hewwo
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);
STRINGBUFFER
Example:
class buffer_example
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer(“Welcome to Java”);
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.
The order in which they appear doesn’t matter, but its good to place them in order.
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.
Private
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.
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.
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.
Any two methods can’t be coded with the same name and parameters but with
different return types.
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 “);
}
//…
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.
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
{
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.
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.
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:
Example-1
//…
String artistname;
int artistage;
public artist(String name,int age)
{
artistname=name;
artistage=age;
}
//…
In the above example, the constructor helps in initializing the fields of the class.
String firstname;
String lastname; NOTES
int artistage;
public artist(String fname,int age)
{
firstname=fname;
artistage=age;
}
System.out.println(art2.firstname);
System.out.println(art2.lastname);
System.out.println(art2.artistage);
}
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.
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:
{
artist art = new artist(); //error ! wont compile, since there is NOTES
no empty constructor.
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,
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.
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
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.
In Java, the derived (subclass) class is derived from the base(super class) class using
the keyword extends.
Example:
}
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
}
}
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.
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
}
}
Number of legs: 4
super keyword
class furniture
{
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:
Number of legs: 4
3.5 INTERFACES
Example-2:
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:
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:
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:
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:
Example:
NOTES
package samplepack;
package samplepack;
3 Now create a folder to place the class files of the package. Let the name of the
folder created be “sample”.
Example:
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
Example
NOTES
javac –classpath sample sampleB.java
Example
set classpath=e:\java_examples\sample;
Example
java sampleB
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
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
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.
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.
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.
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
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:
at arith.main(arith.java:6)
Example-2
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:
The value of c: 0
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 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)
{
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);
}
}
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
{
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”);
}
}
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.
NOTES
NOTES
UNIT IV
AWT CLASSES
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
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 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.
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.
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
It is passed to an applet when one of its various methods, such as paint( ) or update(
), is called.
Drawing Lines
drawLine( ) displays a line in the current drawing color that begins at startX,startY and
ends at endX,endY.
The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle,
respectively. They are shown here:
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.
To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods
are shown here:
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.
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.
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.
JPanel panel;
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(){
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);
}
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.
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.
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.
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.
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.
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.
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.
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
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
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 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)
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.
Thread examples.
NOTES
import java.lang.Thread;
import java.applet.Applet;
public class InfiniteThreadExample extends Applet implements Runnable
{
Thread myThread;
}
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 ) {}
}
}
}
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.
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
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.
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:
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().
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.
import java.lang.*;
import java.applet.Applet;
CDE=new CorruptedDataExample();
for(i=0;i<20;i++) /* start 20 threads */
new Thread(CDE,new String(“booThread”+i)).start();
}
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);
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 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.
1. Atmp num 0
2. Atmp Atmp+10 0
3. Btmp num 0
4. Btmp Btmp+10 0
5. num Btmp 10
. . . .
. . . .
. . . .
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.
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
try {Thread.sleep(10000);}
catch (InterruptedException e ) {}
synchronized (this) /*new*/
{tmp=num;} /*new*/
System.out.println(Thread.currentThread().getName()+
” sees the number: “ + tmp); /*new*/
}
}
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
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.*;
Multiple Locks
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.
void drawRoundTarget()
{
synchronized (RoundSync) /*new*/
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 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
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.
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
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.
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
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 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.
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.
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);
}
int i;
for(i=0;i<numPhils;i++) NOTES
{
Phil[i].resume();
Phil[i].stop();
}
}
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)
{
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.
if(stickHolder==noStick)
try {wait();} catch (InterruptedException e) {}
painter.drawStick(myNum,stickHolder);
Thuy=stickHolder;
stickHolder=noStick;
return Thuy;
}
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.
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));
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.
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
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
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )
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.
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:
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”);
As you can see from the following output, both constructors created identical
Double
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:\
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
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
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
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.
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.)
/* 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
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.
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.
Character includes several static methods that categorize characters and alter their
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
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.
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
Void
The Void class has one field, TYPE, which holds a reference to the Class object for type
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
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.
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:
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
NOTES
UNIT V
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)
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.
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.
Once you have created a StringTokenizer object, the nextToken( ) method is used
to extract consecutive tokens.
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( )
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();
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 defines the following int constants, which are used when you get or set
SimpleTimeZone
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.
Here, time0mode specifies the mode of the starting time, and time1mode specifies the
NOTES mode of the ending time. Valid mode values include
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( )
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:
Random
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.
Here is a sample program run. As you can see, a bell-like distribution of numbers
is obtained.
******************
***************** 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.
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
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).
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.
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”);
}
}
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:
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:
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;
}
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 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.
int offset,
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
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:
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());
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)
// 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.
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( )
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.
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
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
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)
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(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
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.
The seeOuter() function of the inner class can access the outer variable x since MyInner
is also a member of outer class.
To create the instance of an inner class, one must create the instance of the outer class
to tie to the inner class.
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
{
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();
}
}
JDBC DRIVERS:
Type I: “Bridge”
Type II: “Native”
Type III: “Middleware”
Type IV: “Pure”
Type1:
NOTES
Use bridging technology
Requires installation/configuration on client machines
Not good for Web
e.g. ODBC Bridge
Type 2:
Type 3:
Type 4:
JDBC APIs:
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
Overview:
NOTES
DriverManager:
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();
}
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:
Select example:
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:
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,
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
Servlet API:
NOTES
The servlet API provides the following facilities:
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:
HttpServlet: (javax.servlet.http.HttpServlet)
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:
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();
<body>
<form action = “/jhtp5/welcome1” method = “get”>
</form>
</body>
</html>
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:
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:
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)
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
Deserializing
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”
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{ String sayHello() throws RemoteException; }
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).
Building clients:
Bean:
Usage:
1. Requirements
Threadsafe
Serializable or Externalizable
Naming conventions
Component
2. Properties
NOTES
get/set
Indexed
Bound
Constrained
3. Methods
public
4. Events
EventListener
Unicast/Multicast
BeanInfo
Customizer
6. Packaging
JAR file
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.
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).
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
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.
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.
After dropping a JellyBean instance on the BeanBox, the Properties sheet displays
NOTES the JellyBean properties: color, foreground, priceInCents, background, and font.
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.
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.
In this section you will learn more about Beans and the BeanBox by
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. }
21. Create the JAR file. The JAR file will contain the manifest and the SimpleBean class
file:
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
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.
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.
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() {
try {
EventSetDescriptor push = new EventSetDescriptor(beanClass, NOTES
“actionPerformed”,
java.awt.event.ActionListener.class,
“actionPerformed”);
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.
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.
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”.
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.
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:
}
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.
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.
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.
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 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:
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.
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.
If your serializable class contains either of the following two methods (the signatures
must be exact), then the default serialization will not take place.
throws IOException;
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.
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:
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();
}
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
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.
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.
You begin the process of editing these properties by clicking on the property entry in
the sheet.
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:
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.
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
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.
BDK Customizers
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.
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.
NOTES NOTES
NOTES NOTES
NOTES NOTES