14.02.2013 Views

1 of 2 - Rajalakshmi Engineering College

1 of 2 - Rajalakshmi Engineering College

1 of 2 - Rajalakshmi Engineering College

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

IT1451 – WEB TECHNOLOGY<br />

8 TH SEMESTER – INFORMATION TECHNOLOGY<br />

UNIT – 1 NOTES<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 1


History <strong>of</strong> the Internet and World Wide<br />

Outline<br />

Web<br />

History <strong>of</strong> the Internet<br />

Personal Computing<br />

History <strong>of</strong> the World Wide Web<br />

World Wide Web Consortium (W3C)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 2


• ARPANET<br />

History <strong>of</strong> the Internet<br />

– Implemented in late 1960’s by ARPA (Advanced Research<br />

Projects Agency <strong>of</strong> DOD)<br />

– Networked computer systems <strong>of</strong> a dozen universities and<br />

institutions with 56KB communications lines<br />

– Grandparent <strong>of</strong> today’s Internet<br />

– Intended to allow computers to be shared<br />

– Became clear that key benefit was allowing fast<br />

communication between researchers – electronic-mail<br />

(email)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 3


• ARPA’s goals<br />

History <strong>of</strong> the Internet<br />

– Allow multiple users to send and receive info at same time<br />

– Network operated packet switching technique<br />

• Digital data sent in small packages called packets<br />

• Packets contained data, address info, error-control info and<br />

sequencing info<br />

• Greatly reduced transmission costs <strong>of</strong> dedicated communications<br />

lines<br />

– Network designed to be operated without centralized control<br />

• If portion <strong>of</strong> network fails, remaining portions still able to route<br />

packets<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 4


History <strong>of</strong> the Internet<br />

• Transmission Control Protocol (TCP)<br />

– Name <strong>of</strong> protocols for communicating over ARPAnet<br />

– Ensured that messages were properly routed and that they arrived<br />

intact<br />

• Organizations implemented own networks<br />

– Used both for intra-organization and communication<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 5


History <strong>of</strong> the Internet<br />

• Huge variety <strong>of</strong> networking hardware and s<strong>of</strong>tware<br />

appeared<br />

– ARPA achieved inter-communication between all platforms with<br />

development <strong>of</strong> the IP<br />

• Internetworking Protocol<br />

• Current architecture <strong>of</strong> Internet<br />

– Combined set <strong>of</strong> protocols called TCP/IP<br />

• The Internet<br />

– Limited to universities and research institutions<br />

– Military became big user<br />

– Next, government decided to access Internet for commercial<br />

purposes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 6


• Internet traffic grew<br />

History <strong>of</strong> the Internet<br />

– Businesses spent heavily to improve Internet<br />

• Better service their clients<br />

– Fierce competition among communications carriers and hardware<br />

and s<strong>of</strong>tware suppliers<br />

– Result<br />

• Bandwidth (info carrying capacity) <strong>of</strong> Internet increased<br />

tremendously<br />

• Costs plummeted<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 7


• WWW<br />

History <strong>of</strong> the World Wide Web<br />

– Allows computer users to locate and view multimedia-based<br />

documents<br />

– Introduced in 1990 by Tim Berners-Lee<br />

• Internet today<br />

– Mixes computing and communications technologies<br />

– Makes information constantly and instantly available to<br />

anyone with a connection<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 8


• W3C<br />

World Wide Web Consortium (W3C)<br />

– Founded in 1994 by Tim Berners-Lee<br />

• Devoted to developing non-proprietary and interoperable<br />

technologies for the World Wide Web and making the Web<br />

universally accessible<br />

– Standardization<br />

• W3C Recommendations: technologies standardized by W3C<br />

– include Extensible HyperText Markup Language (XHTML),<br />

Cascading Style Sheets (CSS) and the Extensible Markup<br />

Language (XML)<br />

• Document must pass through Working Draft, Candidate<br />

Recommendation and Proposed Recommendation phases before<br />

considered for W3C Recommendation<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 9


Protocols<br />

<br />

<br />

Protocols are agreed<br />

formats for transmitting<br />

data between devices<br />

The protocol determines:<br />

<br />

<br />

<br />

<br />

The error checking<br />

required<br />

Data compression<br />

method used<br />

The way the end <strong>of</strong> a<br />

message is signaled T<br />

The way the device<br />

indicates that it has<br />

received the message<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 10


Internet Protocols<br />

<br />

There are many protocols used by the<br />

Internet and the WWW, including<br />

<br />

<br />

<br />

<br />

TCP/IP<br />

HTTP<br />

FTP<br />

Electronic mail protocols<br />

<br />

<br />

IMAP<br />

POP<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 11


TCP/IP<br />

<br />

The Internet uses two main protocols (developed<br />

by Vicent Cerf and Robert Kahn)<br />

<br />

<br />

Transmission control protocol (TCP)<br />

<br />

<br />

Controls disassembly <strong>of</strong> message into packets at the origin<br />

Reassembles at the destination<br />

Internet protocol (IP)<br />

<br />

Specifies the addressing details for each packet<br />

Each packet is labeled with its origin and destination<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 12


Hypertext Transfer Protocol (HTTP)<br />

<br />

<br />

<br />

The hypertext transfer protocol (HTTP) was<br />

developed by Tim Berners-Lee in 1991<br />

HTTP was designed to transfer pages between<br />

machines<br />

The client (or Web browser) makes a request for<br />

a given page and the Server is responsible for<br />

finding it and returning it to the client<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 13


Hypertext Transfer Protocol (HTTP)<br />

<br />

<br />

Client<br />

The browser connects and requests a page from the server<br />

The server reads the page from the file system, sends it to<br />

the client and terminates the connection<br />

Request sent to Web server<br />

Response sent with files (one for each<br />

Web page, image, sound clip etc.)<br />

Web Server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 14


Electronic Mail Protocols<br />

<br />

<br />

<br />

Electronic mail uses the client/server model<br />

The organisation has an email server<br />

devoted to handling email<br />

<br />

Stores and forwards email messages<br />

Individuals use email client s<strong>of</strong>tware to<br />

read and send email (e.g. Micros<strong>of</strong>t<br />

Outlook, or Netscape Messenger)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 15


Electronic Mail Protocols Cont’d<br />

<br />

<br />

Simple Mail Transfer Protocol (SMTP)<br />

<br />

Specifies format <strong>of</strong> mail messages<br />

Post Office Protocol (POP)<br />

<br />

tells the email server to:<br />

<br />

<br />

<br />

Send mail to the user’s computer and delete it<br />

from the server<br />

Send mail to the user’s computer and do not delete<br />

it from the server<br />

Ask whether new mail has arrived<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 16


Electronic Mail Protocols Cont’d<br />

<br />

Interactive Mail Access Protocol (IMAP)<br />

<br />

<br />

Newer than POP, provides similar functions<br />

with additional features<br />

<br />

<br />

e.g. can send specific messages to the client rather<br />

than all the messages<br />

A user can view email message headers and the<br />

sender’s name before downloading the entire<br />

message<br />

Allows users to delete and search mailboxes<br />

held on the email server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 17


Electronic Mail Protocols Cont’d<br />

<br />

<br />

The disadvantage <strong>of</strong> POP<br />

<br />

You can only access messages from one PC<br />

The disadvantage <strong>of</strong> IMAP<br />

<br />

Since email is stored on the email server,<br />

there is a need for more and more expensive<br />

(high speed) storage space<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 18


MIME<br />

<br />

<br />

MIME - Multi-purpose Internet Mail<br />

Extensions<br />

The MIME protocol lets SMTP transmit<br />

multimedia files including voice, audio,<br />

and binary data across TCP/IP networks.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 19


Introduction to HyperText Markup<br />

Language 4 (HTML 4)<br />

Outline<br />

Introduction<br />

Markup Languages<br />

Editing HTML<br />

Common Tags<br />

Headers<br />

Text Styling<br />

Linking<br />

Images<br />

Formatting Text With <br />

Special Characters, Horizontal Rules and<br />

More Line Breaks<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 20


• HTML<br />

Introduction<br />

– HyperText Markup Language<br />

– HTML is a language for describing web pages.<br />

– Not a procedural programming language like C,<br />

Fortran, Cobol or Pascal<br />

– Markup language<br />

• Identify elements <strong>of</strong> a page so that a browser can render that<br />

page on your computer screen<br />

• Presentation <strong>of</strong> a document vs. structure<br />

• Hypertext Transfer Protocol (HTTP) is the<br />

communication protocol used by the Internet to<br />

transfer hypertext documents.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 21


Basic Definitions<br />

• Web Page: A web page is a document or resource <strong>of</strong> information that<br />

is suitable for the World Wide Web and can be accessed through a web<br />

browser.<br />

• Website: A collection <strong>of</strong> pages on the World Wide Web that are<br />

accessible from the same URL and typically residing on the same<br />

server<br />

• Browser: A web browser is a s<strong>of</strong>tware application for retrieving,<br />

presenting, and traversing information resources on the World Wide<br />

Web<br />

• URL: Uniform Resource Locator, the unique address which identifies<br />

a resource on the Internet for routing purposes<br />

• HTTP: A protocol to transfer hypertext requests and information<br />

between servers and browsers<br />

• Hypertext: Hypertext is text, displayed on a computer, with references<br />

(hyperlinks) to other text that the reader can immediately follow,<br />

usually by a mouse<br />

• Web server: s<strong>of</strong>tware that delivers Web pages and other documents to<br />

browsers using the HTTP protocol<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 22


• Markup language<br />

Markup Languages<br />

– Used to format text and information<br />

• HTML<br />

– Marked up with elements, delineated by tags<br />

– Tags: keywords contained in pairs <strong>of</strong> angle brackets<br />

• HTML tags<br />

– Not case sensitive<br />

– Good practice to keep all the letters in one case<br />

• Forgetting to close tags is a syntax error<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 23


Editing HTML<br />

• HTML files or documents<br />

– Written in source-code form using text editor<br />

– Notepad: Start-Programs-Accessories<br />

• HTML files<br />

– .htm or .html extensions<br />

– Name your files to describe their functionality<br />

– File name <strong>of</strong> your home page should be index.html<br />

• Errors in HTML<br />

– Usually not fatal<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 24


Common Tags<br />

• Always include the … tags<br />

• Comments placed inside tags<br />

• HTML documents<br />

– HEAD section<br />

• Info about the document<br />

• Info in header not generally rendered in display window<br />

• TITLE element names your Web page<br />

– BODY section<br />

• Page content<br />

• Includes text, images, links, forms, etc.<br />

• Elements include backgrounds, link colors and font faces<br />

• P element forms a paragraph, blank line before and after<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 25


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Welcome<br />

8 <br />

9<br />

10 <br />

11<br />

12 Welcome to Our Web Site!<br />

13<br />

14 <br />

15 <br />

Outline<br />

1. HEAD section<br />

1.1 TITLE element<br />

2. BODY section<br />

2.1 P element<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 26


• Headers<br />

Headers<br />

– Simple form <strong>of</strong> text formatting<br />

– Vary text size based on the header’s “level”<br />

– Actual size <strong>of</strong> text <strong>of</strong> header element is selected by<br />

browser<br />

– Can vary significantly between browsers<br />

• CENTER element<br />

– Centers material horizontally<br />

– Most elements are left adjusted by default<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 27


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Headers<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 <br />

14 Level 1 Header <br />

15 Level 2 header <br />

16 Level 3 header <br />

17 Level 4 header <br />

18 Level 5 header <br />

19 Level 6 header <br />

20 <br />

21<br />

22 <br />

23 <br />

Outline<br />

1. Varying header<br />

sizes<br />

1.1 Level 1 is the largest,<br />

level 6 is the smallest<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 28


Header elements H1 through H6<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 29


Text Styling<br />

• Underline style<br />

– …<br />

• Align elements with ALIGN attribute<br />

– right, left or center<br />

• Close nested tags in the reverse order from which they<br />

were opened<br />

• Emphasis (italics) style<br />

– …<br />

• Strong (bold) style<br />

– …<br />

• and tags deprecated<br />

– Overstep boundary between content and presentation<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 30


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Welcome<br />

8 <br />

9<br />

10 <br />

11 Welcome to Our Web Site!<br />

12<br />

13 We have designed this site to teach<br />

14 about the wonders <strong>of</strong> HTML. We have been using<br />

15 HTML since version 2.0,<br />

16 and we enjoy the features that have been added recently. It<br />

17 seems only a short time ago that we read our first HTML<br />

18 book. Soon you will know about many <strong>of</strong> the great new features<br />

19 <strong>of</strong> HTML 4.0.<br />

20<br />

21 Have Fun With the Site!<br />

22<br />

23 <br />

24 <br />

Outline<br />

1. EM, STRONG, and U<br />

tags<br />

1.1 Close nested tags in<br />

reverse order from<br />

which they were<br />

opened<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 31


Stylizing text on Web pages<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 32


Linking<br />

• Links inserted using the A (anchor) element<br />

– Requires HREF attribute<br />

• HREF specifies the URL you would like to link to<br />

– …<br />

– Can link to email addresses, using<br />

…<br />

– Note quotation mark placement<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 33


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Links<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 Here are my favorite Internet Search Engines<br />

14 Click on the Search Engine address to go to that<br />

15 page.<br />

16<br />

17 <br />

18 Yahoo: <br />

19 http://www.yahoo.com<br />

20<br />

21 AltaVista: <br />

22 http://www.altavista.com<br />

23<br />

24 Ask Jeeves: <br />

25 http://www.askjeeves.com<br />

26<br />

27 WebCrawler: <br />

28 http://www.webcrawler.com<br />

29 <br />

30<br />

31 <br />

32 <br />

Outline<br />

1. Anchor element<br />

1.1 HREF attribute<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 34


Linking to other Web pages<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 35


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Contact Page<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 <br />

14 My email address is <br />

15 deitel@deitel.com. Click on the address and your browser<br />

16 will open an email message and address it to me.<br />

17 <br />

18<br />

19 <br />

20 <br />

Outline<br />

1. Anchor element<br />

1.1 mailto link<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 36


• Images as anchors<br />

Images (II)<br />

• Background color<br />

– Preset colors (white, black, blue, red, etc.)<br />

– Hexadecimal code<br />

• First two characters for amount <strong>of</strong> red<br />

• Second two characters for amount <strong>of</strong> green<br />

• Last two characters for amount <strong>of</strong> blue<br />

• 00 is the weakest a color can get<br />

• FF is the strongest a color can get<br />

• Ex. black = #000000<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 37


Images<br />

• Image background<br />

– <br />

– Image does not need to be large as browser tiles image across and<br />

down the screen<br />

• Pixel<br />

– Stands for “picture element”<br />

– Each pixel represents one addressable dot <strong>of</strong> color on the screen<br />

• Insert image into page<br />

– Use tag<br />

• Attributes:<br />

– SRC = “location”<br />

– HEIGHT (in pixels)<br />

– WIDTH (in pixels)<br />

– BORDER (black by default)<br />

– ALT (text description for browsers that have images turned <strong>of</strong>f or<br />

cannot view images)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 38


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Welcome<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 <br />

14 <br />

16 <br />

17<br />

18 <br />

19 <br />

Outline<br />

1.1 Background image<br />

1.2 IMG element<br />

1.3 IMG attributes<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 39


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Nav Bar<br />

8 <br />

9<br />

10 <br />

11 <br />

12<br />

13 <br />

14 <br />

16<br />

17 <br />

18 <br />

20<br />

21 <br />

22 <br />

24<br />

25 <br />

26 <br />

28<br />

29 <br />

30


31 BORDER = "0" ALT = "Header Page"><br />

32<br />

33 <br />

34 <br />

36<br />

37 <br />

38 <br />

40 <br />

41<br />

42 <br />

43 <br />

Outline<br />

3. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 41


Using images as link anchors<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 42


• FONT element<br />

Formatting Text With <br />

– Add color and formatting to text<br />

– FONT attributes:<br />

• COLOR<br />

– Preset or hex color code<br />

– Value in quotation marks<br />

– Note: you can set font color for whole document using<br />

TEXT attribute in BODY element<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 43


Formatting Text With (II)<br />

• SIZE<br />

– To make text larger, set SIZE = “+x”<br />

– To make text smaller, set SIZE = “-x”<br />

– x is the number <strong>of</strong> font point sizes<br />

• FACE<br />

– Font <strong>of</strong> the text you are formatting<br />

– Be careful to use common fonts like Times, Arial, Courier<br />

and Helvetica<br />

– Browser will display default if unable to display specified<br />

font<br />

• Example<br />

…<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 44


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Welcome<br />

8<br />

9<br />

10<br />

11<br />

12Welcome to Our Web Site!<br />

13<br />

14<br />

15We have<br />

16designed this site to teach about the wonders <strong>of</strong><br />

17HTML.<br />

18<br />

19We have been<br />

20using HTML since version 2.0,<br />

21and we enjoy the features that have been added recently.<br />

22<br />

23It<br />

24seems only a short time ago that we read our first HTML<br />

25book.<br />

26<br />

27Soon you will<br />

28know about many <strong>of</strong> the great new feature <strong>of</strong> HTML 4.0.<br />

29<br />

30Have Fun With the Site!<br />

31<br />

32<br />

33<br />

1. FONT tag<br />

Outline<br />

1.1 FONT attributes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 45


Using the FONT element to format text<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 46


Special Characters, Horizontal Rules and<br />

More Line Breaks<br />

• Special characters<br />

– Inserted in code form<br />

– Format always &code;<br />

• Ex. &amp;<br />

– Insert an ampersand<br />

– Codes <strong>of</strong>ten abbreviated forms <strong>of</strong> the character<br />

– Codes can be in hex form<br />

• Ex. &#38; to insert an ampersand<br />

• Strikethrough with DEL element<br />

• Superscript: SUP element<br />

• Subscript: SUB element<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 47


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Contact Page<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 My email address is <br />

14 deitel@deitel.com. Click on the address and your browser<br />

15 will automatically open an email message and address it to my<br />

16 address.<br />

17<br />

18 All information on this site is &copy;<br />

19 Deitel &amp; Associates, 1999.<br />

20<br />

21 <br />

22 <br />

23 <br />

24 You may copy up to 3.14 x 102 characters<br />

25 worth <strong>of</strong> information from this site. Just make sure<br />

26 you do not copy more information than is allowable.<br />

27<br />

28 No permission is needed if you only need to use <br />

29 &lt; &frac14; <strong>of</strong> the information presented here.<br />

30<br />

31 <br />

32 <br />

Outline<br />

1. Special characters<br />

2. Strikethrough<br />

3. Superscript<br />

4. Subscript<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 48


Inserting special characters into HTML<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 49


Special Characters, Horizontal Rules and<br />

More Line Breaks (II)<br />

• Horizontal rule<br />

– tag<br />

– Inserts a line break directly below it<br />

– HR attributes:<br />

• WIDTH<br />

– Adjusts the width <strong>of</strong> the rule<br />

– Either a number (in pixels) or a percentage<br />

• SIZE<br />

– Determines the height <strong>of</strong> the horizontal rule<br />

– In pixels<br />

• ALIGN<br />

– Either left, right or center<br />

• NOSHADE<br />

– Eliminates default shading effect and displays horizontal<br />

rule as a solid-color bar<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 50


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Horizontal Rule<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17Size:4<br />

18Width:75%<br />

19<br />

20<br />

21Size:12<br />

22Width:25%<br />

23<br />

24<br />

25Size:8<br />

26Width:50%<br />

27No shade...<br />

28<br />

29<br />

30<br />

31<br />

Outline<br />

1. Horizontal rules<br />

1.1 HR attributes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 51


Using horizontal rules<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 52


Intermediate HTML 4<br />

Outline<br />

Introduction<br />

Unordered Lists<br />

Nested and Ordered Lists<br />

Basic HTML Tables<br />

Intermediate HTML Tables and Formatting<br />

Basic HTML Forms<br />

More Complex HTML Forms<br />

Internal Linking<br />

Creating and Using Image Maps<br />

Tags<br />

Tag<br />

Nested Tags<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 53


• In this Chapter<br />

– Lists<br />

– Tables<br />

– Internal linking<br />

– Image maps<br />

– Frames<br />

Introduction<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 54


• Unordered list element<br />

Unordered Lists<br />

– Creates a list in which every line begins with a bullet<br />

mark<br />

– … tags<br />

– Each item in unordered list inserted with the (list<br />

item) tag<br />

• Closing tag optional<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 55


1<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Links<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13Here are my favorite Internet Search Engines<br />

14Click on the Search Engine address to go to that<br />

15page.<br />

16<br />

17<br />

18<br />

19<br />

20Yahoo: <br />

21http://www.yahoo.com<br />

22<br />

23Alta Vista: <br />

24http://www.alta-vista.com<br />

25<br />

26Ask Jeeves: <br />

27http://www.askjeeves.com<br />

28<br />

29WebCrawler: <br />

30http://www.webcrawler.com<br />

31<br />

32<br />

33<br />

34<br />

Outline<br />

1. Unordered list<br />

1.1 List items<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 56


Unordered lists with HTML<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 57


• Nested list<br />

Nested and Ordered Lists<br />

– Contained in another list element<br />

– Nesting the new list inside the original<br />

• Browsers<br />

• Indents list one level and changes the bullet type to reflect the<br />

nesting<br />

– Insert a line <strong>of</strong> whitespace after every closed list<br />

• Indent each level <strong>of</strong> a nested list<br />

– Makes the code easier to edit and debug<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 58


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - List<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 The Best Features <strong>of</strong> the Internet<br />

14 <br />

15<br />

16 <br />

17 You can meet new people from countries around<br />

18 the world.<br />

19 You have access to new media as it becomes public:<br />

20<br />

21 <br />

22 <br />

23 <br />

24 New games<br />

25 New applications <br />

26<br />

27 <br />

28 <br />

29 For business<br />

30 For pleasure<br />

Outline<br />

1. Nested lists<br />

1.1 Three levels <strong>of</strong><br />

nesting<br />

1.2 Close tags in<br />

appropriate places<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 59


31 <br />

32 Around the clock news<br />

33 Search engines<br />

34 Shopping<br />

35 Programming<br />

36 <br />

37 HTML<br />

38 Java<br />

39 Dynamic HTML<br />

40 Scripts<br />

41 New languages<br />

42 <br />

43 <br />

44 Links<br />

45 Keeping in touch with old friends<br />

46 It is the technology <strong>of</strong> the future!<br />

47 <br />

48<br />

49 My 3 Favorite CEO's<br />

50<br />

51 <br />

52 <br />

53 <br />

54 Bill Gates<br />

55 Steve Jobs<br />

56 Michael Dell<br />

57 <br />

58<br />

59 <br />

60 <br />

Outline<br />

2. Ordered list<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 60


Nested and ordered lists in HTML<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 61


Nested and Ordered Lists (II)<br />

• Ordered list element<br />

– … tags<br />

– By default, ordered lists use decimal sequence numbers<br />

• (1, 2, 3, …)<br />

– To change sequence type, use TYPE attribute in opening tag<br />

• TYPE = “1” (default)<br />

– Decimal sequence (1, 2, 3, …)<br />

• TYPE = “I”<br />

– Uppercase Roman numerals (I, II, III, …)<br />

• TYPE = “i”<br />

– Lowercase Roman numerals (i, ii, iii, …)<br />

• TYPE = “A”<br />

– Uppercase alphabetical (A, B, C, …)<br />

• TYPE = “a”<br />

– Lowercase alphabetical (a, b, c, …)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 62


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - List<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13Web Site Outline<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

21Home page<br />

22Links page<br />

23 <br />

24 Links to search engines<br />

25 Links to information sites<br />

26 <br />

27 News sites<br />

28 <br />

29 TV based<br />

30 <br />

Outline<br />

1. OL element<br />

1.1 TYPE attribute<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 63


31 CNN<br />

32 Headline News<br />

33 <br />

34 Text based<br />

35 <br />

36 New York Times<br />

37 Washington Post<br />

38 <br />

39 <br />

40 Stock sites<br />

41 <br />

42 Links to "fun" sites<br />

43 <br />

44 Feedback page<br />

45 Contact page<br />

46 HTML Example Pages<br />

47 <br />

48<br />

49 <br />

50 <br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 64


Different types <strong>of</strong> ordered lists<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 65


• Tables<br />

Basic HTML Tables<br />

– Organize data into rows and columns<br />

– All tags and text that apply to the table go inside<br />

… tags<br />

– TABLE element<br />

• Attributes<br />

– BORDER lets you set the width <strong>of</strong> the table’s border in<br />

pixels<br />

– ALIGN: left, right or center<br />

– WIDTH: pixels (absolute) or a percentage<br />

• CAPTION element is inserted directly above the table in the<br />

browser window<br />

– Helps text-based browsers interpret table data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 66


Basic HTML tables (II)<br />

– TABLE element (cont.)<br />

• THEAD element<br />

– Header info<br />

– For example, titles <strong>of</strong> table and column headers<br />

• TR element<br />

– Table row element used for formatting the cells <strong>of</strong><br />

individual rows<br />

• TBODY element<br />

– Used for formatting and grouping purposes<br />

• Smallest area <strong>of</strong> the table we are able to format is data cells<br />

– Two types <strong>of</strong> data cells<br />

» In the header: … suitable for titles and<br />

column headings<br />

» In the table body: …<br />

– Aligned left by default<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 67


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - Tables<br />

8 <br />

9<br />

10 <br />

11<br />

12 Table Example Page<br />

13<br />

14 <br />

15 <br />

16 <br />

17<br />

18 <br />

19 <br />

20 Here is a small sample table.<br />

21<br />

22 <br />

23 <br />

24 <br />

25 <br />

26 This is the head.<br />

27 <br />

28<br />

29 <br />

30 <br />

31 <br />

Outline<br />

1. TABLE element<br />

1.1 BORDER attribute<br />

1.2 CAPTION element<br />

1.3 THEAD element<br />

1.4 TH element<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 68


32 <br />

33 This is the body.<br />

34 <br />

35<br />

36 <br />

37<br />

38 <br />

39 <br />

Outline<br />

1.5 TBODY element<br />

1.6 TD element<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 69


Intermediate HTML Tables and Formatting<br />

• COLGROUP element<br />

– Used to group and format columns<br />

• Each COL element<br />

– In the … tags<br />

– Can format any number <strong>of</strong> columns (specified by the<br />

SPAN attribute)<br />

• Background color or image<br />

– Add to any row or cell<br />

– Use BGCOLOR and BACKGROUND attributes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 70


Intermediate HTML Tables and Formatting<br />

(II)<br />

• Possible to make some data cells larger than others<br />

– ROWSPAN attribute inside any data cell<br />

• Value extends the data cell to span the specified number <strong>of</strong><br />

cells<br />

– COLSPAN attribute<br />

• Value extends the data cell to span the specified number <strong>of</strong><br />

cells<br />

– Modified cells will cover the areas <strong>of</strong> other cells<br />

• Reduces number <strong>of</strong> cells in that row or column<br />

• VALIGN attribute<br />

– top, middle, bottom and baseline<br />

– Default is middle<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 71


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Tables<br />

8<br />

9<br />

10<br />

11Table Example Page<br />

12<br />

13<br />

14 Here is a small sample table.<br />

15<br />

16 <br />

17 <br />

18 This is the Head.<br />

19 <br />

20 <br />

21<br />

22 <br />

23 <br />

24 This is the Body.<br />

25 <br />

26 <br />

27<br />

28<br />

29<br />

30 P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 72<br />

Outline


30<br />

31<br />

32<br />

33<br />

34 Here is a more complex sample table.<br />

35<br />

36 <br />

37 <br />

38 <br />

39 <br />

40 <br />

41 <br />

42 <br />

43<br />

44 <br />

45<br />

46 <br />

47 <br />

48 <br />

49 <br />

50 <br />

52 <br />

53 <br />

54 Camelid comparison<br />

55 Approximate as <strong>of</strong> 8/99<br />

56 <br />

57 <br />

58<br />

59 <br />

60 # <strong>of</strong> Humps P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 73<br />

Outline<br />

1.1COLGROUP element<br />

1.2COL element<br />

1.3ROWSPAN and<br />

COLSPAN attributes<br />

1.4BGCOLOR attribute<br />

1.5 WIDTH attribute<br />

1.6VALIGN attribute


61 Indigenous region<br />

62 Spits?<br />

63 Produces Wool?<br />

64 <br />

65<br />

66 <br />

67<br />

68 <br />

69<br />

70 <br />

71 Camels (bactrian)<br />

72 2<br />

73 Africa/Asia<br />

74 Llama<br />

75 Llama<br />

76 <br />

77<br />

78 <br />

79 Llamas<br />

80 1<br />

81 Andes Mountains<br />

82 <br />

83<br />

84 <br />

85 <br />

86<br />

87 <br />

88 <br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 74


A complex table with formatting and color<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 75


Basic HTML Forms<br />

• Forms<br />

– Collect information from people viewing your site<br />

• FORM element<br />

– METHOD attribute indicates the way the Web server will<br />

organize and send you form output<br />

• Web server: machine that processes browser requests<br />

• METHOD = “post” in a form that causes changes to server<br />

data<br />

• METHOD = “get” in a form that does not cause any changes<br />

in server data<br />

– Form data sent to server as an environment variable<br />

• Processed by scripts<br />

– ACTION attribute<br />

• Path to a script (a CGI script written in Perl, C or other<br />

languages)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 76


Basic HTML Forms (II)<br />

• INPUT element<br />

– Attributes:<br />

• TYPE (required)<br />

– Hidden inputs always have TYPE = “hidden”<br />

– Defines the usage <strong>of</strong> the INPUT element<br />

» TYPE = “text” inserts a one-line text box<br />

• NAME provides a unique identification for INPUT element<br />

• VALUE indicates the value that the INPUT element sends to<br />

the server upon submission<br />

• SIZE<br />

– For TYPE = “text”, specifies the width <strong>of</strong> the text<br />

input, measured in characters<br />

• MAXLENGTH<br />

– For TYPE = “text”, specifies the maximum number <strong>of</strong><br />

characters that the text input will accept<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 77


• INPUT element (cont.)<br />

Basic HTML Forms (III)<br />

– Include textual identifier adjacent to INPUT element<br />

– 2 types <strong>of</strong> INPUT elements that should be inserted into<br />

every form:<br />

• TYPE = “submit” inserts a button that submits data to the<br />

server<br />

– VALUE attribute changes the text displayed on the button<br />

(default is “Submit”)<br />

• TYPE = “reset” inserts a button that clears all entries the<br />

user entered into the form<br />

– VALUE attribute changes the text displayed on the button<br />

(default is “Reset”)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 78


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Forms<br />

8<br />

9<br />

10<br />

11Feedback Form<br />

12<br />

13Please fill out this form to help us improve our site.<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

22<br />

24<br />

26<br />

27<br />

Outline<br />

1. FORM element<br />

1.1Specify METHOD<br />

attribute<br />

1.2Hidden input<br />

elements<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 79


28Name:<br />

29<br />

30<br />

31<br />

32<br />

33<br />

34<br />

35<br />

36<br />

37<br />

38<br />

Outline<br />

1.3Text box INPUT<br />

element<br />

1.4“submit” and<br />

“reset” INPUT<br />

TYPEs<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 80


More Complex HTML Forms<br />

• TEXTAREA element<br />

– Inserts a scrollable text box into FORM<br />

– ROWS and COLS attributes specify the number <strong>of</strong><br />

character rows and columns<br />

• INPUT element<br />

– TYPE = “password”<br />

– Inserts a text box where data displayed as asterisks<br />

• Actual data submitted to server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 81


More Complex HTML Forms (II)<br />

• INPUT element (cont.)<br />

– TYPE = “checkbox” creates a checkbox<br />

• Used individually or in groups<br />

• Each checkbox in a group should have same NAME<br />

• Make sure that the checkboxes within a group have different<br />

VALUE attribute values<br />

– Otherwise, browser will cannot distinguish between them<br />

• CHECKED attribute checks boxes initially<br />

– TYPE = “radio”<br />

• Radio buttons similar in function and usage to checkboxes<br />

• Only one radio button in a group can be selected<br />

• CHECKED attribute indicates which radio button is selected<br />

initially<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 82


• SELECT element<br />

More Complex Forms (III)<br />

– Places a selectable list <strong>of</strong> items inside FORM<br />

• Include NAME attribute<br />

– Add an item to list<br />

• Insert an OPTION element in the …<br />

tags<br />

• Closing OPTION tag optional<br />

– SELECTED attribute applies a default selection to list<br />

– Change the number <strong>of</strong> list options visible<br />

• Including the SIZE = “x” attribute inside the <br />

tag<br />

• x number <strong>of</strong> options visible<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 83


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Forms<br />

8<br />

9<br />

10<br />

11Feedback Form<br />

12<br />

13Please fill out this form to help us improve our site.<br />

14<br />

15<br />

16<br />

17<br />

19<br />

21<br />

23<br />

24Name: <br />

25<br />

26<br />

27<br />

28Comments:<br />

29<br />

30<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 84<br />

Outline<br />

1.1TEXTAREA element


31<br />

32<br />

33<br />

34Email Address:<br />

35<br />

36<br />

37<br />

38Things you liked:<br />

39<br />

40Site design<br />

41<br />

42Links<br />

43<br />

44Ease <strong>of</strong> use<br />

45<br />

46Images<br />

47<br />

48Source code<br />

49<br />

50<br />

51<br />

52<br />

53<br />

54<br />

55<br />

56<br />

57<br />

Outline<br />

1.2“password” TYPE<br />

1.3“checkbox” TYPE<br />

1.4Checkboxes in<br />

same group must<br />

have same NAME,<br />

but different VALUE<br />

attributes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 85


Form including textareas, password boxes<br />

and checkboxes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 86


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - Forms<br />

8<br />

9<br />

10<br />

11Feedback Form<br />

12<br />

13Please fill out this form to help us improve our site.<br />

14<br />

15<br />

16<br />

17<br />

19<br />

21<br />

23<br />

24Name: <br />

25<br />

26<br />

27Comments:<br />

28<br />

29<br />

30<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 87


31Email Address:<br />

32<br />

33<br />

34Things you liked:<br />

35<br />

36Site design<br />

37<br />

38Links<br />

39<br />

40Ease <strong>of</strong> use<br />

41<br />

42Images<br />

43<br />

44Source code<br />

45<br />

46<br />

47<br />

48<br />

49<br />

50<br />

51How did you get to our site?:<br />

52<br />

53Search engine<br />

54<br />

56Links from another site<br />

57<br />

Outline<br />

1.1TYPE = “radio”<br />

creates a radio<br />

button<br />

1.2Only one radio<br />

button in a group<br />

can be selected<br />

1.3CHECKED attribute<br />

specifies which<br />

radio button is<br />

selected by default<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 88


59Deitel.com Web site<br />

60<br />

62Reference in a book<br />

63<br />

65Other<br />

66<br />

68<br />

69<br />

70<br />

71<br />

72Rate our site (1-10):<br />

73<br />

74Amazing:-)<br />

7510<br />

769<br />

778<br />

787<br />

796<br />

805<br />

814<br />

823<br />

832<br />

841<br />

85The Pits:-(<br />

86<br />

87<br />

88<br />

89<br />

90<br />

92<br />

93<br />

Outline<br />

1.4SELECT element<br />

1.5Create menu items<br />

with OPTION<br />

elements<br />

1.6SELECTED attribute<br />

specifies which<br />

option is displayed<br />

by default<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 89


HTML form including radio buttons and<br />

pulldown lists<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 90


• Internal linking<br />

Internal Linking<br />

– Assign location name to individual point in an HTML<br />

file<br />

– Location name can then be added to the page’s URL<br />

• Link to specific point on the page<br />

– Location marked by including a NAME attribute in an A<br />

(anchor) element<br />

• Ex. in list.html<br />

– URL <strong>of</strong> location<br />

• Format: page.html#name<br />

• Ex. list.html#features<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 91


1 <br />

2<br />

3 <br />

4 <br />

5<br />

6 <br />

7 Internet and WWW How to Program - List<br />

8 <br />

9<br />

10 <br />

11<br />

12 <br />

13 <br />

14 <br />

15 The Best Features <strong>of</strong> the Internet<br />

16<br />

17 <br />

18 Go to Favorite CEO's<br />

19 <br />

20<br />

21 <br />

22 You can meet new people from countries around the world.<br />

23 You have access to new media as it becomes public:<br />

24 <br />

25 New games<br />

26 New applications<br />

27 <br />

28 For Business<br />

29 For Pleasure<br />

Outline<br />

1.1Create an internal<br />

link using the NAME<br />

attribute in an A<br />

element<br />

30 P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 92


31 Around the Clock news<br />

32 Search Engines<br />

33 Shopping<br />

34 Programming<br />

35 <br />

36 HTML<br />

37 Java<br />

38 Dynamic HTML<br />

39 Scripts<br />

40 New languages<br />

41 <br />

42 <br />

43 Links<br />

44 Keeping In touch with old friends<br />

45 It is the technology <strong>of</strong> the future!<br />

46 <br />

47<br />

48 <br />

49 My 3 Favorite CEO's<br />

50 Go to Favorite Features<br />

51 <br />

52<br />

53 <br />

54 Bill Gates<br />

55 Steve Jobs<br />

56 Michael Dell<br />

57 <br />

58<br />

59 <br />

60<br />

61 P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 93<br />

Outline<br />

1.2Access internal link<br />

using HREF =<br />

“#features”


Using internal hyperlinks to make your page<br />

more navigable<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 94


• Image maps<br />

Creating and Using Image Maps<br />

– Designate certain sections <strong>of</strong> an image as hotspots<br />

– Use hotspots as anchors for linking<br />

– All elements <strong>of</strong> image map inside …<br />

tags<br />

– tag requires NAME attribute<br />

• Ex. <br />

• Hotspots designated with AREA element<br />

– AREA attributes:<br />

• HREF sets the target for the link on that spot<br />

• SHAPE and COORDS set the characteristics <strong>of</strong> the AREA<br />

• ALT provides alternate textual description<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 95


Creating and Using Image Maps (II)<br />

• AREA element (continued)<br />

– SHAPE = “rect”<br />

• Causes rectangular hotspot to be drawn around the coordinates<br />

given in the COORDS attribute<br />

• COORDS - pairs <strong>of</strong> numbers corresponding to the x and y axes<br />

– x axis extends horizontally from upper-left corner<br />

– y axis extends vertically from upper-left corner<br />

• Ex. <br />

– Rectangular hotspot with upper-left corner <strong>of</strong> rectangle at<br />

(3, 122) and lower-right corner at (73, 143)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 96


Creating and Using Image Maps (III)<br />

• AREA element (continued)<br />

– SHAPE = “poly”<br />

• Causes a hotspot to be created around specified coordinates<br />

• Ex. <br />

– SHAPE = “circle”<br />

• Creates a circular hotspot<br />

• Coordinates <strong>of</strong> center <strong>of</strong> circle and radius <strong>of</strong> circle, in pixels<br />

• Ex. <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 97


Creating and Using Image Maps (IV)<br />

• To use the image map with an IMG element<br />

– Insert the USEMAP = “#name” attribute into the<br />

IMG element<br />

– name - value <strong>of</strong> the NAME attribute in the MAP element<br />

– Ex. <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 98


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7Internet and WWW How to Program - List<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

21<br />

23<br />

25<br />

27<br />

28<br />

29<br />

30


31 COORDS = "28, 22, 24, 68, 46, 114, 84, 111, 99, 56, 86, 13"<br />

32 ALT = "Email the Deitels"><br />

33<br />

34<br />

35<br />

36<br />

38<br />

39<br />

40<br />

41<br />

42<br />

44<br />

45<br />

46<br />

47<br />

Outline<br />

1.6SHAPE =<br />

“circle”<br />

1.7Use image map<br />

with IMG element<br />

via USEMAP attribute<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 100


A picture with links anchored to an image<br />

map<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 101


• Search engines<br />

Tags<br />

– Catalog sites by following links from page to page<br />

– Save identification and classification info<br />

• <br />

– Tells browser that HTML conforms to a<br />

Transitional subset <strong>of</strong> HTML version 4.0<br />

• META tag<br />

– Main HTML element that interacts with search engines<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 102


Tags (II)<br />

• META tags<br />

– Contain two attributes that should always be used:<br />

– NAME identifies type <strong>of</strong> META tag<br />

– CONTENT provides info the search engine will catalog<br />

about your site<br />

• CONTENT <strong>of</strong> a META tag with NAME = “keywords”<br />

– Provides search engines with a list <strong>of</strong> words that describe<br />

key aspects <strong>of</strong> your site<br />

• CONTENT <strong>of</strong> a META tag with NAME = “description”<br />

– Should be 3 to 4 lines<br />

– Used by search engines to catalog and display your site<br />

• META elements<br />

– Not visible to users <strong>of</strong> the site<br />

– Should be placed inside header section<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 103


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

13<br />

14<br />

17<br />

18Internet and WWW How to Program - Welcome<br />

19<br />

20<br />

21<br />

22<br />

23Welcome to Our Web Site!<br />

24<br />

25We have<br />

26designed this site to teach about the wonders <strong>of</strong><br />

27HTML.<br />

28<br />

29We have been<br />

30using HTML since version 2.0,<br />

Outline<br />

1. META tags<br />

1.1NAME =<br />

“keywords”<br />

1.2NAME =<br />

“description”<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 104


31and we enjoy the features that have been added recently.<br />

32<br />

33It<br />

34seems only a short time ago that we read our first HTML<br />

35book.<br />

36<br />

37Soon you will<br />

38know about many <strong>of</strong> the great new feature <strong>of</strong> HTML 4.0.<br />

39<br />

40Have Fun With the Site!<br />

41<br />

42<br />

43<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 105


• Frames<br />

Tag<br />

– Display more than one HTML file at a time<br />

– If used properly, frames make your site more readable and usable<br />

• tag<br />

– Uses Frameset instead <strong>of</strong> Transitional<br />

– Tell the browser that you are using frames<br />

• tags<br />

– Tell the browser the page contains frames<br />

– Details for frames contained within<br />

… tags<br />

– COLS or ROWS attribute gives the width or height <strong>of</strong> each frame<br />

• In pixels or a percentage<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 106


• FRAME elements<br />

Tag (II)<br />

– Specify what files will make up frameset<br />

– FRAME attributes:<br />

• NAME - identifies specific frame, enabling hyperlinks to load in their<br />

intended frame<br />

– TARGET attribute <strong>of</strong> A element<br />

– SRC<br />

– Ex. <br />

– TARGET = “_blank” loads page in a new blank browser<br />

window<br />

– TARGET = “_self” loads page in the same window as<br />

anchor element<br />

– TARGET = “_parent” loads page in the parent FRAMESET<br />

– TARGET = _top” loads page in the full browser window<br />

• Gives the URL <strong>of</strong> the page that will be displayed in the specified<br />

frame<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 107


Tag (III)<br />

• Not all browsers support frames<br />

– Use the NOFRAMES element inside the FRAMESET<br />

– Direct users to a non-framed version<br />

– Provide links for downloading a frames-enabled<br />

browser<br />

• Use <strong>of</strong> frames<br />

– Do not use frames if you can accomplish same with<br />

tables or other, simpler HTML formatting<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 108


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

11<br />

12<br />

15<br />

16Internet and WWW How to Program - Main<br />

17<br />

18<br />

19<br />

20<br />

21<br />

22 <br />

23 <br />

24 <br />

25 <br />

26<br />

27 <br />

28 This page uses frames, but your browser does not support<br />

29 them.<br />

Outline<br />

1.1<br />

declares Frameset<br />

1.2FRAMESET tag gives<br />

the dimensions <strong>of</strong><br />

your frame using<br />

COLS or ROWS<br />

1.3COLS = “110,*”<br />

indicates that the<br />

first frame extends<br />

110 pixels from left<br />

side and the<br />

second frame fills<br />

the remainder <strong>of</strong><br />

the screen<br />

1.4FRAME elements<br />

1.5NOFRAMES element<br />

30 Get Internet Explorer 5 at the P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 109


31 <br />

32 Micros<strong>of</strong>t Web Site<br />

33 <br />

34<br />

35 <br />

36 <br />

Outline<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 110


• FRAME element<br />

Nested Tags<br />

– SCROLLING attribute<br />

• Set to “no” to prevent scroll bars<br />

– NORESIZE attribute prevents user from resizing the<br />

frame<br />

• Nesting frames<br />

– Include the correct number <strong>of</strong> FRAME elements inside<br />

FRAMESET<br />

– Using nested FRAMESET elements<br />

• Indent every level <strong>of</strong> FRAME tag<br />

• Makes page clearer and easier to debug<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 111


1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

12<br />

13<br />

16<br />

17<br />

18 <br />

19<br />

20 <br />

21 <br />

22 <br />

23 <br />

24 <br />

25 <br />

26<br />

27 <br />

28 This page uses frames, but your browser doesn't<br />

29 support them.<br />

Outline<br />

1. Nested FRAMESET<br />

elements<br />

1.1Indent each level <strong>of</strong><br />

FRAMESET and<br />

FRAME elements<br />

1.2Provide<br />

NOFRAMES<br />

element<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 112


30 Get Internet Explorer 5 at the<br />

31 Micros<strong>of</strong>t<br />

32 Web-Site<br />

33<br />

34 <br />

35<br />

36 <br />

37 <br />

Outline<br />

2. Page rendered by<br />

browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 113


JavaScript: Introduction to Scripting<br />

Outline<br />

Introduction<br />

Simple Program: Printing a Line <strong>of</strong> Text in a Web Page<br />

Obtaining User Input with prompt Dialogs<br />

Dynamic Welcome Page<br />

Adding Integers<br />

Memory Concepts<br />

Arithmetic<br />

Decision Making: Equality and Relational Operators<br />

Web Resources<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 114


Objectives<br />

• In this lesson, you will learn:<br />

– To be able to write simple JavaScript programs.<br />

– To be able to use input and output statements.<br />

– To understand basic memory concepts.<br />

– To be able to use arithmetic operators.<br />

– To understand the precedence <strong>of</strong> arithmetic operators.<br />

– To be able to write decision-making statements.<br />

– To be able to use relational and equality operators.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 115


Introduction<br />

• JavaScript scripting language<br />

– Enhances functionality and appearance<br />

– Client-side scripting<br />

• Makes pages more dynamic and interactive<br />

– Foundation for complex server-side scripting<br />

– Program development<br />

– Program control<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 116


• Divided into 3 parts<br />

JavaScript Origin<br />

1. Core – heart <strong>of</strong> the language, includes operators,<br />

expressions, stmts, subprograms<br />

2. Client-side – Collection <strong>of</strong> objects that support<br />

browser and interactions with users( embedded<br />

with HTML)<br />

3. Server-side – useful on web server(less frequently<br />

used)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 117


Java Vs Javascript<br />

• Java supports Oops concept whereas the<br />

JavaScript does not support<br />

• Java is a strongly typed language, JavaScript does<br />

not provide ( dynamically typed)<br />

• Objects in Java are static and are dynamic in<br />

JavaScript<br />

• Main similarity is their syntax<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 118


Simple Program: Printing a Line <strong>of</strong> Text in a<br />

Web Page<br />

• Inline scripting<br />

– Written in the <strong>of</strong> a document<br />

– tag<br />

• Indicate that the text is part <strong>of</strong> a script<br />

• type attribute<br />

– Specifies the type <strong>of</strong> file and the scripting language use<br />

• writeln method<br />

– Write a line in the document<br />

• Escape character ( \ )<br />

– Indicates “special” character is used in the string<br />

• alert method<br />

– Dialog box<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 119


Outline<br />

welcome.html<br />

(1 <strong>of</strong> 1)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 120


Outline<br />

welcome2.html<br />

(1 <strong>of</strong> 1)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 121


Outline<br />

welcome3.html<br />

1 <strong>of</strong> 1<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 122


Outline<br />

welcome4.html<br />

1 <strong>of</strong> 1<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 123


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 124


Simple Program: Printing a Line <strong>of</strong> Text in a<br />

Web Page<br />

Escape sequence Description<br />

\n Newline. Position the screen cursor at the beginning <strong>of</strong> the next line.<br />

\t Horizontal tab. Move the screen cursor to the next tab stop.<br />

\r Carriage return. Position the screen cursor to the beginning <strong>of</strong> the<br />

current line; do not advance to the next line. Any characters output<br />

after the carriage return overwrite the characters previously output on<br />

that line.<br />

\\ Backslash. Used to represent a backslash character in a string.<br />

\" Double quote. Used to represent a double quote character in a string<br />

contained in double quotes. For example,<br />

window.alert( "\"in quotes\"" );<br />

displays "in quotes" in an alert dialog.<br />

\' Single quote. Used to represent a single quote character in a string. For<br />

example,<br />

window.alert( '\'in quotes\'' );<br />

displays 'in quotes' in an alert dialog.<br />

Fig. 7.5 Some common escape sequences.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 125


Dynamic Welcome Page<br />

• A script can adapt the content based on input from<br />

the user or other variables<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 126


Outline<br />

welcome5.html<br />

(1 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 127


23<br />

24 <br />

25<br />

26 <br />

27 Click Refresh (or Reload) to run this script again.<br />

28 <br />

29 <br />

Outline<br />

welcome5.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 128


This is the prompt<br />

to the user.<br />

This is the default value that<br />

appears when the dialog<br />

opens.<br />

Dynamic Welcome Page<br />

This is the text field in which<br />

the user types the value.<br />

Fig. 7.7 Prompt dialog displayed by the window object’s prompt method.<br />

When the user clicks OK, the<br />

value typed by the user is returned<br />

to the program as a string.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 129


Adding Integers<br />

• Prompt user for two integers and calculate the sum<br />

• NaN (not a number)<br />

• parseInt<br />

– Converts its string argument to an integer<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 130


Outline<br />

Addition.html<br />

(1 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 131


24 // read in second number from user as a string<br />

25 secondNumber =<br />

26 window.prompt( "Enter second integer", "0" );<br />

27<br />

28 // convert numbers from strings to integers<br />

29 number1 = parseInt( firstNumber );<br />

30 number2 = parseInt( secondNumber );<br />

31<br />

32 // add the numbers<br />

33 sum = number1 + number2;<br />

34<br />

35 // display the results<br />

36 document.writeln( "The sum is " + sum + "" );<br />

37 // --><br />

38 <br />

39<br />

40 <br />

41 <br />

42 Click Refresh (or Reload) to run the script again<br />

43 <br />

44 <br />

Outline<br />

Addition.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 132


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 133


Memory Concepts<br />

• Variable names correspond to locations in the<br />

computer’s memory<br />

• Every variable has a name, a type, and a value<br />

• Read value from a memory location<br />

– nondestructive<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 134


Memory Concepts<br />

number1 45<br />

Fig. 7.9 Memory location showing the name and value <strong>of</strong> variable number1.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 135


Memory Concepts<br />

number1 45<br />

number2 72<br />

Fig. 7.10 Memory locations after values for variables number1 and number2 have been input.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 136


Memory Concepts<br />

number1 45<br />

number2 72<br />

sum 117<br />

Fig. 7.11 Memory locations after calculating the sum <strong>of</strong> number1 and number2.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 137


Arithmetic<br />

• Many scripts perform arithmetic calculations<br />

– Expressions in JavaScript must be written in straight-line<br />

form<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 138


JavaScript<br />

operation<br />

Arithmetic<br />

operator<br />

Arithmetic<br />

Algebraic<br />

expression<br />

JavaScript<br />

expression<br />

Addition + f + 7 f + 7<br />

Subtraction - p – c p - c<br />

Multiplication<br />

Division<br />

*<br />

/<br />

bm<br />

x<br />

x / y or -- or x y<br />

b * m<br />

x / y<br />

Remainder % r mod s r % s<br />

Fig. 7.12 Arithmetic operators.<br />

Operator(s) Operation(s) Order <strong>of</strong> evaluation (precedence)<br />

*, / or % Multiplication<br />

Division<br />

Modulus<br />

+ or - Addition<br />

Subtraction<br />

Fig. 7.13 Precedence <strong>of</strong> arithmetic operators.<br />

y<br />

Evaluated second. If there are several such<br />

operations, they are evaluated from left to right.<br />

Evaluated last. If there are several such operations,<br />

they are evaluated from left to right.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 139


Step 1.<br />

Step 2.<br />

Step 3.<br />

Step 4.<br />

Step 5.<br />

Step 6.<br />

Arithmetic<br />

y = 2 * 5 * 5 + 3 * 5 + 7;<br />

2 * 5 is 10<br />

y = 10 * 5 + 3 * 5 + 7;<br />

10 * 5 is 50<br />

y = 50 + 3 * 5 + 7;<br />

3 * 5 is 15<br />

y = 50 + 15 + 7;<br />

50 + 15 is 65<br />

y = 65 + 7;<br />

y = 72;<br />

65 + 7 is 72<br />

(Leftmost multiplication)<br />

(Leftmost multiplication)<br />

(Multiplication before addition)<br />

(Leftmost addition)<br />

(Last addition)<br />

(Last operation—place72 intoy )<br />

Fig. 7.14 Order in which a second-degree polynomial is evaluated.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 140


Decision Making: Equality and Relational<br />

Operators<br />

• Decision based on the truth or falsity <strong>of</strong> a<br />

condition<br />

– Equality operators<br />

– Relational operators<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 141


Decision Making: Equality and Relational<br />

Operators<br />

Standard algebraic<br />

equality operator or<br />

relational operator<br />

Equality operators<br />

JavaScript equality<br />

or relational<br />

operator<br />

Sample<br />

JavaScript<br />

condition<br />

Meaning <strong>of</strong><br />

JavaScript<br />

condition<br />

= == x == y x is equal to y<br />

?<br />

Relational operators<br />

!= x != y x is not equal to y<br />

> > x > y x is greater than y<br />

< < x < y x is less than y<br />

<br />

>= x >= y x is greater than or<br />

equal to y<br />

<br />


Outline<br />

welcome6.html<br />

(1 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 143


25 // determine whether the time is PM<br />

26 if ( hour >= 12 )<br />

27 {<br />

28 // convert to a 12 hour clock<br />

29 hour = hour - 12;<br />

30<br />

31 // determine whether it is before 6 PM<br />

32 if ( hour < 6 )<br />

33 document.write( "Good Afternoon, " );<br />

34<br />

35 // determine whether it is after 6 PM<br />

36 if ( hour >= 6 )<br />

37 document.write( "Good Evening, " );<br />

38 }<br />

39<br />

40 document.writeln( name +<br />

41 ", welcome to JavaScript programming!" );<br />

42 // --><br />

43 <br />

44<br />

45 <br />

46<br />

Outline<br />

welcome6.html<br />

(2 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 144


47 <br />

48 Click Refresh (or Reload) to run this script again.<br />

49 <br />

50 <br />

Outline<br />

welcome6.html<br />

(3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 145


Decision Making: Equality and Relational<br />

Operators<br />

Operators Associativity Type<br />

* / % left to right multiplicative<br />

+ - left to right additive<br />

< >= left to right relational<br />

== != left to right equality<br />

= right to left assignment<br />

Fig. 7.17 Precedence and associativity <strong>of</strong> the<br />

operators discussed so far.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 146


Outline<br />

JavaScript: Control Statements I<br />

Introduction<br />

Algorithms<br />

Control Structures<br />

if Selection Statement<br />

if…else Selection Statement<br />

while Repetition Statement<br />

Formulating Algorithms: Case Study 1 (Counter-Controlled<br />

Repetition)<br />

Formulating Algorithms with Top-Down, Stepwise<br />

Refinement: Case Study 2 (Sentinel-Controlled Repetition)<br />

Formulating Algorithms with Top-Down, Stepwise<br />

Refinement: Case Study 3 (Nested Control Structures)<br />

Assignment Operators<br />

Increment and Decrement Operators<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 147


Objectives<br />

• In this lesson, you will learn:<br />

– To understand basic problem-solving techniques.<br />

– To be able to develop algorithms through the process <strong>of</strong> top-<br />

down, stepwise refinement.<br />

– To be able to use the if and if…else selection statements<br />

to choose among alternative actions.<br />

– To be able to use the while repetition statement to execute<br />

statements in a script repeatedly.<br />

– To understand counter-controlled repetition and sentinelcontrolled<br />

repetition.<br />

– To be able to use the increment, decrement and assignment<br />

operators.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 148


• Writing a script<br />

Introduction<br />

– Thorough understanding <strong>of</strong> problem<br />

– Carefully planned approach<br />

– Understand the types <strong>of</strong> building blocks that are available<br />

– Employ proven program-construction principles<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 149


• Actions to be executed<br />

Algorithms<br />

• Order in which the actions are to be executed<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 150


Control Structures<br />

• Sequential execution<br />

– Statements execute in the order they are written<br />

• Transfer <strong>of</strong> control<br />

– Next statement to execute may not be the next one in sequence<br />

• Three control structures<br />

– Sequence structure<br />

– Selection structure<br />

• if<br />

• if…else<br />

• switch<br />

– Repetition structure<br />

• while<br />

• do…while<br />

• for<br />

• for…in<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 151


• Flowchart<br />

Control Structures<br />

– Graphical representation <strong>of</strong> algorithm or portion <strong>of</strong><br />

algorithm<br />

– Flowlines<br />

• Indicate the order the actions <strong>of</strong> the algorithm execute<br />

– Rectangle symbol<br />

• Indicate any type <strong>of</strong> action<br />

– Oval symbol<br />

• A complete algorithm<br />

– Small circle symbol<br />

• A portion <strong>of</strong> algorithm<br />

– Diamond symbol<br />

• Indicates a decision is to be made<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 152


Control Structures<br />

add grade to total total = total + grade;<br />

add 1 to counter counter = counter + 1;<br />

Fig. 8.1 Flowcharting JavaScript’s sequence structure.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 153


Control Structures<br />

JavaScript<br />

Keywords<br />

break case catch continue default<br />

delete do else finally for<br />

function if in instance<strong>of</strong> new<br />

return switch this throw try<br />

type<strong>of</strong> var void while with<br />

Keywords that<br />

are reserved but<br />

not currently<br />

used by<br />

JavaScript<br />

abstract boolean byte char class<br />

const debugger double enum export<br />

extends final float goto implements<br />

import int interface long native<br />

package private protected public short<br />

static super synchronized throws transient<br />

volatile<br />

Fig. 8.2 JavaScript keywords.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 154


if Selection Statement<br />

• Single-entry/single-exit structure<br />

• Indicate action only when the condition evaluates<br />

to true<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 155


if Selection Statement<br />

grade >= 60<br />

false<br />

true<br />

print “Passed”<br />

Fig. 8.3 Flowcharting the single-selection if statement.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 156


if…else Selection Statement<br />

• Indicate different actions to be perform when<br />

condition is true or false<br />

• Conditional operator (?:)<br />

– JavaScript’s only ternary operator<br />

• Three operands<br />

• Forms a conditional expression<br />

• Dangling-else problem<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 157


if…else Selection Statement<br />

print “Failed”<br />

false<br />

grade >= 60<br />

true<br />

print “Passed”<br />

Flowcharting the double-selection if…else statement.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 158


while Repetition Statement<br />

• Repetition structure (loop)<br />

– Repeat action while some condition remains true<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 159


while Repetition Statement<br />

true<br />

product


Formulating Algorithms:<br />

Case Study 1 (Counter-Controlled<br />

Repetition)<br />

• Counter-controlled repetition<br />

– Counter<br />

• Control the number <strong>of</strong> times a set <strong>of</strong> statements executes<br />

– Definite repetition<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 161


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Class Average Program<br />

11<br />

12 <br />

13


24 // Processing Phase<br />

25 while ( gradeCounter <br />

47 <br />

Outline<br />

average.html<br />

(2 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 163


48<br />

49 <br />

50 <br />

51 Click Refresh (or Reload) to run the script again<br />

52 <br />

53 <br />

Outline<br />

average.html<br />

(3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 164


Formulating Algorithms with Top-Down,<br />

Stepwise Refinement: Case Study 2<br />

(Sentinel-Controlled Repetition)<br />

• Indefinite repetition<br />

– Sentinel value<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 165


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Class Average Program:<br />

11 Sentinel-controlled Repetition<br />

12<br />

13 <br />

14


25 // Processing phase<br />

26 // prompt for input and read grade from user<br />

27 grade = window.prompt(<br />

28 "Enter Integer Grade, -1 to Quit:", "0" );<br />

29<br />

30 // convert grade from a string to an integer<br />

31 gradeValue = parseInt( grade );<br />

32<br />

33 while ( gradeValue != -1 ) {<br />

34 // add gradeValue to total<br />

35 total = total + gradeValue;<br />

36<br />

37 // add 1 to gradeCounter<br />

38 gradeCounter = gradeCounter + 1;<br />

39<br />

40 // prompt for input and read grade from user<br />

41 grade = window.prompt(<br />

42 "Enter Integer Grade, -1 to Quit:", "0" );<br />

43<br />

44 // convert grade from a string to an integer<br />

45 gradeValue = parseInt( grade );<br />

46 }<br />

47<br />

Outline<br />

average2.html<br />

(2 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 167


48 // Termination phase<br />

49 if ( gradeCounter != 0 ) {<br />

50 average = total / gradeCounter;<br />

51<br />

52 // display average <strong>of</strong> exam grades<br />

53 document.writeln(<br />

54 "Class average is " + average + "" );<br />

55 }<br />

56 else<br />

57 document.writeln( "No grades were entered" );<br />

58 // --><br />

59 <br />

60 <br />

61<br />

62 <br />

63 Click Refresh (or Reload) to run the script again<br />

64 <br />

65 <br />

Outline<br />

average2.html<br />

(3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 168


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 169


Formulating Algorithms with Top-Down,<br />

Stepwise Refinement: Case Study 3 (Nested<br />

Control Structures)<br />

• Consider problem<br />

• Make observations<br />

• Top-down, stepwise refinement<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 170


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Analysis <strong>of</strong> Examination Results<br />

11<br />

12 <br />

13


25 if ( result == "1" )<br />

26 passes = passes + 1;<br />

27 else<br />

28 failures = failures + 1;<br />

29<br />

30 student = student + 1;<br />

31 }<br />

32<br />

33 // termination phase<br />

34 document.writeln( "Examination Results" );<br />

35 document.writeln(<br />

36 "Passed: " + passes + "Failed: " + failures );<br />

37<br />

38 if ( passes > 8 )<br />

39 document.writeln( "Raise Tuition" );<br />

40 // --><br />

41 <br />

42<br />

43 <br />

44 <br />

45 Click Refresh (or Reload) to run the script again<br />

46 <br />

47 <br />

Outline<br />

analysis.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 172


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 173


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 174


Assignment Operators<br />

• Compound assignment operators<br />

– Abbreviate assignment expressions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 175


Assignment<br />

operator<br />

Assignment Operators<br />

Initial<br />

value <strong>of</strong><br />

variable<br />

Sample<br />

expression<br />

+=<br />

-=<br />

*=<br />

/=<br />

%=<br />

c = 3<br />

d = 5<br />

e = 4<br />

f = 6<br />

g = 12<br />

c += 7 c = c +<br />

7<br />

d -= 4 d = d -<br />

4<br />

e *= 5 e = e *<br />

5<br />

f /= 3 f = f /<br />

3<br />

g %= 9 g = g %<br />

9<br />

Fig. 8.12 Arithmetic assignment operators.<br />

Explanation Assigns<br />

10 to c<br />

1 to d<br />

20 to e<br />

2 to f<br />

3 to g<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 176


Increment and Decrement Operators<br />

• Preincrement or predecrement operator<br />

– Increment <strong>of</strong> decrement operator placed before a variable<br />

• Postincrement or postdecrement operator<br />

– Increment <strong>of</strong> decrement operator placed after a variable<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 177


Increment and Decrement Operators<br />

Operator Called Sample<br />

expression<br />

Explanation<br />

++ preincrement ++a Increment a by 1, then use the<br />

new value <strong>of</strong> a in the<br />

expression in which a resides.<br />

++ postincrement a++ Use the current value <strong>of</strong> a in<br />

the expression in which a<br />

resides, then increment a by 1.<br />

-- predecrement --b Decrement b by 1, then use the<br />

new value <strong>of</strong> b in the<br />

expression in which b resides.<br />

-- postdecrement b-- Use the current value <strong>of</strong> b in<br />

the expression in which b<br />

resides, then decrement b by 1.<br />

Fig. 8.13 increment and decrement operators.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 178


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Preincrementing and Postincrementing<br />

11<br />

12 <br />

13


26 // increment then print 6<br />

27 document.writeln( "" + ++c );<br />

28 document.writeln( "" + c ); // print 6<br />

29 // --><br />

30 <br />

31<br />

32 <br />

33 <br />

Outline<br />

increment.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 180


Increment and Decrement Operators<br />

Operator Associativity Type<br />

++ -- right to left unary<br />

* / % left to right multiplicative<br />

+ - left to right additive<br />

< >= left to right relational<br />

== != left to right equality<br />

?: right to left conditional<br />

= += -= *= /= %= right to left assignment<br />

Fig. 8.15 Precedence and associativity <strong>of</strong> the operators<br />

discussed so far.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 181


Outline<br />

JavaScript: Control Statements II<br />

Essentials <strong>of</strong> Counter-Controlled Repetition<br />

for Repetition Statement<br />

Examples Using the for Statement<br />

switch Multiple-Selection Statement<br />

do…while Repetition Statement<br />

break and continue Statements<br />

Labeled break and continue Statements<br />

Logical Operators<br />

Summary <strong>of</strong> Structured Programming<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 182


Objectives<br />

• In this lesson, you will learn:<br />

– To be able to use the for and do…while repetition<br />

statements to execute statements in a program repeatedly.<br />

– To understand multiple selection using the switch selection<br />

statement.<br />

– To be able to use the break and continue program-control<br />

statements.<br />

– To be able to use the logical operators.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 183


Essentials <strong>of</strong> Counter-Controlled<br />

Repetition<br />

• Counter-controlled repetition<br />

– Name <strong>of</strong> a control<br />

– Initial value<br />

– Increment or decrement<br />

– Final value<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 184


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Counter-Controlled Repetition<br />

11<br />

12 <br />

13 <br />

23 <br />

24<br />

Outline<br />

WhileCounter.html<br />

(1 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 185


25 <br />

26 <br />

Outline<br />

WhileCounter.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 186


for Repetition Statement<br />

• for repetition statement<br />

– Handles all the details <strong>of</strong> counter-controlled repetition<br />

– for structure header<br />

• The first line<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 187


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Counter-Controlled Repetition<br />

11<br />

12 <br />

13 <br />

22 <br />

23<br />

24 <br />

25 <br />

Outline<br />

ForCounter.html<br />

(1 <strong>of</strong> 1)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 188


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 189


for keyword<br />

for Repetition Statement<br />

Control variable name Final value <strong>of</strong> control variable<br />

for which the condition is true<br />

for ( var counter = 1; counter


var counter = 1<br />

Determine<br />

if final value<br />

<strong>of</strong> control<br />

variable<br />

has been<br />

reached.<br />

counter


Examples Using the for Statement<br />

• Summation with for<br />

• Compound interest calculation with for loop<br />

– Math object<br />

• Method pow<br />

• Method round<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 192


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Sum the Even Integers from 2 to 100<br />

11<br />

12 <br />

13 <br />

22 <br />

23<br />

24 <br />

25 <br />

Sum.html<br />

(1 <strong>of</strong> 1)<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 193


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 194


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Calculating Compound Interest<br />

11<br />

12 <br />

13


26 for ( var year = 1; year <br />

35 <br />

36<br />

37 <br />

38 <br />

Outline<br />

Interest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 196


switch Multiple-Selection Statement<br />

• Controlling expression<br />

• Case labels<br />

• Default case<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 197


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Switching between XHTML List Formats<br />

11<br />

12 <br />

13


23 switch ( choice ) {<br />

24 case "1":<br />

25 startTag = "";<br />

26 endTag = "";<br />

27 listType = "Bullet List";<br />

28 break;<br />

29 case "2":<br />

30 startTag = "";<br />

31 endTag = "";<br />

32 listType = "Ordered List: Numbered";<br />

33 break;<br />

34 case "3":<br />

35 startTag = "";<br />

36 endTag = "";<br />

37 listType = "Ordered List: Lettered";<br />

38 break;<br />

39 default:<br />

40 validInput = false;<br />

41 }<br />

42<br />

43 if ( validInput == true ) {<br />

44 document.writeln( listType + startTag );<br />

45<br />

46 for ( var i = 1; i


48<br />

49 document.writeln( endTag );<br />

50 }<br />

51 else<br />

52 document.writeln( "Invalid choice: " + choice );<br />

53 // --><br />

54 <br />

55<br />

56 <br />

57 <br />

58 Click Refresh (or Reload) to run the script again<br />

59 <br />

60 <br />

Outline<br />

SwitchTest.html<br />

(3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 200


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 201


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 202


switch Multiple-Selection Statement<br />

true<br />

case a case a action(s)<br />

case b<br />

.<br />

.<br />

.<br />

false<br />

false<br />

false<br />

case b action(s)<br />

break<br />

break<br />

true<br />

case z case z action(s) break<br />

default action(s)<br />

true<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 203


do…while Repetition Statement<br />

• Similar to the while statement<br />

• Tests the loop continuation condition after the<br />

loop body executes<br />

• Loop body always executes at least once<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 204


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Using the do...while Repetition Statement<br />

11<br />

12 <br />

13 <br />

24 <br />

Outline<br />

DoWhileTest.html<br />

(1 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 205


25<br />

26 <br />

27 <br />

Outline<br />

DoWhileTest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 206


do…while Repetition Structure<br />

action(s)<br />

condition<br />

false<br />

do…while repetition statement flowchart.<br />

true<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 207


• break<br />

break and continue Statements<br />

– Immediate exit from the structure<br />

– Used to escape early from a loop<br />

– Skip the remainder <strong>of</strong> a switch statement<br />

• continue<br />

– Skips the remaining statements in the body <strong>of</strong> the structure<br />

– Proceeds with the next iteration <strong>of</strong> the loop<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 208


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 <br />

11 Using the break Statement in a for Structure<br />

12 <br />

13<br />

14 <br />

15


23 document.writeln(<br />

24 "Broke out <strong>of</strong> loop at count = " + count );<br />

25 // --><br />

26 <br />

27<br />

28 <br />

29 <br />

Outline<br />

BreakTest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 210


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 <br />

11 Using the continue Statement in a for Structure<br />

12 <br />

13<br />

14 <br />

15


24 document.writeln( "Used continue to skip printing 5" );<br />

25 // --><br />

26 <br />

27<br />

28 <br />

29 <br />

Outline<br />

ContinueTest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 212


Labeled break and continue Statements<br />

• Labeled break statement<br />

– Break out <strong>of</strong> a nested set <strong>of</strong> structures<br />

– Immediate exit from that structure and enclosing repetition<br />

structures<br />

– Execution resumes with first statement after enclosing<br />

labeled statement<br />

• Labeled continue statement<br />

– Skips the remaining statements in structure’s body and<br />

enclosing repetition structures<br />

– Proceeds with next iteration <strong>of</strong> enclosing labeled repetition<br />

structure<br />

– Loop-continuation test evaluates immediately after the<br />

continue statement executes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 213


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Using the break Statement with a Label<br />

11<br />

12 <br />

13


26<br />

27 // the following line is skipped<br />

28 document.writeln( "This line should not print" );<br />

29 }<br />

30<br />

31 document.writeln( "End <strong>of</strong> script" );<br />

32 // --><br />

33 <br />

34<br />

35 <br />

36 <br />

Outline<br />

BreakLabelTest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 215


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Using the continue Statement with a Label<br />

11<br />

12 <br />

13


26 }<br />

27 // --><br />

28 <br />

29<br />

30 <br />

31 <br />

Outline<br />

ContinueLabelTest.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 217


• More logical operators<br />

– Logical AND ( && )<br />

– Logical OR ( || )<br />

– Logical NOT ( ! )<br />

Logical Operators<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 218


Logical Operators<br />

expression1 expression2 expression1 &&<br />

expression2<br />

false false false<br />

false true false<br />

true false false<br />

true true true<br />

Fig. 9.15 Truth table for the && (logical AND)<br />

operator.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 219


Logical Operators<br />

expression1 expression2 expression1 ||<br />

expression2<br />

false false false<br />

false true true<br />

true false true<br />

true true true<br />

Fig. 9.16 Truth table for the || (logical OR) operator.<br />

expression !expression<br />

false true<br />

true false<br />

Fig. 9.17 Truth table for operator ! (logical negation).<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 220


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Demonstrating the Logical Operators<br />

11<br />

12 <br />

13


26 "true && true: " + ( true && true ) +<br />

27 "" );<br />

28<br />

29 document.writeln(<br />

30 "Logical OR (||)" +<br />

31 "false || false: " + ( false || false ) +<br />

32 "false || true: " + ( false || true ) +<br />

33 "true || false: " + ( true || false ) +<br />

34 "true || true: " + ( true || true ) +<br />

35 "" );<br />

36<br />

37 document.writeln(<br />

38 "Logical NOT (!)" +<br />

39 "!false: " + ( !false ) +<br />

40 "!true: " + ( !true ) + "" );<br />

41<br />

42 document.writeln( "" );<br />

43 // --><br />

44 <br />

45<br />

46 <br />

47 <br />

Outline<br />

LogicalOperators.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 222


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 223


Logical Operators<br />

Operator Associativity Type<br />

++ -- ! right to left unary<br />

* / % left to right multiplicative<br />

+ - left to right additive<br />

< >= left to right relational<br />

== != left to right equality<br />

&& left to right logical AND<br />

|| left to right logical OR<br />

?: right to left conditional<br />

= += -= *= /= %= right to left assignment<br />

Fig. 9.19 Precedence and associativity <strong>of</strong> the operators discussed<br />

so far.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 224


Summary <strong>of</strong> Structured Programming<br />

• Flowcharts<br />

– Reveal the structured nature <strong>of</strong> programs<br />

• Single-entry/single-exit control structures<br />

– Only one way to enter and one way to exit each control<br />

structure<br />

• Control structure stacking<br />

– The exit point <strong>of</strong> one control structure is connected to the<br />

entry point <strong>of</strong> the next control structure<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 225


Summary <strong>of</strong> Structured Programming<br />

Rep etition<br />

while statement<br />

T<br />

F<br />

statement<br />

do…while<br />

T<br />

F<br />

statement<br />

for<br />

Single-entry/single-exit sequence, selection and repetition structures. (1 <strong>of</strong> 3)<br />

T<br />

F<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 226


Se lection<br />

Summary <strong>of</strong> Structured Programming<br />

if statement<br />

if…else statement<br />

(single se lec tion)<br />

(d ouble se lec tion)<br />

T<br />

F T<br />

F<br />

switch statement<br />

(m ultiple selection)<br />

T<br />

break<br />

F<br />

break<br />

T<br />

F<br />

.<br />

Single-entry/single-exit sequence, selection and repetition structures. (2 <strong>of</strong> 3)<br />

break<br />

T<br />

F<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 227


Sequence<br />

Summary <strong>of</strong> Structured Programming<br />

.<br />

Single-entry/single-exit sequence, selection and repetition structures. (3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 228


Summary <strong>of</strong> Structured Programming<br />

Rules for Forming Structured Programs<br />

1) Begin with the “simplest flowchart” (Fig. 9.22).<br />

2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence.<br />

3) Any rectangle (action) can be replaced by any control structure (sequence, if, if…else,<br />

switch, while, do…while or for).<br />

4) Rules 2 and 3 may be applied as <strong>of</strong>ten as you like and in any order.<br />

Fig. 9.21 Rules for forming structured programs.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 229


Summary <strong>of</strong> Structured Programming<br />

Simplest flowchart.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 230


Summary <strong>of</strong> Structured Programming<br />

Rule 2 Rule 2 Rule 2<br />

Repeatedly applying rule 2 to the simplest flowchart.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 231<br />

.<br />

.<br />

.


Summary <strong>of</strong> Structured Programming<br />

Ru l e 3<br />

Rule 3<br />

Applying rule 3 to the simplest flowchart.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 232


Summary <strong>of</strong> Structured Programming<br />

Stacked building blocks Nested building blocks<br />

Stacked, nested and overlapped building blocks.<br />

Overlapping building blocks<br />

(Illegal in structured programs)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 233


Summary <strong>of</strong> Structured Programming<br />

Unstructured flowchart.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 234


Outline<br />

JavaScript: Functions<br />

Function Definitions<br />

Scope Rules<br />

JavaScript Global Functions<br />

Recursion<br />

Recursion vs. Iteration<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 235


• In this, you will learn:<br />

Objectives<br />

– To understand how to construct programs modularly from<br />

small pieces called functions.<br />

– To be able to create new functions.<br />

– To understand the mechanisms used to pass information<br />

between functions.<br />

– To understand how the visibility <strong>of</strong> identifiers is limited to<br />

specific regions <strong>of</strong> programs.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 236


Functions<br />

• A piece <strong>of</strong> code that performs a specific task<br />

• Built in Function<br />

– alert() – Open a box containing the message<br />

– prompt() – display a prompt window with field for the user<br />

to enter a text string<br />

– toLowerCase()<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 237


Function Definitions<br />

• Format <strong>of</strong> a function definition<br />

function function-name( parameter-list )<br />

{<br />

declarations and statements<br />

}<br />

– Function name any valid identifier<br />

– Parameter list names <strong>of</strong> variables that will receive arguments<br />

• Must have same number as function call<br />

• May be empty<br />

– Declarations and statements<br />

• Function body (“block” <strong>of</strong> code)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 238


<br />

<br />

function about(name, age)<br />

{<br />

}<br />

about("abc",95);<br />

<br />

<br />

<br />

document.write(" All about You");<br />

document.write("Your Name is" + name);<br />

document.write("You are" + age);<br />

.<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 239


Function Definitions<br />

• Finding the maximum <strong>of</strong> 3 numbers<br />

– Prompt for 3 inputs<br />

– Convert to numbers<br />

– Pass to maximum<br />

– Math.max<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 240


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Finding the Maximum <strong>of</strong> Three Values<br />

11<br />

12 <br />

13


24<br />

25 var maxValue = maximum( value1, value2, value3 );<br />

26<br />

27 document.writeln( "First number: " + value1 +<br />

28 "Second number: " + value2 +<br />

29 "Third number: " + value3 +<br />

30 "Maximum is: " + maxValue );<br />

31<br />

32 // maximum method definition (called from line 25)<br />

33 function maximum( x, y, z )<br />

34 {<br />

35 return Math.max( x, Math.max( y, z ) );<br />

36 }<br />

37 // --><br />

38 <br />

39<br />

40 <br />

41 <br />

42 Click Refresh (or Reload) to run the script again<br />

43 <br />

44 <br />

.<br />

Outline<br />

Call function maximum and pass it the value <strong>of</strong><br />

variables value1, value2 and value3. Maximum.html<br />

(2 <strong>of</strong> 2)<br />

Method max returns the larger <strong>of</strong> the two<br />

integers passed to it.<br />

Variables x, y and z get the value <strong>of</strong> variables<br />

value1, value2 and value3, respectively.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 242


Programmer-defined maximum function (1 <strong>of</strong> 2).<br />

Function Definitions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 243


Programmer-defined maximum function (2 <strong>of</strong> 2).<br />

Function Definitions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 244


• Scope<br />

Scope Rules<br />

– How a Variable can be accessed<br />

– Local & Global<br />

– Inside function is local or function scope<br />

• Identifiers exist only between opening and closing braces<br />

• Local variables hide global variables<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 245


<br />

<br />

<br />

<br />

var a=32;<br />

var b=a;<br />

var c=setlocal(17);<br />

document.write("a is "+a);<br />

document.write("b is "+b);<br />

document.write("c is "+c);<br />

function setlocal(d)<br />

{<br />

a=d;<br />

alert("bis inside" + b);<br />

return(a);<br />

}<br />

<br />

<br />

<br />

Example<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 246


JavaScript Global Functions<br />

Global function Description<br />

parseFloat This function takes a string argument and attempts<br />

to convert the beginning <strong>of</strong> the string into a floatingpoint<br />

value. If the conversion is unsuccessful, the<br />

function returns NaN; otherwise, it returns the<br />

converted value (e.g., parseFloat( "abc123.45" )<br />

returns NaN, and parseFloat( "123.45abc" )<br />

returns the value 123.45).<br />

parseInt This function takes a string argument and attempts<br />

to convert the beginning <strong>of</strong> the string into an integer<br />

value. If the conversion is unsuccessful, the function<br />

returns NaN; otherwise, it returns the converted<br />

value (e.g., parseInt( "abc123" ) returns NaN, and<br />

parseInt( "123abc" ) returns the integer value<br />

123). This function takes an optional second<br />

argument, from 2 to 36, specifying the radix (or<br />

base) <strong>of</strong> the number. Base 2 indicates that the first<br />

argument string is in binary format, base 8 indicates<br />

that the first argument string is in octal format and<br />

base 16 indicates that the first argument string is in<br />

hexadecimal format. See see Appendex E, Number<br />

Systems, for more information on binary, octal and<br />

hexadecimal numbers.<br />

unescape This function takes a string as its argument and<br />

returns a string in which all characters previously<br />

encoded with escape are decoded.<br />

Fig. 10.9 JavaScript global functions.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 247


• Recursive functions<br />

– Call themselves<br />

Recursion<br />

• Recursion step or recursive call<br />

• Part <strong>of</strong> return statement<br />

– Must have base case<br />

• Simplest case <strong>of</strong> problem<br />

• Returns value rather than calling itself<br />

– Each recursive call simplifies input<br />

• When simplified to base case, functions return<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 248


5!<br />

5 * 4!<br />

4 * 3!<br />

3 * 2!<br />

2 * 1!<br />

1<br />

Recursion<br />

Final value = 120<br />

5!<br />

5! = 5 * 24 = 120 is returned<br />

5 * 4!<br />

4! = 4 * 6 = 24 is returned<br />

4 * 3!<br />

3! = 3 * 2 = 6 is returned<br />

3 * 2!<br />

2! = 2 * 1 = 2 is returned<br />

2 * 1!<br />

1 returned<br />

1<br />

(a) Procession <strong>of</strong> recursive calls. (b) Values returned from each recursive call.<br />

Recursive evaluation <strong>of</strong> 5!.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 249


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Recursive Factorial Function<br />

11<br />

12 <br />

13 document.writeln( "Factorials <strong>of</strong> 1 to 10" );<br />

14 document.writeln(<br />

15 "" );<br />

16<br />

17 for ( var i = 0; i


23 // Recursive definition <strong>of</strong> function factorial<br />

24 function factorial( number )<br />

25 {<br />

26 if ( number


Factorial calculation with a recursive function.<br />

Recursion<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 252


Example<br />

<br />

<br />

<br />

Calculating Sphere Volumes<br />

<br />

function displayVolume()<br />

{ var radius = parseFloat( myForm.radiusField.value );<br />

window.status = "Volume is " + sphereVolume( radius ); }<br />

function sphereVolume( r )<br />

{ return ( 4.0 / 3.0 ) * Math.PI * Math.pow( r, 3 ); }<br />

<br />

<br />

<br />

Enter radius <strong>of</strong> sphere<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 253


• Iteration<br />

Recursion vs. Iteration<br />

– Explicitly uses repetition structures to achieve result<br />

– Terminates when loop-continuation condition fails<br />

– Often faster than recursion<br />

• Recursion<br />

– Repeats through function calls<br />

– Terminates when base case reached<br />

– Slower due to function call overhead<br />

• Each call generates new copy <strong>of</strong> local variables<br />

– Easy to read and debug when modeling problem with<br />

naturally recursive solution<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 254


Outline<br />

JavaScript: Arrays<br />

Arrays<br />

Declaring and Allocating Arrays<br />

Examples Using Arrays<br />

References and Reference Parameters<br />

Sorting Arrays<br />

Searching Arrays: Linear Search and Binary Search<br />

Multidimensional Arrays<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 255


• In this, you will learn:<br />

Objectives<br />

– To introduce the array data structure.<br />

– To understand the use <strong>of</strong> arrays to store, sort and search lists<br />

and tables <strong>of</strong> values.<br />

– To understand how to declare an array, initialize an array<br />

and refer to individual elements <strong>of</strong> an array.<br />

– To be able to pass arrays to functions.<br />

– To be able to search and sort an array.<br />

– To be able to declare and manipulate multi-dimensional<br />

arrays.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 256


Arrays<br />

• Ordered set <strong>of</strong> data elements which can be accessed thru a<br />

single variable<br />

– Each element referenced by a number<br />

• Start at “zeroth element”<br />

• Subscript or index<br />

– Accessing a specific element<br />

• Name <strong>of</strong> array<br />

• Brackets<br />

• Number <strong>of</strong> element<br />

– Arrays hold mixed data types<br />

• Var data = [“mon”,34, 76.34,”wed”];<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 257


Name <strong>of</strong> array<br />

Position number (index<br />

or subscript) <strong>of</strong> the<br />

element within array c<br />

Arrays<br />

c[ 0 ]<br />

c[ 1 ]<br />

c[ 2 ]<br />

c[ 3 ]<br />

c[ 4 ]<br />

c[ 5 ]<br />

c[ 6 ]<br />

c[ 7 ]<br />

c[ 8 ]<br />

c[ 9 ]<br />

c[ 10 ]<br />

c[ 11 ]<br />

A 12-element array.<br />

-45<br />

6<br />

0<br />

72<br />

1543<br />

-89<br />

0<br />

62<br />

-3<br />

1<br />

6453<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 258<br />

78


Arrays<br />

Operators Associativity Type<br />

() [] . left to right highest<br />

++ -- ! right to left unary<br />

* / % left to right multiplicative<br />

+ - left to right additive<br />

< >= left to right relational<br />

== != left to right equality<br />

&& left to right logical AND<br />

|| left to right logical OR<br />

?: right to left conditional<br />

= += -= *= /= %= right to left assignment<br />

Fig. 11.2 Precedence and associativity <strong>of</strong> the operators discussed so far.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 259


Declaring and Allocating Arrays<br />

• Arrays in memory<br />

– Objects<br />

– Operator new<br />

• Allocates memory for objects<br />

• Dynamic memory allocation operator<br />

var c;<br />

c = new Array( 12 );<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 260


Examples Using Arrays<br />

• Possible to declare and initialize in one step<br />

– Specify list <strong>of</strong> values<br />

• Initializer list<br />

var n = [ 10, 20, 30, 40, 50 ];<br />

var n = new Array( 10, 20, 30, 40, 50 );<br />

– Also possible to only initialize some values<br />

• Leave uninitialized elements blank<br />

• Uninitialized elements default to “undefined”<br />

var n = [ 10, 20, , 40, 50 ];<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 261


<br />

<br />

<br />

Accessing Arrays<br />

<br />

var data = [“mon, “tue”, 34, 76.34];<br />

var len = data.length;<br />

for(var i=0;i


References and Reference Parameters<br />

• Two ways to pass parameters<br />

– Pass-by-value<br />

• Pass copy <strong>of</strong> original value<br />

• Default for numbers and booleans<br />

• Original variable is unchanged<br />

– Pass-by-reference<br />

• How objects are passed, like arrays<br />

• Pass location in memory <strong>of</strong> value<br />

• Allows direct access to original value<br />

• Improves performance<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 263


• Sorting<br />

– Important computing task<br />

Sorting Arrays<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 264


SORTING<br />

<br />

<br />

Sorting an Array with Bubble Sort<br />

<br />

function start()<br />

{ var a = [ 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 ];<br />

document.writeln( "Sorting an Array" );<br />

outputArray( "Data items in original order: ", a );<br />

bubbleSort( a ); // sort the array<br />

outputArray( "Data items in ascending order: ", a );<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 265


SORTING<br />

function outputArray( header, theArray )<br />

{ document.writeln( "" + header + theArray.join( " " ) + "" );<br />

} // sort the elements <strong>of</strong> an array with bubble sort<br />

function bubbleSort( theArray )<br />

{<br />

for ( var pass = 1; pass < theArray.length; ++pass ) for ( var i = 0; i <<br />

theArray.length - 1; ++i )<br />

if ( theArray[ i ] > theArray[ i + 1 ] )<br />

swap( theArray, i, i + 1 ); // swap elements<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 266


SORTING<br />

• // swap two elements <strong>of</strong> an array<br />

• function swap( theArray, first, second )<br />

• { var hold; // temporary holding area for swap<br />

hold = theArray[ first ];<br />

• theArray[ first ] = theArray[ second ];<br />

• theArray[ second ] = hold; }<br />

• <br />

• <br />

• <br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 267


Searching Arrays: Linear Search and Binary<br />

Search<br />

• Searching<br />

– Look for matching key value<br />

• Linear search<br />

– Iterate through each element until match found<br />

– Inefficient<br />

• Worst case scenario, must test entire array<br />

• Binary search<br />

– Requires sorted data<br />

– Cuts search range in half each iteration<br />

– Efficient<br />

• Only look at small fraction <strong>of</strong> elements<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 268


Linear Search<br />

<br />

Linear Search <strong>of</strong> an Array<br />

<br />

var a = new Array( 100 ); // create an Array // fill Array with<br />

even integer values from 0 to 198<br />

for ( var i = 0; i < a.length; ++i ) a[ i ] = 2 * i;<br />

// function called when "Search" button is pressed function<br />

buttonPressed()<br />

{<br />

var searchKey = searchForm.inputVal.value;<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 269


Linear Search<br />

var element = linearSearch( a, parseInt( searchKey ) );<br />

if ( element != -1 )<br />

searchForm.result.value = "Found value in element " +<br />

element;<br />

else<br />

searchForm.result.value = "Value not found"; }<br />

// Search "theArray" for the specified "key" value function<br />

linearSearch( theArray, key )<br />

{ for ( var n = 0; n < theArray.length; ++n )<br />

if ( theArray[ n ] == key ) return n;<br />

return -1; }<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 270


SEARCHING<br />

<br />

<br />

Enter integer search key<br />

<br />

<br />

Result <br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 271


SEARCHING<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 272


zMultidimensional Arrays<br />

• Two-dimensional arrays analogous to tables<br />

– Rows and columns<br />

• Specify row first, then column<br />

– Two subscripts<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 273


Row 0<br />

Row 1<br />

Row 2<br />

Multidimensional Arrays<br />

Column 0 Column 1 Column 2 Column 3<br />

a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]<br />

a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]<br />

a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]<br />

Column subscript (or index)<br />

Row subscript (or index)<br />

Array name<br />

Two-dimensional array with three rows and four columns.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 274


Multidimensional Arrays<br />

• Declaring and initializing multidimensional arrays<br />

– Group by row in square brackets<br />

– Treated as arrays <strong>of</strong> arrays<br />

– Creating array b with one row <strong>of</strong> two elements and a second<br />

row <strong>of</strong> three elements:<br />

var b = [ [ 1, 2 ], [ 3, 4, 5 ] ];<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 275


Multidimensional Arrays<br />

• Also possible to use new operator<br />

– Create array b with two rows, first with five columns and<br />

second with three:<br />

var b;<br />

b = new Array( 2 );<br />

b[ 0 ] = new Array( 5 );<br />

b[ 1 ] = new Array( 3 );<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 276


Outline<br />

JavaScript: Objects<br />

Introduction<br />

Thinking About Objects<br />

Math Object<br />

String Object<br />

Fundamentals <strong>of</strong> Characters and Strings<br />

Methods <strong>of</strong> the String Object<br />

Character-Processing Methods<br />

Searching Methods<br />

HTML Markup Methods<br />

Date Object<br />

document Object<br />

window Object<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 277


Introduction<br />

• Use JavaScript to manipulate every element <strong>of</strong><br />

HTML document from a script<br />

• Reference for several <strong>of</strong> JavaScript’s built-in<br />

objects<br />

• Demonstrates the capabilities<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 278


• Objects<br />

– Attributes<br />

– Behaviors<br />

Thinking About Objects<br />

– Encapsulate date and methods<br />

– Property <strong>of</strong> information hiding<br />

– Details hidden within the objects themselves<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 279


Math Object<br />

• Allow the programmer to perform many common<br />

mathematical calculations<br />

Method Description Example<br />

abs( x ) absolute value <strong>of</strong> x abs( 7.2 ) is 7.2<br />

abs( 0.0 ) is 0.0<br />

abs( -5.6 ) is 5.6<br />

ceil( x ) rounds x to the smallest ceil( 9.2 ) is 10.0<br />

integer not less than x ceil( -9.8 ) is -9.0<br />

cos( x ) trigonometric cosine <strong>of</strong> x cos( 0.0 ) is 1.0<br />

(x in radians)<br />

exp( x ) exponential method ex exp( 1.0 ) is 2.71828<br />

exp( 2.0 ) is 7.38906<br />

floor( x ) rounds x to the largest floor( 9.2 ) is 9.0<br />

integer not greater than x floor( -9.8 ) is -10.0<br />

log( x ) natural logarithm <strong>of</strong> x log( 2.718282 ) is 1.0<br />

(base e)<br />

log( 7.389056 ) is 2.0<br />

max( x, y ) larger value <strong>of</strong> x and y max( 2.3, 12.7 ) is 12.7<br />

max( -2.3, -12.7 ) is -2.3<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 280


Math Object<br />

min( x, y ) smaller value <strong>of</strong> x min( 2.3, 12.7 ) is 2.3<br />

and y<br />

min( -2.3, -12.7 ) is -12.7<br />

pow( x, y ) x raised to power y pow( 2.0, 7.0 ) is 128.0<br />

(xy)<br />

pow( 9.0, .5 ) is 3.0<br />

round( x ) rounds x to the round( 9.75 ) is 10<br />

closest integer round( 9.25 ) is 9<br />

sin( x ) trigonometric sine <strong>of</strong><br />

x (x in radians)<br />

sin( 0.0 ) is 0.0<br />

sqrt( x ) square root <strong>of</strong> x sqrt( 900.0 ) is 30.0<br />

sqrt( 9.0 ) is 3.0<br />

tan( x ) trigonometric tangent tan( 0.0 ) is 0.0<br />

<strong>of</strong> x<br />

(x in radians)<br />

Fig. 12.1 Math object methods.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 281


String Object<br />

• JavaScript’s string and character-processing<br />

capabilities<br />

• Appropriate for processing names, addresses,<br />

credit card information, etc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 282


Fundamentals <strong>of</strong> Characters and Strings<br />

• Characters<br />

– Fundamental building blocks <strong>of</strong> JavaScript programs<br />

• String<br />

– Series <strong>of</strong> characters treated as a single unit<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 283


Methods <strong>of</strong> the String Object<br />

Method Description<br />

charAt( index ) Returns a string containing the character at the specified index. If there is no<br />

character at the index, charAt returns an empty string. The first character is<br />

located at index 0.<br />

charCodeAt( index ) Returns the Unicode value <strong>of</strong> the character at the specified index. If there is<br />

no character at the index, charCodeAt returns NaN (Not a Number).<br />

concat( string ) Concatenates its argument to the end <strong>of</strong> the string that invokes the method.<br />

The string invoking this method is not modified; instead a new String is<br />

returned. This method is the same as adding two strings with the string<br />

concatenation operator + (e.g., s1.concat( s2 ) is the same as s1 +<br />

s2).<br />

fromCharCode(<br />

Converts a list <strong>of</strong> Unicode values into a string containing the corresponding<br />

value1, value2, )<br />

indexOf(<br />

substring, index )<br />

lastIndexOf(<br />

substring, index )<br />

characters.<br />

String object methods.<br />

Searches for the first occurrence <strong>of</strong> substring starting from position index in<br />

the string that invokes the method. The method returns the starting index <strong>of</strong><br />

substring in the source string or –1 if substring is not found. If the index<br />

argument is not provided, the method begins searching from index 0 in the<br />

source string.<br />

Searches for the last occurrence <strong>of</strong> substring starting from position index and<br />

searching toward the beginning <strong>of</strong> the string that invokes the method. The<br />

method returns the starting index <strong>of</strong> substring in the source string or –1 if<br />

substring is not found. If the index argument is not provided, the method<br />

begins searching from the end <strong>of</strong> the source string.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 284


Methods <strong>of</strong> the String Object<br />

slice( start, end ) Returns a string containing the portion <strong>of</strong> the string from index start<br />

through index end. If the end index is not specified, the method returns a<br />

string from the start index to the end <strong>of</strong> the source string. A negative end<br />

index specifies an <strong>of</strong>fset from the end <strong>of</strong> the string starting from a<br />

position one past the end <strong>of</strong> the last character (so –1 indicates the last<br />

character position in the string).<br />

split( string ) Splits the source string into an array <strong>of</strong> strings (tokens) where its string<br />

argument specifies the delimiter (i.e., the characters that indicate the end<br />

substr(<br />

start, length )<br />

<strong>of</strong> each token in the source string).<br />

Returns a string containing length characters starting from index start in<br />

the source string. If length is not specified, a string containing characters<br />

from start to the end <strong>of</strong> the source string is returned.<br />

substring(<br />

start, end )<br />

Returns a string containing the characters from index start up to but not<br />

including index end in the source string.<br />

toLowerCase() Returns a string in which all uppercase letters are converted to lowercase<br />

letters. Non-letter characters are not changed.<br />

toUpperCase() Returns a string in which all lowercase letters are converted to uppercase<br />

letters. Non-letter characters are not changed.<br />

toString() Returns the same string as the source string.<br />

valueOf() Returns the same string as the source string.<br />

String object methods.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 285


• Split(string) – splits the string with delimiter<br />

var a = “wel come”<br />

var b = a.split(“ “)<br />

Solution: wel & come<br />

• Substring(start, end)<br />

Example: var a = “welcome”<br />

Var b = a.substring(0,2); wel<br />

Var c = a.substring(3,6); come<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 286


• charAt<br />

Character Processing Methods<br />

– Returns the character at specific position<br />

• charCodeAt<br />

– Returns Unicode value <strong>of</strong> the character at specific position<br />

• fromCharCode<br />

– Returns string created from series <strong>of</strong> Unicode values<br />

• toLowerCase<br />

– Returns lowercase version <strong>of</strong> string<br />

• toUpperCase<br />

– Returns uppercase version <strong>of</strong> string<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 287


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Character Processing Methods<br />

11<br />

12 <br />

13


26 document.writeln( "'" + s2 + "' in lowercase is '" +<br />

27 s2.toLowerCase() + "'" );<br />

28 document.writeln( "'" + s2 + "' in uppercase is '"<br />

29 + s2.toUpperCase() + "'" );<br />

30 // --><br />

31 <br />

32<br />

33 <br />

34 <br />

Outline<br />

CharacterProcessing.<br />

html<br />

2 <strong>of</strong> 2<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 289


HTML Markup Methods<br />

• Anchor<br />

– Anchor <br />

• Blink<br />

– blinking text <br />

• Fixed<br />

– monospaced text <br />

• Strike<br />

– strike out text <br />

• Subscript<br />

– subscript <br />

• Superscript<br />

– superscript <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 290


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 XHTML Markup Methods <strong>of</strong> the String Object<br />

11<br />

12 <br />

13


26 document.writeln(<br />

27 "This is text with a " + subText.sub() );<br />

28 document.writeln(<br />

29 "This is text with a " + supText.sup() );<br />

30 document.writeln(<br />

31 "" + linkText.link( "#top" ) );<br />

32 // --><br />

33 <br />

34<br />

35 <br />

36 <br />

Outline<br />

MarkupMethods.html<br />

2 <strong>of</strong> 2<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 292


Date Object<br />

• Provides methods for date and time manipulations<br />

Method Description<br />

Returns a number from 1 to 31 representing the day <strong>of</strong> the month in local time or UTC, respectively.<br />

getDate()<br />

getUTCDate()<br />

getDay()<br />

getUTCDay()<br />

getFullYear()<br />

getUTCFullYear()<br />

getHours()<br />

getUTCHours()<br />

getMilliseconds()<br />

getUTCMilliSeconds()<br />

getMinutes()<br />

getUTCMinutes()<br />

getMonth()<br />

getUTCMonth()<br />

getSeconds()<br />

getUTCSeconds()<br />

getTime()<br />

getTimezoneOffset()<br />

setDate( val )<br />

setUTCDate( val )<br />

Fig. 12.8 Methods <strong>of</strong> the Date object.<br />

Returns a number from 0 (Sunday) to 6 (Saturday) representing the day <strong>of</strong> the week in local time or UTC,<br />

respectively.<br />

Returns the year as a four-digit number in local time or UTC, respectively.<br />

Returns a number from 0 to 23 representing hours since midnight in local time or UTC, respectively.<br />

Returns a number from 0 to 999 representing the number <strong>of</strong> milliseconds in local time or UTC, respectively.<br />

The time is stored in hours, minutes, seconds and milliseconds.<br />

Returns a number from 0 to 59 representing the minutes for the time in local time or UTC, respectively.<br />

Returns a number from 0 (January) to 11 (December) representing the month in local time or UTC,<br />

respectively.<br />

Returns a number from 0 to 59 representing the seconds for the time in local time or UTC, respectively.<br />

Returns the number <strong>of</strong> milliseconds between January 1, 1970 and the time in the Date object.<br />

Returns the difference in minutes between the current time on the local computer and UTC—previously<br />

known as Greenwich Mean Time (GMT).<br />

Sets the day <strong>of</strong> the month (1 to 31) in local time or UTC, respectively.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 293


Method Description<br />

setFullYear( y, m, d )<br />

setUTCFullYear( y, m, d )<br />

setHours( h, m, s, ms )<br />

setUTCHours( h, m, s, ms )<br />

setMilliSeconds( ms )<br />

setUTCMilliseconds( ms )<br />

setMinutes( m, s, ms )<br />

setUTCMinutes( m, s, ms )<br />

setMonth( m, d )<br />

setUTCMonth( m, d )<br />

setSeconds( s, ms )<br />

setUTCSeconds( s, ms )<br />

Fig. 12.8 Methods <strong>of</strong> the Date object.<br />

Date Object<br />

Sets the year in local time or UTC, respectively. The second and third<br />

arguments representing the month and the date are optional. If an optional<br />

argument is not specified, the current value in the Date object is used.<br />

Sets the hour in local time or UTC, respectively. The second, third and fourth<br />

arguments representing the minutes, seconds and milliseconds are optional. If<br />

an optional argument is not specified, the current value in the Date object is<br />

used.<br />

Sets the number <strong>of</strong> milliseconds in local time or UTC, respectively.<br />

Sets the minute in local time or UTC, respectively. The second and third<br />

arguments representing the seconds and milliseconds are optional. If an<br />

optional argument is not specified, the current value in the Date object is<br />

used.<br />

Sets the month in local time or UTC, respectively. The second argument<br />

representing the date is optional. If the optional argument is not specified, the<br />

current date value in the Date object is used.<br />

Sets the second in local time or UTC, respectively. The second argument<br />

representing the milliseconds is optional. If this argument is not specified, the<br />

current millisecond value in the Date object is used.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 294


Date Object<br />

Method Desc ription<br />

setTime( ms )<br />

Sets the time based on its argument—the number <strong>of</strong> elapsed milliseconds<br />

since January 1, 1970.<br />

toLocaleString()<br />

toUTCString()<br />

toString()<br />

valueOf()<br />

Fig. 12.8 Methods <strong>of</strong> the Date object.<br />

Returns a string representation <strong>of</strong> the date and time in a form specific to the<br />

computer’s locale. For example, September 13, 2001 at 3:42:22 PM is<br />

represented as 09/13/01 15:47:22 in the United States and 13/09/01<br />

15:47:22 in Europe.<br />

Returns a string representation <strong>of</strong> the date and time in the form: 19 Sep<br />

2001 15:47:22 UTC<br />

Returns a string representation <strong>of</strong> the date and time in a form specific to the<br />

locale <strong>of</strong> the computer (Mon Sep 19 15:47:22 EDT 2001 in the United<br />

States).<br />

The time in number <strong>of</strong> milliseconds since midnight, January 1, 1970.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 295


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Date and Time Methods<br />

11<br />

12 <br />

13


25 document.writeln( "getDate: " + current.getDate() +<br />

26 "getDay: " + current.getDay() +<br />

27 "getMonth: " + current.getMonth() +<br />

28 "getFullYear: " + current.getFullYear() +<br />

29 "getTime: " + current.getTime() +<br />

30 "getHours: " + current.getHours() +<br />

31 "getMinutes: " + current.getMinutes() +<br />

32 "getSeconds: " + current.getSeconds() +<br />

33 "getMilliseconds: " +<br />

34 current.getMilliseconds() +<br />

35 "getTimezoneOffset: " +<br />

36 current.getTimezoneOffset() );<br />

37<br />

38 document.writeln(<br />

39 "Specifying arguments for a new Date" );<br />

40 var anotherDate = new Date( 2001, 2, 18, 1, 5, 0, 0 );<br />

41 document.writeln( "Date: " + anotherDate );<br />

42<br />

43 document.writeln(<br />

44 "Set methods for local time zone" );<br />

45 anotherDate.setDate( 31 );<br />

46 anotherDate.setMonth( 11 );<br />

47 anotherDate.setFullYear( 2001 );<br />

48 anotherDate.setHours( 23 );<br />

49 anotherDate.setMinutes( 59 );<br />

Outline<br />

DateTime.html<br />

2 <strong>of</strong> 3<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 297


50 anotherDate.setSeconds( 59 );<br />

51 document.writeln( "Modified date: " + anotherDate );<br />

52 // --><br />

53 <br />

54<br />

55 <br />

56 <br />

Outline<br />

DateTime.html<br />

3 <strong>of</strong> 3<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 298


document Object<br />

• Manipulate document that is currently visible in<br />

the browser window<br />

Method or Property Description<br />

write( string ) Writes the string to the XHTML document as<br />

XHTML code.<br />

writeln( string ) Writes the string to the XHTML document as<br />

XHTML code and adds a newline character at<br />

the end.<br />

document.cookie This property is a string containing the values<br />

<strong>of</strong> all the cookies stored on the user’s computer<br />

for the current document. See Section 12.9,<br />

Using Cookies.<br />

document.lastModified This property is the date and time that this<br />

document was last modified.<br />

Fig. 12.12 Important document object methods and properties.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 299


window Object<br />

• Provides methods for manipulating browser window<br />

Method or Property Description<br />

open( url, name, options ) Creates a new window with the URL <strong>of</strong> the window set to<br />

url, the name set to name, and the visible features set by<br />

the string passed in as option.<br />

prompt( prompt, default ) Displays a dialog box asking the user for input. The text<br />

<strong>of</strong> the dialog is prompt, and the default value is set to<br />

default.<br />

close() Closes the current window and deletes its object from<br />

memory.<br />

window.focus() This method gives focus to the window (i.e., puts the<br />

window in the foreground, on top <strong>of</strong> any other open<br />

browser windows).<br />

window.document This property contains the document object representing<br />

the document currently inside the window.<br />

window.closed This property contains a boolean value that is set to true if<br />

the window is closed, and false if it is not.<br />

window.opener This property contains the window object <strong>of</strong> the window<br />

that opened the current window, if such a window exists.<br />

Fig. 12.14 Important window object methods and properties.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 300


IT1451 – WEB TECHNOLOGY<br />

8 TH SEMESTER – INFORMATION TECHNOLOGY<br />

UNIT – 2 NOTES<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 301


Dynamic HTML: Object Model and<br />

Outline<br />

Collections<br />

Introduction<br />

Object Referencing<br />

Collections all and children<br />

Dynamic Styles<br />

Dynamic Positioning<br />

Using the frames Collection<br />

navigator Object<br />

Summary <strong>of</strong> the DHTML Object Model<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 302


Introduction<br />

• Dynamic HTML Object Model<br />

– Allows Web authors to control the presentation <strong>of</strong> their pages<br />

– Gives them access to all the elements on their pages<br />

• Web page<br />

– Elements, forms, frames, tables<br />

– Represented in an object hierarchy<br />

• Scripting<br />

– Retrieve and modify properties and attributes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 303


Object Referencing<br />

• The simplest way to reference an element is by using the<br />

element’s id attribute.<br />

• The element is represented as an object<br />

– HTML attributes become properties that can be manipulated by<br />

scripting<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 304


Object Referencing<br />

<br />

<br />

Object Model<br />

<br />

function start()<br />

{<br />

alert(ptext.innerText);<br />

ptext.innerText = "Thanks for coming";<br />

} <br />

<br />

<br />

Welcome to our web page<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 305


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 306


• Collections<br />

Collections all and children<br />

– Arrays <strong>of</strong> related objects on a page<br />

– all<br />

• all the HTML elements in a document<br />

– children<br />

• Specific element contains that element’s child elements<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 307


Dynamic Styles<br />

• Element’s style can be changed dynamically<br />

• Dynamic HTML Object Model also allows you to change<br />

the class attribute<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 308


dynamicstyle.html<br />

<br />

Object Model <br />

function start()<br />

{ var a = prompt( "Enter a color name for the " +<br />

"background <strong>of</strong> this page", "" );<br />

document.body.style.backgroundColor = a;<br />

}<br />

<br />

Welcome to our<br />

Web site! <br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 309


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 310


dynamicstyle2<br />

Object Model<br />

<br />

.bigText { font-size: 3em; font-weight: bold }<br />

.smallText { font-size: .75em }<br />

<br />

<br />

function start()<br />

{ var a = prompt( "Enter a className for the text " +<br />

"(bigText or smallText)", "" );<br />

pText.className = a; }<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 311


dynamicstyle2<br />

<br />

Welcome to our Web site! <br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 312


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 313


Dynamic Positioning<br />

• HTML elements can be positioned with scripting<br />

– Declare an element’s CSS position property to be either absolute<br />

or relative<br />

– Move the element by manipulating any <strong>of</strong> the top, left, right or<br />

bottom CSS properties<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 314


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 Dynamic Positioning<br />

11<br />

12 <br />

13


26 function run()<br />

27 {<br />

28 count += speed;<br />

29<br />

30 if ( ( count % 200 ) == 0 ) {<br />

31 speed *= -1;<br />

32 direction = !direction;<br />

33<br />

34 pText.style.color =<br />

35 ( speed < 0 ) ? "red" : "blue" ;<br />

36 firstLine =<br />

37 ( speed < 0 ) ? "Text shrinking" : "Text growing";<br />

38 pText.style.fontFamily =<br />

39 fontStyle[ ++fontStylecount % 3 ];<br />

40 }<br />

41<br />

42 pText.style.fontSize = count / 3;<br />

43 pText.style.left = count;<br />

44 pText.innerHTML = firstLine + " Font size: " +<br />

45 count + "px";<br />

46 }<br />

47 // --><br />

48 <br />

49 <br />

50<br />

Outline<br />

dynamicposition<br />

.html<br />

(2 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 316


51 <br />

52 <br />

54 Welcome!<br />

55 <br />

56 <br />

Outline<br />

dynamicposition<br />

.html<br />

(3 <strong>of</strong> 3)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 317


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 318


Using the frames Collection<br />

• Referencing elements and objects in different frames by<br />

using the frames collection<br />

<br />

<br />

Frames collection <br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 319


The frames collection<br />

function start()<br />

{ var text = prompt( "What is your name?", "" );<br />

parent.frames( "lower" ).document.write( "Hello, " +<br />

text + "" ); }<br />

<br />

Cross-frame<br />

scripting! <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 320


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 321


navigator Object<br />

• Netscape, Mozilla, Micros<strong>of</strong>t’s Internet Explorer<br />

– Others as well<br />

• Contains information about the Web browser<br />

• Allows Web authors to determine what browser the user is<br />

using<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 322


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 The navigator Object<br />

11<br />

12 <br />

13


26 // --><br />

27 <br />

28 <br />

29<br />

30 <br />

31 Redirecting your browser to the appropriate page,<br />

32 please wait...<br />

33 <br />

34 <br />

Outline<br />

navigator.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 324


Summary <strong>of</strong> the DHTML Object Model<br />

Key<br />

window<br />

object<br />

collection<br />

document<br />

frames<br />

history<br />

navigator<br />

location<br />

event<br />

screen<br />

document<br />

document<br />

plugins<br />

Fig. 13.10 DHTML Object Model.<br />

all<br />

anchors<br />

applets<br />

body<br />

embeds<br />

filters<br />

forms<br />

images<br />

links<br />

plugins<br />

scripts<br />

styleSheets<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 325


Summary <strong>of</strong> the DHTML Object Model<br />

Object or collection Description<br />

Objects<br />

window Represents the browser window and provides access to the document object contained<br />

in the window. If the window contains frames a separate window object is created<br />

automatically for each frame, to provide access to the document rendered in the frame.<br />

Frames are considered to be subwindows in the browser.<br />

document Represents the XHTML document rendered in a window. The document object<br />

provides access to every element in the XHTML document and allows dynamic<br />

modification <strong>of</strong> the XHTML document.<br />

body Provides access to the body element <strong>of</strong> an XHTML document.<br />

history Keeps track <strong>of</strong> the sites visited by the browser user. The object provides a script<br />

programmer with the ability to move forward and backward through the visited sites, but<br />

for security reasons does not allow the actual site URLs to be manipulated.<br />

navigator Contains information about the Web browser, such as the name <strong>of</strong> the browser, the<br />

version <strong>of</strong> the browser, the operating system on which the browser is running and other<br />

information that can help a script writer customize the user’s browsing experience.<br />

location Contains the URL <strong>of</strong> the rendered document. When this object is set to a new URL, the<br />

browser immediately switches (navigates) to the new location.<br />

event Can be used in an event handler to obtain information about the event that occurred (e.g.,<br />

the mouse x-y coordinates during a mouse event).<br />

screen Contains information about the computer screen for the computer on which the browser<br />

is running. Information such as the width and height <strong>of</strong> the screen in pixels can be used to<br />

determine the size at which elements should be rendered in a Web page.<br />

Fig. 13.11 Objects in the Internet Explorer 6 Object Model.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 326


Summary <strong>of</strong> the DHTML Object Model<br />

Object or collection Description<br />

Collections<br />

all Many objects have an all collection that provides access to every element contained in<br />

the object. For example, the body object’s all collection provides access to every<br />

element in the body element <strong>of</strong> an XHTML document.<br />

anchors Collection contains all the anchor elements (a) that have a name or id attribute. The<br />

elements appear in the collection in the order they were defined in the XHTML<br />

document.<br />

applets Contains all the applet elements in the XHTML document. Currently, the most<br />

common applet elements are Java applets.<br />

embeds Contains all the embed elements in the XHTML document.<br />

forms Contains all the form elements in the XHTML document. The elements appear in the<br />

collection in the order they were defined in the XHTML document.<br />

frames Contains window objects that represent each frame in the browser window. Each frame<br />

is treated as its own subwindow.<br />

images Contains all the img elements in the XHTML document. The elements appear in the<br />

collection in the order they were defined in the XHTML document.<br />

links Contains all the anchor elements (a) with an href property. This collection also<br />

contains all the area elements that represent links in an image map.<br />

Fig. 13.11 Objects in the Internet Explorer 6 Object Model.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 327


Summary <strong>of</strong> the DHTML Object Model<br />

Object or collection Description<br />

plugins Like the embeds collection, this collection contains all the embed elements in the<br />

XHTML document.<br />

scripts Contains all the script elements in the XHTML document.<br />

styleSheets Contains styleSheet objects that represent each style element in the XHTML<br />

document and each style sheet included in the XHTML document via link.<br />

Fig. 13.11 Objects in the Internet Explorer 6 Object Model.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 328


Dynamic HTML: Event Model<br />

Outline<br />

Introduction<br />

Event onclick<br />

Event onload<br />

Error Handling with onerror<br />

Tracking the Mouse with Event onmousemove<br />

Rollovers with onmouseover and onmouseout<br />

Form Processing with onfocus and onblur<br />

More Form Processing with onsubmit and onreset<br />

Event Bubbling<br />

More DHTML Events<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 329


• Event model<br />

– Scripts can respond to user<br />

Introduction<br />

– Content becomes more dynamic<br />

– Interfaces become more intuitive<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 330


• onClick<br />

Event onclick<br />

– Invoked when user clicks the mouse on a specific item<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 331


Event onclick<br />

<br />

<br />

DHTML Event Model - ONCLICK<br />

<br />

<br />

<br />

alert( "Hi there" );<br />

<br />

<br />

<br />

<br />

Click on this text!<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 332


Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 333


Event onload<br />

• onload event<br />

– Fires when an element finishes loading<br />

– Used in the body element<br />

– Initiates a script after the page loads into the client<br />

<br />

<br />

<br />

function mymessage()<br />

{<br />

alert("This message was triggered from the onload event");<br />

}<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 334


• onerror event<br />

Error Handling with onerror<br />

– Execute specialized error-handling code<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 335


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 DHTML Event Model - onerror<br />

11 <br />

12


26 // Writes to the status bar at the<br />

27 // bottom <strong>of</strong> the window.<br />

28 window.status = "Error: " + errType + " on line " +<br />

29 errLineNum;<br />

30<br />

31 // Returning a value <strong>of</strong> true cancels the<br />

32 // browser’s reaction.<br />

33 return true;<br />

34 }<br />

35 // --><br />

36 <br />

37 <br />

38<br />

39 <br />

40<br />

41 <br />

43<br />

44 <br />

45 <br />

Outline<br />

onerror.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 337


• onmousemove<br />

Tracking the Mouse with Event<br />

onmousemove<br />

– Fires repeatedly when the user moves the mouse over the<br />

Web page<br />

– Gives position <strong>of</strong> the mouse<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 338


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 DHTML Event Model - onmousemove event<br />

11 <br />

12 <br />

19 <br />

20 <br />

21<br />

22 <br />

24<br />

Outline<br />

onmousemove.html<br />

(1 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 339


25 (0, 0)<br />

26 <br />

28<br />

29 <br />

30 <br />

Outline<br />

onmousemove.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 340


Rollovers with onmouseover and<br />

onmouseout<br />

• Events fired by mouse movements<br />

– onmouseover<br />

• Mouse cursor moves over element<br />

– Onmouseout<br />

• Mouse cursor leaves element<br />

– Onmousedown<br />

• Fires when a mouse button is pressed down<br />

– Onmouseup<br />

• Fires when a mouse button is released<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 341


Rollovers with onmouseover and<br />

onmouseout<br />

<br />

<br />

<br />

Mouse over this text<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 342


Onmousedown, Onmouseup<br />

<br />

<br />

function lighton()<br />

{<br />

document.getElementById('myimage').src="bulbon.gif";<br />

}<br />

function light<strong>of</strong>f()<br />

{<br />

document.getElementById('myimage').src="bulb<strong>of</strong>f.gif";<br />

}<br />

<br />

<br />

<br />

<br />

Click to turn on the light<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 343


onmousemove<br />

<br />

<br />

<br />

var i=1;<br />

function moveright()<br />

{<br />

document.getElementById('header').style.position="relative";<br />

document.getElementById('header').style.left=i;<br />

i++;<br />

}<br />

<br />

<br />

<br />

Move the mouse over this page <br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 344


Form Processing with onfocus and<br />

onblur<br />

• onfocus event fires when element gains focus<br />

• onblur event fires when element loses focus<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 345


Form Processing with onfocus<br />

<br />

<br />

<br />

function message()<br />

{<br />

alert("This alert box was triggered by the onfocus event handler");<br />

}<br />

<br />

<br />

<br />

<br />

Enter your name: <br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 346


Form Processing with onblur<br />

<br />

<br />

<br />

function message()<br />

{<br />

alert("This alert box was triggered by the onblur event handler");<br />

}<br />

<br />

<br />

<br />

The onblur event occurs when an element loses focus. Try to click or write in<br />

the input field below, then click elsewhere in the document so the input field<br />

loses focus.<br />

<br />

Enter your name: <br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 347


More Form Processing with onsubmit and<br />

onreset<br />

• onsubmit and onreset are useful events for processing forms<br />

<br />

<br />

<br />

function confirmInput()<br />

{<br />

fname=document.forms[0].fname.value;<br />

alert("Hello " + fname + "! You will now be redirected to www.rajalakshmi.org ");<br />

}<br />

<br />

<br />

<br />

<br />

Enter your name: <br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 348


Form Processing with onreset<br />

<br />

<br />

<br />

function message()<br />

{<br />

alert("This alert box was triggered by the onreset event handler");<br />

}<br />

<br />

<br />

<br />

<br />

Enter your name: <br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 349


Event Bubbling<br />

• Crucial part <strong>of</strong> the event model<br />

• Process whereby events fired in child elements<br />

“bubble” up to their parent elements<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 350


1 <br />

2 <br />

4<br />

5 <br />

6 <br />

7<br />

8 <br />

9 <br />

10 DHTML Event Model - Event Bubbling<br />

11<br />

12 <br />

13


26<br />

27 document.onclick = documentClick;<br />

28 // --><br />

29 <br />

30 <br />

31<br />

32 <br />

33<br />

34 Click here!<br />

35 Click here, too!<br />

36 <br />

37 <br />

Outline<br />

bubbling.html<br />

(2 <strong>of</strong> 2)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 352


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 353


More DHTML Events<br />

• Remaining DHTML events and their descriptions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 354


More DHTML Events<br />

Event Description<br />

Clipboard events<br />

onbeforecut Fires before a selection is cut to the clipboard.<br />

onbeforecopy Fires before a selection is copied to the clipboard.<br />

onbeforepaste Fires before a selection is pasted from the clipboard.<br />

oncopy Fires when a selection is copied to the clipboard.<br />

oncut Fires when a selection is cut to the clipboard.<br />

onabort Fires if image transfer has been interrupted by user.<br />

onpaste Fires when a selection is pasted from the clipboard.<br />

Data binding events<br />

onafterupdate Fires immediately after a databound object has been updated.<br />

onbeforeupdate Fires before a data source is updated.<br />

oncellchange Fires when a data source has changed.<br />

ondataavailable Fires when new data from a data source become available.<br />

ondatasetchanged Fires when content at a data source has changed.<br />

ondatasetcomplete Fires when transfer <strong>of</strong> data from the data source has<br />

completed.<br />

onerrorupdate Fires if an error occurs while updating a data field.<br />

onrowenter Fires when a new row <strong>of</strong> data from the data source is<br />

available.<br />

onrowexit Fires when a row <strong>of</strong> data from the data source has just<br />

finished.<br />

onrowsdelete Fires when a row <strong>of</strong> data from the data source is deleted.<br />

onrowsinserted Fires when a row <strong>of</strong> data from the data source is inserted.<br />

Fig. 14.10 Dynamic HTML events.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 355


More DHTML Events<br />

Event<br />

Keyboard events<br />

Description<br />

onhelp Fires when the user initiates help (i.e., by pressing the F1 key).<br />

onkeydown Fires when the user pushes down a key.<br />

onkeypress Fires when the user presses a key.<br />

onkeyup<br />

Marquee events<br />

Fires when the user ends a key press.<br />

onbounce Fires when a scrolling marquee bounces back in the other<br />

direction.<br />

onfinish Fires when a marquee finishes its scrolling.<br />

onstart<br />

Mouse events<br />

Fires when a marquee begins a new loop.<br />

oncontextmenu Fires when the context menu is shown (right-click).<br />

ondblclick Fires when the mouse is double clicked.<br />

ondrag Fires during a mouse drag.<br />

ondragend Fires when a mouse drag ends.<br />

ondragenter Fires when something is dragged onto an area.<br />

ondragleave Fires when something is dragged out <strong>of</strong> an area.<br />

ondragover Fires when a drag is held over an area.<br />

ondragstart Fires when a mouse drag begins.<br />

ondrop Fires when a mouse button is released over a valid target<br />

during a drag.<br />

onmousedown Fires when a mouse button is pressed down.<br />

Fig. 14.10 Dynamic HTML events.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 356


More DHTML Events<br />

Event Description<br />

onmouseup Fires when a mouse button is released.<br />

Miscellaneous events<br />

onafterprint Fires immediately after the document prints.<br />

onbeforeeditfocus Fires before an element gains focus for editing.<br />

onbeforeprint Fires before a document is printed.<br />

onbeforeunload Fires before a document is unloaded (i.e., the window was closed or a link was<br />

clicked).<br />

onchange Fires when a new choice is made in a select element, or when a text input is<br />

changed and the element loses focus.<br />

onfilterchange Fires when a filter changes properties or finishes a transition (see Chapter 15,<br />

Dynamic HTML: Filters and Transitions).<br />

onlosecapture Fires when the releaseCapture method is invoked.<br />

onpropertychange Fires when the property <strong>of</strong> an object is changed.<br />

onreadystatechange Fires when the readyState property <strong>of</strong> an element<br />

changes.<br />

onreset Fires when a form resets (i.e., the user clicks a reset button).<br />

onresize Fires when the size <strong>of</strong> an object changes (i.e., the user resizes a window or frame).<br />

onscroll Fires when a window or frame is scrolled.<br />

onselect Fires when a text selection begins (applies to input or<br />

textarea).<br />

onselectstart Fires when the object is selected.<br />

onstop Fires when the user stops loading the object.<br />

onunload Fires when a page is about to unload.<br />

Fig. 14.10 Dynamic HTML events.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 357


Dynamic HTML: Filters and Transitions<br />

Outline<br />

Introduction<br />

Flip Filters: flipv and fliph<br />

Transparency with the chroma Filter<br />

Creating Image masks<br />

Miscellaneous Image Filters: invert, gray and xray<br />

Adding shadows to Text<br />

Creating Gradients with alpha<br />

Making Text glow<br />

Creating Motion with blur<br />

Using the wave Filter<br />

Advanced Filters: dropShadow and light<br />

blendTrans Transition<br />

revealTrans Transition<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 358


• Filters<br />

Introduction<br />

– Cause changes that are persistent<br />

• Transitions<br />

– Temporary<br />

– Allow transfer from one page to another with pleasant visual<br />

effect<br />

• For example, random dissolve<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 359


Flip Filters: flipv and fliph<br />

• flipv and fliph filters mirror text or images<br />

vertically and horizontally, respectively<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 360


<br />

Example<br />

The flip filter<br />

<br />

<br />

<br />

<br />

Text<br />

Text<br />

<br />

<br />

<br />

Text<br />

Text<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 361


fliph filter applied<br />

Both fliph and flipv<br />

filters applied<br />

No filters applied<br />

flipv filter applied<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 362


Transparency with the chroma Filter<br />

• chroma filter applies transparency effects<br />

dynamically<br />

– Without using a graphics editor to hard-code transparency<br />

into the image<br />

• onchange<br />

– Fires when the value <strong>of</strong> a form changes<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 363


Creating Image masks<br />

• Background is a solid color<br />

• Foreground is transparent to the image or color<br />

behind it<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 364


<br />

<br />

Mask Filter<br />

<br />

<br />

Mask Filter<br />

Example<br />

<br />

<br />

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 365


Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 366


Image Filters: invert, gray and xray<br />

• invert filter<br />

– Negative image effect<br />

• Dark areas become light and light areas become dark<br />

• gray filter<br />

– Grayscale image effect<br />

• All color is stripped from the image, only brightness data<br />

remains<br />

• xray filter<br />

– X-ray effect<br />

• Inversion <strong>of</strong> the grayscale effect<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 367


Example<br />

<br />

<br />

Misc. Image filters<br />

<br />

<br />

<br />

Normal<br />

Grayscale<br />

Xray<br />

Invert<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 368


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 369


• shadow filter<br />

– Showing effect<br />

• Three-dimensional appearance<br />

<br />

<br />

<br />

h2<br />

{<br />

width:100%;<br />

}<br />

<br />

<br />

<br />

Adding shadows to Text<br />

Glow<br />

Blur<br />

Flip H<br />

Flip V<br />

Shadow<br />

Drop Shadow<br />

Wave<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 370


lendTrans Transition<br />

• Example <strong>of</strong> the blendTrans transition<br />

– Creates a smooth fade-in/fade-out effect<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 371


lendTrans Transition<br />

• The blendTrans() filter fades an element out or<br />

in. It can be applied only through scripting.<br />

• Three steps are required.<br />

1. First, a duration value is established giving the<br />

number <strong>of</strong> seconds for the transition to take<br />

place. The general format is shown below.<br />

• object.style.filter="blendTrans(duration=secon<br />

ds)"<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 372


lendTrans Transition<br />

2. the filter's apply() method is run, followed by the<br />

setting <strong>of</strong> the element's visibility ("visible" when<br />

fading in and "hidden" when fading out).<br />

• object.filters.blendTrans.apply()<br />

object.style.visibility="visible|hidden"<br />

3. Finally, the filter's play() method is run to<br />

perform the blend.<br />

• object.filters.blendTrans.play()<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 373


<br />

<br />

function Blend()<br />

{<br />

if (document.getElementById("Macaws").style.visibility == "visible") {<br />

document.getElementById("Macaws").style.filter="blendTrans(duration=3)";<br />

document.getElementById("Macaws").filters.blendTrans.apply();<br />

document.getElementById("Macaws").style.visibility="hidden";<br />

document.getElementById("Macaws").filters.blendTrans.play();<br />

document.getElementById("Lion").style.filter="blendTrans(duration=3)";<br />

document.getElementById("Lion").filters.blendTrans.apply();<br />

document.getElementById("Lion").style.visibility="visible";<br />

document.getElementById("Lion").filters.blendTrans.play();<br />

} else {<br />

document.getElementById("Lion").style.filter="blendTrans(duration=3)";<br />

document.getElementById("Lion").filters.blendTrans.apply();<br />

document.getElementById("Lion").style.visibility="hidden";<br />

document.getElementById("Lion").filters.blendTrans.play();<br />

document.getElementById("Macaws").style.filter="blendTrans(duration=3)";<br />

document.getElementById("Macaws").filters.blendTrans.apply();<br />

document.getElementById("Macaws").style.visibility="visible";<br />

document.getElementById("Macaws").filters.blendTrans.play();<br />

}<br />

}<br />

Outline<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 374


• revealTrans filter<br />

revealTrans Transition<br />

– Create pr<strong>of</strong>essional-style transitions<br />

– From box out to random dissolve<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 375


Filters: revealTrans()<br />

• First, duration and transition values are<br />

established giving the number <strong>of</strong> seconds for the<br />

transition to take place and the type <strong>of</strong> transition to<br />

apply.<br />

• object.style.filter="revealTrans(duration=second<br />

s, transition=n)"<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 376


• 0 - Box in<br />

1 - Box out<br />

2 - Circle in<br />

3 - Circle out<br />

4 - Wipe up<br />

5 - Wipe down<br />

6 - Wipe right<br />

7 - Wipe left<br />

8 - Vertical blinds<br />

9 - Horizontal blinds<br />

10 - Checkerboard across<br />

11 - Checkerboard down<br />

12 - Random dissolve<br />

13 - Split vertical in<br />

14 - Split vertical out<br />

15 - Split horizontal in<br />

16 - Split horizontal out<br />

17 - Strips left down<br />

18 - Strips left up<br />

19 - Strips right down<br />

20 - Strips right up<br />

21 - Random bars horizontal<br />

22 - Random bars vertical<br />

23 - Any Random<br />

Transition types<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 377


Filters: revealTrans()<br />

• Second, the filter's apply() method is run,<br />

followed by the setting <strong>of</strong> the element's visibility<br />

("visible" when fading in and "hidden" when<br />

fading out).<br />

• object.filters.revealTrans.apply()<br />

object.style.visibility="visible|hidden"<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 378


Filters: revealTrans()<br />

• Finally, the filter's play() method is run to perform<br />

the blend.<br />

• object.filters.revealTrans.play()<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 379


Filters: revealTrans()<br />

function Blend()<br />

{ if (document.getElementById("Macaws").style.visibility == "visible") {<br />

document.getElementById("Macaws").style.filter="revealTrans(duration=2,<br />

transition=8)";<br />

document.getElementById("Macaws").filters.revealTrans.apply();<br />

document.getElementById("Macaws").style.visibility="hidden";<br />

document.getElementById("Macaws").filters.revealTrans.play();<br />

document.getElementById("Lion").style.filter="revealTrans(duration=2, transition=8)";<br />

document.getElementById("Lion").filters.revealTrans.apply();<br />

document.getElementById("Lion").style.visibility="visible";<br />

document.getElementById("Lion").filters.revealTrans.play(); } else {<br />

document.getElementById("Lion").style.filter="revealTrans(duration=2, transition=8)";<br />

document.getElementById("Lion").filters.revealTrans.apply();<br />

document.getElementById("Lion").style.visibility="hidden";<br />

document.getElementById("Lion").filters.revealTrans.play();<br />

document.getElementById("Macaws").style.filter="revealTrans(duration=2,<br />

transition=8)";<br />

document.getElementById("Macaws").filters.revealTrans.apply();<br />

document.getElementById("Macaws").style.visibility="visible";<br />

document.getElementById("Macaws").filters.revealTrans.play(); } }<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 380


Dynamic HTML: Data Binding with<br />

Outline<br />

Tabular Data Control<br />

Introduction<br />

Simple Data Binding<br />

Moving within a Recordset<br />

Binding to an img<br />

Binding to a table<br />

Sorting table Data<br />

Data Binding Elements<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 381


• Data binding<br />

Introduction<br />

– Data no longer reside exclusively on the server<br />

– Data can be maintained on the client<br />

– Eliminate server activity and network delays<br />

• Data Source Objects (DSOs)<br />

– Tabular Data Control (TDC)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 382


• Data file<br />

– Header row<br />

Simple Data Binding<br />

• Specifies the names <strong>of</strong> the columns below<br />

– Text qualifiers ( @ )<br />

• Enclose data in each field<br />

– Field delimiter ( | )<br />

• Separate each field<br />

– Recordset<br />

– Eg: @ColorName@|@ColorHexRGBValue@<br />

@green@|@#008000@<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 383


Simple data binding and recordset manipulation<br />

<br />

<br />

Intro to Data Binding<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 384


recordSet = Colors.recordset;<br />

function reNumber()<br />

{<br />

if( !recordSet.EOF )<br />

recordNumber.innerText = recordSet.absolutePosition;<br />

else<br />

recordNumber.innerText = " ";<br />

}<br />

function forward()<br />

{<br />

if( !recordSet.EOF )<br />

recordSet.MoveNext();<br />

else<br />

recordSet.MoveFirst();<br />

colorSample.style.backgroundColor = colorRGB.innerText;<br />

reNumber();<br />

}<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 385


HTML Color Table<br />

Click to move forward in the recordset.<br />

Color Name: <br />

<br />

Color RGB Value: <br />

<br />

<br />

Currently viewing record number<br />

<br />

<br />

Color Sample<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 386


75 <br />

76 <br />

Outline<br />

introdatabind.html<br />

(4 <strong>of</strong> 4)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 387


Binding to an img<br />

• Many different types <strong>of</strong> HTML elements can be<br />

bound to data sources<br />

– Binding to an img element<br />

• Changing the recordset updates the src attribute <strong>of</strong> the image<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 388


1 image<br />

2 numbers/0.gif<br />

3 numbers/1.gif<br />

4 numbers/2.gif<br />

5 numbers/3.gif<br />

6 numbers/4.gif<br />

7 numbers/5.gif<br />

8 numbers/6.gif<br />

9 numbers/7.gif<br />

10 numbers/8.gif<br />

11 numbers/9.gif<br />

images.txt<br />

(1 <strong>of</strong> 1)<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 389


Binding data to an image<br />

<br />

Binding to a IMG<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 390


function move( whereTo )<br />

{<br />

switch( whereTo ) {<br />

•<br />

case "first":<br />

recordSet.MoveFirst();<br />

break;<br />

case "previous":<br />

if( recordSet.BOF )<br />

recordSet.MoveLast();<br />

else<br />

recordSet.MovePrevious();<br />

break;<br />

case "next":<br />

}<br />

}<br />

if( recordSet.EOF )<br />

recordSet.MoveFirst();<br />

else<br />

recordSet.MoveNext();<br />

break;<br />

case "last":<br />

recordSet.MoveLast();<br />

break;<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 391


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 392


Binding to a table<br />

• Binding data to a table is perhaps the most<br />

important <strong>of</strong> data binding<br />

– Different from the data binding we’ve seen<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 393


Using Data Binding with tables<br />

<br />

Data Binding and Tables<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 394


Binding Data to a TABLE<br />

<br />

<br />

<br />

Color Name<br />

Color RGB Value<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 395


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 396


Sorting table Data<br />

• Manipulate a large data source<br />

– Need to sort data<br />

• Can be accomplished by the Sort property <strong>of</strong> the TDC<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 397


Sorting TABLE data<br />

<br />

Data Binding and Tables<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 398


Sorting Data<br />

<br />

<br />

Sort by:<br />

<br />

Color Name (Ascending)<br />

Color Name (Descending)<br />

<br />

<br />

<br />

<br />

Color Name<br />

Color RGB Value<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 399


Sorting Data<br />

<br />

<br />

Sort by:<br />

<br />

Color Name<br />

(Ascending)<br />

Color Name<br />

(Descending)<br />

<br />

<br />

<br />

<br />

Color Name<br />

Color RGB Value<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 400


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 401


Data Binding Elements<br />

• Exactly how a data source is displayed by the<br />

browser depends on the HTML element to which<br />

the data is bound<br />

– Different elements may use the data for different purposes.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 402


Data Binding Elements<br />

Element Bindable Property/Attribute<br />

a href<br />

div Contained text<br />

frame href<br />

iframe href<br />

img src<br />

input type = "button" value (button text)<br />

input type = "checkbox" checked (use a boolean value in the data)<br />

input type = "hidden" value<br />

input type = "password" value<br />

input type = "radio" checked (use a boolean value in the data)<br />

input type = "text" value<br />

marquee Contained text<br />

param value<br />

select Selected option<br />

span Contained text<br />

table Cell elements (see Section 16.6)<br />

textarea Contained text (value)<br />

Fig. 16.10 XHTML elements that allow data binding.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 403


IT1451 – WEB TECHNOLOGY<br />

8 TH SEMESTER – INFORMATION TECHNOLOGY<br />

UNIT – 3 NOTES<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 404


Multimedia: Audio, Video, Speech<br />

Outline<br />

Synthesis and Recognition<br />

Introduction<br />

Audio and Video<br />

Adding Background Sounds with the bgsound Element<br />

Adding Video with the img Element’s dynsrc Property<br />

Adding Audio or Video with the embed Element<br />

Using the Windows Media Player ActiveX Control<br />

RealOne Player Plug-in<br />

Synchronized Multimedia Integration Language (SMIL)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 405


• Multimedia<br />

Introduction<br />

– Use <strong>of</strong> sound, images, graphics and video<br />

– Add sound, video, and animated characters to Web-based<br />

applications<br />

– Streaming technologies<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 406


Audio and Video<br />

• Can be embedded in Web page<br />

• Can be downloaded “on-demand”<br />

• Encoding algorithm or codec<br />

– Transforms raw audio or video into a format that Web<br />

browsers can display<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 407


Adding Background Sounds with the<br />

bgsound Element<br />

• bgsound element<br />

– src property<br />

• Specifies the URL <strong>of</strong> audio clip to be played<br />

– loop property<br />

• Specifies number <strong>of</strong> times audio clip will play<br />

– balance property<br />

• Specifies balance between left and right speakers<br />

– volume property<br />

• Determines volume <strong>of</strong> audio clip<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 408


Back ground Audio<br />

<br />

The BGSOUND Element<br />


Background Music via the BGSOUND Element<br />

Jazz Gospel<br />

This sound is from the free sound downloads at the<br />

<br />

Micros<strong>of</strong>t Developer Network downloads site.<br />

<br />

Use the fields below to change the number <strong>of</strong> iterations<br />

and the volume for the audio clip<br />

Press Stop to stop playing the sound.<br />

Press Refresh to begin playing the sound again.<br />

<br />

Loop [-1 = loop forever]<br />

<br />

Volume [-10000 (low) to 0 (high)]<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 410


.<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 411


Adding Video with the img Element’s<br />

dynsrc Property<br />

• img element<br />

– Incorporates both images and videos<br />

– src property<br />

• Indicates source is image<br />

– dynsrc property<br />

• Indicates source is video clip<br />

– start property<br />

• Specifies when video should start playing<br />

• Event fileopen<br />

– Video should play as soon as it loads<br />

• Event mouseover<br />

– Video should play when user position mouse over video<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 412


DynamicIMG<br />

<br />

<br />

An Embedded Image Using the DYNSRC Property<br />

<br />

<br />

<br />

An Embedded Video Using the IMG Element's DYNSRC<br />

Property<br />

Spinning Globe and New Age Music<br />

This video is from the<br />

<br />

NASA Multimedia Gallery<br />

This sound is from the free sound downloads at the<br />

<br />

Micros<strong>of</strong>t Developer Network downloads site.<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 413


<br />

<br />

This page will play the audio clip and video in a<br />

loop.The video will not begin playing until you move<br />

the mouse over the video.Press Stop to stop<br />

playing the sound and the video.<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 414


Adding Audio or Video with the embed<br />

Element<br />

• Element embed<br />

– Embeds media clip into Web page<br />

– Displays a graphical user interface (GUI)<br />

– Supported by both Micros<strong>of</strong>t Internet Explorer and Netscape<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 415


• Element embed<br />

RealOne Player Plug-in<br />

– Embed RealOne Player plug-ins for video and audio<br />

– Attribute type<br />

• Specifies MIME type <strong>of</strong> embedded file<br />

– Attributes width and height<br />

• Specify dimensions <strong>of</strong> space the control occupies<br />

– Attribute autostart<br />

• Determines whether media start playing when page loads<br />

– Attribute controls<br />

• Specifies which controls users can access<br />

– Attribute src<br />

• Set to location <strong>of</strong> streaming media<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 416


Synchronized Multimedia Integration<br />

Language (SMIL)<br />

• Coordinate wide range <strong>of</strong> multimedia elements<br />

• XML-based description language<br />

• Organize text, audio, video to occur<br />

simultaneously or sequentially<br />

• Provide time reference for all instances <strong>of</strong> text and<br />

media<br />

• Specifies source and presentation <strong>of</strong> multimedia<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 417


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 418


e-Business & e-Commerce<br />

Outline<br />

Introduction<br />

e-Business Models<br />

Storefront Model<br />

Shopping-Cart Technology<br />

Auction Model<br />

Portal Model<br />

Name-Your-Price Model<br />

Comparison-Pricing Model<br />

Bartering Model<br />

Building an e-Business<br />

e-Marketing<br />

Branding<br />

Marketing Research<br />

e-Mail Marketing<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 419


e-Business & e-Commerce<br />

Promotions<br />

Consumer Tracking<br />

Electronic Advertising<br />

Search Engines<br />

Affiliate Programs<br />

Public Relations<br />

Customer Relationship Management (CRM)<br />

Online Payments<br />

Credit-Card Payment<br />

Digital Cash and e-Wallets<br />

Micropayments<br />

Smart Cards<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 420


e-Business & e-Commerce<br />

Security<br />

Public-Key Cryptography<br />

Cryptanalysis<br />

Key Agreement Protocols<br />

Key Management<br />

Secure Sockets Layer (SSL)<br />

WTLS<br />

IPSec and Virtual Private Networks (VPN)<br />

Security Attacks<br />

Network Security<br />

Legal Issues<br />

Privacy<br />

Defamation<br />

Sexually Explicit Speech<br />

SPAM<br />

Copyright and Patents<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 421


e-Business & e-Commerce<br />

XML and e-Commerce<br />

Introduction to Wireless Technology and m-Business<br />

m-Business<br />

Identifying User Location<br />

E911 Act<br />

Location-Identification Technologies<br />

Wireless Marketing, Advertising and Promotions<br />

Wireless Payment Options<br />

Privacy and the Wireless Internet<br />

Web Resources<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 422


Introduction<br />

• Successful online businesses<br />

– Recognize need or demand<br />

– Meet that need<br />

– Must constantly readjust to shifting trends and technologies<br />

– Personalization<br />

• Trade<strong>of</strong>f between convenience and privacy concerns<br />

• E-commerce vs. e-business<br />

– E-commerce<br />

• Aspects <strong>of</strong> doing business online that relate to exchanges with<br />

customers, partners and vendors.<br />

• Forexample, suppliers interact with manufacturers, customers interact<br />

with sales representatives<br />

– E-business<br />

• All aspects <strong>of</strong> e-commerce, plus internal operations <strong>of</strong> doing business<br />

e-business not included under the category <strong>of</strong> e-commerce<br />

• For example, production, development, corporate infrastructure and<br />

product management are aspects <strong>of</strong> e-business not included under the<br />

category <strong>of</strong> e-commerce<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 423


• e-business<br />

e-Business Models<br />

– Expansion <strong>of</strong> old technologies and techniques<br />

• Electronic Funds Transfers<br />

– Requires new business models and categories<br />

– Pioneered by early e-businesses<br />

• Amazon.com, eBay, Yahoo among others<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 424


Storefront Model<br />

• Simulates the experience <strong>of</strong> shopping in a store<br />

– Very common<br />

– Provides services similar to real “brick and mortar” store<br />

• Transaction processing<br />

• Security<br />

• On-line Payment<br />

• Information storage<br />

– Products organized into catalogs that users can browse and<br />

search<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 425


Shopping-Cart Technology<br />

• Shopping-cart metaphor<br />

– Holds items a user has selected to buy<br />

– Merchant server contains database <strong>of</strong> available items<br />

– User puts all desired items in the cart<br />

– When finished, user “checks out”<br />

• Prices totaled<br />

• Shipping, tax and other charges applied<br />

• Shipping and payment details gathered<br />

• Order confirmed<br />

• Ex: www.amazon.com<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 426


• Online auctions<br />

Auction Model<br />

– Buyers bid on items made available by various sellers<br />

• No fixed price<br />

– Very attractive to customers<br />

• Often able to get lower prices on goods than traditional stores<br />

– Site is searchable to allow easy location <strong>of</strong> desired items<br />

– Site receives a commission on each sale<br />

– Model also employed in business-to-business transactions<br />

– www.eBay.com is the leading auction site<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 427


Auction Model<br />

eBay home page.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 428


Auction Model<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 429


Portal Model<br />

• Portal sites - <strong>of</strong>fer visitors the chance to find almost<br />

anything they are looking for in one place.<br />

• Combine many services into one page<br />

• News<br />

• Sports<br />

• Weather<br />

• Web searches<br />

– Horizontal portals<br />

• Search engines<br />

• Aggregate information on broad range <strong>of</strong> topics<br />

– Vertical portals<br />

• Information on narrow range <strong>of</strong> topics<br />

– Convenient, centralized access to information<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 430


Name-Your-Price Model<br />

• Empowers customers by allowing them to state the<br />

price they are willing to pay for products and services.<br />

• User submits price they are willing to pay<br />

– Site then passes it along to partner sites, who evaluate the <strong>of</strong>fer<br />

– If accepted, user must pay that price<br />

– If rejected, user may submit a new price<br />

– Many such sites employ intelligent agents<br />

• Search, arrange, analyze large amounts <strong>of</strong> data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 431


Comparison-Pricing Model<br />

• Poll merchants for lowest price on an item<br />

– Often generate revenue through partnerships with other sites<br />

– Convenient way to search multiple merchants<br />

– Not always the true best price<br />

• Non-partner merchants might have better <strong>of</strong>fers, but be<br />

unlisted<br />

– Can employ search-engine technology to automatically seek<br />

out best prices or related products<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 432


Bartering Model<br />

• The <strong>of</strong>fering <strong>of</strong> one item in exchange for another<br />

• Trade items rather than currency<br />

– Similar to auction sites<br />

– Merchant and customer haggle to a fair price<br />

– In practice, transaction is usually a combination <strong>of</strong> currency<br />

and items<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 433


• Multiple approaches<br />

Building an e-Business<br />

– Turnkey solutions (a prepackaged e-business)<br />

• Ready-made e-Business sites [store.yahoo.com]<br />

– e-Business templates<br />

• Outline business’ structure<br />

• Design details left open to owner<br />

– Outsource control entirely to a specialized firm<br />

• Expensive<br />

• Little hassle, lets experts control it while you control your<br />

business<br />

– Build original, custom solution<br />

• Allows maximum control, makes your site unique<br />

• Most expensive, involves “reinventing the wheel”<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 434


• Marketing campaign<br />

e-Marketing<br />

– Marketing your site through multiple means<br />

• Market research<br />

• Advertising<br />

• Promotions<br />

• Branding<br />

• Public Relations (PR)<br />

• Search engines<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 435


• Brand<br />

Branding<br />

– Name, logo or symbol that defines company’s products or<br />

services<br />

• Unique<br />

• Recognizable<br />

• Easy to remember<br />

– Brand equity<br />

• “Value” <strong>of</strong> the brand<br />

• Customer perception and loyalty<br />

– Companies with existing brand may more easily establish<br />

their brand on the Internet<br />

• New companies must work to establish trust in their brand<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 436


• Marketing research<br />

– Marketing mix<br />

Marketing Research<br />

• Product or service details<br />

• Pricing<br />

• Promotion<br />

• Distribution<br />

– Focus groups<br />

– Interviews<br />

– Surveys and questionnaires<br />

– Secondary research<br />

• Reviewing pre-existing data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 437


• e-Mail marketing<br />

e-Mail Marketing<br />

– Part <strong>of</strong> the reach <strong>of</strong> the campaign<br />

• Span <strong>of</strong> people who marketing should target<br />

– Direct mail vs. indirect mail<br />

• Direct mail is personalized to the individual recipient<br />

• Direct is <strong>of</strong>ten more effective<br />

• Offers right product at right time<br />

• Tailor mailing to customer’s interests<br />

– Opt-in e-Mail lists<br />

• Customer chooses to subscribe<br />

• Send newsletters with information on <strong>of</strong>fers and promotions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 438


• e-Mail marketing, cont.<br />

e-Mail Marketing<br />

– Dangers <strong>of</strong> e-Mail marketing<br />

• Do not flood customers with too much e-Mail in too short a<br />

time<br />

• Do not send unsolicited e-Mail<br />

– Spam<br />

– Gives company a poor reputation, illegal in some areas<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 439


• Promotions<br />

– Attract visitors<br />

– Encourage purchasing<br />

– Increase brand loyalty<br />

Promotions<br />

– Should not be only reason people purchase from your<br />

company<br />

• Sign <strong>of</strong> weak product or brand<br />

– Be sure cost <strong>of</strong> promotion is not so great that no pr<strong>of</strong>it is<br />

ever seen<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 440


• Consumer tracking<br />

– Keep user pr<strong>of</strong>iles<br />

– Record visits<br />

Consumer Tracking<br />

– Analyze results <strong>of</strong> advertising and promotion<br />

– Helps define target market<br />

• Group toward whom it is most pr<strong>of</strong>itable to target marketing<br />

resources<br />

– Log files contain many useful details<br />

• IP address<br />

• Time and frequency <strong>of</strong> visits<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 441


Consumer Tracking<br />

• Consumer tracking, cont.<br />

– Cookies<br />

• Text file stored on customer’s computer<br />

• Can contain record <strong>of</strong> user’s actions, preferences, buying<br />

habits<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 442


• Advertising<br />

Electronic Advertising<br />

– Establish and strengthen branding<br />

– Publish URL in all advertising<br />

• Internet advertising becoming important<br />

– Links and banners on sites viewed <strong>of</strong>ten by target market<br />

• Can be interactive or animated<br />

– Allow advertising on your site in return for payment<br />

– Pop-up ads<br />

• Appear in a separate window when page loads<br />

• Often extremely irritating to customers<br />

• Actually decrease interest in advertised product due to negative<br />

association with pop-up ad<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 443


Electronic Advertising<br />

• Search engine advertising<br />

– Pay for better placement <strong>of</strong> your site in search results<br />

– Sites that receive more clicks on their advertisements move<br />

higher in rankings<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 444


• Search engines<br />

Search Engines<br />

– Scan websites for desired content<br />

– Being highly ranked in search results important<br />

• People tend not to browse results too deeply<br />

– Some sites base your ranking on meta tags<br />

• Hidden XHTML tags that contain information about site<br />

• Keywords, title, summary<br />

– Others simply “spider” the site<br />

• Program reads content and decides what is important<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 445


• Search engines, cont.<br />

Search Engines<br />

– Google a leading search engine<br />

• Uses complex formulas to rank pages<br />

• Number <strong>of</strong> sites linked to you, and their ranking<br />

• Number <strong>of</strong> clicks on your site<br />

• Relevance to keyword user is searching for<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 446


• Affiliate programs<br />

Affiliate Programs<br />

– Company pays other sites to be affiliates<br />

• Advertise the company’s products<br />

• When their ad leads to purchases from the company, affiliate<br />

site receives a commission<br />

– Increases exposure and number <strong>of</strong> site visits<br />

– Amazon.com has large, successful affiliate program<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 447


• Public relations<br />

Public Relations<br />

– Provide customers with latest information<br />

• Products and services<br />

• Sales<br />

• Promotions<br />

– Press releases<br />

– Presentations and speeches<br />

– e-Mail<br />

– Crisis management<br />

• Issue statements regarding company problems<br />

• Minimize damage to company, brand and reputation<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 448


Customer Relationship Management<br />

• Customer Relationship Management (CRM)<br />

– Provision and maintenance <strong>of</strong> quality service<br />

– Communicate with customers<br />

– Deliver responses to customers’ wants and needs<br />

– Customer satisfaction key to successful business<br />

• Much easier, less expensive to retain customers than attract<br />

new customers<br />

– Challenging for online businesses<br />

• Transactions not conducted in person<br />

• Requires innovative new techniques<br />

– eCRM, iCRM<br />

• Refer to CRM conduced via Internet, interchangeable terms<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 449


Customer Relationship Management<br />

• Aspects <strong>of</strong> CRM<br />

– Call handling<br />

• Management <strong>of</strong> calls between customers and service<br />

representatives<br />

– Sales tracking<br />

– Transaction support<br />

• Support for people and technology involved in keeping<br />

transactions running smoothly<br />

– Personalization <strong>of</strong> customer experience<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 450


Online Payments<br />

• Electronic Funds Transfer<br />

– Basis for online payments<br />

– Multiple ways <strong>of</strong> conducting EFT’s and presenting them to<br />

the customer<br />

– Many companies <strong>of</strong>fer EFT solutions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 451


Credit-Card Payment<br />

• Online credit-card payment<br />

– Popular and common<br />

• Many people have and are familiar with credit cards<br />

– Some customers have security and privacy concerns<br />

– Require merchant account at bank<br />

• Special card-not-present (CNP) account for online transactions<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 452


• Digital cash<br />

Digital Cash and e-Wallets<br />

– Stored electronically<br />

– Analogous to traditional bank account<br />

• Customers deposit money<br />

– Overcomes drawbacks <strong>of</strong> credit cards<br />

• e-Wallets<br />

• Digital cash accounts <strong>of</strong>ten allow deposits in form <strong>of</strong> checks or<br />

bank transfers<br />

• Allows merchants to accept customers without credit cards<br />

– Store billing and shipping information<br />

– Fill out forms at compatible sites in one click<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 453


• Micropayments<br />

Micropayments<br />

– Merchants <strong>of</strong>ten charged fee for credit card transactions<br />

• For small items, fee can exceed cost <strong>of</strong> item<br />

– Micropayments allow merchants to avoid this problem<br />

• Add together all small transactions and pay percentage <strong>of</strong> that<br />

– Similar to concept <strong>of</strong> phone bill<br />

• Pay one large sum monthly rather than tiny sum per each use<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 454


• Smart cards<br />

– Memory cards<br />

Smart Cards<br />

• Only allow for storage <strong>of</strong> information<br />

– Microprocessor cards<br />

• Like tiny computers<br />

• Can do processing in addition to storing data<br />

– Contact interface<br />

• Card inserted into reading device for use<br />

– Contactless interface<br />

• Data transmitted via wireless device inside card<br />

– Data and money protected by personal identification number<br />

(PIN)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 455


Security<br />

• Security is an increasingly important concern<br />

– Highly confidential data being transmitted all the time<br />

• Credit cards, social security numbers, business data<br />

– Attackers attempt to steal, corrupt or otherwise compromise<br />

this data<br />

– Requirements for successful secure transaction:<br />

• Privacy<br />

• Integrity<br />

• Authentication<br />

• Authorization<br />

• Non-repudiation<br />

– Also concerned with availability <strong>of</strong> site<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 456


• Cryptography<br />

Public-Key Cryptography<br />

– Transforms data using cipher or cryptostream<br />

– Key acts as password that combined with cipher will decrypt<br />

encoded message into original message<br />

– Early cryptography relied on symmetric cryptography<br />

• Same key used to encrypt and decrypt<br />

• Problem <strong>of</strong> how to securely transmit key itself arose<br />

– Solution was public-key cryptography<br />

• Two related but different keys used<br />

• Sender uses receiver’s public key to encode<br />

• Receiver decodes with private key<br />

• Keys long enough that guessing or cracking them takes so<br />

much time it is not worth the effort<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 457


Public-Key Cryptography<br />

Encrypting and decrypting a message using public-key cryptography.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 458


• Digital signatures<br />

Public-Key Cryptography<br />

– Same concept as physical written signatures<br />

• Authenticate signer<br />

• Difficult to forge<br />

– Part <strong>of</strong> public-key cryptography<br />

– Generated by running phrase through hash function<br />

• Returns hash value<br />

– Hash value for a phrase is over 99% guaranteed unique<br />

• ie., two different phrases very unlikely to generate same value<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 459


Public-Key Cryptography<br />

Authentication with a public-key algorithm.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 460


Public-Key Cryptography<br />

• Public Key Infrastructure<br />

– Digital certificates<br />

• Digital documents issued by certification authority<br />

• Name <strong>of</strong> individual/group<br />

• Public key<br />

• Serial number<br />

• Expiration date<br />

• Signature <strong>of</strong> trusted authority<br />

– Certificate repositories<br />

• Hold database <strong>of</strong> public digital certificates<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 461


Public-Key Cryptography<br />

• PKI implementations<br />

– More secure than standard point-<strong>of</strong>-sale (POS) transactions<br />

• Strong encryption can take decades to crack using current<br />

technology<br />

– RSA encryption popular choice for PKI<br />

• Developed at MIT in 1977<br />

– Pretty Good Privacy (PGP)<br />

• Implementation <strong>of</strong> PKI<br />

• Very popular way to encrypt e-mail<br />

• Operates using web <strong>of</strong> trust<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 462


Cryptanalysis<br />

• Searching for weaknesses in encryption<br />

– Try to find ways to decrypt ciphertext without having key<br />

– Not just done by malicious attackers<br />

• Researchers want to find and fix flaws before attackers find<br />

and exploit them<br />

• Cryptanalytic attacks<br />

– Common attack searches for relationship between ciphertext<br />

and key<br />

– Easier when all or part <strong>of</strong> decoded message known in<br />

advance<br />

• Goal not to determine original message, but to discover key<br />

• Allows attacker to forge messages from that sender<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 463


• Preventative measures<br />

– Key expiration dates<br />

Cryptanalysis<br />

• If attacker breaks or steals key, only useful for limited time<br />

– Exchange secret keys securely with public-key cryptography<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 464


Key-Agreement Protocols<br />

• Public-key encryption not perfect solution<br />

– Requires significant computing power<br />

– Best used to exchange secret keys once, then use those keys<br />

for rest <strong>of</strong> transaction<br />

• Key-agreement protocol<br />

– Protocol is set <strong>of</strong> rules for communication<br />

– Digital envelope most common<br />

• Encrypt message using secret key<br />

• Encrypt secret key with public-key encryption<br />

• Both encrypted portions sent to receiver<br />

• Receiver decrypts secret key using private key<br />

• Receiver then uses decrypted secret key to decrypt message<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 465


Key-Agreement Protocols<br />

Creating a digital envelope.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 466


Key Management<br />

• Protecting private keys vital to security<br />

– Key generates possible source <strong>of</strong> vulnerability<br />

• Susceptible to brute-force cracking<br />

• If keys always chosen from small subset <strong>of</strong> all possible keys,<br />

much easier to crack<br />

• Algorithm must generate random keys from large set <strong>of</strong><br />

possible keys<br />

– Key should be very long<br />

– Common standard is 128 bits<br />

• 2 to the 128 power<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 467


Secure Sockets Layer (SSL)<br />

• SSL facilitates secure online communications<br />

– Developed by Netscape<br />

– Built into most browsers and servers<br />

• Standard Internet communication process<br />

– Data sent and received through sockets<br />

• S<strong>of</strong>tware mechanism that sends, receives and interprets<br />

network data<br />

– Transmission Control Protocol/Internet Protocol (TCP/IP)<br />

• Standard protocol for Internet communication<br />

• Controls how data is transferred and interpreted over networks<br />

– Messages broken down into packets<br />

• Add ordering, routing and error-correction information<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 468


Secure Sockets Layer (SSL)<br />

• Standard Internet communication process, cont.<br />

– Packet’s destination is an IP address<br />

• Unique number that identifies computer on network<br />

– TCP puts received packets in order and checks for errors<br />

• Can request retransmission if errors discovered<br />

– Only basic error checking exists<br />

• Attackers can forge data with relative ease<br />

• More secure methods necessary to increase integrity<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 469


• SSL<br />

Secure Sockets Layer (SSL)<br />

– Layer on top <strong>of</strong> TCP/IP<br />

– Implements public-key encryption using RSA algorithm<br />

– Generates secret key referred to as session key<br />

• Rest <strong>of</strong> transaction encrypted using this key<br />

– Messages still sent through TCP/IP after encryption step<br />

– Generally used for point-to-point connections<br />

• One computer communicating with another directly<br />

– Transport Layer Security (TLS) another similar technology<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 470


• SSL, cont.<br />

Secure Sockets Layer (SSL)<br />

– SSL does not protect data stored on server<br />

• Only data that is currently traveling across network<br />

– Stored data should be encrypted by another means<br />

– Always take standard precautions against cracker attacks<br />

• Making SSL more efficient<br />

– Encryption taxing on server resources<br />

– Dedicated SSL encoding/decoding hardware exists<br />

• Peripheral component interface (PCI) cards<br />

• Offload these tasks from CPU<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 471


WTLS<br />

• Wireless Transport Layer Security<br />

– Security layer for Wireless Application Protocol (WAP)<br />

• WAP used for wireless communication on cell phones and<br />

other devices<br />

– Provides authentication, integrity, privacy and denial-<strong>of</strong>-<br />

service protection<br />

– Encrypts data sent between WAP device and WAP gateway<br />

• Where wireless network connects to wired network<br />

– Data translated from WTLS to SSL at gateway<br />

• For an instant, data is unencrypted<br />

• WAP gap<br />

• No successful WAP gap attack ever reported<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 472


IPSec and Virtual Private Networks (VPN)<br />

• Types <strong>of</strong> networks<br />

– Local Area Network (LAN)<br />

• Connects physically close computers<br />

– Wide Area Network (WAN)<br />

• Connect computers in multiple locations<br />

• Employ private phone lines, radio waves or other techniques<br />

– Virtual Private Network (VPN)<br />

• Leverage Internet to simulate LAN for multiple remote<br />

networks and wireless users<br />

• Secure tunnel over Internet<br />

• Data protected by encryption<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 473


IPSec and Virtual Private Networks (VPN)<br />

• Internet Protocol Security (IPSec)<br />

– Developed by Internet <strong>Engineering</strong> Task Force (IETF)<br />

– Uses public-key and symmetric-key cryptography<br />

– Protects against data manipulation and IP-spo<strong>of</strong>ing<br />

– Conceptually similar to SSL<br />

• Secures entire network rather than point-to-point transaction<br />

– Often employs RSA or Diffie-Hellman encryption for key<br />

exchange<br />

– DES or 3DES used for secret key<br />

• IPSec packets<br />

– Three components<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 474


IPSec and Virtual Private Networks (VPN)<br />

• IPSec packets, cont.<br />

– Authentication header (AH)<br />

• Verifies identity <strong>of</strong> sender and integrity <strong>of</strong> data<br />

– Encapsulating security payload (ESP)<br />

• Encrypts packet to prevent reading while in transit<br />

– Internet Key Exchange (IKE)<br />

• Authenticates encryption keys<br />

• VPN shortcomings<br />

– Time consuming and complicated to initially set up<br />

– Must be careful who is given access<br />

• VPN users essentially the same as LAN users<br />

• Potentially have access to sensitive data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 475


Security Attacks<br />

• Security <strong>of</strong> greater concern than ever before<br />

– Great variety <strong>of</strong> attacks to defend against<br />

– Denial <strong>of</strong> Service (DoS) and Distributed DOS (DDoS)<br />

• Flood server or network with data packets<br />

• Prevents any legitimate traffic from passing through<br />

• DDoS occurs when attacker gains control <strong>of</strong> multiple machines<br />

• Uses them all to coordinate massive attack<br />

– Viruses<br />

• Malicious programs<br />

• Attach to or overwrite legitimate programs<br />

• Vary in severity from minor irritations to complete destruction<br />

<strong>of</strong> hard drive data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 476


Security Attacks<br />

• Security <strong>of</strong> greater concern than ever before, cont.<br />

– Worms<br />

• Similar to viruses<br />

• Able to reproduce and spread over networks<br />

• Generate extreme amount <strong>of</strong> traffic, slowing networks<br />

• CodeRed and ILOVEYOU two infamous worms<br />

• Attackers commonly called hackers or crackers<br />

– Traditionally, terms not interchangeable<br />

• Hacker is a skilled programmer and computer user<br />

• Cracker maliciously attacks computers for personal gain<br />

• In practice, hacker has become blanket term for computer<br />

criminals<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 477


Security Attacks<br />

• Protecting against attacks<br />

– S<strong>of</strong>tware exists to mitigate effects <strong>of</strong> these attacks<br />

– Anti-Virus s<strong>of</strong>tware<br />

• Detects and deletes viruses and worms before they execute<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 478


• Network security<br />

Network Security<br />

– Allow authorized users access they need<br />

– Prevent unauthorized users from accessing and damaging<br />

network<br />

– Firewall a vital tool for network security<br />

• Protects LANs from unauthorized traffic<br />

• Placed between external Internet connection and computers on<br />

local network<br />

• Blocks or allows traffic based on rules set by administrator<br />

• Administrator must balance users’ needs for functionality<br />

against need for network security<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 479


• Network security, cont.<br />

Network Security<br />

– Intrusion detection systems (IDS)<br />

• Detect that an attacker has penetrated the firewall<br />

• Monitor network traffic and log files<br />

• If intrusion detected, immediately closes that connection and<br />

alerts administrator through various means<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 480


Legal Issues<br />

• Internet poses new challenges to lawmakers<br />

– File-sharing could redefine copyright laws<br />

– Marketing techniques clash with users’ desire for privacy<br />

– Cyberspace requires rethinking many traditional legal ideas<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 481


Privacy<br />

• U.S. Constitution has no explicit right to privacy<br />

– Regardless, privacy is vital concern to many people<br />

– Many sites track activity and personal information<br />

• Provides personalization and sometimes better service<br />

• Balanced against desire for marketers to not follow one’s every<br />

move or steal valuable information<br />

– Affects company employees as well as customers<br />

• Companies install key loggers or keystroke cops<br />

• Monitor what employees do on their system<br />

• Right <strong>of</strong> company to ensure employees doing their jobs versus<br />

employee desire for privacy and free-speech<br />

– Idea <strong>of</strong> right to privacy still being created in courts<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 482


• Defamation<br />

Defamation<br />

– Consists <strong>of</strong> slander and libel<br />

• Slander is spoken<br />

• Libel is written or spoken in a broader context than slander<br />

• Proving defamation<br />

– Plaintiff must:<br />

• Show that statement was written, spoken or broadcast<br />

• Reasonably identify individual responsible<br />

• Show that the statement is indeed defamatory<br />

• Show that the statement was intended to cause harm and<br />

known to be false<br />

• Show evidence <strong>of</strong> injury or actual loss<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 483


Sexually Explicit Speech<br />

• Pornography is protected by First Amendment<br />

– Obscenity is not<br />

– Miller test has been deciding factor between the two<br />

• Obscenity “appeals to the prurient interest”<br />

• Lacks serious literary, artistic, political or scientific value<br />

– In cyberspace, community standards are different<br />

• “Communities” not defined by physical location<br />

• Issues <strong>of</strong> jurisdiction are unclear<br />

– Problem <strong>of</strong> what standard to hold Internet to<br />

• Broadcast laws restrict content rather than audience<br />

• Print laws use non-content-related means<br />

• Restrict audience rather than content<br />

• Either is possible on the Internet<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 484


• E-mail marketing<br />

– Can be useful or harmful<br />

Spam<br />

– Unsolicited mass-mailings, or Spam, strongly frowned on<br />

• Many Internet users received hundreds per day<br />

• Content <strong>of</strong>ten at best irrelevant and at worst highly <strong>of</strong>fensive<br />

• Possible for children to receive pornography, for example<br />

– Legislation being created to deal with Spam epidemic<br />

– S<strong>of</strong>tware to combat spam also exists<br />

– Spammers constantly evolve to circumvent new measures<br />

– One <strong>of</strong> the toughest usability and privacy issues with<br />

Internet today<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 485


• Copyright<br />

Copyrights and Patents<br />

– Protection given to author <strong>of</strong> original piece<br />

• Protects an expression <strong>of</strong> idea, not idea itself<br />

– Incentive to create by guaranteeing credit for work<br />

• Life <strong>of</strong> author plus 70 years<br />

– Digital technology has made copyright gray area<br />

• Fair use vs. piracy<br />

• Copies can be perfect, not cheap imitations<br />

– Movies and MP3-encoded music files hottest area <strong>of</strong> debate<br />

• File-sharing programs lets users download copyrighted works<br />

freely<br />

• Costs distributors and artists money<br />

• Users want to sample music, enjoy lower prices<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 486


• Patents<br />

Copyrights and Patents<br />

– Grant creator sole rights to a discovery<br />

– Designed to foster invention and innovation<br />

• Guarantees new idea cannot be stolen from inventor<br />

– Possible to patent method <strong>of</strong> doing business<br />

• Must be non-obvious to person skilled in relevant field<br />

– Also contentious area<br />

• Some feel patents stifle rather than foster innovation<br />

• 20-year duration may be too long in fast-paced s<strong>of</strong>tware world<br />

• Some companies file patents solely to pr<strong>of</strong>it from infringement<br />

lawsuits later on<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 487


XML and e-Commerce<br />

• Extensible Markup Language (XML)<br />

– One parent <strong>of</strong> HTML<br />

– Allows users to create customized tags to mark up data<br />

• Share data in standard, easily-used format worldwide<br />

• Portable between multiple applications and platforms<br />

– Some industries have standard XML formats already<br />

• MathML<br />

• CML<br />

• XMI<br />

• OSD<br />

– Facilitates Electronic Data Interchange (EDI)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 488


Introduction to Wireless Technology and m-<br />

Business<br />

• Wireless technology<br />

– One <strong>of</strong> technology’s fastest growing sectors<br />

– Brings communications and Internet everywhere<br />

– Wireless devices support increasing number <strong>of</strong> features<br />

– Convergence beginning to occur<br />

• Features <strong>of</strong> several distinct products combined into one<br />

• PDAs merging with cell phones<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 489


• Mobile business<br />

m-Business<br />

– E-business enabled by wireless technology<br />

– Relatively new, but rapidly growing<br />

– Access critical business information anytime, anywhere<br />

• Employees can conduct their duties more easily<br />

• Customers can interact with online businesses in new ways and<br />

locations<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 490


Identifying User Location<br />

• Location-identification technologies<br />

– Determine users’ physical location to within yards<br />

– Useful in wireless marketing<br />

• Send promotion data when user is near relevant location<br />

– Great benefits to emergency services<br />

• Quickly and accurately locate victims<br />

– Made possible by relationships between wireless providers,<br />

networks and users<br />

– Multipath errors can cause problems<br />

• Signals reflecting <strong>of</strong>f nearby objects<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 491


• Enhanced 911 Act<br />

E911 Act<br />

– Standardize 911 service across mobile devices<br />

– Improve response time to calls made from cell phones<br />

– First phase requires cellular providers to disclose phone<br />

number <strong>of</strong> caller as well as location <strong>of</strong> nearest cell site<br />

– Second phase requires disclosure <strong>of</strong> location <strong>of</strong> caller to<br />

within 125 meters<br />

– Several benefits<br />

• Callers <strong>of</strong>ten do not know their exact location<br />

• If call breaks up, emergency responders can still send help<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 492


Location-Identification Technologies<br />

• Methods <strong>of</strong> locating the user<br />

– Triangulation<br />

• Analyze angle <strong>of</strong> signals from at least two fixed points<br />

– Information presented as geocode<br />

• Latitude and longitude<br />

– Different methods have varying degrees <strong>of</strong> accuracy<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 493


Location-Identification Technologies<br />

Technology Degree <strong>of</strong> Accuracy<br />

Cell <strong>of</strong> Origin (COO) Least accurate. User could be anywhere in tower’s range.<br />

Meets only Phase I <strong>of</strong> E911 Act.<br />

Angle <strong>of</strong> Arrival (AOA) Fairly accurate. User is within the overlap <strong>of</strong> two towers’<br />

cell sites. Used primarily in rural areas where there are<br />

Time Difference <strong>of</strong> Arrival<br />

(TDOA)<br />

Enhanced Observed Time<br />

Difference (E-OTD)<br />

fewer towers. Complies with Phase II <strong>of</strong> E911.<br />

Accurate. User’s location is determined by triangulating<br />

from three locations. Complies with Phase II <strong>of</strong> E911. Most<br />

effective when towers are close together.<br />

Accurate. User’s location is determined by triangulating<br />

from three locations. Complies with Phase II <strong>of</strong> E911.<br />

Location Pattern Matching Accurate. User’s location is determined by analyzing<br />

multipath interference in a given area, making the method<br />

Global Positioning Systems<br />

(GPS)<br />

Fig. 38.6 Location-identification technologies.<br />

more effective for locating a device in an urban area.<br />

Highly accurate. Satellites determine a user’s location<br />

anywhere on earth. However, GPS is not as effective when<br />

the user is indoors.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 494


Wireless Marketing, Advertising and<br />

Promotions<br />

• Wireless Marketing<br />

– Wireless technology provides unique opportunities<br />

– Augments, rather than replaces, traditional marketing<br />

• Push and pull strategies<br />

– Pull<br />

– Push<br />

• Users request data to be sent in real-time<br />

• Company delivers messages at time it deems appropriate<br />

– Using either one, advertising should be opt-in<br />

• User must have explicitly requested the service<br />

• Otherwise it is essentially spam, creates ill-will toward the<br />

company<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 495


Wireless Marketing, Advertising and<br />

Promotions<br />

• Effective wireless marketing<br />

– Must deliver right content at right time<br />

– Perfect match for location-identification technology<br />

– Advertisers much more likely to place and pay more for ads<br />

that are well-targeted and likely to generate response<br />

– Several challenges and obstacles<br />

• Security<br />

• Ensuring ads display properly on diverse devices<br />

• Additional middle-men<br />

• Selecting suitable publisher and transmission technology<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 496


Wireless Marketing, Advertising and<br />

Promotions<br />

• Short Message Service<br />

– Deliver simple text-only messages<br />

– Interactivity is limited<br />

– Nearly no load time<br />

– Work well for quick, simple alerts<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 497


Wireless Payment Options<br />

• Wireless payments (m-payments)<br />

– Must be secure and reliable, like standard online payments<br />

– Mobile Virtual Network Operators (MVNOs)<br />

• Purchase bandwidth and re-brand with added services<br />

• Option for banks to make micropayments pr<strong>of</strong>itable<br />

– M-wallets<br />

• Users store billing and shipping information<br />

• Recall with one click<br />

• Promote convenience<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 498


Privacy and the Wireless Internet<br />

• Privacy problems magnified on wireless devices<br />

– Transmissions can be intercepted<br />

– Users located accurately<br />

– Accepted standard is opt-in policy<br />

• Users request to be sent information<br />

• Consumer should always expect the information they receive<br />

• No unauthorized information sharing with partners<br />

– Sometimes double opt-in<br />

• User requests information, then has to confirm decision<br />

– Opt-out frowned upon<br />

• Send users information unless they say to stop<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 499


• CITA<br />

Privacy and the Wireless Internet<br />

– Cellular Telecommunications and Internet Association<br />

– Group that has created guidelines for consumer privacy<br />

– Outlined four guidelines<br />

• Alert consumers when location being identified<br />

• Always use opt-in marketing<br />

• Consumers able to access their own information<br />

• Same protections <strong>of</strong>fered by all devices and carriers<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 500


Outline<br />

Web Servers<br />

Introduction<br />

HTTP Request Types<br />

System Architecture<br />

Client-Side Scripting versus Server-Side Scripting<br />

Accessing Web Servers<br />

Micros<strong>of</strong>t Internet Information Services (IIS)<br />

Micros<strong>of</strong>t Internet Information Services (IIS) 5.0<br />

Micros<strong>of</strong>t Internet Information Services (IIS) 6.0<br />

Apache Web Server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 501


• Web server<br />

Introduction<br />

– Responds to client requests by providing resources<br />

• URI (Uniform Resource Identifier)<br />

• Web server and client communicate with<br />

platform-independent Hypertext Transfer Protocol<br />

(HTTP)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 502


Introduction<br />

IIS 5.0 IIS 6.0 Apache Web server<br />

Company Micros<strong>of</strong>t Micros<strong>of</strong>t<br />

Apache S<strong>of</strong>tware<br />

Corporation Corporation Foundation<br />

Version 5.0 6.0 2.0.47<br />

Released 2/17/00 3/28/03 7/10/03<br />

Platforms Windows 2000, Windows Server 2003 Windows NT/2000/XP,<br />

Windows XP<br />

Mac OS X, Linux and<br />

other UNIX-based<br />

platforms,<br />

experimentally supports<br />

Windows 95/98<br />

Brief The most popular The newest release <strong>of</strong> Currently the most<br />

description Web server for<br />

Windows 2000.<br />

IIS from Micros<strong>of</strong>t. popular Web server.<br />

Price Included with Included with Freeware.<br />

Windows 2000<br />

and Windows<br />

XP.<br />

Windows Server 2003<br />

Fig. 21.1 Web servers discussed in this chapter.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 503


• Request methods<br />

– get<br />

– post<br />

HTTP Request Types<br />

– Retrieve and send client form data to Web server<br />

– Post data to a server-side form handler<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 504


System Architecture<br />

• Multi-tier application (n-tier application)<br />

– Information tier (data or bottom tier)<br />

• Maintains data for the application<br />

• Stores data in a relational database management system<br />

(RDBMS)<br />

– Middle tier<br />

• Implements business logic and presentation logic<br />

• Control interactions between application clients and<br />

application data<br />

– Client tier (top tier)<br />

• Application’s user interface<br />

• Users interact directly with the application through the client<br />

tier<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 505


Client-Side Scripting Vs Server-Side Scripting<br />

• Client-side scripts<br />

– Validate user input<br />

• Reduce requests needed to be passed to server<br />

• Access browser<br />

• Enhance Web pages with DHTML, ActiveX controls, and<br />

applets<br />

• Server-side scripts<br />

– Executed on server<br />

– Generate custom response for clients<br />

– Wide range <strong>of</strong> programmatic capabilities<br />

– Access to server-side s<strong>of</strong>tware that extends server<br />

functionality<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 506


Accessing Web Servers<br />

• Request documents from Web servers<br />

– Host names<br />

– Local Web servers<br />

• Access through machine name or localhost<br />

– Remote Web servers<br />

• Access through machine name<br />

– Domain name or Internet Protocol (IP) address<br />

• Domain name server (DNS)<br />

– Computer that maintains a database <strong>of</strong> host names and<br />

their corresponding IP address<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 507


Micros<strong>of</strong>t Internet Information Services<br />

(IIS)<br />

• An enterprise-level Web server that is included<br />

with Windows<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 508


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

• FTP Site<br />

– Used for transferring large files across the Internet<br />

• HTTP Site<br />

– Used most frequently to request documents from Web<br />

servers<br />

• SMTP Virtual Server<br />

– Sends and receives electronic mail<br />

• Web Site Content Directory<br />

– Directory containing the documents that clients will view<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 509


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

Internet Information Services window <strong>of</strong> Internet Services Manager.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 510


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

Virtual Directory Creation Wizard welcome dialog.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 511


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

Virtual Directory Alias dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 512


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

Web Site Content Directory dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 513


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

5.0<br />

Access Permissions dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 514


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

6.0<br />

Internet Information (IIS) Services Manager window.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 515


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

6.0<br />

Virtual Directory Creation Wizard welcome dialog.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 516


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

6.0<br />

Virtual Directory Alias dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 517


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

6.0<br />

Web Site Content Directory dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 518


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

6.0<br />

Access Permissions dialog <strong>of</strong> the Virtual Directory Creation Wizard.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 519


Apache Web Server<br />

• Currently the most popular Web server<br />

• Stability<br />

• Efficiency<br />

• Portability<br />

• Open-source<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 520


Apache Web Server<br />

Apache Web server starting. (Courtesy <strong>of</strong> The Apache S<strong>of</strong>tware Foundation.)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 521


Requesting Documents<br />

• Request HTML documents from IIS 6.0 or<br />

Apache<br />

– Launch Internet Explorer<br />

– Enter HTML document’s location in Address field<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 522


HTML<br />

Requesting test.html from IIS 5.<br />

Requesting test.html from IIS 6 or Apache.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 523


Web Resources<br />

• www.micros<strong>of</strong>t.com/msdownload/ntoptionpack/askwiz.asp<br />

• www.w3.org/Protocols<br />

• www.apache.org<br />

• httpd.apache.org<br />

• httpd.apache.org/docs-2.0<br />

• www.apacheweek.com<br />

• linuxtoday.com/stories/18780.html<br />

• www.iisanswers.com<br />

• www.iisadministrator.com<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 524


Outline<br />

CSS Vs SSS<br />

Introduction<br />

HTTP Request Types<br />

System Architecture<br />

Client-Side Scripting versus Server-Side Scripting<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 525


• Web server<br />

Introduction<br />

– Responds to client requests by providing resources<br />

• URI (Uniform Resource Identifier)<br />

• Web server and client communicate with<br />

platform-independent Hypertext Transfer Protocol<br />

(HTTP)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 526


• Request methods<br />

– get<br />

– post<br />

HTTP Request Types<br />

– Retrieve and send client form data to Web server<br />

– Post data to a server-side form handler<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 527


URI, URL, URN<br />

• URL - Uniform Resource Locator<br />

used to identify an Internet resource, and can be<br />

specified in a single line <strong>of</strong> text.<br />

• URN - Uniform Resource Name<br />

used to identify an Internet resource, without the<br />

use <strong>of</strong> a scheme, and can be specified in a single<br />

line <strong>of</strong> text.<br />

• URI - Uniform Resource Identifier (URI)<br />

used by a browser to identify a single document,<br />

and it too can be specified in a single line <strong>of</strong> text.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 528


Examples<br />

• URL http://www.abc.org/iis/review1.htm<br />

• URN www.abc.org/iis/review1.htm#one<br />

• URI http://www.abc.org/iis/review1.htm.html#one<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 529


System Architecture<br />

• Multi-tier application (n-tier application)<br />

– Information tier (data or bottom tier)<br />

• Maintains data for the application<br />

• Stores data in a relational database management system<br />

(RDBMS)<br />

– Middle tier<br />

• Implements business logic and presentation logic<br />

• Control interactions between application clients and<br />

application data<br />

– Client tier (top tier)<br />

• Application’s user interface<br />

• Users interact directly with the application through the client<br />

tier<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 530


Client-Side Scripting versus Server-Side<br />

Scripting<br />

• Client-side scripts<br />

– Validate user input<br />

• Reduce requests needed to be passed to server<br />

• Access browser<br />

• Enhance Web pages with DHTML, ActiveX controls, and<br />

applets<br />

• Server-side scripts<br />

– Executed on server<br />

– Generate custom response for clients<br />

– Wide range <strong>of</strong> programmatic capabilities<br />

– Access to server-side s<strong>of</strong>tware that extends server<br />

functionality<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 531


Client-side scripts<br />

Types<br />

• VBScript , JavaScript and Ajax<br />

(Asynchronous JavaScript and XML )<br />

Server-side scripts<br />

• ASP, JSP, PHP (Hypertext Preprocessor-<br />

OSS), Cold fusion etc<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 532


Comparison<br />

JavaScript VBScript<br />

• Scripting language Scripting language<br />

• Object-based Not object-based<br />

• Supports functions Supports functions<br />

• Secure Secure<br />

• C/C++ based Subset <strong>of</strong> Visual Basic<br />

• Supported by all browsers Presently supported by<br />

only Internet Explorer<br />

• Easy to learn Based on BASIC<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 533


Micros<strong>of</strong>t Internet Information Services (IIS)<br />

• FTP Site<br />

– Used for transferring large files across the Internet<br />

• HTTP Site<br />

– Used most frequently to request documents from Web<br />

servers<br />

• SMTP Virtual Server<br />

– Sends and receives electronic mail<br />

• Web Site Content Directory<br />

– Directory containing the documents that clients will view<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 534


IT1451 – WEB TECHNOLOGY<br />

8 TH SEMESTER – INFORMATION TECHNOLOGY<br />

UNIT – 4 NOTES<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 535


Outline<br />

Database: SQL<br />

Introduction<br />

Relational Database Model<br />

Relational Database Overview: Books.mdb Database<br />

SQL (Structured Query Language)<br />

Basic SELECT Query<br />

WHERE Clause<br />

ORDER BY Clause<br />

Merging Data from Multiple Tables: INNER JOIN<br />

Joining Data from Tables Authors, AuthorISBN,<br />

Titles and Publishers<br />

INSERT Statement<br />

UPDATE Statement<br />

DELETE Statement<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 536


• Database<br />

– Integrated collection <strong>of</strong> data<br />

Introduction<br />

– Database management system (DBMS)<br />

• Store and organize data consistent with database’s format<br />

• Relational database<br />

– SQL (Structured Query Language)<br />

• Queries<br />

• Manipulate data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 537


• Composed <strong>of</strong> tables<br />

• Row<br />

– Number column<br />

– Primary key<br />

Relational Database Model<br />

• Reference data in the table<br />

• A column or set <strong>of</strong> columns in table contains unique data<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 538


Row<br />

Relational Database Model<br />

number name department salary location<br />

23603 Jones 413 1100 New Jersey<br />

24568 Kerwin 413 2000 New Jersey<br />

34589 Larson 642 1800 Los Angeles<br />

35761 Myers 611 1400 Orlando<br />

47132 Neumann 413 9000 New Jersey<br />

78321 Stephens 611 8500 Orlando<br />

Primary key<br />

Column<br />

Fig. 22.1 Relational database structure <strong>of</strong> an Employee table.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 539


Relational Database Model<br />

department location<br />

413 New Jersey<br />

611 Orlando<br />

642 Los Angeles<br />

Fig. 22.2 Table formed by selecting department and location data from the Employee table.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 540


Relational Database Overview: Books.mdb<br />

Database<br />

• Primary key uniquely identifies each row<br />

– Rule <strong>of</strong> Entity Integrity<br />

• Composite primary key<br />

• Lines connecting tables<br />

– Relationships<br />

• One-to-many relationship<br />

• Foreign key<br />

– Join multiple tables<br />

– Rule <strong>of</strong> Referential Integrity<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 541


Relational Database Overview: Books.mdb<br />

Database<br />

Field Description<br />

authorID Author’s ID number in the database. In the Books.mdb<br />

database, this Integer column is defined as autoincrement.<br />

For each new row inserted in this table, the<br />

database increments the authorID value, ensuring that<br />

each row has a unique authorID. This column represents<br />

the table’s primary key.<br />

firstName Author’s first name (a String).<br />

lastName Author’s last name (a String).<br />

Fig. 22.3 Authors table from Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 542


Relational Database Overview: Books.mdb<br />

Database<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

3 Tem Nieto<br />

4 Kate Steinbuhler<br />

5 Sean Santry<br />

6 Ted Lin<br />

7 Praveen Sadhu<br />

8 David McPhie<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

11 Ben Wiedermann<br />

12 Jonathan Liperi<br />

Fig. 22.4 Data from the Authors table <strong>of</strong><br />

Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 543


Relational Database Overview: Books.mdb<br />

Database<br />

Field Description<br />

publisherID The publisher’s ID number in the database. This<br />

auto-incremented Integer is the table’s primary<br />

key.<br />

publisherName The name <strong>of</strong> the publisher (a String).<br />

Fig. 22.5 Publishers table from Books.mdb.<br />

publisherID publisherName<br />

1 Prentice Hall<br />

2 Prentice Hall PTG<br />

Fig. 22.6 Data from the Publishers table <strong>of</strong> Books.mdb.<br />

Field Description<br />

authorID The author’s ID number, which allows the database<br />

to associate each book with a specific author. The<br />

integer ID number in this column must also appear<br />

in the Authors table.<br />

isbn The ISBN number for a book (a String).<br />

Fig. 22.7 AuthorISBN table from Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 544


Relational Database Overview: Books.mdb<br />

Database<br />

authorID isbn authorID isbn<br />

1 0130895725 2 0139163050<br />

1 0132261197 2 013028419x<br />

1 0130895717 2 0130161438<br />

1 0135289106 2 0130856118<br />

1 0139163050 2 0130125075<br />

1 013028419x 2 0138993947<br />

1 0130161438 2 0130852473<br />

1 0130856118 2 0130829277<br />

1 0130125075 2 0134569555<br />

1 0138993947 2 0130829293<br />

1 0130852473 2 0130284173<br />

1 0130829277 2 0130284181<br />

1 0134569555 2 0130895601<br />

1 0130829293 3 013028419x<br />

1 0130284173 3 0130161438<br />

1 0130284181 3 0130856118<br />

1 0130895601 3 0134569555<br />

2 0130895725 3 0130829293<br />

2 0132261197 3 0130284173<br />

2 0130895717 3 0130284181<br />

2 0135289106 4 0130895601<br />

Fig. 22.8 Data from AuthorISBN table in Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 545


Relational Database Overview: Books.mdb<br />

Database<br />

Field Description<br />

isbn ISBN number <strong>of</strong> the book (a String).<br />

title Title <strong>of</strong> the book (a String).<br />

editionNumber Edition number <strong>of</strong> the book (a String).<br />

copyright Copyright year <strong>of</strong> the book (an Integer).<br />

description Description <strong>of</strong> the book (a String).<br />

publisherID Publisher’s ID number (an Integer). This value<br />

must correspond to an ID number in the<br />

Publishers table.<br />

imageFile Name <strong>of</strong> the file containing the book’s cover image<br />

(a String).<br />

price Suggested retail price <strong>of</strong> the book (a real number).<br />

[Note: The prices shown in this database are for<br />

example purposes only.]<br />

Fig. 22.9 Titles table from Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 546


Relational Database Overview: Books.mdb<br />

Database<br />

isbn title edition-<br />

Number<br />

publisherID copyright<br />

price<br />

0130923613 Python How to Program 1 1 2002 $69.95<br />

0130622214 C# How to Program 1 1 2002 $69.95<br />

0130341517 Java How to Program 4 1 2002 $69.95<br />

0130649341 The Complete Java Training 4<br />

Course<br />

2 2002 $109.95<br />

0130895601 Advanced Java 2 Platform<br />

How to Program<br />

1 1 2002 $69.95<br />

0130308978 Internet and World Wide<br />

Web How to Program<br />

2 1 2002 $69.95<br />

0130293636 Visual Basic .NET How to<br />

Program<br />

2 1 2002 $69.95<br />

0130895636 The Complete C++ Training 3<br />

Course<br />

2 2001 $109.95<br />

0130895512 The Complete e-Business & 1<br />

e-Commerce Programming<br />

Training Course<br />

2 2001 $109.95<br />

Fig. 22.10 Portion <strong>of</strong> the data from the Titles table <strong>of</strong> Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 547


Relational Database Overview: Books.mdb<br />

Database<br />

Fig. 22.11 Table relationships in Books.mdb.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 548


SQL (Structured Query Language)<br />

SQL keyword Description<br />

SELECT Selects (retrieves) columns from one or more tables.<br />

FROM Specifies tables from which to get columns or delete<br />

rows. Required in every SELECT and DELETE<br />

statement.<br />

WHERE Specifies criteria that determine the rows to be<br />

retrieved.<br />

INNER JOIN Joins rows from multiple tables to produce a single<br />

set <strong>of</strong> rows.<br />

GROUP BY Specifies criteria for grouping rows.<br />

ORDER BY Specifies criteria for ordering rows.<br />

INSERT Inserts data into a specified table.<br />

UPDATE Updates data in a specified table.<br />

DELETE Deletes data from a specified table.<br />

CREATE Creates a new table.<br />

DROP Deletes an existing table.<br />

COUNT Returns the number <strong>of</strong> records that satisfy given<br />

search criteria.<br />

Fig. 22.12 SQL keywords.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 549


Basic SELECT Query<br />

• SELECT * FROM tableName<br />

– SELECT * FROM Authors<br />

– SELECT authorID, lastName FROM Authors<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 550


Basic SELECT Query<br />

authorID lastName authorID lastName<br />

1 Deitel 7 Sadhu<br />

2 Deitel 8 McPhie<br />

3 Nieto 9 Yaeger<br />

4 Steinbuhler 10 Zlatkina<br />

5 Santry 11 Wiedermann<br />

6 Lin 12 Liperi<br />

Fig. 22.13 authorID and lastName from the Authors table.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 551


WHERE Clause<br />

• Specify selection criteria for query<br />

– SELECT columnName1, columnName2, … FROM tableName<br />

WHERE criteria<br />

• SELECT title, editionNumber, copyright FROM<br />

Titles WHERE copyright > 1999<br />

– LIKE<br />

• Pattern matching<br />

– Asterisk ( * )<br />

• SELECT authorID, firstName, lastName<br />

FROM Authors WHERE lastName LIKE ‘D*’<br />

– Question mark ( ? )<br />

• SELECT authorID, firstName, lastName<br />

FROM Authors WHERE lastName LIKE ‘?I*’<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 552


WHERE Clause<br />

Title editionNumber copyright<br />

Internet and World Wide Web How to<br />

Program<br />

2 2002<br />

Java How to Program 4 2002<br />

The Complete Java Training Course 4 2002<br />

The Complete e-Business & e-Commerce<br />

Programming Training Course<br />

1 2001<br />

The Complete Internet & World Wide Web<br />

Programming Training Course<br />

2 2001<br />

The Complete Perl Training Course 1 2001<br />

The Complete XML Programming Training<br />

Course<br />

1 2001<br />

C How to Program 3 2001<br />

C++ How to Program 3 2001<br />

The Complete C++ Training Course 3 2001<br />

e-Business and e-Commerce How to Program 1 2001<br />

Internet and World Wide Web How to<br />

Program<br />

1 2000<br />

The Complete Internet and World Wide Web<br />

Programming Training Course<br />

1 2000<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 553


WHERE Clause<br />

Java How to Program (Java 2) 3 2000<br />

The Complete Java 2 Training Course 3 2000<br />

XML How to Program 1 2001<br />

Perl How to Program 1 2001<br />

Advanced Java 2 Platform How to<br />

Program<br />

1 2002<br />

e-Business and e-Commerce for Managers 1 2000<br />

Wireless Internet and Mobile Business 1 2001<br />

How to Program<br />

C# How To Program 1 2002<br />

Python How to Program 1 2002<br />

Visual Basic .NET How to Program 2 2002<br />

Fig. 22.14 Titles with copyrights after 1999 from table Titles.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 554


WHERE Clause<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

Fig. 22.15 Authors from the Authors table whose last names start with D.<br />

authorID firstName lastName<br />

3 Tem Nieto<br />

6 Ted Lin<br />

11 Ben Wiedermann<br />

12 Jonathan Liperi<br />

Fig. 22.16 Authors from table Authors whose last names contain i as their<br />

second letter.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 555


ORDER BY Clause<br />

• Arranged in ascending or descending order<br />

– SELECT columnName1, columnName2, … FROM<br />

tableName ORDER BY column ASC<br />

• SELECT authorID, firstName, lastName FROM<br />

Authors ORDER BY lastName ASC<br />

– SELECT columnName1, columnName2, … FROM<br />

tableName ORDER BY column DESC<br />

• SELECT authorID, firstName, lastName FROM<br />

Authors ORDER BY lastName DESC<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 556


ORDER BY Clause<br />

authorID firstName lastName<br />

2 Paul Deitel<br />

1 Harvey Deitel<br />

6 Ted Lin<br />

12 Jonathan Liperi<br />

8 David McPhie<br />

3 Tem Nieto<br />

7 Praveen Sadhu<br />

5 Sean Santry<br />

4 Kate Steinbuhler<br />

11 Ben Wiedermann<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

Fig. 22.17 Authors from table Authors in ascending order by<br />

lastName.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 557


ORDER BY Clause<br />

authorID firstName lastName<br />

10 Marina Zlatkina<br />

9 Cheryl Yaeger<br />

11 Ben Wiedermann<br />

4 Kate Steinbuhler<br />

5 Sean Santry<br />

7 Praveen Sadhu<br />

3 Tem Nieto<br />

8 David McPhie<br />

12 Jonathan Liperi<br />

6 Ted Lin<br />

2 Paul Deitel<br />

1 Harvey Deitel<br />

Fig. 22.18 Authors from table Authors in descending order by<br />

lastName.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 558


ORDER BY Clause<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

6 Ted Lin<br />

12 Jonathan Liperi<br />

8 David McPhie<br />

3 Tem Nieto<br />

7 Praveen Sadhu<br />

5 Sean Santry<br />

4 Kate Steinbuhler<br />

11 Ben Wiedermann<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

Fig. 22.19 Authors from table Authors in ascending order by<br />

lastName and by firstName.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 559


ORDER BY Clause<br />

isbn title edition- copy- price<br />

Number right<br />

0130895601 Advanced Java 2 Platform 1<br />

How to Program<br />

2002 $69.95<br />

0131180436 C How to Program 1 1992 $69.95<br />

0130895725 C How to Program 3 2001 $69.95<br />

0132261197 C How to Program 2 1994 $49.95<br />

0130622214 C# How To Program 1 2002 $69.95<br />

0135289106 C++ How to Program 2 1998 $49.95<br />

0131173340 C++ How to Program 1 1994 $69.95<br />

0130895717 C++ How to Program 3 2001 $69.95<br />

013028419X e-Business and e-<br />

Commerce How to<br />

Program<br />

1 2001 $69.95<br />

0130308978 Internet and World Wide<br />

Web How to Program<br />

2 2002 $69.95<br />

0130161438 Internet and World Wide<br />

Web How to Program<br />

1 2000 $69.95<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 560


ORDER BY Clause<br />

0130341517 Java How to Program 4 2002 $69.95<br />

0136325890 Java How to Program 1 1998 $69.95<br />

0130284181 Perl How to Program 1 2001 $69.95<br />

0130923613 Python How to<br />

Program<br />

1 2002 $69.95<br />

0130293636 Visual Basic .NET<br />

How to Program<br />

2 2002 $69.95<br />

0134569555 Visual Basic 6 How to 1 1999 $69.95<br />

Program<br />

0130622265 Wireless Internet and<br />

Mobile Business How<br />

to Program<br />

1 2001 $69.95<br />

0130284173 XML How to Program 1 2001 $69.95<br />

Fig. 22.20 Books from table Titles whose titles end with How<br />

to Program in ascending order by title.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 561


Merging Data from Multiple Tables: INNER<br />

JOIN<br />

• Normalize databases<br />

– Ensure database does not store data redundantly<br />

– SELECT columnName1, columnName2, … FROM table1<br />

INNER JOIN table2 ON table1, columnName =<br />

table2.columnName<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 562


Merging Data from Multiple Tables: INNER<br />

JOIN<br />

firstName lastName isbn firstName lastName isbn<br />

Harvey Deitel 0130895601 Harvey Deitel 0130856118<br />

Harvey Deitel 0130284181 Harvey Deitel 0130161438<br />

Harvey Deitel 0130284173 Harvey Deitel 013028419x<br />

Harvey Deitel 0130829293 Harvey Deitel 0139163050<br />

Harvey Deitel 0134569555 Harvey Deitel 0135289106<br />

Harvey Deitel 0130829277 Harvey Deitel 0130895717<br />

Harvey Deitel 0130852473 Harvey Deitel 0132261197<br />

Harvey Deitel 0138993947 Harvey Deitel 0130895725<br />

Harvey Deitel 0130125075 Paul Deitel 0130895601<br />

Paul Deitel 0130284181 Paul Deitel 0135289106<br />

Paul Deitel 0130284173 Paul Deitel 0130895717<br />

Paul Deitel 0130829293 Paul Deitel 0132261197<br />

Paul Deitel 0134569555 Paul Deitel 0130895725<br />

Paul Deitel 0130829277 Tem Nieto 0130284181<br />

Paul Deitel 0130852473 Tem Nieto 0130284173<br />

Paul Deitel 0138993947 Tem Nieto 0130829293<br />

Paul Deitel 0130125075 Tem Nieto 0134569555<br />

Paul Deitel 0130856118 Tem Nieto 0130856118<br />

Paul Deitel 0130161438 Tem Nieto 0130161438<br />

Paul Deitel 013028419x Tem Nieto 013028419x<br />

Paul Deitel 0139163050 Sean Santry 0130895601<br />

Fig. 22.21 Authors from table Authors and ISBN numbers <strong>of</strong> the authors’<br />

books, sorted in ascending order by lastName and firstName.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 563


1 SELECT Titles.title, Titles.isbn, Authors.firstName,<br />

2 Authors.lastName, Titles.copyright,<br />

3 Publishers.publisherName<br />

4 FROM<br />

5 ( Publishers INNER JOIN Titles<br />

6 ON Publishers.publisherID = Titles.publisherID )<br />

7 INNER JOIN<br />

8 ( Authors INNER JOIN AuthorISBN<br />

9 ON Authors.authorID = AuthorISBN.authorID )<br />

10 ON Titles.isbn = AuthorISBN.isbn<br />

11 ORDER BY Titles.title<br />

Fig. 22.22<br />

(1 <strong>of</strong> 1)<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 564


Joining Data from Tables Authors,<br />

AuthorISBN, Titles and Publishers<br />

Title isbn first- last- copy- publisher-<br />

Name Name right Name<br />

Advanced Java 2 Platform<br />

How to Program<br />

0130895601 Paul Deitel 2002 Prentice Hall<br />

Advanced Java 2 Platform<br />

How to Program<br />

0130895601 Harvey Deitel 2002 Prentice Hall<br />

Advanced Java 2 Platform<br />

How to Program<br />

0130895601 Sean Santry 2002 Prentice Hall<br />

C How to Program 0131180436 Harvey Deitel 1992 Prentice Hall<br />

C How to Program 0131180436 Paul Deitel 1992 Prentice Hall<br />

C How to Program 0132261197 Harvey Deitel 1994 Prentice Hall<br />

C How to Program 0132261197 Paul Deitel 1994 Prentice Hall<br />

C How to Program 0130895725 Harvey Deitel 2001 Prentice Hall<br />

C How to Program 0130895725 Paul Deitel 2001 Prentice Hall<br />

C# How To Program 0130622214 Tem Nieto 2002 Prentice Hall<br />

C# How To Program 0130622214 Paul Deitel 2002 Prentice Hall<br />

C# How To Program 0130622214 Cheryl Yaeger 2002 Prentice Hall<br />

C# How To Program 0130622214 Marina Zlatkina 2002 Prentice Hall<br />

C# How To Program 0130622214 Harvey Deitel 2002 Prentice Hall<br />

C++ How to Program 0130895717 Paul Deitel 2001 Prentice Hall<br />

C++ How to Program 0130895717 Harvey Deitel 2001 Prentice Hall<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 565


Joining Data from Tables Authors,<br />

AuthorISBN, Titles and Publishers<br />

C++ How to Program 0131173340 Paul Deitel 1994 Prentice Hall<br />

C++ How to Program 0131173340 Harvey Deitel 1994 Prentice Hall<br />

C++ How to Program 0135289106 Harvey Deitel 1998 Prentice Hall<br />

C++ How to Program 0135289106 Paul Deitel 1998 Prentice Hall<br />

e-Business and e-Commerce 0130323640<br />

for Managers<br />

Harvey Deitel 2000 Prentice Hall<br />

e-Business and e-Commerce 0130323640<br />

for Managers<br />

Kate Steinbuhler 2000 Prentice Hall<br />

e-Business and e-Commerce 0130323640<br />

for Managers<br />

Paul Deitel 2000 Prentice Hall<br />

e-Business and e-Commerce 013028419X<br />

How to Program<br />

Harvey Deitel 2001 Prentice Hall<br />

e-Business and e-Commerce 013028419X<br />

How to Program<br />

Paul Deitel 2001 Prentice Hall<br />

e-Business and e-Commerce 013028419X<br />

How to Program<br />

Tem Nieto 2001 Prentice Hall<br />

Fig. 22.23 Portion <strong>of</strong> the result set produced by the query in Fig. 22.22.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 566


• Inserts new row in table<br />

INSERT Statement<br />

– INSERT INTO tableName ( columnName1, columnName2,<br />

…, columnNameN ) VALUES ( value1 , value2, …, valueN )<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 567


INSERT Statement<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

3 Tem Nieto<br />

4 Kate Steinbuhler<br />

5 Sean Santry<br />

6 Ted Lin<br />

7 Praveen Sadhu<br />

8 David McPhie<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

11 Ben Wiedermann<br />

12 Jonathan Liperi<br />

13 Sue Smith<br />

Fig. 22.24 Table Authors after an INSERT operation to add a<br />

row.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 568


• Modifies data in a table<br />

UPDATE Statement<br />

– UPDATE tableName SET columnName1 = value1,<br />

columnName2 = value2, …, columnNameN = valueN<br />

WHERE criteria<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 569


UPDATE Statement<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

3 Tem Nieto<br />

4 Kate Steinbuhler<br />

5 Sean Santry<br />

6 Ted Lin<br />

7 Praveen Sadhu<br />

8 David McPhie<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

11 Ben Wiedermann<br />

12 Jonathan Liperi<br />

13 Sue Jones<br />

Fig. 22.25 Table Authors after an UPDATE operation to<br />

change a row.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 570


• Removes data from a table<br />

– DELETE FROM tableName<br />

WHERE criteria<br />

DELETE Statement<br />

authorID firstName lastName<br />

1 Harvey Deitel<br />

2 Paul Deitel<br />

3 Tem Nieto<br />

4 Kate Steinbuhler<br />

5 Sean Santry<br />

6 Ted Lin<br />

7 Praveen Sadhu<br />

8 David McPhie<br />

9 Cheryl Yaeger<br />

10 Marina Zlatkina<br />

11 Ben Wiedermann<br />

12 Jonathan Liperi<br />

Fig. 22.26 Table Authors after a DELETE operation to remove<br />

a row.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 571


Active Server Pages (ASP)<br />

<br />

Server-side technologies<br />

<br />

<br />

Dynamically creates Web pages<br />

<br />

Use client information, server information<br />

and information from the Internet<br />

Active Server Pages (ASP)<br />

<br />

<br />

Micros<strong>of</strong>t Server-side technology<br />

Dynamically build documents in response to<br />

client requests<br />

<br />

Deliver dynamic Web content<br />

HTML, DHTML, ActiveX controls, client-side<br />

scripts and Java applets<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 572


ASP – Cont.<br />

<br />

<br />

<br />

<br />

<br />

<br />

Dynamically edit, change or add any content <strong>of</strong> a Web<br />

page<br />

Respond to user queries or data submitted from HTML<br />

forms<br />

Access any data or databases and return the results<br />

to a browser<br />

Customize a Web page to make it more useful for<br />

individual users<br />

The advantages <strong>of</strong> using ASP instead <strong>of</strong> CGI and Perl,<br />

are those <strong>of</strong> simplicity and speed<br />

Provide security since your ASP code can not be<br />

viewed from the browser<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 573


How Active Server Pages Work<br />

<br />

Active Server Pages<br />

<br />

<br />

<br />

<br />

<br />

Processed by scripting engine<br />

Server-side ActiveX control<br />

.asp file extension<br />

Can contain HTML tags<br />

Scripting written with VBScript<br />

JavaScript also used<br />

Others (Independent S<strong>of</strong>tware Vendors)<br />

Communication with Server<br />

Client HTTP request to server<br />

Active server page processes request and<br />

returns results<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 574


Write text with ASP<br />

<br />

<br />

<br />

<br />

<br />

An ASP file can also contain server<br />

scripts, surrounded by the delimiters .<br />

Server scripts are executed on the<br />

server, and can contain any expressions,<br />

statements, procedures, or operators valid<br />

for the scripting language you prefer to<br />

use.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 575


ASP Variables<br />

<br />

<br />

<br />


Loop<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 577


ASP Procedures<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Result: <br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 578


Forms – a.html<br />

<br />

First<br />

Name: Last<br />

Name: <br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 579


simpleform.asp<br />

Welcome<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 580


ASP Cookies<br />

<br />

<br />

<br />

<br />

A cookie is <strong>of</strong>ten used to identify a<br />

user.<br />

A cookie is a small file that the server<br />

embeds on the user's computer.<br />

Each time the same computer<br />

requests a page with a browser, it will<br />

send the cookie too.<br />

With ASP, you can both create and<br />

retrieve cookie values.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 581


Example<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 582


ASP Session Object<br />

<br />

<br />

<br />

The Session object is used to store<br />

information about, or change settings<br />

for a user session.<br />

Variables stored in the Session object<br />

hold information about one single user,<br />

and are available to all pages in one<br />

application.<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 583


ASP Objects<br />

<br />

ASP Response<br />

ASP Request<br />

ASP Application<br />

ASP Session<br />

ASP Server<br />

ASP Error<br />

ASP FileSystem<br />

ASP TextStream<br />

ASP Drive<br />

ASP File<br />

ASP Folder<br />

ASP Dictionary<br />

ASP ADO<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 584


Response Object<br />

<br />

<br />

used to send output to the user from the<br />

server<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 585


Request Object<br />

<br />

<br />

used to get information from the user<br />

<br />

<br />

<br />

First name:<br />

<br />

<br />

Last name:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 586


Session Object<br />

<br />

<br />

The Session object is used to store<br />

information about, or change settings<br />

for a user session.<br />

Variables stored in the Session object<br />

hold information about one single<br />

user, and are available to all pages in<br />

one application.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 587


Ex: A session's timeout<br />

<br />

<br />

<br />

<br />

<br />

This example sets and returns the timeout (in minutes) for the session.<br />

<br />

<br />

<br />

<br />

<br />

Output:<br />

Default Timeout is: 20 minutes. (Server time out)<br />

Timeout is now: 30 minutes. (Now client can change to 30 min as session<br />

out)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 588


FileSystemObject<br />

<br />

<br />

The FileSystemObject is used to<br />

access the file system on the server.<br />

This object can manipulate files,<br />

folders, and directory paths.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 589


Does a specified file exist?<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 590


FileSystemObject - methods<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

DriveExists- Checks if a specified drive exists<br />

File Exists -Checks if a specified file exists<br />

Folder Exists -Checks if a specified folder exists<br />

Copy File - Copies one or more files from one<br />

location to another<br />

Copy Folder- Copies one or more folders from<br />

one location to another<br />

Delete File - Deletes one or more specified files<br />

Delete Folder - Deletes one or more specified<br />

folders<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 591


ADO - ActiveX Data Objects<br />

ADO can be used to access databases from<br />

your web pages.<br />

The common way to access a database from<br />

inside an ASP page is to:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Create an ADO connection to a database<br />

Open the database connection<br />

Create an ADO recordset<br />

Open the recordset<br />

Extract the data you need from the recordset<br />

Close the recordset<br />

Close the connection<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 592


Create a DSN-less Database<br />

Connection<br />

If you have a database called “test.mdb"<br />

located in a web directory like<br />

"c:/webdata/", you can connect to the<br />

database with the following ASP code:<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 593


Display records<br />

<br />

<br />

<br />

<br />

First create a database connection,<br />

Then a recordset,<br />

and then display the data in HTML.<br />

<br />

<br />


Example<br />

<br />

set rs = Server.CreateObject("ADODB.recordset")<br />

rs.Open "Select * from Customers", conn<br />

do until rs.EOF<br />

for each x in rs.Fields<br />

Response.Write(x.name)<br />

Response.Write(" = ")<br />

Response.Write(x.value & "")<br />

next<br />

Response.Write("")<br />

rs.MoveNext<br />

loop<br />

rs.close<br />

conn.close<br />

%><br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 595


CASE STUDY<br />

<br />

<br />

<br />

Add a Record to a Table in a<br />

Database<br />

Update a Record in a Table<br />

Delete a Record in a Table<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 596


Add a Record to a Table in a<br />

Database<br />

<br />

To add a new record to the<br />

Customers table in the Test database<br />

<br />

First create a form that contains the<br />

fields we want to collect data from<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 597


Main.html<br />

<br />

<br />

CustomerID:<br />

<br />

Company Name:<br />

<br />

Contact Name: <br />

<br />

Address: <br />

<br />

City: <br />

Postal Code: <br />

<br />

Country: <br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 598


adds.asp<br />

When the user presses the submit button the form is sent to a<br />

file called "adds.asp"<br />

<br />


Contd.<br />

on error resume next<br />

conn.Execute sql,recaffected<br />

if err0 then<br />

Response.Write("No update permissions!")<br />

else Response.Write("" & recaffected & "<br />

record added")<br />

end if<br />

conn.close %><br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 600


Quiz<br />

1. What does ASP stand for?<br />

<br />

<br />

<br />

<br />

Active Server Pages<br />

A Server Page<br />

All Standard Pages<br />

Active Standard Pages<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 601


2. ASP server scripts are<br />

surrounded by delimiters, which?<br />

<br />

<br />

<br />

<br />

...<br />

...<br />

...<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 602


3. How do you write "Hello World" in<br />

ASP ?<br />

Response.Write("Hello World")<br />

Document.Write("Hello World")<br />

"Hello World"<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 603


4. "


5. What is the default scripting<br />

language in ASP?<br />

<br />

<br />

<br />

<br />

VBScript<br />

EcmaScript<br />

PERL<br />

JavaScript<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 605


6. How can you script your ASP code in<br />

JavaScript?<br />

<br />

<br />

<br />

<br />

JavaScript is the default scripting language<br />

End the document with: <br />

Start the document with: <br />

Start the document with: <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 606


7. How do you get information from<br />

a form that is submitted using<br />

the "get" method?<br />

<br />

<br />

Request.Form<br />

Request.QueryString<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 607


8. How do you get information from<br />

a form that is submitted using the<br />

"post" method?<br />

<br />

<br />

Request.QueryString<br />

Request.Form<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 608


9. Which ASP property is used to<br />

identify a user?<br />

<br />

<br />

<br />

The Server object<br />

The Application object<br />

An ASP Cookie<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 609


10. All users <strong>of</strong> the same application<br />

share ONE Session object.<br />

<br />

<br />

True<br />

False<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 610


11. If a user has Cookies enabled, a<br />

session variable is available to all<br />

pages in one application.<br />

<br />

<br />

True<br />

False<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 611


Answers<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

1- Active Server Pages<br />

2 - <br />

3 - Response.Write("Hello World")<br />

4 -


8.1 Introduction<br />

- SGML is a meta-markup language<br />

- Developed in the early 1980s; ISO std. In 1986<br />

- HTML was developed using SGML in the early<br />

1990s - specifically for Web documents<br />

- Two problems with HTML:<br />

1. Fixed set <strong>of</strong> tags and attributes<br />

- User cannot define new tags or attributes<br />

- So, the given tags must fit every kind <strong>of</strong><br />

document, and the tags cannot connote<br />

any particular meaning<br />

2. There are no restrictions on arrangement or<br />

order <strong>of</strong> tag appearance in a document<br />

- One solution to the first <strong>of</strong> these problems:<br />

Let each group <strong>of</strong> users define their own tags<br />

(with implied meanings)<br />

(i.e., design their own “HTML”s using SGML)<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 613<br />

1


8.1 Introduction (continued)<br />

- Problem with using SGML:<br />

- It’s too large and complex to use, and it is very<br />

difficult to build a parser for it<br />

- A better solution: Define a lite version <strong>of</strong> SGML<br />

- XML is not a replacement for HTML<br />

- HTML is a markup language used to describe the<br />

layout <strong>of</strong> any kind <strong>of</strong> information<br />

- XML is a meta-markup language that can be used<br />

to define markup languages that can define the<br />

meaning <strong>of</strong> specific kinds <strong>of</strong> information<br />

- XML is a very simple and universal way <strong>of</strong> storing<br />

and transferring data <strong>of</strong> any kind<br />

- XML does not predefine any tags<br />

- XML has no hidden specifications<br />

- All documents described with an XML-derived<br />

markup language can be parsed with a single<br />

parser<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 614<br />

2


8.1 Introduction (continued)<br />

- We will refer to an XML-based markup language as<br />

a tag set<br />

- Strictly speaking, a tag set is an XML application,<br />

but that terminology can be confusing<br />

-An XML processor is a program that parses XML<br />

documents and provides the parts to an application<br />

- Documents that use an XML-based markup<br />

language is an XML document<br />

- Both IE6 and NS7 support basic XML<br />

8.2 The Syntax <strong>of</strong> XML<br />

- The syntax <strong>of</strong> XML is in two distinct levels:<br />

1. The general low-level rules that apply to all<br />

XML documents<br />

2. For a particular XML tag set, either a document<br />

type definition (DTD) or an XML schema<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 615<br />

3


8.2 The Syntax <strong>of</strong> XML (continued)<br />

- General XML Syntax<br />

- XML documents consist <strong>of</strong>:<br />

1. data elements<br />

2. markup declarations (instructions for the XML<br />

parser)<br />

3. processing instructions (for the application<br />

program that is processing the data in the<br />

document)<br />

- All XML documents begin with an XML declaration:<br />

<br />

- XML comments are just like HTML comments<br />

- XML names:<br />

- Must begin with a letter or an underscore<br />

- They can include digits, hyphens, and periods<br />

- There is no length limitation<br />

- They are case sensitive (unlike HTML names)<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 616<br />

4


8.2 The Syntax <strong>of</strong> XML (continued)<br />

- Syntax rules for XML: same as those <strong>of</strong> XHTML<br />

- Every XML document defines a single root<br />

element, whose opening tag must appear as<br />

the first line <strong>of</strong> the document<br />

- An XML document that follows all <strong>of</strong> these rules<br />

is well formed<br />

<br />

<br />

1960 <br />

Cessna <br />

Centurian <br />

Yellow with white trim <br />

<br />

Gulfport <br />

Mississippi <br />

<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 617<br />

5


8.2 The Syntax <strong>of</strong> XML (continued)<br />

- Attributes are not used in XML the way they are in<br />

HTML<br />

- In XML, you <strong>of</strong>ten define a new nested tag to<br />

provide more info about the content <strong>of</strong> a tag<br />

- Nested tags are better than attributes, because<br />

attributes cannot describe structure and the<br />

structural complexity may grow<br />

- Attributes should always be used to identify<br />

numbers or names <strong>of</strong> elements (like HTML id and<br />

name attributes)<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 618<br />

6


8.2 The Syntax <strong>of</strong> XML (continued)<br />

<br />

<br />

...<br />

<br />

<br />

<br />

Maggie Dee Magpie <br />

...<br />

<br />

<br />

<br />

<br />

Maggie <br />

Dee <br />

Magpie <br />

<br />

...<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 619<br />

7


8.3 XML Document Structure<br />

- An XML document <strong>of</strong>ten uses two auxiliary files:<br />

- One to specify the structural syntactic rules<br />

- One to provide a style specification<br />

- An XML document has a single root element, but<br />

<strong>of</strong>ten consists <strong>of</strong> one or more entities<br />

- Entities range from a single special character to<br />

a book chapter<br />

- An XML document has one document entity<br />

- All other entities are referenced in the<br />

document entity<br />

- Reasons for entity structure:<br />

1. Large documents are easier to manage<br />

2. Repeated entities need not be literally repeated<br />

3. Binary entities can only be referenced in the<br />

document entities (XML is all text!)<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 620<br />

8


8.3 XML Document Structure (continued)<br />

- When the XML parser encounters a reference to<br />

a non-binary entity, the entity is merged in<br />

- Entity names:<br />

- No length limitation<br />

- Must begin with a letter, a dash, or a colon<br />

- Can include letters, digits, periods, dashes,<br />

underscores, or colons<br />

- A reference to an entity has the form:<br />

&entity_name;<br />

- One common use <strong>of</strong> entities is for special<br />

characters that may be used for markup delimiters<br />

- These are predefined (as in XHTML):<br />

< &lt;<br />

> &gt;<br />

& &amp;<br />

" &quot;<br />

' &apos;<br />

- The user can only define entities in a DTD<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 621<br />

9


8.3 XML Document Structure (continued)<br />

- If several predefined entities must appear near<br />

each other in a document, it is better to avoid<br />

using entity references<br />

- Character data section<br />

<br />

e.g., instead <strong>of</strong><br />

use<br />

Start &gt; &gt; &gt; &gt; HERE<br />

&lt; &lt; &lt; &lt;<br />

>>> HERE


8.4 Document Type Definitions<br />

- A DTD is a set <strong>of</strong> structural rules called<br />

declarations<br />

- These rules specify a set <strong>of</strong> elements, along<br />

with how and where they can appear in a<br />

document<br />

- Purpose: provide a standard form for a collection<br />

<strong>of</strong> XML documents and define a markup<br />

language for them<br />

- Not all XML documents have or need a DTD<br />

- The DTD for a document can be internal or<br />

external<br />

- All <strong>of</strong> the declarations <strong>of</strong> a DTD are enclosed in<br />

the block <strong>of</strong> a DOCTYPE markup declaration<br />

- DTD declarations have the form:<br />

<br />

- There are four possible declaration keywords:<br />

ELEMENT, ATTLIST, ENTITY, and NOTATION<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

11<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 623


8.4 Document Type Definitions (continued)<br />

- Declaring Elements<br />

- Element declarations are similar to BNF<br />

- An element declaration specifies the name <strong>of</strong> an<br />

an element, and the element’s structure<br />

- If the element is a leaf node <strong>of</strong> the document tree,<br />

its structure is in terms <strong>of</strong> characters<br />

- If it is an internal node, its structure is a list <strong>of</strong><br />

children elements (either leaf or internal nodes)<br />

- General form:<br />

<br />

e.g.,<br />

<br />

memo<br />

from to date re body<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

12<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 624


8.4 Document Type Definitions (continued)<br />

- Declaring Elements (continued)<br />

- Child elements can have modifiers, +, *, ?<br />

e.g.,<br />

<br />

- Leaf nodes specify data types, most <strong>of</strong>ten<br />

PCDATA, which is an acronym for parsable<br />

character data<br />

- Data type could also be EMPTY (no content)<br />

and ANY (can have any content)<br />

- Example <strong>of</strong> a leaf declaration:<br />

<br />

- Declaring Attributes<br />

- General form:<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

13<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 625


8.4 Document Type Definitions (continued)<br />

- Declaring Attributes (continued)<br />

- Attribute types: there are ten different types, but<br />

we will consider only CDATA<br />

- Default values:<br />

a value<br />

#FIXED value (every element will have<br />

this value),<br />

#REQUIRED (every instance <strong>of</strong> the element must<br />

have a value specified), or<br />

#IMPLIED (no default value and need not specify<br />

a value)<br />

<br />

<br />

<br />

<br />

<br />

...<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

14<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 626


8.4 Document Type Definitions (continued)<br />

- Declaring Entities<br />

- Two kinds:<br />

-A general entity can be referenced anywhere in<br />

the content <strong>of</strong> an XML document<br />

-A parameter entity can be referenced only in<br />

a markup declaration<br />

- General form <strong>of</strong> declaration:<br />

<br />

e.g., <br />

- A reference: &jfk;<br />

- If the entity value is longer than a line, define it<br />

in a separate file (an external text entity)<br />

<br />

SHOW planes.dtd<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

15<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 627


8.4 Document Type Definitions (continued)<br />

- XML Parsers<br />

- Always check for well formedness<br />

- Some check for validity, relative to a given DTD<br />

- Called validating XML parsers<br />

- You can download a validating XML parser from:<br />

http://xml.apache.org/xerces-j/index.html<br />

- Internal DTDs<br />

<br />

- External DTDs<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

16<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 628


8.5 Namespaces<br />

-A markup vocabulary is the collection <strong>of</strong> all <strong>of</strong> the<br />

element types and attribute names <strong>of</strong> a markup<br />

language (a tag set)<br />

- An XML document may define its own tag set and<br />

also use those <strong>of</strong> another tag set - CONFLICTS!<br />

-An XML namespace is a collection <strong>of</strong> names used<br />

in XML documents as element types and attribute<br />

names<br />

- The name <strong>of</strong> an XML namespace has the form <strong>of</strong><br />

a URI<br />

- A namespace declaration has the form:<br />

<br />

- The prefix is a short name for the namespace,<br />

which is attached to names from the<br />

namespace in the XML document<br />

<br />

- In the document, you can use <br />

- Purposes <strong>of</strong> the prefix:<br />

1. Shorthand<br />

2. URI includes characters that are illegal in XML<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

17<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 629


8.5 Namespaces (continued)<br />

- Can declare two namespaces on one element<br />

<br />

-The gmcars element can now use gm names and<br />

html names<br />

- One namespace can be made the default by<br />

leaving the prefix out <strong>of</strong> the declaration<br />

8.6 XML Schemas<br />

- Problems with DTDs:<br />

1. Syntax is different from XML - cannot be parsed<br />

with an XML parser<br />

2. It is confusing to deal with two different syntactic<br />

forms<br />

3. DTDs do not allow specification <strong>of</strong> particular<br />

kinds <strong>of</strong> data<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

18<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 630


8.6 XML Schemas (continued)<br />

- XML Schemas is one <strong>of</strong> the alternatives to DTD<br />

- Two purposes:<br />

1. Specify the structure <strong>of</strong> its instance XML<br />

documents<br />

2. Specify the data type <strong>of</strong> every element and<br />

attribute <strong>of</strong> its instance XML documents<br />

- Schemas are written using a namespace:<br />

http://www.w3.org/2001/XMLSchema<br />

- Every XML schema has a single root, schema<br />

The schema element must specify the namespace<br />

for schemas as its xmlns:xsd attribute<br />

- Every XML schema itself defines a tag set, which<br />

must be named<br />

targetNamespace =<br />

"http://cs.uccs.edu/planeSchema"<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

19<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 631


8.6 XML Schemas (continued)<br />

- If we want to include nested elements, we must<br />

set the elementFormDefault attribute to<br />

qualified<br />

- The default namespace must also be specified<br />

xmlns = "http://cs.uccs.edu/planeSchema"<br />

- A complete example <strong>of</strong> a schema element:<br />

<br />

targetNamespace =<br />

"http://cs.uccs.edu/planeSchema"<br />

<br />

xmlns = "http://cs.uccs.edu/planeSchema"<br />

<br />

elementFormDefault = "qualified"><br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

20<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 632


8.6 XML Schemas (continued)<br />

- Defining an instance document<br />

- The root element must specify the namespaces<br />

it uses<br />

1. The default namespace<br />

2. The standard namespace for instances<br />

(XMLSchema-instance)<br />

3. The location where the default namespace is<br />

defined, using the schemaLocation attribute,<br />

which is assigned two values<br />


8.6 XML Schemas (continued)<br />

- XMLS defines 44 data types<br />

- Primitive: string, Boolean, float, …<br />

- Derived: byte, decimal, positiveInteger, …<br />

- User-defined (derived) data types – specify<br />

constraints on an existing type (the base type)<br />

- Constraints are given in terms <strong>of</strong> facets<br />

(totalDigits, maxInclusive, etc.)<br />

- Both simple and complex types can be either<br />

named or anonymous<br />

- DTDs define global elements (context is irrelevant)<br />

- With XMLS, context is essential, and elements<br />

can be either:<br />

1. Local, which appears inside an element<br />

that is a child <strong>of</strong> schema, or<br />

2. Global, which appears as a child <strong>of</strong> schema<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

22<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 634


8.6 XML Schemas (continued)<br />

- Defining a simple type:<br />

- Use the element tag and set the name and type<br />

attributes<br />

<br />

- An instance could have:<br />

Yellow-bellied sap sucker <br />

- Element values can be constant, specified with<br />

the fixed attribute<br />

fixed = "three-toed"<br />

- User-Defined Types<br />

- Defined in a simpleType element, using facets<br />

specified in the content <strong>of</strong> a restriction<br />

element<br />

- Facet values are specified with the value<br />

attribute<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

23<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 635


8.6 XML Schemas (continued)<br />

<br />

<br />

<br />

<br />

<br />

- There are several categories <strong>of</strong> complex types,<br />

but we discuss just one, element-only elements<br />

- Element-only elements are defined with the<br />

complexType element<br />

- Use the sequence tag for nested elements that<br />

must be in a particular order<br />

- Use the all tag if the order is not important<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

24<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 636


8.6 XML Schemas (continued)<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

- Nested elements can include attributes that give<br />

the allowed number <strong>of</strong> occurrences<br />

(minOccurs, maxOccurs, unbounded)<br />

SHOW planes.xsd and planes1.xml<br />

- We can define nested elements elsewhere<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

25<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 637


8.6 XML Schemas (continued)<br />

- The global element can be referenced in the<br />

complex type with the ref attribute<br />

<br />

- Validating Instances <strong>of</strong> XML Schemas<br />

- Can be done with several different tools<br />

- One <strong>of</strong> them is xsv, which is available from:<br />

http://www.ltg.ed.ac.uk/~ht/xsv-status.html<br />

- Note: If the schema is incorrect (bad format), xsv<br />

reports that it cannot find the schema<br />

8.7 Displaying Raw XML Documents<br />

- There is no presentation information in an XML<br />

document<br />

- An XML browser should have a default style sheet<br />

for an XML document that does not specify one<br />

- You get a stylized listing <strong>of</strong> the XML<br />

SHOW planes.xml<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

26<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 638


8.8 Displaying XML Documents with<br />

CSS<br />

- A CSS style sheet for an XML document is just a<br />

list <strong>of</strong> its tags and associated styles<br />

- The connection <strong>of</strong> an XML document and its style<br />

sheet is made through an xml-stylesheet<br />

processing instruction<br />

<br />

--> SHOW planes.css and run planes.xml<br />

8.9 XSLT Style Sheets<br />

- XSL began as a standard for presentations <strong>of</strong> XML<br />

documents<br />

- Split into three parts:<br />

- XSLT – Transformations<br />

- XPATH - XML Path Language<br />

- XSL-FO - Formatting objects<br />

- XSLT uses style sheets to specify<br />

transformations<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

27<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 639


8.9 XSLT Style Sheets (continued)<br />

- An XSLT processor merges an XML document into<br />

an XSLT style sheet<br />

- This merging is a template-driven process<br />

- An XSLT style sheet can specify page layout,<br />

page orientation, writing direction, margins, page<br />

numbering, etc.<br />

- The processing instruction we used for connecting<br />

a XSLT style sheet to an XML document is used to<br />

connect an XSLT style sheet to an XML document<br />

<br />

- An example:<br />

<br />

<br />

<br />

<br />

1977 <br />

Cessna <br />

Skyhawk <br />

Light blue and white <br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

28<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 640


8.9 XSLT Style Sheets (continued)<br />

- An XSLT style sheet is an XML document with a<br />

single element, stylesheet, which defines<br />

namespaces<br />

<br />

- If a style sheet matches the root element <strong>of</strong> the<br />

XML document, it is matched with the template:<br />

<br />

- A template can match any element, just by naming<br />

it (in place <strong>of</strong> /)<br />

- XSLT elements include two different kinds <strong>of</strong><br />

elements, those with content and those for which<br />

the content will be merged from the XML doc<br />

- Elements with content <strong>of</strong>ten represent HTML<br />

elements<br />

<br />

Happy Easter!<br />

<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

29<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 641


8.9 XML Transformations and Style<br />

Sheets (continued)<br />

- XSLT elements that represent HTML elements are<br />

simply copied to the merged document<br />

- The XSLT value-<strong>of</strong> element<br />

- Has no content<br />

- Uses a select attribute to specify part <strong>of</strong> the XML<br />

data to be merged into the new document<br />


8.10 XML Processors<br />

- Purposes:<br />

1. Check the syntax <strong>of</strong> a document for wellformedness<br />

2. Replace all references to entities by their<br />

definitions<br />

3. Copy default values (from DTDs or schemas)<br />

into the document<br />

4. If a DTD or schema is specified and the<br />

processor includes a validating parser, the<br />

structure <strong>of</strong> the document is validated<br />

- Two ways to check well-formedness:<br />

1. A browser with an XML parser<br />

2. A stand-alone XML parser<br />

- There are two different approaches to designing<br />

XML processors:<br />

- SAX and the DOM approach<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

31<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 643


8.10 XML Processors (continued)<br />

- The SAX (Simple API for XML) Approach:<br />

- Widely accepted and supported<br />

- Based on the concept <strong>of</strong> event processing:<br />

- Every time a syntactic structure (e.g., a tag, an<br />

attribute, etc.) is recognized, the processor<br />

raises an event<br />

- The application defines event handlers to<br />

respond to the syntactic structures<br />

- The DOM Approach<br />

- The DOM processor builds a DOM tree structure<br />

<strong>of</strong> the document<br />

(Similar to the processing by a browser <strong>of</strong> an<br />

XHTML document)<br />

- When the tree is complete, it can be traversed<br />

and processed<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

32<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 644


8.10 XML Processors (continued)<br />

- Advantages <strong>of</strong> the DOM approach:<br />

1. Good if any part <strong>of</strong> the document must be<br />

accessed more than once<br />

2. If any rearrangement <strong>of</strong> the document must be<br />

done, it is facilitated by having a<br />

representation <strong>of</strong> the whole document in<br />

memory<br />

3. Random access to any part <strong>of</strong> the document is<br />

possible<br />

4. Because the whole document is parsed before<br />

any processing takes place, processing <strong>of</strong> an<br />

invalid document is avoided<br />

- Disadvantages <strong>of</strong> the DOM approach:<br />

1. Large documents require a large memory<br />

2. The DOM approach is slower<br />

- Note: Most DOM processors use a SAX front<br />

end<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

33<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 645


8.10 Web Services<br />

- The ultimate goal <strong>of</strong> Web services:<br />

- Allow different s<strong>of</strong>tware in different places,<br />

written in different languages and resident on<br />

different platforms, to connect and interoperate<br />

- The Web began as provider <strong>of</strong> markup documents,<br />

served through the HTTP methods, GET and<br />

POST<br />

- An information service system<br />

- A Web service is closely related to an information<br />

service<br />

- Rather than having a server provide documents,<br />

the server provides services, through serverresident<br />

s<strong>of</strong>tware<br />

- The same Web server can provide both<br />

documents and services<br />

- The original Web services were provided via<br />

Remote Procedure Call (RPC), through two<br />

technologies, DCOM and CORBA<br />

- DCOM and CORBA use different protocols,<br />

which defeats the goal <strong>of</strong> universal component<br />

interoperability<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

34<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 646


8.10 Web Services (continued)<br />

- There are three roles required to provide and use<br />

Web services:<br />

1. Service providers<br />

2. Service requestors<br />

3. A service registry<br />

- Web Serviced Definition Language (WSDL)<br />

- Used to describe available services, as well as<br />

<strong>of</strong> message protocols for their use<br />

- Such descriptions reside on the Web server<br />

- Universal Description, Discovery, and<br />

Integration Service (UDDI)<br />

- Used to create Web services registry, and also<br />

methods that allow a remote system to<br />

determine which services are available<br />

- Standard Object Access Protocol (SOAP)<br />

- An XML-based specification that defines the<br />

forms <strong>of</strong> messages and RPCs<br />

- Supports the exchange <strong>of</strong> information among<br />

distributed systems<br />

Chapter 8 © 2005 by Addison Wesley Longman, Inc.<br />

35<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 647


IT1451 – WEB TECHNOLOGY<br />

8 TH SEMESTER – INFORMATION TECHNOLOGY<br />

UNIT – 5 NOTES<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 648


Servlets<br />

Introduction<br />

Servlet Overview and Architecture<br />

Interface Servlet and the Servlet Life Cycle<br />

HttpServlet Class<br />

HttpServletRequest Interface<br />

HttpServletResponse Interface<br />

Handling HTTP get Requests<br />

Setting Up the Apache Tomcat Server<br />

Deploying a Web Application<br />

Handling HTTP get Requests Containing Data<br />

Handling HTTP post Requests<br />

Redirecting Requests to Other Resources<br />

Multi-Tier Applications: Using JDBC from a Servlet<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 649


Objectives<br />

1. To execute servlets with the Apache Tomcat server.<br />

2. To be able to respond to HTTP requests from an<br />

HttpServlet.<br />

3. To be able to redirect requests to static and dynamic Web<br />

resources.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 650


Servlets<br />

• A Servlet is a program that runs on a server and<br />

generates a response upon a HTTP request<br />

• Building Web pages on the fly is useful (and commonly<br />

done) for a number <strong>of</strong> reasons:<br />

• The Web page is based on data submitted by the user.<br />

For example the results pages from search engines are<br />

generated this way, and programs that process orders for e-<br />

commerce sites do this as well.<br />

• The data changes frequently. For example, a weather-<br />

report or news headlines page might build the page<br />

dynamically, perhaps returning a previously built page if it<br />

is still up to date.<br />

• The Web page uses information from corporate<br />

databases or other such sources. For example, you<br />

would use this for making a Web page at an on-line store<br />

that lists current prices and number <strong>of</strong> items in stock.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 651


Servlets<br />

• A client sends an HTTP request to the server or<br />

servlet container.<br />

• The server or servlet container receives the request<br />

and directs it to be processed by the appropriate<br />

servlet.<br />

• The servlet does its processing, which may include<br />

interacting with a database or other server-side<br />

components such as other servlets, JSPs<br />

• The servlet returns its results to the client—<br />

normally in the form <strong>of</strong> an HTML<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 652


Servlet Overview and Architecture<br />

• Servlet container (servlet engine)<br />

– Server that executes a servlet<br />

• Web servers and application servers<br />

– Sun ONE Application Server<br />

– Micros<strong>of</strong>t’s Internet Information Server (IIS)<br />

– Apache HTTP Server<br />

– BEA’s WebLogic Application Server<br />

– IBM’s WebSphere Application Server<br />

– World Wide Web Consortium’s Jigsaw Web Server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 653


Interface Servlet and the Servlet Life Cycle<br />

• Interface Servlet<br />

– All servlets must implement this interface<br />

– All methods <strong>of</strong> interface Servlet are invoked by servlet<br />

container<br />

• Servlet life cycle<br />

– Servlet container invokes the servlet’s init method<br />

– Servlet’s service method handles requests<br />

– Servlet’s destroy method releases servlet resources when<br />

the servlet container terminates the servlet<br />

• Servlet implementation<br />

– GenericServlet<br />

– HttpServlet<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 654


Interface Servlet and the Servlet Life Cycle<br />

(Cont.)<br />

• Init - This method is automatically called once<br />

during a servlet’s execution cycle to initialize the<br />

servlet.<br />

• service method - which receives the request,<br />

processes the request and sends a response to the<br />

client.<br />

• Destroy - This “cleanup” method is called when a<br />

servlet is terminated by its servlet container.<br />

Resources used by the servlet, such as an open file<br />

or an open database connection, should be<br />

deallocated here.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 655


HttpServlet Class<br />

• which defines enhanced processing capabilities for<br />

servlets that extend the functionality <strong>of</strong> a Web<br />

server.<br />

• Two most common HTTP request types<br />

– get requests - gets (or retrieves) information from a server.<br />

– post requests – posts (or sends) data to a server.<br />

• Method doGet responds to get requests<br />

• Method doPost responds to post requests<br />

• HttpServletRequest and<br />

HttpServletResponse objects<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 656


• Web server<br />

HttpServletRequest Interface<br />

– creates an HttpServletRequest object<br />

– passes it to the servlet’s service method<br />

• HttpServletRequest object contains the request from the<br />

client<br />

• Methods: getParameter, Enumeration, getCookies, getSession<br />

HttpServletResponse Interface<br />

• Web server<br />

– creates an HttpServletResponse object<br />

– passes it to the servlet’s service method<br />

• Methods: addCookie, PrintWriter, setContentType<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 657


Example 1: HTTPGetServlet.java<br />

// Creating and sending a page to the client<br />

import javax.servlet.*;<br />

import javax.servlet.http.*;<br />

import java.io.*;<br />

public class HTTPGetServlet extends HttpServlet {<br />

public void doGet( HttpServletRequest request,<br />

HttpServletResponse response )<br />

throws ServletException, IOException<br />

{<br />

PrintWriter output;<br />

response.setContentType( "text/html" ); // content type<br />

output = response.getWriter(); // get writer<br />

// create and send HTML page to client<br />

StringBuffer buf = new StringBuffer();<br />

buf.append( "\n" );<br />

buf.append( "A Simple Servlet Example\n" );<br />

buf.append( "\n" );<br />

buf.append( "Welcome to Servlets!\n" );<br />

buf.append( "" );<br />

output.println( buf.toString() );<br />

output.close(); // close PrintWriter stream<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 658


HTTPGetServlet.html<br />

<br />

<br />

Servlet HTTP GET Example<br />

<br />

<br />

<br />

Click the button to have the servlet send an HTML document<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 659


Setting Up the Apache Tomcat Server<br />

• Download Tomcat (version 4.1.27)<br />

– jakarta.apache.org/site/binindex.cgi<br />

• Define environment variables<br />

– JAVA_HOME<br />

– CATALINA_HOME<br />

• Start the Tomcat server<br />

– startup<br />

• Launch the Tomcat server<br />

– http://localhost:8080/<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 660


Setting Up the Apache Tomcat Server<br />

(Cont.).<br />

Tomcat documentation home page. (Courtesy <strong>of</strong> The Apache S<strong>of</strong>tware Foundation.)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 661


Deploying a Web Application<br />

• Web applications<br />

– JSPs, servlets and their supporting files<br />

• Deploying a Web application<br />

– Directory structure<br />

• Context root<br />

– Web application archive file (WAR file)<br />

– Deployment descriptor<br />

• web.xml<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 662


Deploying a Web Application (Cont.)<br />

Directory Description<br />

context root This is the root directory for the Web application. All the<br />

JSPs, HTML documents, servlets and supporting files such<br />

as images and class files reside in this directory or its<br />

subdirectories. The name <strong>of</strong> this directory is specified by the<br />

Web application creator. To provide structure in a Web<br />

application, subdirectories can be placed in the context root.<br />

For example, if your application uses many images, you<br />

might place an images subdirectory in this directory. The<br />

examples <strong>of</strong> this chapter use jhtp5 as the context root.<br />

WEB-INF This directory contains the Web application deployment<br />

descriptor (web.xml).<br />

WEB-INF/classes This directory contains the servlet class files and other<br />

supporting class files used in a Web application. If the<br />

classes are part <strong>of</strong> a package, the complete package directory<br />

structure would begin here.<br />

WEB-INF/lib This directory contains Java archive (JAR) files. The JAR<br />

files can contain servlet class files and other supporting class<br />

files used in a Web application.<br />

Fig. 36.8 Web application standard directories.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 663


Deployment<br />

<br />

<br />

Advanced Java How to Program JSP and Servlet <br />

<br />

This is the Web application in which we demonstrate our JSP and Servlet examples.<br />

<br />

<br />

<br />

welcome1<br />

A simple servlet that handles an HTTP get request. <br />

com.deitel.advjhtp1.servlets.WelcomeServlet <br />

<br />

<br />

<br />

welcome1<br />

/welcome1<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 664


Deploying a Web Application (Cont.)<br />

• Invoke WelcomeServlet example<br />

– /iw3htp3/welcome1<br />

• /iw3htp3 specifies the context root<br />

• /welcome1 specifies the URL pattern<br />

• URL pattern formats<br />

– Exact match<br />

• /iw3htp3/welcome1<br />

– Path mappings<br />

• /iw3htp3/example/*<br />

– Extension mappings<br />

• *.jsp<br />

– Default servlet<br />

• /<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 665


Deploying a Web Application (Cont.)<br />

WelcomeServlet Web application directory and file structure<br />

iw3htp3<br />

servlets<br />

WelcomeServlet.html<br />

WEB-INF<br />

web.xml<br />

classes<br />

WelcomeServlet.class<br />

Fig. 36.10 Web application directory and file structure for<br />

WelcomeServlet.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 666


Handling HTTP post Requests<br />

• HTTP post request<br />

– Post data from an HTML form to a server-side form handler<br />

– Browsers cache Web pages<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 667


Using cookies.<br />

import javax.servlet.*;<br />

import javax.servlet.http.*;<br />

import java.io.*;<br />

CookieExample.java<br />

public class CookieExample extends HttpServlet {<br />

private final static String names[] = { "C", "C++", "Java", "Visual Basic 6" };<br />

private final static String isbn[] = { “11", “12", “13", “14" };<br />

public void doPost( HttpServletRequest request, HttpServletResponse response )<br />

throws ServletException, IOException<br />

{<br />

PrintWriter output;<br />

String language = request.getParameter( "lang" );<br />

Cookie c = new Cookie( language, getISBN( language ) );<br />

c.setMaxAge( 120 ); // seconds until cookie removed<br />

response.addCookie( c ); // must precede getWriter<br />

response.setContentType( "text/html" );<br />

output = response.getWriter();<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 668


send HTML page to client<br />

output.println( "" );<br />

output.println( "Cookies" );<br />

output.println( "" );<br />

output.println( "Welcome to Cookies!" );<br />

output.println( "" );<br />

output.println( language );<br />

output.println( " is a great language." );<br />

output.println( "" );<br />

output.close(); // close stream<br />

}<br />

public void doGet( HttpServletRequest request, HttpServletResponse response )<br />

throws ServletException, IOException<br />

{<br />

PrintWriter output;<br />

Cookie cookies[];<br />

cookies = request.getCookies(); // get client's cookies<br />

response.setContentType( "text/html" );<br />

output = response.getWriter();<br />

output.println( "" );<br />

output.println( "Cookies II" );<br />

output.println( "" );<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 669


if ( cookies != null && cookies.length != 0 ) {<br />

output.println( "Recommendations" );<br />

// get the name <strong>of</strong> each cookie<br />

for ( int i = 0; i < cookies.length; i++ )<br />

output.println(<br />

cookies[ i ].getName() + " How to Program. " +<br />

"ISBN#: " + cookies[ i ].getValue() + "" );<br />

}<br />

else {<br />

output.println( "No Recommendations" );<br />

output.println( "You did not select a language or" );<br />

output.println( "the cookies have expired." );<br />

}<br />

output.println( "" );<br />

output.close(); // close stream<br />

}<br />

private String getISBN( String lang )<br />

{<br />

for ( int i = 0; i < names.length; ++i )<br />

if ( lang.equals( names[ i ] ) )<br />

return isbn[ i ];<br />

return ""; // no matching string found<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 670


<br />

Cookies<br />

<br />

SelectLanguage.html<br />

<br />

<br />

Select a programming language:<br />

C<br />

C++<br />

<br />

Java<br />

<br />

Visual Basic 6<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 671


Cookies<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 672


import javax.servlet.*;<br />

import javax.servlet.http.*;<br />

import java.io.*;<br />

SessionExample.java<br />

public class SessionExample extends HttpServlet {<br />

private final static String names[] = { "C", "C++", "Java", "Visual Basic 6" };<br />

private final static String isbn[] = { “11", “12", “13", “14" };<br />

public void doPost( HttpServletRequest request, HttpServletResponse response )<br />

throws ServletException, IOException<br />

{<br />

PrintWriter output;<br />

String language = request.getParameter( "lang" );<br />

// Get the user's session object.<br />

// Create a session (true) if one does not exist.<br />

HttpSession session = request.getSession( true );<br />

// add a value for user's choice to session<br />

session.putValue( language, getISBN( language ) );<br />

response.setContentType( "text/html" );<br />

output = response.getWriter();<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 673


SessionExample.java<br />

// send HTML page to client<br />

output.println( "" );<br />

output.println( "Sessions" );<br />

output.println( "" );<br />

output.println( "Welcome to Sessions!" );<br />

output.println( "" );<br />

output.println( language );<br />

output.println( " is a great language." );<br />

output.println( "" );<br />

output.close(); // close stream<br />

}<br />

public void doGet( HttpServletRequest request, HttpServletResponse response )<br />

throws ServletException, IOException<br />

{<br />

PrintWriter output;<br />

// Get the user's session object.<br />

// Don't create a session (false) if one does not exist.<br />

HttpSession session = request.getSession( false );<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 674


get names <strong>of</strong> session object's values<br />

String valueNames[];<br />

SessionExample.java<br />

if ( session != null )<br />

valueNames = session.getValueNames();<br />

else<br />

valueNames = null;<br />

response.setContentType( "text/html" );<br />

output = response.getWriter();<br />

output.println( "" );<br />

output.println( "Sessions II" );<br />

output.println( "" );<br />

if ( valueNames != null && valueNames.length != 0 ) {<br />

output.println( "Recommendations" );<br />

// get value for each name in valueNames<br />

for ( int i = 0; i < valueNames.length; i++ ) {<br />

String value = (String) session.getValue( valueNames[ i ] );<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 675


SessionExample.java<br />

output.println(<br />

valueNames[ i ] + " How to Program. " + "ISBN#: " + value + "" );<br />

}<br />

}<br />

else {<br />

output.println( "No Recommendations" );<br />

output.println( "You did not select a language" );<br />

}<br />

output.println( "" );<br />

output.close(); // close stream<br />

}<br />

private String getISBN( String lang )<br />

{<br />

for ( int i = 0; i < names.length; ++i )<br />

if ( lang.equals( names[ i ] ) )<br />

return isbn[ i ];<br />

return ""; // no matching string found<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 676


<br />

Sessions<br />

<br />

SelectLanguage.html<br />

<br />

<br />

Select a programming language:<br />

C<br />

C++<br />

Java<br />

Visual Basic 6<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 677


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 678


Multi-Tier Applications: Using JDBC from a<br />

Servlet<br />

• Three-tier distributed applications<br />

– User interface<br />

– Business logic<br />

– Database access<br />

• Web servers <strong>of</strong>ten represent the middle tier<br />

• Three-tier distributed application example<br />

– Student<br />

– Student.html<br />

– MS Access database – student.mdb<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 679


Multi-Tier Applications: Using JDBC from a<br />

Servlet (Cont.)<br />

Descriptor element Value<br />

servlet element<br />

servlet-name animalsurvey<br />

description Connecting to a database from a servlet.<br />

servlet-class com.deitel.iw3htp3.servlets.SurveyServlet<br />

init-param<br />

param-name databaseDriver<br />

param-value sun.jdbc.odbc.JdbcOdbcDriver<br />

init-param<br />

param-name databaseName<br />

param-value jdbc:odbc:animalsurvey<br />

servlet-mapping element<br />

servlet-name animalsurvey<br />

url-pattern /animalsurvey<br />

Fig. 36.22 Deployment descriptor information for servlet SurveyServlet.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 680


Displaying Student Details<br />

Student.html<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 681


import javax.servlet.http.*;<br />

import java.io.*;<br />

import javax.servlet.*;<br />

import java.sql.*;<br />

Displaying Student Details<br />

public class Student extends HttpServlet {<br />

Connection dbConn ;<br />

public void doPost(HttpServletRequest req, HttpServletResponse res)<br />

throws IOException, ServletException<br />

{<br />

try {<br />

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;<br />

dbConn = DriverManager.getConnection("jdbc:odbc:Student“ ,"","") ;<br />

}<br />

catch(ClassNotFoundException e)<br />

{ System.out.println(e); }<br />

catch(Exception e)<br />

{ System.out.println(e);}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 682


es.setContentType("text/html");<br />

PrintWriter out = res.getWriter();<br />

String mrollno = req.getParameter("rollno") ;<br />

try {<br />

PreparedStatement ps = dbConn.prepareStatement("select * from stud<br />

where rollno = ?") ;<br />

ps.setString(1, mrollno) ;<br />

ResultSet rs = ps.executeQuery() ;<br />

out.println("");<br />

out.println("");<br />

out.println("");<br />

out.println("Hello World!");<br />

out.println("");<br />

out.println("");<br />

out.println("");<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 683


while(rs.next())<br />

{<br />

out.println("Roll No. : ");<br />

out.println("" + rs.getString(1) + "");<br />

out.println("Name : ");<br />

out.println("" + rs.getString(2) +"");<br />

out.println("Branch : ");<br />

out.println("" + rs.getString(3) + "");<br />

out.println("10th Mark : ");<br />

out.println("" + rs.getString(4) + "");<br />

out.println("12th Mark : ");<br />

out.println("" + rs.getString(5) + "");<br />

}<br />

out.println("");<br />

out.println("");<br />

out.println("");<br />

}<br />

catch (Exception e)<br />

{ System.out.println(e); }<br />

}<br />

} P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 684


• Database Structure – Student.mdb<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 685


web.xml<br />

<br />


Step 1: Create ODBC connection for the database<br />

Step 2: Open Web Browser and type<br />

Step 3: http://localhost:8080<br />

Step 4: Select Tomcat Manager<br />

Step 5: Deploy the war file and Run<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 687


Outline<br />

JavaServer Pages (JSP)<br />

Introduction<br />

JavaServer Pages Overview<br />

First JavaServer Page Example<br />

Implicit Objects<br />

Scripting<br />

Scripting Components<br />

Scripting Example<br />

Standard Actions<br />

Action<br />

Action<br />

Action<br />

Directives<br />

page Directive<br />

include Directive<br />

Case Study: Guest Book<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 688


• JavaServer Pages<br />

Introduction<br />

– Extension <strong>of</strong> Servlet technology<br />

• Web content delivery<br />

• Reuse existing Java components<br />

– Without programming Java<br />

• Create custom tags<br />

– Encapsulate complex functionality<br />

• Classes and interfaces specific to JSP<br />

– Package javax.servlet.jsp<br />

– Package javax.servlet.jsp.tagext<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 689


• Key components<br />

– Directives<br />

– Actions<br />

– Scriptlets<br />

– Tag libraries<br />

JavaServer Pages Overview<br />

– JavaServer Pages (JSP) technology is the Java platform<br />

technology for delivering dynamic content to web clients in<br />

a portable, secure and well-defined way.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 690


JavaServer Pages Overview (cont.)<br />

• Directive<br />

– Message to JSP container<br />

• i.e., program that compiles/executes JSPs<br />

• They are used to set global values such as class declaration,<br />

methods to be implemented, output content type, etc.<br />

• They do not produce any output to the client.<br />

• The three directives<br />

– are page - specify page settings,<br />

– to include content from other resources and<br />

– to specify custom tag libraries for use in a JSP.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 691


• Actions<br />

JavaServer Pages Overview (cont.)<br />

– Predefined JSP tags that encapsulate functionality<br />

– Often performed based on information from client request<br />

– Can be used to create Java objects for use in scriptlets<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 692


• Scriptlet<br />

JavaServer Pages Overview (cont.)<br />

– Also called “Scripting Elements”<br />

• A scriptlet consists <strong>of</strong> one or more valid Java statements.<br />

• A scriptlet is a block <strong>of</strong> Java code that is executed at request-<br />

processing time.<br />

• A scriptlet is enclosed between "".<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 693


JavaServer Pages Overview (cont.)<br />

• Custom Tag Library<br />

– JSP’s tag extension mechanism<br />

– Enables programmers to define new tags<br />

• Tags encapsulate complex functionality<br />

– Tags can manipulate JSP content<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 694


Advantages <strong>of</strong> JSP?<br />

• vs. Active Server Pages (ASP). ASP is a similar technology from Micros<strong>of</strong>t. The<br />

advantages <strong>of</strong> JSP are tw<strong>of</strong>old. First, the dynamic part is written in Java, not Visual<br />

Basic or other MS-specific language, so it is more powerful and easier to use. Second, it<br />

is portable to other operating systems and non-Micros<strong>of</strong>t Web servers.<br />

• vs. Pure Servlets. JSP doesn't give you anything that you couldn't in principle do with a<br />

servlet. But it is more convenient to write (and to modify!) regular HTML than to have a<br />

zillion println statements that generate the HTML. Plus, by separating the look from the<br />

content you can put different people on different tasks: your Web page design experts<br />

can build the HTML, leaving places for your servlet programmers to insert the dynamic<br />

content.<br />

• vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including<br />

externally-defined pieces into a static Web page. JSP is better because it lets you use<br />

servlets instead <strong>of</strong> a separate program to generate that dynamic part. Besides, SSI is<br />

really only intended for simple inclusions, not for "real" programs that use form data,<br />

make database connections, and the like.<br />

• vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a<br />

useful capability, but only handles situations where the dynamic information is based on<br />

the client's environment. With the exception <strong>of</strong> cookies, HTTP and form submission<br />

data is not available to JavaScript. And, since it runs on the client, JavaScript can't<br />

access server-side resources like databases, catalogs, pricing information, and the like.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 695


A First JavaServer Page Example<br />

• Simple JSP example<br />

– Demonstrates<br />

• Fixed-template data (HTML markup)<br />

• Creating a Java object (java.util.Date)<br />

• Automatic conversion <strong>of</strong> JSP expression to a String<br />

• meta element to refresh Web page at specified interval<br />

– First invocation <strong>of</strong> clock.jsp<br />

• Notice the delay while:<br />

– JSP container translates the JSP into a servlet<br />

– JSP container compiles the servlet<br />

– JSP container executes the servlet<br />

• Subsequent invocations should not experience the same delay<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 696


clock.jsp<br />

<br />

<br />

A Simple JSP Example<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 697


Clock.jsp<br />

Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 698


Implicit Objects<br />

• Implicit Objects<br />

– Provide access to many servlet capabilities within a JSP<br />

– Four scopes<br />

• Application scope<br />

– Objects owned by the container application<br />

– Any servlet or JSP can manipulate these objects<br />

• Page scope<br />

– Objects that exist only in page in which they are defined<br />

– Each page has its own instance <strong>of</strong> these objects<br />

• Request scope<br />

– Objects exist for duration <strong>of</strong> client request<br />

– Objects go out <strong>of</strong> scope when response sent to client<br />

• Session scope<br />

– Objects exist for duration <strong>of</strong> client’s browsing session<br />

– Objects go out <strong>of</strong> scope when client terminates session or when<br />

session timeout occurs<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 699


Implicit object Description<br />

Application Scope<br />

application This javax.servlet.ServletContext object represents the container in which the JSP<br />

executes.<br />

Page Scope<br />

config This javax.servlet.ServletConfig object represents the JSP configuration options. As<br />

with servlets, configuration options can be specified in a Web application descriptor.<br />

exception This java.lang.Throwable object represents the exception that is passed to the JSP error<br />

page. This object is available only in a JSP error page.<br />

out This javax.servlet.jsp.JspWriter object writes text as part <strong>of</strong> the response to a<br />

request. This object is used implicitly with JSP expressions and actions that insert string content<br />

in a response.<br />

page This java.lang.Object object represents the this reference for the current JSP instance.<br />

pageContext This javax.servlet.jsp.PageContext object hides the implementation details <strong>of</strong> the<br />

underlying servlet and JSP container and provides JSP programmers with access to the implicit<br />

objects discussed in this table.<br />

response This object represents the response to the client and is normally an instance <strong>of</strong> a class that<br />

implements HttpServletResponse (package javax.servlet.http). If a protocol<br />

other than HTTP is used, this object is an instance <strong>of</strong> a class that implements<br />

javax.servlet.ServletResponse.<br />

Request Scope<br />

request This object represents the client request. The object normally is an instance <strong>of</strong> a class that<br />

implements HttpServletRequest (package javax.servlet.http). If a protocol other<br />

than HTTP is used, this object is an instance <strong>of</strong> a subclass <strong>of</strong> javax.servlet.Servlet-<br />

Request.<br />

Session Scope<br />

session This javax.servlet.http.HttpSession object represents the client session<br />

information if such a session has been created. This object is available only in pages that<br />

participate in a session.<br />

Fig. 37.2 JSP implicit objects.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 700


• Scripting<br />

Scripting<br />

– Dynamically generated content<br />

– Insert Java code and logic in JSP using scripting<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 701


Scripting Components<br />

• JSP scripting components<br />

– Scriptlets (delimited by )<br />

– Comments<br />

• JSP comments (delimited by )<br />

• HTML comments (delimited by )<br />

• Java’s comments (delimited by // and /* and */)<br />

– Expressions (delimited by )<br />

– Declarations (delimited by )<br />

Ex: <br />

– Escape sequences<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 702


Scripting Components (cont.)<br />

Literal Escape<br />

sequence<br />

Description<br />

in the response to the client.<br />

'<br />

"<br />

\<br />

\'<br />

\"<br />

\\<br />

As with string literals in a Java program, the escape<br />

sequences for characters ', " and \ allow these<br />

characters to appear in attribute values. Remember that<br />

the literal text in a JSP becomes string literals in the<br />

servlet that represents the translated JSP.<br />

Fig. 37.3 JSP escape sequences.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 703


Scripting Example<br />

• Demonstrate basic scripting capabilities<br />

– Responding to get requests<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 704


welcome.jsp<br />

<br />

<br />

Processing "get" requests with data<br />

<br />

<br />

<br />

<br />

Hello , <br />

Welcome to JavaServer Pages!<br />

<br />

<br />

<br />

Type your first name and press Submit<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 705


Outline<br />

welcome.jsp<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 706


Standard Actions<br />

• JSP standard actions<br />

– Provide access to common tasks performed in a JSP<br />

• Including content from other resources<br />

– Dynamically includes another resource in a JSP. As the JSP executes,<br />

the referenced resource is included and processed.<br />

• Forwarding requests to other resources<br />

– Forwards request processing to another JSP, servlet or static page.<br />

– This action terminates the current JSP’s execution<br />

– Allows a plug-in component to be added to a page in the form <strong>of</strong> a<br />

– browser-specific object or embed HTML element.<br />

• Interacting with JavaBeans<br />

Specifies that the JSP uses a JavaBean instance. This action specifies<br />

the scope <strong>of</strong> the bean and assigns it an ID that scripting components can use to manipulate<br />

the bean.<br />

– Sets a property in the specified JavaBean instance.<br />

Gets a property in the specified JavaBean instance and converts<br />

the result to a string for output in the response.<br />

– Delimited by and <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 707


Standard Actions<br />

Action Description<br />

Dynamically includes another resource in a JSP. As the JSP executes, the<br />

referenced resource is included and processed.<br />

Forwards request processing to another JSP, servlet or static page. This<br />

action terminates the current JSP’s execution.<br />

Allows a plug-in component to be added to a page in the form <strong>of</strong> a<br />

browser-specific object or embed HTML element. In the case <strong>of</strong> a<br />

Java applet, this action enables the downloading and installation <strong>of</strong> the<br />

Java Plug-in, if it is not already installed on the client computer.<br />

Used with the include, forward and plugin actions to specify<br />

additional name/value pairs <strong>of</strong> information for use by these actions.<br />

JavaBean Manipulation<br />

Specifies that the JSP uses a JavaBean instance. This action specifies the<br />

scope <strong>of</strong> the bean and assigns it an ID that scripting components can use<br />

to manipulate the bean.<br />

Sets a property in the specified JavaBean instance. A special feature <strong>of</strong><br />

this action is automatic matching <strong>of</strong> request parameters to bean properties<br />

<strong>of</strong> the same name.<br />

Gets a property in the specified JavaBean instance and converts the result<br />

to a string for output in the response.<br />

Fig. 37.5 JSP standard actions.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 708


Action<br />

• action<br />

– Enables dynamic content to be included in a JSP<br />

– More flexible than include directive<br />

• Requires more overhead when page contents change frequently<br />

• banner.html, toc.html, clock.jsp, include.jsp and the images<br />

directory into the jsp directory<br />

• Open your Web browser and enter the following URL to test<br />

• http://localhost:8080/jsp/include.jsp<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 709


Banner.html<br />

<br />

Internet and World Wide Web Programming<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 710


Toc.html<br />

<br />

<br />

Publications <br />

<br />

What's New <br />

<br />

Downloads/Resources <br />

<br />

FAQ (Frequently Asked Questions) <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 711


clock.jsp<br />

<br />

<br />

<br />

A Simple JSP Example<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 712


Include.jsp (Action)<br />

<br />

Using jsp:include<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 713


Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 714


Action<br />

• action<br />

– Enables JSP to forward request to different resources<br />

• Can forwarded requests only resources in same context<br />

• action<br />

– Specifies name/value pairs <strong>of</strong> information<br />

• Name/Value pairs are passed to other actions<br />

Example: forward1.jsp forwards the request to<br />

forward2.jsp<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 715


forward1.jsp<br />

<br />

Forward request to another JSP<br />

<br />

<br />

<br />

<br />

<br />


else {<br />

forward1.jsp<br />

%> <br />

<br />

Type your first name and press Submit<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 717


Forward2.jsp<br />

Processing a forwarded request<br />

<br />

<br />

Hello <br />

<br />

Your request was received and forwarded at<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 718


Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 719


Action<br />

• action<br />

– Enables JSP to manipulate Java object<br />

• Creates Java object or locates an existing object for use in JSP<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 720


Action (cont.)<br />

Attribute Description<br />

id The name used to manipulate the Java object with actions<br />

and . A variable <strong>of</strong><br />

this name is also declared for use in JSP scripting elements. The name<br />

specified here is case sensitive.<br />

scope The scope in which the Java object is accessible—page, request,<br />

session or application. The default scope is page.<br />

class The fully qualified class name <strong>of</strong> the Java object.<br />

beanName The name <strong>of</strong> a bean that can be used with method instantiate <strong>of</strong><br />

class java.beans.Beans to load a JavaBean into memory.<br />

type The type <strong>of</strong> the JavaBean. This can be the same type as the class<br />

attribute, a superclass <strong>of</strong> that type or an interface implemented by that<br />

type. The default value is the same as for attribute class. A<br />

ClassCastException occurs if the Java object is not <strong>of</strong> the type<br />

specified with attribute type.<br />

Fig. 37.13 Attributes <strong>of</strong> the action.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 721


A JavaBean that rotates advertisements.<br />

package com.jsp;<br />

public class Rotator {<br />

private String images[] = { "images/a.jpg", "images/b.jpg" };<br />

Rotator.java<br />

private String links[] = {"http://www.rec.com/a.html","http://www.rec.com/b.html“ };<br />

private int selectedIndex = 0;<br />

// returns image file name for current ad<br />

public String getImage()<br />

{<br />

return images[ selectedIndex ];<br />

}<br />

// returns the URL for ad's corresponding Web site<br />

public String getLink()<br />

{<br />

return links[ selectedIndex ];<br />

}<br />

// update selectedIndex so next calls to getImage and<br />

// getLink return a different advertisement<br />

public void nextAd()<br />

{<br />

selectedIndex = ( selectedIndex + 1 ) % images.length;<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 722


adrotator.jsp<br />

<br />

<br />

<br />

AdRotator Example<br />

<br />

<br />

<br />

<br />

AdRotator Example<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 723


Outline<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 724


Action (cont.)<br />

Attribute Description<br />

name The ID <strong>of</strong> the JavaBean for which a property (or properties) will be<br />

set.<br />

property The name <strong>of</strong> the property to set. Specifying "*" for this attribute<br />

causes the JSP to match the request parameters to the properties <strong>of</strong> the<br />

bean. For each request parameter that matches (i.e., the name <strong>of</strong> the<br />

request parameter is identical to the bean’s property name), the<br />

corresponding property in the bean is set to the value <strong>of</strong> the parameter.<br />

If the value <strong>of</strong> the request parameter is "", the property value in the<br />

bean remains unchanged.<br />

param If request parameter names do not match bean property names, this<br />

attribute can be used to specify which request parameter should be<br />

used to obtain the value for a specific bean property. This attribute is<br />

optional. If this attribute is omitted, the request parameter names must<br />

match bean property names.<br />

value The value to assign to a bean property. The value typically is the result<br />

<strong>of</strong> a JSP expression. This attribute is particularly useful for setting<br />

bean properties that cannot be set using request parameters. This<br />

attribute is optional. If this attribute is omitted, the JavaBean property<br />

must be <strong>of</strong> a type that can be set using request parameters.<br />

Fig. 37.16 Attributes <strong>of</strong> the action.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 725


• JSP directives<br />

– Messages to JSP container<br />

– Enable programmer to:<br />

• Specify page settings<br />

Directives<br />

• Include content from other resources<br />

• Specify custom-tag libraries<br />

– Delimited by <br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 726


Directives (cont.)<br />

Directive Description<br />

page Defines page settings for the JSP container to<br />

process.<br />

include Causes the JSP container to perform a translationtime<br />

insertion <strong>of</strong> another resource’s content. As the<br />

JSP is translated into a servlet and compiled, the<br />

referenced file replaces the include directive and<br />

is translated as if it were originally part <strong>of</strong> the JSP.<br />

taglib Allows programmers to define new tags in the form<br />

<strong>of</strong> tag libraries, which can be used to encapsulate<br />

functionality and simplify the coding <strong>of</strong> a JSP.<br />

Fig. 37.17 JSP directives.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 727


• JSP page directive<br />

page Directive<br />

– Specifies JSP’s global settings in JSP container<br />

Attribute Description<br />

language The scripting language used in the JSP. Currently, the only valid value<br />

for this attribute is java.<br />

extends Specifies the class from which the translated JSP will be inherited. This<br />

attribute must be a fully qualified class name.<br />

import Specifies a comma-separated list <strong>of</strong> fully qualified type names and/or<br />

packages that will be used in the current JSP. When the scripting<br />

language is java, the default import list is java.lang.*,<br />

javax.servlet.*, javax.servlet.jsp.* and<br />

javax.servlet.http.*. If multiple import properties are<br />

specified, the package names are placed in a list by the container.<br />

session Specifies whether the page participates in a session. The values for this<br />

attribute are true (participates in a session—the default) or false<br />

(does not participate in a session). When the page is part <strong>of</strong> a session,<br />

implicit object session is available for use in the page. Otherwise,<br />

session is not available, and using session in the scripting code<br />

results in a translation-time error.<br />

Fig. 37.18 Attributes <strong>of</strong> the page directive.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 728


page Directive (cont.)<br />

Attribute Description<br />

buffer Specifies the size <strong>of</strong> the output buffer used with the implicit object out. The<br />

value <strong>of</strong> this attribute can be none for no buffering, or a value such as 8kb<br />

(the default buffer size). The JSP specification indicates that the buffer used<br />

must be at least the size specified.<br />

autoFlush When set to true (the default), this attribute indicates that the output buffer<br />

used with implicit object out should be flushed automatically when the<br />

buffer fills. If set to false, an exception occurs if the buffer overflows.<br />

This attribute’s value must be true if the buffer attribute is set to none.<br />

isThreadSafe Specifies if the page is thread safe. If true (the default), the page is<br />

considered to be thread safe, and it can process multiple requests at the same<br />

time. If false, the servlet that represents the page implements interface<br />

java.lang.SingleThreadModel and only one request can be<br />

processed by that JSP at a time. The JSP standard allows multiple instances<br />

<strong>of</strong> a JSP to exists for JSPs that are not thread safe. This enables the container<br />

to handle requests more efficiently. However, this does not guarantee that<br />

resources shared across JSP instances are accessed in a thread-safe manner.<br />

info Specifies an information string that describes the page. This string is returned<br />

by the getServletInfo method <strong>of</strong> the servlet that represents the<br />

translated JSP. This method can be invoked through the JSP’s implicit page<br />

object.<br />

Fig. 37.18 Attributes <strong>of</strong> the page directive.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 729


page Directive (cont.)<br />

Attribute Description<br />

errorPage Any exceptions in the current page that are not caught<br />

are sent to the error page for processing. The error page<br />

implicit object exception references the original<br />

exception.<br />

isErrorPage Specifies if the current page is an error page that will be<br />

invoked in response to an error on another page. If the<br />

attribute value is true, the implicit object exception<br />

is created and references the original exception that<br />

occurred. If false (the default), any use <strong>of</strong> the<br />

exception object in the page results in a translationtime<br />

error.<br />

contentType Specifies the MIME type <strong>of</strong> the data in the response to<br />

the client. The default type is text/html.<br />

Fig. 37.18 Attributes <strong>of</strong> the page directive.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 730


include Directive<br />

• JSP include directive<br />

– Includes content <strong>of</strong> another resource at JSP translation time<br />

• Not as flexible as action<br />

– The include directive has only one attribute—file—that specifies<br />

the URL <strong>of</strong> the page to include.<br />

– The difference between directive include and action<br />

only if the included content changes.<br />

– For example, if the definition <strong>of</strong> an HTML document changes<br />

after it is included with directive include, future invocations <strong>of</strong><br />

the JSP will show the original content <strong>of</strong> the HTML document,<br />

not the new content.<br />

– In contrast, action is processed in each request to<br />

the JSP. Therefore, changes to included content would be<br />

apparent in the next request to the JSP that uses action<br />

.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 731


includeDirective.jsp<br />

<br />

Using the include directive<br />

<br />

<br />


Tag Libraries<br />

• Custom tag libraries define one or more custom<br />

tags that JSP implementors can use to create<br />

dynamic content.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 733


customTagWelcome.jsp<br />

<br />

<br />

<br />

Simple Custom Tag<br />

Example <br />

<br />

The following text demonstrates a custom tag:<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 734


WelcomeTagHandler.java<br />

Custom tag handler that handles a simple tag.<br />

package com.deitel.adv.jsp.taglibrary;<br />

import java.io.*;<br />

import javax.servlet.jsp.*;<br />

import javax.servlet.jsp.tagext.*;<br />

public class WelcomeTagHandler extends TagSupport {<br />

// Method called to begin tag processing<br />

public int doStartTag() throws JspException<br />

{<br />

// attempt tag processing<br />

try {<br />

// obtain JspWriter to output content<br />

JspWriter out = pageContext.getOut();<br />

// output content<br />

out.print( "Welcome to JSP Tag Libraries!" );<br />

}<br />

// rethrow IOException to JSP container as JspException<br />

catch( IOException ioException ) {<br />

throw new JspException( ioException.getMessage() );<br />

}<br />

return SKIP_BODY; // ignore the tag's body<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 735


Case Study: Guest Book<br />

Demonstrate<br />

• Enables users to place their first name, last name<br />

and e-mail address into a guest book database.<br />

• After submitting their information, users see a<br />

Web page containing all the users in the guest<br />

book. Each person’s e-mail address is displayed<br />

as a hyperlink that allows the user to send an e-<br />

mail message to the person.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 736


• GuestBean.java - JavaBean to store data for a guest in<br />

the guest book.<br />

• GuestDataBean.java - Class GuestDataBean makes a<br />

database connection and supports - inserting and<br />

retrieving data from the database.<br />

• JSPs guestBookLogin.jsp<br />

• guestBookView.jsp<br />

• The example demonstrates action . In<br />

addition, the example introduces the JSP page directive.<br />

– Action <br />

– JSP page directive<br />

– Use <strong>of</strong> JDBC<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 737


.package com.jsp.beans;<br />

GuestBean.java<br />

JavaBean to store data for a guest in the<br />

guestbook<br />

public class GuestBean {<br />

private String firstName, lastName, email;<br />

public void setFirstName( String name ) // set the guest's first name<br />

{<br />

firstName = name;<br />

}<br />

public String getFirstName() // get the guest's first name<br />

{<br />

return firstName;<br />

}<br />

public void setLastName( String name ) // set the guest's last name<br />

{<br />

lastName = name;<br />

}<br />

public String getLastName() // get the guest's last name<br />

{<br />

return lastName;<br />

}<br />

public void setEmail( String address ) // set the guest's email address<br />

{<br />

email = address;<br />

}<br />

public String getEmail() // get the guest's email address<br />

{<br />

return email;<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 738


GuestDataBean.java<br />

Class GuestDataBean makes a database connection and<br />

supports<br />

inserting and retrieving data from the database.<br />

// package com.jsp.beans;<br />

import java.io.*;<br />

import java.sql.*;<br />

import java.util.*;<br />

public class GuestDataBean {<br />

private Connection conn;<br />

private Statement st;<br />

// construct TitlesBean object<br />

public GuestDataBean() throws Exception<br />

{<br />

// load the Cloudscape driver<br />

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );<br />

// connect to the database<br />

conn = DriverManager.getConnection("jdbc:odbc:guestbook" );<br />

st= connection.createStatement();<br />

} // return an ArrayList <strong>of</strong> GuestBeans<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 739


GuestDataBean.java<br />

public List getGuestList() throws SQLException<br />

{<br />

List guestList = new ArrayList();<br />

// obtain list <strong>of</strong> titles<br />

ResultSet results = statement.executeQuery(<br />

"SELECT firstName, lastName, email FROM guests" );<br />

// get row data<br />

while ( results.next() ) {<br />

GuestBean guest = new GuestBean();<br />

guest.setFirstName( results.getString( 1 ) );<br />

guest.setLastName( results.getString( 2 ) );<br />

guest.setEmail( results.getString( 3 ) );<br />

guestList.add( guest );<br />

}<br />

return guestList;<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 740


insert a guest in guestbook database<br />

GuestDataBean.java<br />

public void addGuest( GuestBean guest ) throws SQLException<br />

{<br />

statement.executeUpdate( "INSERT INTO guests ( firstName, " +<br />

"lastName, email ) VALUES ( '" + guest.getFirstName() + "', '" +<br />

guest.getLastName() + "', '" + guest.getEmail() + "' )" );<br />

}<br />

// close statements and terminate database connection<br />

protected void finalize()<br />

{<br />

// attempt to close database connection<br />

try {<br />

st.close();<br />

conn.close();<br />

}<br />

// process SQLException on close operation<br />

catch ( SQLException sqlException ) {<br />

sqlException.printStackTrace();<br />

}<br />

}<br />

}<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 741


guestBookLogin.jsp<br />

<br />

<br />

<br />

<br />

<br />

Guest Book Login<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 742


<br />

<br />

<br />

Enter your first name, last name and email address to register in our guest book.<br />

<br />

<br />

First name<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 743


Last name<br />

guestBookLogin.jsp<br />

<br />

Email<br />

<br />

<br />

/td> <br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 744


guestBookView.jsp<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Guest List<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 745


Guest List<br />

<br />

<br />

Last name<br />

First name<br />

Email<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 746


guestBookView.jsp<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 747


Case Study: Guest Book (Cont.)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 748


Case Study: Guest Book (Cont.)<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 749


P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 750


Steps to Install<br />

&<br />

Deploy in Tomcat Server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 751


1. Select the Install file and double click.<br />

2. Click Next to Continue.<br />

Steps to Install and Deploy in Tomcat Server<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 752


3. Click I Agree.<br />

4. Check all the option and click Next.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 753


5. Delete the default path and type D:\tomcat.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 754


6. Type the admin password.<br />

7. Select the path <strong>of</strong> the JVM.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 755


8. The system displays the installation progress.<br />

9. Make check for Run Apache Tomcat and Show and uncheck Show Readme.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 756


10. The system display the following screen.<br />

11. Open the Internet Explorer and type http://localhost:8080/<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 757


12. Create the necessary Java for Servlet, HTML and MDB files and store in a directory.<br />

13. Compile the Java file.<br />

14. Create a folder WEB-INF under your folder.<br />

15. Create a folder named CLASSES under Web-INF folder and copy all your .class files.<br />

16. Copy the WEB.XML file from d:\tomcat\webapps\ROOT\WEB-INF folder to your WEB-<br />

INF folder. Edit the servlet-name, servlet-class in servlet tag and also servlet-name, urlpattern<br />

in servlet-mapping.<br />

<br />

<br />

<br />

Welcome to Tomcat<br />

<br />

Welcome to Tomcat<br />

<br />

<br />

<br />

Banking<br />

Banking<br />

<br />

<br />

Banking<br />

/Banking<br />

<br />

<br />

<br />

17. Create a war file by using the following syntax:<br />

D:\Student:>jar –cvf Banking.war .<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 758


18. Open the tomcat home and select Tomcat Manager. Type the password and click Ok.<br />

19. The system opens the following screen.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 759


20. Go to the WAR file to deploy option and select Browse.<br />

21. Select your war file and click Open.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 760


22. Click the Deploy button.<br />

23. After successful deployment you will get a screen like as below:<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 761


24. Open the browser by typing the URL as http://localhost:8080/Banking.<br />

P.Kumar, <strong>Rajalakshmi</strong> <strong>Engineering</strong> COllege 762

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!