Sunteți pe pagina 1din 151

Department Of Computer Science & Engineering

Web Technology Lab Manual

BALAJI INSTITUTE OF TECHNOLOGY AND SCIENCE


Department of computer science & engineering

In-charge !repare" by#

OD $ppro%e" & &e%ie'e" by# Issue" by#

!rincipal '(e(f Date#

WT Lab Manual

Departmrnt of Computer Science and Engineering

BALAJI INSTITUTE OF TECHNOLOGY AND SCIENCE


Laknepally(V), Narsampet, Warangal DE!$&TME)T O* COM!+TE& SCIE)CE & E),I)EE&I),

Lab Manual for the Academic Year !""#" ($n accordance %ith &N'( syllabus)
)(*&+,' )'.+AM Web 'echnology LA* ,)+

/0102

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

INDEX

S. No " ! % Lab #bjecti$e Introduction &bout Lab (uidelines to Students List of Lab E*ercises '

Contents

Page.No % ' )

'+" Syllabus ,rograms -./T01 '+! &dditional and &d$anced ,rograms

) 2 3 7

Solutions for ./T0 ,rograms Solutions for &dditional ,rograms 5i$a 6uestions 8eference Boo9s

3 24 "!! "'7

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

LAB OBJECTIVE #$er$ie: #bject Web Technologies Basic Concept of Web Technologies &d$ance ,rogramming

1) Introduction to object oriented programming concepts ja$a as an object oriented programming language+ Introduction to ja$a application and applets control structures methods arrays+ 2) #bject based and object oriented programming creating pac9ages using o$erloaded constructors static class $ariables data abstraction and information hiding relation bet:een super class objects and subclass objects composition $erses inheritance polymorphism dynamic method binding abstract super classes and concrete super classes ;inheriting interface use of inner classes and :rapper classes String to 9eni<er and String Suffer classes+ 3) 8ole of object oriented programming in designing (0I ;(raphs and .a$a!4o$er$ie: of s:ing e$ent handling= adapter classes and layout managers+ &d$ance (0I components .,opup Menus .Des9top,ane ad$ance layout managers+ 4) E*ception handling and multithreading in object oriented programming When e*ception handling should be used ja$a e*ception handling ; e*ceptions and inheritance multithreading in ja$a thread synchroni<ation daemon threads 8unnable interface >iles and streams in ja$a 5) /et:or9 and Database handling through object oriented programming ;using .#SC ;processing 6ueries o$er$ie: of ser$let ;introduction to net:or9ing ;establishing a simple ser$er and a client ; introduction to 8MI ; implementing the remote interface+

Balaji Institute of Technology and Science

'

WT Lab Manual

Departmrnt of Computer Science and Engineering

INTRODUCTION ABOUT LAB There are )) systems -Compa6 ,resario1 installed in this Lab+ Their configurations are as follo:s? ,rocessor 8&M Aard Dis9 Mouse /et:or9 Interface card Software &ll systems are configured in DUAL BOOT mode i+e+= Students can boot from Windo:s C, or Linu* as per their lab re6uirement+ This is $ery useful for students because they are familiar :ith different #perating Systems so that they can e*ecute their programs in different programming en$ironments+ Each student has a separate login for database access Oracle ! cl!ent $ersion is installed in all systems+ #n the ser$er= account for each student has been created+ This is $ery useful because students can sa$e their :or9 - scenariosD= plEs6l programs= data related projects =etc1 in their o:n accounts+ Each student :or9 is safe and secure from other students+ Latest Technologies li9e DOTNET and J"EE are installed in some systems+ Before submitting their final project= they can start doing mini project from !nd year on:ards+ #AS# $ #acro Asse%&ler ' is installed in all the systems Students can e*ecute their assembly language programs using M&SM+ M&SM is $ery useful students because :hen they e*ecute their programs they can see contents of ,rocessor Reg!sters and ho: eac( !nstr)ct!on is being e*ecuted in the CPU+ ? ? ? ? ? &MD &thelon @ "+)2 (A< !B) MB '4 (B #ptical Mouse ,resent

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

8ational 8ose Soft:are is installed in some systems+ 0sing this soft:are= students can depict 0ML diagrams of their projects+ Soft:ares installed? C= CFF= .DG"+B= M&SM= #>>ICE C,= .!EE and D#T/ET= 8ational 8ose+ Systems are pro$ided for students in the "?" ratio+ Systems are assigned numbers and same system is allotted for students :hen they do the lab+

Balaji Institute of Technology and Science

WT Lab Manual *)!+el!nes to St)+ents

Departmrnt of Computer Science and Engineering

E6uipment in the lab for the use of student community+ Students need to maintain a proper decorum in the computer lab+ Students must use the e6uipment :ith care+ &ny damage is caused is punishable+ Students are re6uired to carry their obser$ation E programs boo9 :ith completed e*ercises :hile entering the lab+ Students are supposed to occupy the machines allotted to them and are not supposed to tal9 or ma9e noise in the lab+ The allocation is put up on the lab notice board+ Lab can be used in free time E lunch hours by the students :ho need to use the systems should ta9e prior permission from the lab in charge+ Lab records need to be submitted on or before date of submission+ Students are not supposed to use floppy dis9s 0se of computer net:or9 is encouraged+

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

,e& Tec(nolog!es S-lla&)s Progra%s $JNTU' S.No " Progra%s De$elop static pages -using only ATML1 of an online Boo9 store+ The pages should resemble? :::+ama<on+com The :ebsite should consist the follo:ing pages+ Aome page= 8egistration and user Login= 0ser profile page= Boo9s catalog= Shopping cart= ,ayment By credit card= order confirmation+ 5alidate the registration= user login= user profile and payment by credit card pages using .a$aScript+ Create and sa$e an CML document at the ser$er= :hich contains "4 users information+ Write a program :hich ta9es 0ser Id as input and returns the user details by ta9ing the user information from the CML document+ Bean &ssignments a+ Create a .a$aBeans :hich gi$es the e*change $alue of I/8 -Indian 8upees1 into e6ui$alent &mericanECanadianE&ustralian Dollar $alue+ b+ Create a simple Bean :ith a label :hich is the count of number of clic9s+ Then create a Bean info class such that only the count properly is $isible in the property Windo:+ c+ Create t:o Beans ; a1 Geypad b1 Display pad+ &fter that integrate the t:o Beans to ma9e it :or9 as a calculator+ d+ Create t:o Beans Traffic Light-implemented as a label :ith only three bac9ground colors red= green= yello:1 and &utomobile-Implemented as a Te*t Bo* :hich states its stateEmo$ement1+ The state of the &utomobile should depend on the follo:ing Light Transition table+ Install T#MC&T :eb ser$er+ Con$ert the static :eb pages of assignments ! into dynamic :eb pages using ser$lets and coo9ies+ Aint? 0serDs information -user id= pass:ord= credit card number1 :ould be stored in :eb+ Cml+ Each user should ha$e a separate shopping cart+ 8edo the pre$ious tas9 using .S, by con$erting the static :eb pages of assignment ! into dynamic :eb pages+ Create a database :ith user information and boo9s information+ The boo9s catalogue should be dynamically loaded from the database+ >ollo: the M5C architecture :hile doing the :ebsite+ Implement the HAello WorldI program using .S, Struts >rame:or9+ 0sing .a$aScript sort gi$en array in ascending and descending order+

! %

'

2 3

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

,e& Tec(nolog!es Progra%s $JNTU' sol)t!ons .. De/elo0 stat!c 0ages $)s!ng onl- 1T#L' of an onl!ne Boo2 store. T(e 0ages s(o)l+ rese%&le3 www.a%a4on.co% T(e we&s!te s(o)l+ cons!st t(e follow!ng 0ages. 1o%e 0age5 Reg!strat!on an+ )ser Log!n5 User 0rof!le 0age5 Boo2s catalog5 S(o00!ng cart5 Pa-%ent B- cre+!t car+5 or+er conf!r%at!on. 1o%ePage.(t%l JATMLK JAE&DKJTITLEKAome,ageJETITLEKJEAE&DK JB#DLK This is Aome,age JA8K JM&8M0EEKWWW+B##GS+C#MJEM&8M0EEK JIM( S8CNO+Pboo9+gifO heightNO%4QO :idthNO'4QOK J,8EK J& A8E>NO+PAome,age"+htmlOKAome ,ageJE&K J& A8E>NO+P8egistration"+htmlOK8egistration 0ser LoginJE&K J& A8E>NO+P0ser,rofile"+htmlOK0ser ,rofile ,ageJE&K J& A8E>NO+PBoo9sCatalog"+htmlOKBoo9s CatalogJE&K J& A8E>NO+PShoppingCart"+htmlOKShopping CartJE&K J& A8E>NO+P,ayment"+htmlOK,ayment By CreditCardJE&K J& A8E>NO+P#rderConfirmation"+htmlOK#rder ConfirmationJE&K JE,8EK JEB#DLK JEATMLK

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

Reg!strat!on..(t%l JATMLKJAE&DKJTITLEK8egistrationJETITLEKJEAE&DK JB#DLKThis is 8egistration 0ser LoginJA8K JM&8M0EEKWWW+B##GS+C#MJEM&8M0EEK JIM( S8CNO+Pboo9+gifO heightNO%4QO :idthNO'4QOK J,8EKJ& A8E>NOPWEBSITEPAome,age+htmlOKAome ,ageJE&K J& A8E>NO+PAome,age"+htmlOKAome ,ageJE&K J& A8E>NO+P8egistration"+htmlOK8egistration 0ser LoginJE&K J& A8E>NO+P0ser,rofile"+htmlOK0ser ,rofile ,ageJE&K J& A8E>NO+PBoo9sCatalog"+htmlOKBoo9s CatalogJE&K J& A8E>NO+PShoppingCart"+htmlOKShopping CartJE&K J& A8E>NO+P,ayment"+htmlOK,ayment By CreditCardJE&K J& A8E>NO+P#rderConfirmation"+htmlOK#rder ConfirmationJE&K JE,8EKJ>#8MKJpreK Enter your /ame ?JI/,0T TL,ENOTECTOK Enter ,ass:ord ?JI/,0T TL,ENO,&SSW#8DOK Confirm ,ass:ord?JI/,0T TL,ENO,&SSW#8DOK (ender ?JInput typeNO8adioO nameNO8"O $alueNOMaleOKMale JInput typeNO8adioO nameNO8"O $alueNO>emaleOK>emale Country ?JselectKJoption selectedKSelect CountryJEoptionK Joption $alueNOindOKIndiaJEoptionK Joption $alueNOusaOK0nited States of &mericaJEoptionK Joption $alueNOengOKEnglandJEoptionK Joption $alueNOausOK&ustraliaJEoptionK Joption $alueNOgerOK(ermanyJEoptionK Joption $alueNOpolOK,olandJEoptionK JEselectKJcenterKJInput typeNOsubmitOKJinput typeNOresetOKJEcenterK JEpreKJEformKJEB#DLKJEATMLK

Balaji Institute of Technology and Science

"4

WT Lab Manual
USERLOGIN1.HTML:-

Departmrnt of Computer Science and Engineering

JhtmlKJheadKJtitleK0serLoginJEtitleKJEheadK JbodyK JIM( S8CNO+Pboo9+gifO heightNO%4QO :idthNO'4QOK J,8EKJ& A8E>NOPWEBSITEPAome,age+htmlOKAome ,ageJE&K J& A8E>NO+PAome,age"+htmlOKAome ,ageJE&K J& A8E>NO+P8egistration"+htmlOK8egistration 0ser LoginJE&K J& A8E>NO+P0ser,rofile"+htmlOK0ser ,rofile ,ageJE&K J& A8E>NO+PBoo9sCatalog"+htmlOKBoo9s CatalogJE&K J& A8E>NO+PShoppingCart"+htmlOKShopping CartJE&K J& A8E>NO+P,ayment"+htmlOK,ayment By CreditCardJE&K J& A8E>NO+P#rderConfirmation"+htmlOK#rder ConfirmationJE&K JE,8EK 0serId ? Jinput typeNOte*tO idNOuidOKJbrEK ,ass:ord? Jinput typeNOte*tO idNOp:dOKJbr EK JbrEKJinput type NObuttonO $alue NOLogInOK JEbodyKJEhtmlK

Balaji Institute of Technology and Science

""

WT Lab Manual

Departmrnt of Computer Science and Engineering

PA6#ENT..1t%l37 JATMLKJAE&DKJTITLEK,ayment By Credit CardJETITLEKJEAE&DK JB#DLK JM&8M0EEKWWW+B##GS+C#MJEM&8M0EEK JIM( S8CNO+Pboo9+gifO heightNO%4QO :idthNO'4QOK J,8EK J& A8E>NO+PAome,age"+htmlOKAome ,ageJE&K J& A8E>NO+P8egistration"+htmlOK8egistration 0ser LoginJE&K J& A8E>NO+P0ser,rofile"+htmlOK0ser ,rofile ,ageJE&K J& A8E>NO+PBoo9sCatalog"+htmlOKBoo9s CatalogJE&K J& A8E>NO+PShoppingCart"+htmlOKShopping CartJE&K J& A8E>NO+P,ayment"+htmlOK,ayment By CreditCardJE&K J& A8E>NO+P#rderConfirmation"+htmlOK#rder ConfirmationJE&K JE,8EK Jtable borderN4 cellpaddingN4 cellspacingN4 :idthN'44K JtrKJthKWhat :ill it ta9e to pay off my credit cardRJEthKJEtrK JtrKJtd alignNcenterK Jtable cellpaddingN! cellspacingN4K JtrKJtd alignNOrightOKEnter your credit card balance? SJEtdKJtdKJinput si<eN) typeNte*t idNOc:BalanceOKJEtdKJEtrK JtrKJtd alignNOrightOKEnter the credit cardTs interest rate? JEtdKJtdKJinput si<eN) typeNte*t idNOc:8ateOKQJEtdKJEtrK JtrKJtd alignNOrightOKEnter payment amount per month? SJEtdKJtdKJinput si<eN) typeNte*t idNOc:Monthly&mountOKJEtdKJEtrK JtrKJtd alignNOcenterOKJbKorJEbKJEtdKJtdK JEtdKJEtrK JtrKJtd alignNOrightOKEnter desired months until debt free? JEtdKJtdKJinput si<eN) typeNte*t idNOc:DesiredMonthsOKJEtdKJEtrK JtrKJtd colspanN! alignNOcenterOKJinput typeNbutton $alueNOCalculateO onclic9NOc:Calc-1UOKJEtdKJEtrK JtrKJtd colspanN! alignNOcenterO idNOc:8esultOKJEtdKJEtrK JEtableKJEtdKJEtrK JEtableK JEB#DLK JEATMLK

Balaji Institute of Technology and Science

"!

WT Lab Manual

Departmrnt of Computer Science and Engineering

". Val!+ate t(e reg!strat!on5 )ser log!n5 )ser 0rof!le an+ 0a-%ent &- cre+!t car+ 0ages )s!ng Ja/aScr!0t. RE*ISTRATION 8OR# JAtmlK JAeadK JTitleK0ser 8egistration >ormJEtitleK Jmeta http e6ui$NOContent TypeO contentNOte*tEhtmlU charsetNiso 33B7 "OK Jscript languageNO.a$aScriptO fptypeNOdynamicanimationOK JEscriptK JEheadK JbodyK Jscript languageNOja$ascriptOK function $erify-form1 V if-document+formsW4X+elementsW4X+$alueNNOO1 V alert-O,lease Enter 0ser IDO1U document+formsW4X+elementsW4X+focus-1U return-false1U Y if-document+formsW4X+elementsW"X+$alueNNOO1 V alert-O,lease Enter your ,ass:ordO1U document+formsW4X+elementsW!X+$alueNOOU document+formsW4X+elementsW"X+focus-1U return-false1U Y if-document+formsW4X+elementsW"X+$alue+lengthJ'1 V alert-O,ass:ord must be greater than ' characterO1U document+formsW4X+elementsW"X+$alueNOOU document+formsW4X+elementsW!X+$alueNOOU document+formsW4X+elementsW"X+focus-1U return-false1U Y if-document+formsW4X+elementsW!X+$alueNNOO1 V alert-O,lease Enter your Confirm ,ass:ordO1U document+formsW4X+elementsW!X+focus-1U return-false1U Y if--document+formsW4X+elementsW"X+length1ZN-document+formsW4X+elementsW!X+length11 V alert-OLour ,ass:ord does not match :ith Confirm ,ass:ordO1U document+formsW4X+elementsW"X+$alueNOOU Balaji Institute of Technology and Science "%

WT Lab Manual document+formsW4X+elementsW!X+$alueNOOU document+formsW4X+elementsW"X+focus-1U return-false1U Y

Departmrnt of Computer Science and Engineering

if--document+formsW4X+elementsW"X+length1NN-document+formsW4X+elementsW!X+length11 V if--document+formsW4X+elementsW"X+$alue1ZN-document+formsW4X+elementsW!X+$alue11 V alert-OLour ,ass:ord does not match :ith Confirm ,ass:ordO1U document+formsW4X+elementsW"X+$alueNOOU document+formsW4X+elementsW!X+$alueNOOU document+formsW4X+elementsW"X+focus-1U return-false1U Y Y if-document+formsW4X+elementsW%X+$alueNNOsec6O1 V alert-O,lease Select your Security MuestionO1U document+formsW4X+elementsW%X+focus-1U return-false1U Y if-document+formsW4X+elementsW'X+$alueNNOO1 V alert-O,lease &ns:er the security 6uestionO1U document+formsW4X+elementsW'X+focus-1U return-false1U Y if-document+formsW4X+elementsWBX+$alueNNODDO1 V alert-O,lease select Day of D#BO1U document+formsW4X+elementsWBX+focus-1U return-false1U Y if-document+formsW4X+elementsW)X+$alueNNOMMO1 V alert-O,lease select Month of D#BO1U document+formsW4X+elementsW)X+focus-1U return-false1U Y if-document+formsW4X+elementsW2X+$alueNNOLLLLO1 V alert-O,lease select Lear of D#BO1U document+formsW4X+elementsW2X+focus-1U return-false1U Balaji Institute of Technology and Science "'

WT Lab Manual

Departmrnt of Computer Science and Engineering

Y if-document+formsW4X+elementsW"4X+chec9edNNtrue1 V if-document+formsW4X+elementsW""X+$alueNNOO1 V alert-O,lease Enter your >irst /ameO1U document+formsW4X+elementsW""X+focus-1U return-false1U Y if-document+formsW4X+elementsW"%X+$alueNNOO1 V alert-O,lease Enter your Last /ameO1U document+formsW4X+elementsW"%X+focus-1U return-false1U Y if-document+formsW4X+elementsW"'X+$alueNNOLearO1 V alert-O,lease specify your &cademic yearO1U document+formsW4X+elementsW"'X+focus-1U return-false1U Y if-document+formsW4X+elementsW"BX+$alueNNOadminyearO1 V alert-O,lease put your &dmission LearO1U document+formsW4X+elementsW"BX+focus-1U return-false1U Y if-document+formsW4X+elementsW")X+$alueNNOdeptO1 V alert-O,lease Select your DepartmentO1U document+formsW4X+elementsW")X+focus-1U return-false1U Y if-document+formsW4X+elementsW"2X+$alueNNOO1 V alert-O,lease put your 8oll /umberO1U document+formsW4X+elementsW"2X+focus-1U return-false1U Y if-document+formsW4X+elementsW"2X+$alueZNOO1 V r4Ndocument+formsW4X+elementsW"2X+$alue+inde*#f-T!T1U r"Ndocument+formsW4X+elementsW"2X+$alue+inde*#f-T9T1U r!NparseInt-document+formsW4X+elementsW"2X+$alue+char&t-!11U r%Ndocument+formsW4X+elementsW"2X+$alue+inde*#f-T[T1U r'NparseInt-document+formsW4X+elementsW"2X+$alue+char&t-'11U Balaji Institute of Technology and Science "B

WT Lab Manual

Departmrnt of Computer Science and Engineering

rBNparseInt-document+formsW4X+elementsW"2X+$alue+char&t-B11U r)NparseInt-document+formsW4X+elementsW"2X+$alue+char&t-)11U lenNdocument+formsW4X+elementsW"2X+$alue+lengthU if--lenZN21\\-r4ZN41\\-r"ZN"1\\-r%ZN%1\\-r'K) \\ r'J"1\\-r!K7 \\ r!J"11 V alert-O/ot a $alid 8oll /umberO1U document+formsW4X+elementsW"2X+focus-1U return-false1 Y s:itch-document+formsW4X+elementsW")X+$alue1 V case TITT? if-r'ZN)\\-rBK'\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of Information TechnologyO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U case TCSET? if-r'ZNB\\-rBK'\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of Computer ScienceO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U case TECET? if-r'ZN'\\-rBK'\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of ElectronicsO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U case TEET? if-r'ZN!\\-rBK)\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of Electrical EngineeringO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U case TMET? if-r'ZN%\\-rBK)\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of Mechanical EngineeringO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U Balaji Institute of Technology and Science ")

WT Lab Manual

Departmrnt of Computer Science and Engineering

case TCET? if-r'ZN"\\-rBK)\\rBJ41\\-r)K7\\r)J411 V alert-O/ot a $alid 8oll /umber of Ci$il EngineeringO1U document+formsW4X+elementsW")X+focus-1U return-false1 Ybrea9U Y s:itch-document+formsW4X+elementsW"'X+$alue1 V case T>ourthT? if-r!ZN"\\-r!K7\\r!J411 V alert-O/ot a $alid 8oll /umber of >orth LearO1U document+formsW4X+elementsW"'X+focus-1U return-false1 Ybrea9U case TThirdT? if-r!ZN!\\-r!K7\\r!J411 V alert-O/ot a $alid 8oll /umber of Third LearO1U document+formsW4X+elementsW"'X+focus-1U return-false1 Ybrea9U case TSecondT? if-r!ZN%\\-r!K7\\r!J411 V alert-O/ot a $alid 8oll Second LearO1U document+formsW4X+elementsW"'X+focus-1U return-false1 Ybrea9U case T>irstT? if-r!ZN'\\-r!K7\\r!J411 V alert-O/ot a $alid 8oll /umber of >irst LearO1U document+formsW4X+elementsW"'X+focus-1U return-false1 Ybrea9U Y Y Y if-document+formsW4X+elementsW!'X+chec9edNNtrue1 V if-document+formsW4X+elementsW!BX+$alueNNOO1 V Balaji Institute of Technology and Science "2

WT Lab Manual

Departmrnt of Computer Science and Engineering

alert-O,lease Enter Lour >irst /ameO1U document+formsW4X+elementsW!BX+focus-1U return-false1U Y if-document+formsW4X+elementsW!2X+$alueNNOO1 V alert-O,lease Enter your Last /ameO1U document+formsW4X+elementsW!2X+focus-1U return-false1U Y if-document+formsW4X+elementsW%4X+$alueNNOstateO1 V alert-O,lease Select Lour StateO1U document+formsW4X+elementsW%4X+focus-1U return-false1U Y if-document+formsW4X+elementsW%"X+$alueNNOcountryO1 V alert-O,lease Select Lour StateO1U document+formsW4X+elementsW%"X+focus-1U return-false1U Y if--document+formsW4X+elementsW%!X+$alueNNOpincodeO1\\ -document+formsW4X+elementsW%%X+$alueNOO11 V alert-O,lease Select Lour ,incode #8 if #ther specifyO1U document+formsW4X+elementsW%!X+focus-1U return-false1U Y Y Y function studentclear-form1 V document+formsW4X+elementsW""X+$alueNOOU document+formsW4X+elementsW"!X+$alueNOOU document+formsW4X+elementsW"%X+$alueNOOU document+formsW4X+elementsW"'X+$alueNOLearOU document+formsW4X+elementsW"BX+$alueNOadminyearOU document+formsW4X+elementsW")X+$alueNOdeptOU document+formsW4X+elementsW"2X+$alueNOOU document+formsW4X+elementsW"3X+$alueNOselecthostelOU document+formsW4X+elementsW"7X+$alueNOOU document+formsW4X+elementsW!4X+$alueNOOU document+formsW4X+elementsW!"X+$alueNOOU document+formsW4X+elementsW!!X+$alueNOOU document+formsW4X+elementsW!%X+$alueNOOU Balaji Institute of Technology and Science "3

WT Lab Manual

Departmrnt of Computer Science and Engineering

document+formsW4X+elementsW!BX+focus-1U Y function generalclear-form1 V document+formsW4X+elementsW!BX+$alueNOOU document+formsW4X+elementsW!)X+$alueNOOU document+formsW4X+elementsW!2X+$alueNOOU document+formsW4X+elementsW!3X+$alueNOCityOU document+formsW4X+elementsW!7X+$alueNOOU document+formsW4X+elementsW%4X+$alueNOstateOU document+formsW4X+elementsW%"X+$alueNOcountryOU document+formsW4X+elementsW%!X+$alueNOpincodeOU document+formsW4X+elementsW%%X+$alueNOOU document+formsW4X+elementsW%'X+$alueNOOU document+formsW4X+elementsW%BX+$alueNOOU document+formsW4X+elementsW%)X+$alueNOOU document+formsW4X+elementsW%2X+$alueNOOU document+formsW4X+elementsW""X+focus-1U Y JEscriptK Jbody onLoadNdocument+ forms W4X+elementsW4X+focus-1UK Jform action NOO methodNOpostO onsubmitNOreturn $erify-this+form1OK JcenterK Jp dynamicanimationNOfp&nimelastic8ight>,"O idNOfp&nimelastic8ight>,"O styleNOposition? relati$e ZimportantU $isibility? hiddenO languageNO.a$ascript"+!OK Jfont si<eNO)O faceNOMonotype Corsi$aO colorNO]344444OK/e: 0ser 8egistration >ormJEfontKJEpK JEcenterKJbrK JiKJfont colorNO]>>4444OKJfont faceNO&rialO si<eNO'OK^JEfontK Jfont faceNO&rialO si<eNO!OKJEfontK JEfontK Jfont faceNO&rialO si<eNO!OcolorNO]>>4444OKIndicates all the fields are mandatoryJEfontKJEiKJhrKJpK Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont si<eNO "O faceNO&rial= Ael$etica=sans serifOK0ser ID?JEfontKJbK Jinput nameNOuidO si<eNO")O ma*lengthNO"BOKJEbK Jfont colorNO]>>44%%O si<eNO!OK JEfontK Jfont faceNO&rialO si<eNO!O colorNO]>>44%%OK -Contains only letters -a <1= numbers -4 71 and underscore1JEfontK JbrKJbKJbrKJEbK Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont si<eNO "O faceNO&rial= Ael$etica=sans serifOK,ass:ord? JEfontK Jfont colorNO]444444OK JEfontK Jinput typeNOpass:ordO nameNOps:dO si<eNO7O ma*lengthNO"4O styleNOfont :eight? boldU color?]443444OK Jfont colorNO]444444Osi<eNO!OKJfont faceNO&rialOK JEfontKJEfontK Jfont colorNO]>>44%%O si<eNO!O faceNO&rialOK Balaji Institute of Technology and Science "7

WT Lab Manual

Departmrnt of Computer Science and Engineering

-,ass:ord should be of minimum '-four1 and ma*imum "4-ten1 characters+ JEfontK Jfont si<eNO!OKJfont colorNO]>>44%%O faceNO&rialOK1 JEfontKJEpK JEfontK Jfont faceNO&8I&L= AEL5ETIC&O si<eNO "OK JEfontK Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont faceNO&8I&L= AEL5ETIC&O si<eNO "OK8e Type ,ass:ordJEfontK?Jfont si<eNO!OK JbK Jinput typeNOpass:ordO nameNOcps:dO si<eNO""O ma*lengthNO"4O styleNOcolor?]443444O KJEbK JEfontK JhrKJfont si<eNO!OK JEfontKJfont si<eNO!OK Jfont faceNO&rial= Ael$etica= sans serifOK JiKIf you forget your pass:ord= you can retrie$e it by ans:ering your uni6ue hint 6uestion+ JbrK >rame your 6uestion such that only you 9no: its ans:er+ JEiKJEfontKJEfontK Jdi$ alignNOcenterOKJfont faceNO&rialO si<eNO!OKJEfontK Jfont faceNO&rialO si<eNO!OKJEfontKJfont faceNO&rialO si<eNO!OKJEfontK Jfont faceNO&rialO si<eNO!OKJEfontKJEdi$K Jfont faceNO&rialO si<eNO!OKJbrKJEfontK Jfont si<eNO!OKJfont faceNO&rialOK Jfont faceNO&rial=Ael$etica= sans serifOKJEfontKJEfontKJEfontK Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont si<eNO!OKJfont faceNO&rialOK Jfont faceNO&rial= Ael$etica=sans serifOK Select a 6uestion forgetting pass:ord JEfontKJbK? JEbKJEfontK Jselect si<eNO"O nameNOsec6OK Joption selected $alueNOsec6OKSelect a hit 6uestion RJEoptionK Joption $alueNOWhat is your fa$ourite multimedia soft:areROK What is your fa$ourite multimedia soft:areRJEoptionK Joption $alueNOWhat is your fa$ourite gameROKWhat is your fa$ourite gameRJEoptionK Joption $alueNO:ho is your fa$ourite Cric9eterROK :ho is your fa$ourite Cric9eterR JEoptionK Joption $alueNO:ho is your fa$ourite >ilm &ctorROK :ho is your fa$ourite >ilm &ctorRJEoptionK Joption $alueNO:hat is your fa$ourite foodROK:hat is your fa$ourite foodRJEoptionK JEselectKJfont faceNO&rialOKJbK JEbKJEfontKJEfontKJpKJfont si<eNO!OK JEfontKJfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont colorNO]444444O faceNO&rial= Ael$etica= sans serifO si<eNO!OKAit &ns:er? Jinput typeNOte*tO nameNOsecaO si<eNO!"OK JEfontK Jfont si<eNO!OKJfont colorNO]444444O faceNO&rial= Ael$etica=sans serifOK JEfontK Jfont colorNO]>>44%%O faceNO&rialOK-Write Lour &ns:er1JEfontK Jfont colorNO]444444O faceNO&rialOK JEfontK JEfontKJfont si<eNO"OKJEpKJEfontK JhrKJdi$ alignNOleftOKJfont faceNO&rial=Ael$etica= sans serifO si<eNO "OK DD MM LLLL JbrK JEfontK Balaji Institute of Technology and Science !4

WT Lab Manual

Departmrnt of Computer Science and Engineering

Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifOcolorNO]>>4444OK^JEfontK Jfont faceNO&rial= Ael$etica= sans serifO si<eNO "OKDate of Birth? Jselect si<eNO"O nameNODDOK Joption selected $alueNODDOKDayJEoptionK Joption $alueNO"OK"JEoptionK Joption $alueNO!OK!JEoptionK Joption $alueNO%OK%JEoptionK Joption $alueNO'OK'JEoptionK Joption $alueNOBOKBJEoptionK Joption $alueNO)OK)JEoptionK Joption $alueNO2OK2JEoptionK Joption $alueNO3OK3JEoptionK Joption $alueNO7OK7JEoptionK Joption $alueNO"4OK"4JEoptionK Joption $alueNO""OK""JEoptionK Joption $alueNO"!OK"!JEoptionK Joption $alueNO"%OK"%JEoptionK Joption $alueNO"'OK"'JEoptionK Joption $alueNO"BOK"BJEoptionK Joption $alueNO")OK")JEoptionK Joption $alueNO"2OK"2JEoptionK Joption $alueNO"3OK"3JEoptionK Joption $alueNO"7OK"7JEoptionK Joption $alueNO!4OK!4JEoptionK Joption $alueNO!"OK!"JEoptionK Joption $alueNO!!OK!!JEoptionK Joption $alueNO!%OK!%JEoptionK Joption $alueNO!'OK!'JEoptionK Joption $alueNO!BOK!BJEoptionK Joption $alueNO!)OK!)JEoptionK Joption $alueNO!2OK!2JEoptionK Joption $alueNO!3OK!3JEoptionK Joption $alueNO!7OK!7JEoptionK Joption $alueNO%4OK%4JEoptionK Joption $alueNO%"OK%"JEoptionK JEselectK Jselect si<eNO"O nameNOMMOK Joption selected $alueNOMMOKMonthJEoptionKJoption $alueNO.&/0&8LOK.&/JEoptionK Joption $alueNO>EB80&8LOK>EBJEoptionK Joption $alueNOM&8CAOKM&8JEoptionK Joption $alueNO&,8ILOK&,8JEoptionK Joption $alueNOM&LOKM&LJEoptionK Joption $alueNO.0/EOK.0/JEoptionK Joption $alueNO.0LLOK.0LJEoptionK Joption $alueNO&0(0STOK&0(JEoptionK Joption $alueNOSE,TEMBE8OKSE,JEoptionK Joption $alueNO#CT#BE8OK#CTJEoptionKJoption $alueNO/#5EMBE8OK/#5JEoptionK Joption $alueNODECEMBE8OKDECJEoptionK JEselectK Jselect si<eNO"O nameNOLLLLOK Joption selected $alueNOLLLLOKLearJEoptionK Joption $alueNO"724OK"724JEoptionK Joption $alueNO"72"OK"72"JEoptionK Joption $alueNO"72!OK"72!JEoptionK Joption $alueNO"72%OK"72%JEoptionK Joption $alueNO"72'OK"72'JEoptionK Joption $alueNO"72BOK"72BJEoptionK Joption $alueNO"72)OK"72)JEoptionK Joption $alueNO"722OK"722JEoptionK Joption $alueNO"723OK"723JEoptionK Joption $alueNO"727OK"727JEoptionK Joption $alueNO"734OK"734JEoptionK Joption $alueNO"73"OK"73"JEoptionK Joption $alueNO"73!OK"73!JEoptionK Joption $alueNO"73%OK"73%JEoptionK Joption $alueNO"73'OK"73'JEoptionK Joption $alueNO"73BOK"73BJEoptionK Joption $alueNO"73)OK"73)JEoptionK Joption $alueNO"732OK"732JEoptionK Joption $alueNO"733OK"733JEoptionK Joption $alueNO"737OK"737JEoptionK Joption $alueNO"774OK"774JEoptionK Joption $alueNO"77"OK"77"JEoptionK Joption $alueNO"77!OK"77!JEoptionK Joption $alueNO"77%OK"77%JEoptionK Joption $alueNO"77'OK"77'JEoptionK Joption $alueNO"77BOK"77BJEoptionK Joption $alueNO"77)OK"77)JEoptionK Joption $alueNO"77)OK"77)JEoptionK Joption $alueNO"772OK"772JEoptionK Balaji Institute of Technology and Science !"

WT Lab Manual

Departmrnt of Computer Science and Engineering

Joption $alueNO"773OK"773JEoptionK Joption $alueNO"777OK"777JEoptionK Joption $alueNO!444OK!444JEoptionK Joption $alueNO!44"OK!44"JEoptionK Joption $alueNO!44!OK!44!JEoptionK Joption $alueNO!44%OK!44%JEoptionK Joption $alueNO!44'OK!44'JEoptionK Joption $alueNO!44BOK!44BJEoptionK Joption $alueNO!44)OK!44)JEoptionK Joption $alueNO!442OK!442JEoptionK Joption $alueNO!443OK!443JEoptionK Joption $alueNO!447OK!447JEoptionK Joption $alueNO!4"4OK!4"4JEoptionK Joption $alueNO!4""OK!4""JEoptionK Joption $alueNO!4"!OK!4"!JEoptionK Joption $alueNO!4"%OK!4"%JEoptionK Joption $alueNO!4"'OK!4"'JEoptionK Joption $alueNO!4"BOK!4"BJEoptionK Joption $alueNO!4")OK!4")JEoptionK Joption $alueNO!4"2OK!4"2JEoptionK Joption $alueNO!4"3OK!4"3JEoptionK Joption $alueNO!4"7OK!4"7JEoptionK Joption $alueNO!4!4OK!4!4JEoptionK JEselectKJEfontKJEdi$K JpKJfont faceNO&rial= Ael$etica= sans serifO si<eNO "OKJEfontK Jfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK^JEfontK Jfont faceNO&rial= Ael$etica= sans serifO si<eNO "OK (ender? Jinput typeNOradioO nameNOMaleO $alueNOmaleO chec9edK Male JI/,0T typeNradio $alueNOfemaleO nameNOMaleO K>emale JEfontK JhrKJfont si<eNO'O faceNO&rial= Ael$etica= sans serifO colorNO]>>4444OK JEfontK JpK Jfont faceNO&rial= Ael$etica= sans serifO si<eNO "OK Contact /o? Jinput typeNOte*tO si<eNO)O nameNOstdO ma*lengthNO)OKJbK JEbK Jinput typeNOte*tO si<eNO7O nameNOphnoO ma*lengthNO7OK JEfontKJpK Jfont faceNO&rial= Ael$etica= sans serifO si<eNO "OK Mobile /o? Jinput typeNOte*tO si<eNO"3O nameNOmobnoO ma*lengthNO"BOKJEfontKJpK Jfont faceNO&rial= Ael$etica= sans serifO si<eNO "OK Email?Jinput typeNOte*tO nameNOemailO si<eNO!4OK JEfontKJpKJhrK Jdi$ alignNOleftOKJfont faceNO&rial= Ael$etica= sans serifO si<eNO "OK JEfontK JEdi$KJcenterK Jinput typeNOsubmitO $alueNOSubmitO nameNOSubmitO tabinde*NO!BOK Jinput typeNOresetO $alueNO8esetO nameNO8esetO tabinde*NO!)OK JEcenterKJEformKJEbodyKJEhtmlK

Balaji Institute of Technology and Science

!!

WT Lab Manual OUTPUT37

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

!%

WT Lab Manual

Departmrnt of Computer Science and Engineering

USER LO*IN37 JhtmlKJheadKJscriptK function f-1 V if-document+getElementById-TuidT1+$alue+lengthNN41 alert-O0serId canTt e blan9O1U else if-document+getElementById-Tp:dT1+$alue+lengthNN41 alert-O,ass:ord canTt be blan9O1U else V alert-Osuccessfully logged inO1U :indo:+open-OWelcome+htmlO1UY Y JEscriptKJEheadK JbodyK 0serId ? Jinput typeNOte*tO idNOuidOKJbrEK ,asso:rd? Jinput typeNOte*tO idNOp:dOKJbr EK JbrEKJinput type NObuttonO onclic9NOf-1O $alue NOLogInOK JEbodyKJEhtmlK OUTPUT37

Balaji Institute of Technology and Science

!'

WT Lab Manual USER PRO8ILE JhtmlK JheadK JtitleK0ser ,rofileJEtitleK JEheadK JbodyK JA"K0ser ,rofileJEA"K JArK JpreK JbK/ame ? JEbK JbKE Mail ID ? JEbK JbKDate of Birth ? JEbK JbK(ender ? JEbK JbKCity ? JEbK JbKState ? JEbK JbKCountry ? JEbK JbK_ip Code ? JEbK JEpreK JEBodyK JEAtmlK OUTPUT37

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

!B

WT Lab Manual PA6#ENT B6 CREDIT CARD

Departmrnt of Computer Science and Engineering

JATMLKJAE&DK Jstyle typeNOte*tEcssOK JZ +c:CalcVborder?"p* solid blac9UY +c:Calc TAVbac9ground color?]BBBBBBUY +c:Calc TDVbac9ground color?]DDDDDDUY K JEstyleK Jscript typeNOte*tEja$ascriptOK function c:Calc-1 V if -c:Balance+$alueNNTT1 V alert-T,lease enter your credit card balance+T1U returnUY if -c:8ate+$alueNNTT1 V alert-T,lease enter your credit cardPTs interest rate+T1U returnUY if - -c:Monthly&mount+$alueNNTT `` c:DesiredMonths+$alueNNTT1 \\ -c:Monthly&mount+$alueZNTT `` c:DesiredMonths+$alueZNTT1 1 V alert-T,lease enter either a payment amount or desired months+T1U returnUY $ar m8ateN-c:8ate+$alueE"441E"!U if -c:Monthly&mount+$alueNNTT1 V $ar paymentNc:Balance+$alue^-m8ate1 E -" Math+po:--"Fm8ate1=- c:DesiredMonths+$alue11 1U paymentNMath+round-payment^"441E"44U c:8esult+innerATMLNOIt :ill cost SO F payment+to>i*ed-!1 F O a month to pay off this card and :ill cost you a total of SO F -payment^c:DesiredMonths+$alue1+to>i*ed-!1 F O+OU Y else V $ar remainingBalanceNc:Balance+$alueU $ar min,aymentNm8ate^c:Balance+$alueU $ar monthsN4U $ar last,aymentU if -min,aymentKc:Monthly&mount+$alue1 V alert -TLour monthly payment is less than the monthly interest charged by this card+T1U returnUY :hile -remainingBalanceK41 V monthsFFU remainingBalanceNremainingBalance^-" F m8ate1 c:Monthly&mount+$alueU Y c:8esult+innerATMLNOIt :ill ta9e O F months F O months to pay off this card and :ill cost you a total of SO F -c:Monthly&mount+$alue^months1+to>i*ed-!1 F O+OU Y Balaji Institute of Technology and Science !)

WT Lab Manual Y JEscriptK JEAE&DK JB#DLK

Departmrnt of Computer Science and Engineering

Jtable classNOc:CalcO borderN4 cellpaddingN4 cellspacingN4 :idthN'44K JtrKJthKWhat :ill it ta9e to pay off my credit cardRJEthKJEtrK JtrKJtd alignNcenterK Jtable cellpaddingN! cellspacingN4K JtrKJtd alignNOrightOKEnter your credit card balance? SJEtdKJtdKJinput si<eN) typeNte*t idNOc:BalanceOKJEtdKJEtrK JtrKJtd alignNOrightOKEnter the credit cardTs interest rate? JEtdKJtdKJinput si<eN) typeNte*t idNOc:8ateOKQJEtdKJEtrK JtrKJtd alignNOrightOKEnter payment amount per month? SJEtdKJtdKJinput si<eN) typeNte*t idNOc:Monthly&mountOKJEtdKJEtrK JtrKJtd alignNOcenterOKJbKorJEbKJEtdKJtdK JEtdKJEtrK JtrKJtd alignNOrightOKEnter desired months until debt free? JEtdKJtdKJinput si<eN) typeNte*t idNOc:DesiredMonthsOKJEtdKJEtrK JtrKJtd colspanN! alignNOcenterOKJinput typeNbutton $alueNOCalculateO onclic9NOc:Calc-1UOKJEtdKJEtrK JtrKJtd colspanN! alignNOcenterO idNOc:8esultOKJEtdKJEtrK JEtableKJEtdKJEtrK JtrK Jtd alignNright $alignNtop styleNOfont :eight?boldUfont si<e?3ptUOK ,o:ered by?Ja hrefNOhttp?EE:::+creditor:eb+comECreditCardsEO targetNO[blan9OK Jimg srcNOhttp?EE:::+creditor:eb+comEimagesElogosmall+pngO borderNO4O altNOCompare credit cards and find credit card tools and articles at Creditor WebOKJEaKJEtdKJEtrK JEtableK JpKJcenterK Jfont faceNOarial= hel$eticaO si<eO !OK>ree .a$aScripts pro$idedJbrKby Ja hrefNOhttp?EEsrinu+comOKThe .a$aScript SourceJEaKJEfontK JEcenterKJpK JEB#DLK JEATMLK

Balaji Institute of Technology and Science

!2

WT Lab Manual OUTPUT37

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

!3

WT Lab Manual

Departmrnt of Computer Science and Engineering

3.Create and save an XML document at the server. Which contains user information or Progra% To I%0le%ent Log!nfor% Us!ng Se/lets PRO*RA# f!lena%e3Use/al!+at!on.(t%l JhtmlK JheadK JtitleKLogin >ormJEtitleK JEheadK Jbody leftmarginN2B topmarginN2B bgcolorN:hiteK Jform methodN(et actionNOhttp?"!2+4+4+"?3434P$alidationOK 0ser/ame`nbspU`nbspUJinput typeNte*t nameNuserK JbrKJbrK ,ass:ord`nbspU`nbspUJinput typeNpass:ord nameNpassK JbrKJbrK Jinput typeNsubmit $alueNsubmitK`nbspU`nbspU`nbspU Jinput typeNreset nameNcancelK JEformK JEbodyK JEhtmlK f!lena%e3UserVal!+.9a/a import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U import ja$a+io+^U public class 0ser5alid e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6=AttpSer$let8esponse res1thro:s Ser$letE*ception=I#E*ception V res+setContentType-Ote*tEhtmlO1U ,rintWriter outNres+getWriter-1U String usrNres+get,arameter-OuserO1U String p:dNres+get,arameter-OpassO1U if-usr e6uals-Ona$eenO1``p:d e6uals-Ona$O1 out+println-OSuccessfully Logged inO1U else out+println-O0nsuccessfulO1U Y Y Balaji Institute of Technology and Science !7

WT Lab Manual f!lena%e3we&.:%l

Departmrnt of Computer Science and Engineering

J:eb appK Jser$letK Jser$let nameK0ser5alidationJEser$let nameK Jser$let classK0ser5alidJEser$let classK JEser$letK Jser$let mappingK Jser$let nameK0ser5alidationJEser$let nameK Jurl patternKE$alidationE^JEurl patternK JEser$let mappingK JE:eb appK

Balaji Institute of Technology and Science

%4

WT Lab Manual

Departmrnt of Computer Science and Engineering

'+De/elo0!ng a S!%0le Bean Us!ng t(e BD;3 This section presents an e*ample that sho:s ho: to de$elop a simple Bean and connect it to other components $ia the BDG+ #ur ne: component is called the Colors Bean+ It appears as either a rectangle or ellipse that is filled :ith a color+ & color is chosen at random :hen the Bean begins e*ecution+ & public method can be in$o9ed to change it+ Each time the mouse is clic9ed on the Bean= another random color is chosen+ There is one &oolean readE:rite property that determines the shape+ The BDG is used to lay out an application :ith one instance of the Colors Bean and one instance of the O)rB)tton Bean+ The button is labeled HChange+I Each time it is pressed= the color changes+ S#>TW&8E DE5EL#,ME/T 0SI/( .&5& Create a /e: Bean Aere are the steps that you must follo: to create a ne: Bean? "+ Create a directory for the ne: Bean+ !+ Create the .a$a source file-s1+ %+ Compile the source file-s1+ '+ Create a manifest file+ B+ (enerate a .&8 file+ )+ Start the BDG+ 2+ Test+ The follo:ing sections discuss each of these steps in detail+ Create a Directory for the /e: Bean Lou need to ma9e a directory for the Bean+ To follo: along :ith this e*ample= create c3<&+2<+e%o<s)nw<+e%o<colors+ Then change to that directory+ The Colors and #urButton Beans Create the Source >ile for the /e: Bean The source code for the Colors component is sho:n in the follo:ing listing+ It is located in the file Colors.9a/a+ The !%0ort statement at the beginning of the file places it in the pac9age named s)nw.+e%o.colors+ 8ecall from Chapter 7 that the directory hierarchy corresponds to the pac9age hierarchy+ Therefore= this file must be located in a subdirectory named s)nw<+e%o<colors relati$e to the CLASSPAT1 en$ironment $ariable+ The color of the component is determined by the pri$ate Color $ariable color= and its shape is determined by the pri$ate &oolean $ariable rectang)lar+ The constructor defines an anonymous inner class that e*tends #o)seA+a0ter and o$errides its %o)sePresse+$ ' method+ The c(ange$ ' method is in$o9ed in response to mouse presses+ The component is initiali<ed to a rectangular shape of !44 by "44 pi*els+ The c(ange$ ' method is in$o9ed to select a random color and repaint the component+ The getRectang)lar$ ' and setRectang)lar$ ' methods pro$ide access to the one property of this Bean+ The c(ange$ ' method calls ran+o%Color$ ' to choose a color and then calls re0a!nt$ ' to ma9e the change $isible+ /otice that the 0a!nt$ ' method uses the rectang)lar and color $ariables to determine ho: to present the Bean+ EE & simple Bean+ pac9age sun:+demo+colorsU Balaji Institute of Technology and Science %"

WT Lab Manual

Departmrnt of Computer Science and Engineering

import ja$a+a:t+^U import ja$a+a:t+e$ent+^U public class Colors e*tends Can$as V transient pri$ate Color colorU pri$ate boolean rectangularU public Colors-1 V addMouseListener-ne: Mouse&dapter-1 V public $oid mouse,ressed-MouseE$ent me1 V change-1U Y Y1U rectangular N falseU setSi<e-!44= "441U change-1U Y public boolean get8ectangular-1 V return rectangularU Y public $oid set8ectangular-boolean flag1 V this+rectangular N flagU repaint-1U Y public $oid change-1 V S#>TW&8E DE5EL#,ME/T 0SI/( .&5& color N randomColor-1U repaint-1U Y pri$ate Color randomColor-1 V int r N -int1-!BB^Math+random-11U int g N -int1-!BB^Math+random-11U int b N -int1-!BB^Math+random-11U return ne: Color-r= g= b1U Y public $oid paint-(raphics g1 V Dimension d N getSi<e-1U int h N d+heightU int : N d+:idthU g+setColor-color1U if-rectangular1 V g+fill8ect-4= 4= : "= h "1U Y else V g+fill#$al-4= 4= : "= h "1U Y Y Balaji Institute of Technology and Science %!

WT Lab Manual

Departmrnt of Computer Science and Engineering

Y Compile the Source Code for the /e: Bean Compile the source code to create a class file+ Type the follo:ing? ja$ac Colors+ja$a+ Create a Manifest >ile Lou must no: create a manifest file+ >irst= s:itch to the c3<&+2<+e%o directory+ This is the directory in :hich the manifest files for the BDG demos are located+ ,ut the source code for your manifest file in the file colors.%ft+ It is sho:n here? /ame? sun:EdemoEcolorsEColors+class .a$a Bean? True This file indicates that there is one .class file in the .&8 file and that it is a .a$a Bean+ /otice that the Colors.class file is in the pac9age s)nw.+e%o.colors and in the subdirectory s)nw<+e%o<colors relati$e to the current directory+ (enerate a .&8 >ile Beans are included in the ToolBo* :indo: of the BDG only if they are in .&8 files in the directory c3<&+2<9ars+ These files are generated :ith the jar utility+ Enter the follo:ing? jar cfm ++PjarsPcolors+jar colors+mft sun:PdemoPcolorsP^+class This command creates the file colors.9ar and places it in the directory c3<&+2<9ars+ -Lou may :ish to put this in a batch file for future use+1 Start the BDG Change to the directory c3<&+2<&ean&o: and type r)n+ This causes the BDG to start+ Lou should see three :indo:s= titled ToolBo*= BeanBo*= and ,roperties+ The ToolBo* :indo: should include an entry labeled HColorsI for your ne: Bean+ Create an Instance of the Colors Bean &fter you complete the preceding steps= create an instance of the Colors Bean in the BeanBo* :indo:+ Test your ne: component by pressing the mouse any:here :ithin its borders+ Its color immediately changes+ 0se the ,roperties :indo: to change the rectang)lar property from false to tr)e+ Its shape immediately changes+ Create and Configure an Instance of the #urButton Bean Create an instance of the O)rB)tton Bean in the BeanBo* :indo:+ Then follo: these steps? "+ (o to the ,roperties :indo: and change the label of the Bean to HChangeI+ Lou should see that the button appearance changes immediately :hen this property is changed+ !+ (o to the menu bar of the BeanBo* and select Edit \ E$ents \ action \ action,erformed+ %+ Mo$e the cursor so that it is inside the Colors Bean display area= and clic9 the left mouse button+ Lou should see the E$ent Target Dialog dialog bo*+ '+ The dialog bo* allo:s you to choose a method that should be in$o9ed :hen this button is clic9ed+ Select the entry labeled HchangeI and clic9 the #G button+ Lou should see a message bo* appear $ery briefly= stating that the tool is H(enerating and compiling adaptor class+I B+ Clic9 on the button+ Lou should see the color change+ Lou might :ant to e*periment :ith the Colors Bean a bit before mo$ing on+ #utput? Balaji Institute of Technology and Science %%

WT Lab Manual

Departmrnt of Computer Science and Engineering

Create t:o Beans Traffic Light-implemented as a label :ith only three bac9ground colors red= green= yello:1 and &utomobile-Implemented as a Te*t Bo* :hich states its stateEmo$ement1+ The state of the &utomobile should depend on the follo:ing Light Transition table+ E^Japplet codeNOSig+classO heightN%44 :idthN!44KJEappletK^E import ja$a+a:t+^U import ja$a+lang+StringU import ja$a+a:t+e$ent+^U import ja$a+applet+&ppletU import ja$a+applet+^U public class Sig e*tends &pplet implements ItemListenerV boolean c"=c!=c%U String s"U Chec9bo* r"=r!=r%U Chec9bo*(roup cbgU public $oid init-1V cbgNne: Chec9bo*(roup-1U ,anel pNne: ,anel-1U p+setLayout-ne: (ridLayout-11U add-r"Nne: Chec9bo*-OredO=cbg=false11U add-r!Nne: Chec9bo*-Oyello:O=cbg=false11U add-r%Nne: Chec9bo*-OgreenO=cbg=false11U r"+addItemListener-this1U r!+addItemListener-this1U Balaji Institute of Technology and Science %'

WT Lab Manual r%+addItemListener-this1U Y

Departmrnt of Computer Science and Engineering

public $oid paint-(raphics g1 V g+setColor-Color+red1U g+dra:#$al-"4= %4= !4= !41U EESystem+out+println-s"1U g+setColor-Color+yello:1U g+dra:#$al-"4= )4= !4= !41U g+setColor-Color+green1U g+dra:#$al-"4= 74= !4= !41U s"Ncbg+getSelectedChec9bo*-1+getLabel-1U if-s"NNOredO1 V g+setColor-Color+red1U g+fill#$al-"4= %4= !4= !41U Y else if-s"NNOyello:O1 V g+setColor-Color+yello:1U g+fill#$al-"4= )4= !4= !41U Y else if-s"NNOgreenO1 V g+setColor-Color+green1U g+fill#$al-"4= 74= !4= !41U Y Y public $oid itemStateChanged-ItemE$ent ie1 V repaint-1U Y Y

Balaji Institute of Technology and Science

%B

WT Lab Manual =3 SERVELETS > COO;IES

Departmrnt of Computer Science and Engineering

Rea+!ng Ser/let Para%eters The Ser/letRe?)est class includes methods that allo: you to read the names and $alues of parameters that are included in a client re6uest+ We :ill de$elop a ser$let that illustrates their use+ The e*ample contains t:o files? PostPara%eters.(t% defines a Web page= and PostPara%etersSer/let.9a/a defines a ser$let+ The ATML source code for PostPara%eters.(t% is sho:n in the follo:ing listing+ It defines a table that contains t:o labels and t:o te*t fields+ #ne of the labels is Employee and the other is ,hone+ The form also includes a submit button+ /otice that the action parameter of the form tag specifies a 08L+ The 08L identifies the ser$let to process the ATT, ,#ST re6uest+ JhtmlK JbodyK JcenterK Jform nameNO>orm"O methodNOpostO actionNOhttp?EElocalhost?3434Eser$letE,ost,arametersSer$letOK JtableK JtrK JtdKJBKEmployeeJEtdK JtdKJinput typeNte*tbo* nameNOeO si<eNO!BO $alueNOOKJEtdK JEtrK JtrK JtdKJBK,honeJEtdK JtdKJinput typeNte*tbo* nameNOpO si<eNO!BO $alueNOOKJEtdK JEtrK JEtableK Jinput typeNsubmit $alueNOSubmitOK JEbodyK JEhtmlK The source code for PostPara%etersSer/let.9a/a is sho:n in the follo:ing listing+ The ser$ice- 1 method is o$erridden to process client re6uests+ The getPara%eterNa%es$ ' method returns an enumeration of the parameter names+ These are processed in a loop+ Lou can see that the parameter name and $alue are output to the client+ The parameter $alue is obtained $ia the getPara%eter$ ' method+ import ja$a+io+^U import ja$a+util+^U import ja$a*+ser$let+^U public class ,ost,arametersSer$let e*tends (enericSer$let V public $oid ser$ice-Ser$let8e6uest re6uest= Ser$let8esponse response1 thro:s Ser$letE*ception= I#E*ception V Balaji Institute of Technology and Science %)

WT Lab Manual

Departmrnt of Computer Science and Engineering

EE (et print :riter ,rintWriter p: N response+getWriter-1U EE (et enumeration of parameter names Enumeration e N re6uest+get,arameter/ames-1U EE Display parameter names and $alues :hile-e+hasMoreElements-11 V String pname N -String1e+ne*tElement-1U p:+print-pname F O N O1U String p$alue N re6uest+get,arameter-pname1U p:+println-p$alue1U Y p:+close-1U Y Y

The e*ample contains t:o files? Color*et.(t% defines a Web page= and Color*etSer/let.9a/a defines a ser$let+ The ATML source code for Color*et.(t% is sho:n in the follo:ing listing+ It defines a form that contains a select element and a submit button+ /otice that the action parameter of the form tag specifies a 08L+ The 08L identifies a ser$let to process the ATT, (ET re6uest+ JbodyK JcenterK Jform nameNO>orm"O actionNOhttp?EElocalhost?3434Eser$letEColor(etSer$letOK JBKColor?JEBK Jselect nameNOcolorO si<eNO"OK Joption $alueNO8edOK8edJEoptionK Joption $alueNO(reenOK(reenJEoptionK Joption $alueNOBlueOKBlueJEoptionK JEselectK JbrKJbrK Jinput typeNsubmit $alueNOSubmitOK JEformK JEbodyK JEhtmlK method is o$erridden to process any ATT, (ET re6uests that are sent to this ser$let+ It uses the getPara%eter$ ' method of 1tt0Ser/letRe?)est to obtain the selection that :as made by the user+ & response is then formulated+ import ja$a+io+^U import ja$a*+ser$let+^U Balaji Institute of Technology and Science %2

WT Lab Manual

Departmrnt of Computer Science and Engineering

import ja$a*+ser$let+http+^U public class Color(etSer$let e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s Ser$letE*ception= I#E*ception V String color N re6uest+get,arameter-OcolorO1U response+setContentType-Ote*tEhtmlO1U ,rintWriter p: N response+getWriter-1U p:+println-OJBKThe selected color is? O1U p:+println-color1U p:+close-1U

The ATML source code for ColorPost.(t% is sho:n in the follo:ing listing+ It is identical to Color*et.(t% e*cept that the method parameter for the form tag e*plicitly specifies that the ,#ST method should be used= and the action parameter for the form tag specifies a different ser$let+ JhtmlK JbodyK JcenterK Jform nameNO>orm"O methodNOpostO actionNOhttp?EElocalhost?3434Eser$letEColor,ostSer$letOK JBKColor?JEBK Jselect nameNOcolorO si<eNO"OK Joption $alueNO8edOK8edJEoptionK Joption $alueNO(reenOK(reenJEoptionK Joption $alueNOBlueOKBlueJEoptionK JEselectK JbrKJbrK Jinput typeNsubmit $alueNOSubmitOK JEformK JEbodyK JEhtmlK The source code for ColorPostSer/let.9a/a is sho:n in the follo:ing listing+ The +oPost$ ' method is o$erridden to process any ATT, ,#ST re6uests that are sent to this ser$let+ It uses the getPara%eter$ ' method of 1tt0Ser/letRe?)est to obtain the selection that :as made by the user+ & response is then formulated+ import ja$a+io+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class Color,ostSer$let e*tends AttpSer$let V public $oid do,ost-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 Balaji Institute of Technology and Science %3

WT Lab Manual

Departmrnt of Computer Science and Engineering

thro:s Ser$letE*ception= I#E*ception V String color N re6uest+get,arameter-OcolorO1U response+setContentType-Ote*tEhtmlO1U ,rintWriter p: N response+getWriter-1U p:+println-OJBKThe selected color is? O1U p:+println-color1U p:+close-1U Y Y

Balaji Institute of Technology and Science

%7

WT Lab Manual

Departmrnt of Computer Science and Engineering

@. JSP So)rce Co+e for Bas!c Ar!t(%et!c E:a%0le JhtmlK JheadK JtitleK.S, !+4 E*pression Language Basic &rithmeticJEtitleK JEheadK JbodyK Jh"K.S, !+4 E*pression Language Basic &rithmeticJEh"K JhrK This e*ample illustrates basic E*pression Language arithmetic+ &ddition -F1= subtraction - 1= multiplication -^1= di$ision -E or di$1= and modulus -Q or mod1 are all supported+ Error conditions= li9e di$ision by <ero= are handled gracefully+ JbrK Jbloc96uoteK JcodeK Jtable borderNO"OK JtheadK JtdKJbKEL E*pressionJEbKJEtdK JtdKJbK8esultJEbKJEtdK JEtheadK JtrK JtdKPSV"YJEtdK JtdKSV"YJEtdK JEtrK JtrK JtdKPSV" F !YJEtdK JtdKSV" F !YJEtdK JEtrK JtrK JtdKPSV"+! F !+%YJEtdK JtdKSV"+! F !+%YJEtdK JEtrK JtrK JtdKPSV"+!E' F "+'YJEtdK JtdKSV"+!E' F "+'YJEtdK JEtrK JtrK JtdKPSV ' !YJEtdK JtdKSV ' !YJEtdK JEtrK JtrK JtdKPSV!" ^ !YJEtdK JtdKSV!" ^ !YJEtdK JEtrK Balaji Institute of Technology and Science '4

WT Lab Manual JtrK JtdKPSV%E'YJEtdK JtdKSV%E'YJEtdK JEtrK JtrK JtdKPSV% di$ 'YJEtdK JtdKSV% di$ 'YJEtdK JEtrK JtrK JtdKPSV%E4YJEtdK JtdKSV%E4YJEtdK JEtrK JtrK JtdKPSV"4Q'YJEtdK JtdKSV"4Q'YJEtdK JEtrK JtrK JtdKPSV"4 mod 'YJEtdK JtdKSV"4 mod 'YJEtdK JEtrK JtrK JtdKPSV-"NN!1 R % ? 'YJEtdK JtdKSV-"NN!1 R % ? 'YJEtdK JEtrK JEtableK JEcodeK JEbloc96uoteK JEbodyK JEhtmlK

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

'"

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT3 JSP ".A E:0ress!on Lang)age 7 Bas!c Ar!t(%et!c EL E:0ress!on SV"Y SV" F !Y SV"+! F !+%Y SV"+!E' F "+'Y SV ' !Y SV!" ^ !Y SV%E'Y SV% di$ 'Y SV%E4Y SV"4Q'Y SV"4 mod 'Y Res)lt " % %+B "!44"+' ) '! 4+2B 4+2B Infinity ! !

SV-"NN!1 R % ? 'Y '

Balaji Institute of Technology and Science

'!

WT Lab Manual

Departmrnt of Computer Science and Engineering

So)rce Co+e for f)nct!ons.9s0 JhtmlK JheadK JtitleK.S, !+4 E*pression Language >unctionsJEtitleK JEheadK JbodyK Jh"K.S, !+4 E*pression Language >unctionsJEh"K JhrK &n upgrade from the .STL e*pression language= the .S, !+4 EL also allo:s for simple function in$ocation+ >unctions are defined by tag libraries and are implemented by a .a$a programmer as static methods+ Jbloc96uoteK JuKJbKChange ,arameterJEbKJEuK Jform actionNOfunctions+jspO methodNO(ETOK foo N Jinput typeNOte*tO nameNOfooO $alueNOSVfn?escapeCml-paramWOfooOX1YOK Jinput typeNOsubmitOK JEformK JbrK JcodeK Jtable borderNO"OK JtheadK JtdKJbKEL E*pressionJEbKJEtdK JtdKJbK8esultJEbKJEtdK JEtheadK JtrK JtdKPSVparamWOfooOXYJEtdK JtdKSVfn?escapeCml-paramWOfooOX1Y`nbspUJEtdK JEtrK JtrK JtdKPSVmy?re$erse-paramWOfooOX1YJEtdK JtdKSVmy?re$erse-fn?escapeCml-paramWOfooOX11Y`nbspUJEtdK JEtrK JtrK JtdKPSVmy?re$erse-my?re$erse-paramWOfooOX11YJEtdK JtdKSVmy?re$erse-my?re$erse-fn?escapeCml-paramWOfooOX111Y`nbspUJEtdK JEtrK JtrK JtdKPSVmy?count5o:els-paramWOfooOX1YJEtdK JtdKSVmy?count5o:els-fn?escapeCml-paramWOfooOX11Y`nbspUJEtdK JEtrK JEtableK JEcodeK JEbloc96uoteK JEbodyK JEhtmlK Balaji Institute of Technology and Science '%

WT Lab Manual So)rce Co+e for Calen+ar E:a%0le JATMLK JAE&DKJTITLEK Calendar? & .S, &,,LIC&TI#/ JETITLEKJEAE&DK JB#DL B(C#L#8NO:hiteOK

Departmrnt of Computer Science and Engineering

JQa page languageNOja$aO importNOcal+^O QK Jjsp?useBean idNOtableO scopeNOsessionO classNOcal+TableBeanO EK JQ table+process8e6uest-re6uest1U if -table+get,rocessError-1 NN false1 V QK JZ html table goes here K JCE/TE8K JT&BLE WIDTAN)4Q B(C#L#8Nyello: CELL,&DDI/(N"BK JT8K JTD &LI(/NCE/TE8K J& A8E>Ncal"+jspRdateNpre$K pre$ JE&K JTD &LI(/NCE/TE8K Calendar?JQN table+getDate-1 QKJETDK JTD &LI(/NCE/TE8K J& A8E>Ncal"+jspRdateNne*tK ne*t JE&K JET8K JET&BLEK JZ the main table K JT&BLE WIDTAN)4Q B(C#L#8Nlightblue B#8DE8N" CELL,&DDI/(N"4K JT8K JTAK Time JETAK JTAK &ppointment JETAK JET8K J>#8M META#DN,#ST &CTI#/Ncal"+jspK JQ for-int iN4U iJtable+getEntries-1+get8o:s-1U iFF1 V cal+Entry entr N table+getEntries-1+getEntry-i1U QK JT8K JTDK J& A8E>Ncal!+jspRtimeNJQN entr+getAour-1 QKK JQN entr+getAour-1 QK JE&K JETDK JTD B(C#L#8NJQN entr+getColor-1 QKK Balaji Institute of Technology and Science ''

WT Lab Manual

Departmrnt of Computer Science and Engineering

JQ out+print-util+ATML>ilter+filter-entr+getDescription-111U QK JETDK JET8K JQ Y QK JE>#8MK JET&BLEK JB8K JZ footer K JT&BLE WIDTAN)4Q B(C#L#8Nyello: CELL,&DDI/(N"BK JT8K JTD &LI(/NCE/TE8K JQ out+print-util+ATML>ilter+filter-table+get/ame-111U QK ? JQ out+print-util+ATML>ilter+filter-table+getEmail-111U QK JETDK JET8K JET&BLEK JECE/TE8K JQ Y else V QK Jfont si<eNBK Lou must enter your name and email address correctly+ JEfontK JQ Y QK JEB#DLK JEATMLK

Balaji Institute of Technology and Science

'B

WT Lab Manual OUT PUT

Departmrnt of Computer Science and Engineering

,lease Enter the follo:ing information? /ame Email


ANANT ananth!r

"u#mit

pre$ T!%e 3am 7am "4am ""am "!pm "pm !pm %pm 'pm Bpm )pm 2pm

Calendar?BE!7E!443 A00o!nt%ent

ne*t

/one /one /one /one /one /one /one /one /one /one /one /one

Balaji Institute of Technology and Science

')

WT Lab Manual

Departmrnt of Computer Science and Engineering

0/r 3 0/rBg%a!l.co%
Calender! JATMLK JAE&DKJTITLEK Calendar? & .S, &,,LIC&TI#/ JETITLEKJEAE&DK JB#DL B(C#L#8NO:hiteOK Jjsp?useBean idNOtableO scopeNOsessionO classNOcal+TableBeanO EK JQ String time N re6uest+get,arameter -OtimeO1U QK J>#/T SI_ENBK ,lease add the follo:ing e$ent? JB8K Jh%K Date JQN table+getDate-1 QK JB8K Time JQN util+ATML>ilter+filter-time1 QK JEh%K JE>#/TK J>#8M META#DN,#ST &CTI#/Ncal"+jspK JB8K JB8K JI/,0T /&MENOdateO TL,ENAIDDE/ 5&L0ENOcurrentOK JB8K JI/,0T /&MENOtimeO TL,ENAIDDE/ 5&L0ENJQN util+ATML>ilter+filter-time1 QK JB8K Jh!K Description of the e$ent JI/,0T /&MENOdescriptionO TL,ENTECT SI_EN!4K JEh!K JB8K JI/,0T TL,ENS0BMIT 5&L0ENOsubmitOK JE>#8MK JEB#DLK JEATMLK

Balaji Institute of Technology and Science

'2

WT Lab Manual PRO*RA# 8OR JSP7Ser/let7JSP Source Code for .S, calling ser$let JATMLK Jbody bgcolorNO:hiteOK JZ >or:ard to a ser$let K Jjsp?for:ard pageNOEser$letTo.spO EK JEhtmlK #8 So)rce Co+e for Ser/let call!ng JSP import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U

Departmrnt of Computer Science and Engineering

public class ser$letTo.sp e*tends AttpSer$let V public $oid do(et -AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 V try V EE Set the attribute and >or:ard to hello+jsp re6uest+set&ttribute -Oser$let/ameO= Oser$letTo.spO1U getSer$letConfig-1+getSer$letConte*t-1+get8e6uestDispatcher-OEjsptoser$Ehello+jspO1+for:ard-r e6uest= response1U Y catch -E*ception e*1 V e*+printStac9Trace -1U Y Y Y OUT PUT I ha$e been in$o9ed by ser$letTo.sp Ser$let+

Balaji Institute of Technology and Science

'3

WT Lab Manual

Departmrnt of Computer Science and Engineering

C. So)rce Co+e for 1ello,orl+ E:a%0le import ja$a+io+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class AelloWorld e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s I#E*ception= Ser$letE*ception V response+setContentType-Ote*tEhtmlO1U ,rintWriter out N response+getWriter-1U out+println-OJhtmlKO1U out+println-OJheadKO1U out+println-OJtitleKAello WorldZJEtitleKO1U out+println-OJEheadKO1U out+println-OJbodyKO1U out+println-OJh"KAello WorldZJEh"KO1U out+println-OJEbodyKO1U out+println-OJEhtmlKO1U Y Y OUTPUT3 1ello ,orl+D

Balaji Institute of Technology and Science

'7

WT Lab Manual

Departmrnt of Computer Science and Engineering

So)rce Co+e for Sess!on E:a%0le import ja$a+io+^U import ja$a+util+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class SessionE*ample e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s I#E*ception= Ser$letE*ception V response+setContentType-Ote*tEhtmlO1U ,rintWriter out N response+getWriter-1U AttpSession session N re6uest+getSession-true1U EE print session info Date created N ne: Date-session+getCreationTime-11U Date accessed N ne: Date-session+getLast&ccessedTime-11U out+println-OID O F session+getId-11U out+println-OCreated? O F created1U out+println-OLast &ccessed? O F accessed1U EE set session info if needed String data/ame N re6uest+get,arameter-Odata/ameO1U if -data/ame ZN null `` data/ame+length-1 K 41 V String data5alue N re6uest+get,arameter-Odata5alueO1U session+set&ttribute-data/ame= data5alue1U Y EE print session contents Enumeration e N session+get&ttribute/ames-1U :hile -e+hasMoreElements-11 V String name N -String1e+ne*tElement-1U String $alue N session+get&ttribute-name1+toString-1U out+println-name F O N O F $alue1U Y Y Y

Balaji Institute of Technology and Science

B4

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT37 Sessions E*ample Session ID? EBBB2&E32EEC3BB)&43'BB>>'2E7)"BB Created? Thu May !7 "!?44?43 IST !443 Last &ccessed? Thu May !7 "!?44?'% IST !443 The follo:ing data is in your session? &/&/TA N "!%' 8&M N '%!" /ame of Session &ttribute? 5alue of Session &ttribute?
"u#mit $uer%

(ET based form? /ame of Session &ttribute? 5alue of Session &ttribute?


"u#mit $uer%

08L encoded

Balaji Institute of Technology and Science

B"

WT Lab Manual

Departmrnt of Computer Science and Engineering

Us!ng Coo2!es /o:= letTs de$elop a ser$let that illustrates ho: to use coo9ies+ The ser$let is in$o9ed :hen a form on a Web page is submitted+ The e*ample contains three files= as summari<ed here? 8!le Descr!0t!on &ddCoo9ie+htm &llo:s a user to specify a $alue for the coo9ie named M yCoo9ie &ddCoo9ieSer$let+ja$a , rocesses the submission of &ddCoo9ie+htm (etCoo9iesSer$let+ja$a D isplays coo9ie $alues The ATML source code for A++Coo2!e.(t% is sho:n in the follo:ing listing+ This page contains a te*t field in :hich a $alue can be entered+ The page also includes a submit button+ When this button is pressed= the $alue in the te*t field is sent to A++Coo2!eSer/let $ia an ATT, ,#ST re6uest+ JhtmlK JbodyK JcenterK Jform nameNO>orm"O methodNOpostO actionNOhttp?EElocalhost?3434Eser$letE&ddCoo9ieSer$letOK JBKEnter a $alue for MyCoo9ie?JEBK Jinput typeNte*tbo* nameNOdataO si<eN!B $alueNOOK Jinput typeNsubmit $alueNOSubmitOK JEformK JEbodyK JEhtmlK The source code for A++Coo2!eSer/let.9a/a is sho:n in the follo:ing listing+ It gets the $alue of the parameter named OdataO+ It then creates a Coo2!e object that has the name OMyCoo9ieO and contains the $alue of the OdataO parameter+ The coo9ie is then added to the header of the ATT, response $ia the a++Coo2!e$ ' method+ & feedbac9 message is then :ritten to the bro:ser+ import ja$a+io+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class &ddCoo9ieSer$let e*tends AttpSer$let V public $oid do,ost-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s Ser$letE*ception= I#E*ception V EE (et parameter from ATT, re6uest String data N re6uest+get,arameter-OdataO1U Coo9ie coo9ie N ne: Coo9ie-OMyCoo9ieO= data1U EE &dd coo9ie to ATT, response response+addCoo9ie-coo9ie1U Balaji Institute of Technology and Science B!

WT Lab Manual

Departmrnt of Computer Science and Engineering

EE Write output to bro:ser response+setContentType-Ote*tEhtmlO1U ,rintWriter p: N response+getWriter-1U p:+println-OJBKMyCoo9ie has been set toO1U p:+println-data1U p:+close-1U Y Y The source code for *etCoo2!esSer/let.9a/a is sho:n in the follo:ing listing+ It in$o9es the getCoo2!es$ ' method to read any coo9ies that are included in the ATT, (ET re6uest+ The names and $alues of these coo9ies are then :ritten to the ATT, response+ #bser$e that the getNa%e$ ' and getVal)e$ ' methods are called to obtain this information+ import ja$a+io+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class (etCoo9iesSer$let e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s Ser$letE*ception= I#E*ception V EE (et coo9ies from header of ATT, re6uest Coo9ieWX coo9ies N re6uest+getCoo9ies-1U EE Display these coo9ies response+setContentType-Ote*tEhtmlO1U ,rintWriter p: N response+getWriter-1U p:+println-OJBKO1U for-int i N 4U i J coo9ies+lengthU iFF1 V String name N coo9iesWiX+get/ame-1U String $alue N coo9iesWiX+get5alue-1U p:+println-Oname N O F name F OU $alue N O F $alue1U Y p:+close-1U Y Y The follo:ing ser$let illustrates ho: to use session state+ The getSess!on$ ' method gets the current session+ & ne: session is created if one does not already e*ist+ The getVal)e$ ' method is called to obtain the object that is bound to the name OdateO+ That object is a Date object that encapsulates the date and time :hen this page :as last accessed+ -#f course= there is no such binding :hen the page is first accessed+1 & Date object encapsulating the current date and time is then created+ The 0)tVal)e$ ' method is called to bind the name OdateO to this object+ import ja$a+io+^U Balaji Institute of Technology and Science B%

WT Lab Manual

Departmrnt of Computer Science and Engineering

import ja$a+util+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class DateSer$let e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s Ser$letE*ception= I#E*ception V EE (et the AttpSession object AttpSession hs N re6uest+getSession-true1U EE (et :riter response+setContentType-Ote*tEhtmlO1U ,rintWriter p: N response+getWriter-1U p:+print-OJBKO1U EE Display dateEtime of last access Date date N -Date1hs+get5alue-OdateO1U if-date ZN null1 V p:+print-OLast access? O F date F OJbrKO1U Y EE Display current dateEtime date N ne: Date-1U hs+put5alue-OdateO= date1U p:+println-OCurrent date? O F date1U YY

Balaji Institute of Technology and Science

B'

WT Lab Manual

Departmrnt of Computer Science and Engineering

So)rce Co+e for Coo2!e E:a%0le import ja$a+io+^U import ja$a*+ser$let+^U import ja$a*+ser$let+http+^U public class Coo9ieE*ample e*tends AttpSer$let V public $oid do(et-AttpSer$let8e6uest re6uest= AttpSer$let8esponse response1 thro:s I#E*ception= Ser$letE*ception V response+setContentType-Ote*tEhtmlO1U ,rintWriter out N response+getWriter-1U EE print out coo9ies Coo9ieWX coo9ies N re6uest+getCoo9ies-1U for -int i N 4U i J coo9ies+lengthU iFF1 V Coo9ie c N coo9iesWiXU String name N c+get/ame-1U String $alue N c+get5alue-1U out+println-name F O N O F $alue1U Y EE set a coo9ie String name N re6uest+get,arameter-Ocoo9ie/ameO1U if -name ZN null `` name+length-1 K 41 V String $alue N re6uest+get,arameter-Ocoo9ie5alueO1U Coo9ie c N ne: Coo9ie-name= $alue1U response+addCoo9ie-c1U Y Y Y

Balaji Institute of Technology and Science

BB

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT3 Coo9ies E*ample Lour bro:ser is sending the follo:ing coo9ies? Coo9ie /ame? &/&/TA Coo9ie 5alue? 32)B'% Coo9ie /ame? 8&M Coo9ie 5alue? "!%'B)2 Lou just sent the follo:ing coo9ie to your bro:ser? /ame? (080/&/&G 5alue? B4444)4 Create a coo9ie to send to your bro:ser /ame? 5alue?
&'(A &M)ATN 5****2*

"u#mit $uer%

Balaji Institute of Technology and Science

B)

WT Lab Manual

Departmrnt of Computer Science and Engineering

,r!te we& 0rogra% to !%0le%ent 1ello wor+ JWeb appK Jser$letK Jser$let nameKsimple ser$let JEser$let nameK Jser$let classK Aello WordJEser$let classK JEser$letK Jser$let mappingK Jser$let nameKsimple ser$letJEser$let nameK Jun patternKser$letJun patternK JEWeb appK OUTPUT ? Aello Word Progra% for S!%0le Ser/let import ja$a+io+^U import ja$a*+ser$let+^U public class simpleSer$let e*tends (enericSer$letV public $oid ser$ice-Ser$let8e6uest re6uest=Ser$let8esponse response1 thro:s Ser$letE*ception=I#E*ceptionV res+setcontenttype Type-OTECTEATMLO1U ,rintWriter outNres+get:riter-1U out+println-OJhtmlKO1U out+println-OJheadKO1U out+println-OJtitleKthis is simple ser$letJEtitleK1U out+println-OJEheadO1U out+println-OJEbodyKO1U out+println-JcenterJbKtesting ser$eletJEbKJEcenter1U out+println-JEbodyK1U out+println-JEhtmlK1U out+close-1U Y Y

Balaji Institute of Technology and Science

B2

WT Lab Manual

Departmrnt of Computer Science and Engineering

Progra% for Post0ar%eter Ser/let import ja$a+io+^U import ja$a+util+^U import ja$a*+ser$let+^U public class ,ost,arametersSer$let e*tends (enericSer$letV public $oid ser$ice-Ser$let8e6uest re6uest=Ser$let8esponse response1 thro:s Ser$letE*ception=I#E*ceptionV EE(et print:riter ,rintWriter p:Nresponse+getWriter-1U EE(et enumeration of parameternames Enumeration eNre6uest+get,arameter/ames-1U EEDisplay parameter names and $alues :hile-e+hasMoreElements-11 V String pnameN-String1e+ne*tElement-1U p:+print-pnameFONO1U String p$alueNre6uest+get,arameter-pname1U p:+println-p$alue1U Y p:+close-1U Y Y

Balaji Institute of Technology and Science

B3

WT Lab Manual

Departmrnt of Computer Science and Engineering

C I%0le%ent t(e E1ello ,orl+DF 0rogra% )s!ng JSP Str)ts 8ra%ewor2. So)rce Co+e for t(e 1ello ,orl+ Tag E:a%0le JhtmlK JheadK JtitleK.S, !+4 E*amples Aello World SimpleTag AandlerJEtitleK JEheadK JbodyK Jh"K.S, !+4 E*amples Aello World SimpleTag AandlerJEh"K JhrK JpKThis tag handler simply echos OAello= WorldZO ItTs an e*ample of a $ery basic SimpleTag handler :ith no body+JEpK JbrK JbKJuK8esult?JEuKJEbK Jmytag?helloWorldEK JEbodyK JEhtmlK OUT PUT This tag handler simply echos OAello= WorldZO ItTs an e*ample of a $ery basic SimpleTag handler :ith no body+ Res)lt3 Aello= :orldZ

Balaji Institute of Technology and Science

B7

WT Lab Manual Progra% 3 De0os!t

Departmrnt of Computer Science and Engineering

JATMLK JAE&DK J, &LI(/NO8I(ATOKJ& A8E>NOT8&/S&CTI#/S+ATMLOKA#MEJE&KJE,K JTITLEK DE,#SIT DET&ILS JETITLEKJB8KJB8KJB8KJB8KJB8K JCE/TE8KJ>#/T SI_ENO)O C#L#8NO]4444CCOK J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9EresoursesEDeposit+comOK DE,#SIT DET&ILSJE>#/TKJB8KJB8KJB8K &CC#0/T /#+?JI/,0T TL,ENOte*tO /&MENO&CC#0/T/#OKJB8KJB8K &M#0/T?JI/,0T TL,ENOte*tO /&MENO&MTOKJB8KJB8K JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOK`nbspU`nbspU`nbspU`nbspU JI/,0T TL,ENOresetOK JE>#8MK JECE/TE8K JEAE&DK JB#DLK JEB#DLK JEATMLK OUTPUT DE,#SIT DET&ILS

&CC#0/T /#+? &M#0/T?


"+'M&T (eset

Balaji Institute of Technology and Science

)4

WT Lab Manual Progra% 3 Transact!ons

Departmrnt of Computer Science and Engineering

JATMLK JAE&DK JTITLEK T8&/S&CTI#/SJETITLEKJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOO /&MENOT8&/S&CTI#/OK JCE/TE8K J>#/T SI_ENO)OC#L#8NO]4444CCOK T8&/S&CTI#/S JE>#/TKJB8KJB8KJB8KJB8K J0LK JLIKJ& A8E>NO5IEWB&L+ATMLOK5IEW B&L&/CEJE&KJB8KJB8K JLIKJ& A8E>NODE,DET&ILS+ATMLOKDE,#SITJE&KJB8KJB8K JLIKJ& A8E>NOWITADET&ILS+ATMLOKWITAD8&WJE&KJB8KJB8K JLIKJ& A8E>NOCL#SI/(+ATMLOKCL#SE &/ &CC#0/TJE&KJB8KJB8K JLIKJ& A8E>NO0SE8DET&ILS+ATMLOKECITJE&KJE0LK JECE/TE8K JE>#8MK JEAE&DK JEB#DLK JEB#DLK JEATMLK OUTPUT 3

T8&/S&CTI#/S

5IEW B&L&/CE

DE,#SIT WITAD8&W CL#SE &/ &CC#0/T ECIT

Balaji Institute of Technology and Science

)"

WT Lab Manual Progra% 3 User Deta!ls

Departmrnt of Computer Science and Engineering

GATMLK JAE&DK JTITLEK 0SE8 DET&ILS JETITLEK JCE/TE8KJB8KJB8KJB8KJB8KJB8K J>#/T SI_ENO)O C#L#8NO]4444CCOK0SE8 DET&ILSJE>#/TKJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOOK 0SE8/&ME? JI/,0T TL,ENOte*tO /&MENO0/OKJB8KJB8K ,&SSW#8D? JI/,0T TL,ENOte*tO /&MENO,WOKJB8KJB8KJB8K I/5&LID 0SE8/&ME #8 ,&SSW#8D+ JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOKJB8KJB8KJB8K Clic9 here to CreateJ& A8E>NO/EW&CC+ATMLOK/EW 0SE8JE&KJECE/TE8K JE>#8MK JEAE&DK JB#DLK JEB#DLK JEATMLK O)t0)t3

0SE8 DET&ILS

0SE8/&ME? ,&SSW#8D?

I/5&LID 0SE8/&ME #8 ,&SSW#8D+ Clic9 here to Create ?/EW 0SE8

"+'M&T

Balaji Institute of Technology and Science

)!

WT Lab Manual

Departmrnt of Computer Science and Engineering

Progra% 3 ,!t(+raw JATMLK JAE&DK J, &LI(/NO8I(ATOKJ& A8E>NOT8&/S&CTI#/S+ATMLOKA#MEJE&KJE,K JTITLEK WITAD8&W&L DET&ILS JETITLEKJB8KJB8KJB8KJB8KJB8K JCE/TE8KJ>#/T SI_ENO)O C#L#8NO]4444CCOK WITAD8&W&L DET&ILSJE>#/TKJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9EresoursesEWithdra:+comOK &CC#0/T /#+?JI/,0T TL,ENOte*tO /&MENO&CC#0/T/#OKJB8KJB8K &M#0/T?JI/,0T TL,ENOte*tO /&MENO&MTOKJB8KJB8K JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOK`nbspU`nbspU`nbspU`nbspU JI/,0T TL,ENOresetOK JE>#8MK JECE/TE8K JEAE&DK JB#DLK JEB#DLK JEATMLK

O)t0)t3
WITAD8&W&L DET&ILS

&CC#0/T /#+? &M#0/T?


"+'M&T (eset

Balaji Institute of Technology and Science

)%

WT Lab Manual Progra% 3 Clos!ng

Departmrnt of Computer Science and Engineering

JATMLK JAE&DK J, &LI(/NO8I(ATOKJ& A8E>NOT8&/S&CTI#/S+ATMLOKA#MEJE&KJE,K JTITLEK CL#SI/( &CC#0/T JETITLEKJB8KJB8KJB8KJB8KJB8K JCE/TE8KJ>#/T SI_ENO)O C#L#8NO]4444CCOK CL#SI/( &CC#0/TJE>#/TKJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9EresoursesEClose+comOK &CC#0/T /#+?JI/,0T TL,ENOte*tO /&MENO&CC#0/T/#OKJB8KJB8K JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOK`nbspU`nbspU`nbspU`nbspU JI/,0T TL,ENOresetOK JE>#8MK JECE/TE8K JEAE&DK JB#DLK JEB#DLK JEATMLK O)t0)t3 CL#SI/( &CC#0/T

&CC#0/T /#+?
"+'M&T (eset

Balaji Institute of Technology and Science

)'

WT Lab Manual

Departmrnt of Computer Science and Engineering

Progra% 3 User Acco)nt JATMLK JAE&DK JTITLEK /EW &CC#0/T >#8M JETITLEK JCE/TE8KJB8KJ>#/T SI_ENO)O C#L#8NO]4444CCOK /EW &CC#0/T >#8MJE>#/TKJB8KJB8K J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9ESignup+comOK JT&BLEK JT8K JTDK/&ME?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO/&MEOKJETDKJB8K JET8K JT8K JTDK&(E?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO&(EOKL8SJETDKJB8K JET8K JT8K JTDK#CC0,&TI#/?JETDK JTDKJSELECT /&MENO#CC0,&TI#/OK J#,TI#/ 5&L0ENOO SELECTEDKB0SI/ESSJE#,TI#/K J#,TI#/ 5&L0ENOOKST0DE/TJE#,TI#/K JESELECTKJB8KJETDK JET8K JT8K JTDK&DD8ESS?JETDK JTDKJTECT&8E& /&MENO&DD8ESSO 8#WSNO%O C#LSNO!4OKJETECT&8E&KJB8KJETDK JET8K JT8K JTDK,A#/E /0MBE8?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO,A#/EOKJB8KJETDK JET8K JT8K JTDK&CC#0/T TL,E?JETDK JTDKJSELECT /&MENO&CCTL,EOK J#,TI#/ 5&L0ENOO SELECTEDKS&5I/(JE#,TI#/K J#,TI#/ 5&L0ENOOKC088E/T&CC JESELECTKJB8KJETDK JET8K JT8K JTDK#,E/I/( D&TE?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO#,E/D&TEOKJB8KJETDK JET8K JT8K JTDK0SE8/&ME?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO0SE8/&MEOKJB8KJETDK JET8K Balaji Institute of Technology and Science )B

WT Lab Manual

Departmrnt of Computer Science and Engineering

JT8K JTDK,&SSW#8D?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO,&SSW#8DOKJB8KJETDK JET8K JT8K JTDK8EE/TE8 ,&SSW#8D?JETDK JTDKJI/,0T TL,ENOte*tO /&MENO8E,WDOKJETDK JET8K JT8KJB8K JTDKJI/,0T TL,ENOsubmitO 5&L0ENOS0BMITO KJET8KJB8KJB8KJB8K JTDKJI/,0T TL,ENOresetOKJETDK JE>#8MK JECE/TE8K JEAE&DK JB#DLK JEB#DLK JEATMLK O)t0)t3 /EW &CC#0/T >#8M /&ME? &(E? #CC0,&TI#/? &DD8ESS? ,A#/E /0MBE8? &CC#0/T TL,E? #,E/I/( D&TE? 0SE8/&ME? ,&SSW#8D? 8EE/TE8 ,&SSW#8D?
"+'M&T "A-&N. '+"&N,""

L8S

(eset

Progra% 3 User Deta!ls Balaji Institute of Technology and Science ))

WT Lab Manual

Departmrnt of Computer Science and Engineering

JATMLK JAE&DK JTITLEK 0SE8 DET&ILS JETITLEK JCE/TE8KJB8KJB8KJB8KJB8KJB8K J>#/T SI_ENO)O C#L#8NO]4444CCOK0SE8 DET&ILSJE>#/TKJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9ELogin+comOK 0SE8/&ME? JI/,0T TL,ENOte*tO /&MENO0/OKJB8KJB8K ,&SSW#8D? JI/,0T TL,ENOte*tO /&MENO,WOKJB8KJB8KJB8K JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOKJB8KJB8KJB8K Clic9 here to CreateJ& A8E>NO/EW&CC+ATMLOK/EW 0SE8JE&KJECE/TE8K JE>#8MK JEAE&DK JB#DLK JEB#DLK JEATMLK O)t0)t3 0SE8 DET&ILS

0SE8/&ME? ,&SSW#8D?

"+'M&T

Clic9 here to Create/EW 0SE8

Balaji Institute of Technology and Science

)2

WT Lab Manual

Departmrnt of Computer Science and Engineering

Progra% 3 V!ew Balance JATMLK JAE&DK J, &LI(/NO8I(ATOKJ& A8E>NOT8&/S&CTI#/S+ATMLOKA#MEJE&KJE,K JTITLEK5IEW B&L&/CEJETITLEKJB8KJB8KJB8KJB8KJB8KJB8KJB8K J>#8M META#DN,#ST &CTI#/NOhttp?EElocalhost?3434Eban9EBalance+comO /&MENOB&LOK JCE/TE8KJ>#/T SI_ENO)O C#L#8NO]4444CCOK 5IEW B&L&/CEJE>#/TKJB8KJB8KJB8KJB8K &CC#0/T /0MBE8?`nbspU`nbspU`nbspUJI/,0T TL,ENOte*tO /&MENO&CC/#OKJB8KJB8KJB8K JI/,0T TL,ENOsubmitO 5&L0ENOS0BMITOK JECE/TE8K JE>#8MK JEAE&DK JB#DLK JEB#DLK JEATMLK O)t0)t 3 5IEW B&L&/CE

&CC#0/T /0MBE8?

"+'M&T

Balaji Institute of Technology and Science

)3

WT Lab Manual

Departmrnt of Computer Science and Engineering

3+ Enter into the follo:ing te*t bo* O%' !! !% '%B %% ! " '% B'O= for e*ample= and press the sort numerically button+ #r= try entering Othis is a testO= and press the sort alpha button+ Jscript typeNOte*tEja$ascriptOK function sortit-a=b1V return-a b1 Y function sort$alues-param1V $ar input$aluesNdocument+sorter+sorter!+$alue+split-O O1 if -paramNN41 EEif sort alphabetically input$alues+sort-1 else EEelse if sort numerically input$alues+sort-sortit1 document+sorter+sorter!+$alueNTT for -iN4UiJinput$alues+length "UiFF1 document+sorter+sorter!+$alueNdocument+sorter+sorter!+$alueFinput$aluesWiXFO O document+sorter+sorter!+$alueFNinput$aluesWinput$alues+length "X Y JEscriptK Jform nameNOsorterOK JpK Jte*tarea ro:sNO"4O nameNOsorter!O colsNOB4O :rapNO$irtualOKJEte*tareaKJbrK Jinput typeNObuttonO $alueNOSort alphabeticallyO onClic9NOsort$alues-41OK Jinput typeNObuttonO $alueNOSort numericallyO onClic9NOsort$alues-"1OK Jinput typeNOresetO $alueNO8esetOK JEformK

Balaji Institute of Technology and Science

)7

WT Lab Manual O)t0)t

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

24

WT Lab Manual A++!t!onal Progra%s s.no " ! % ' B ) 2 3 7 "4 "" "! "% "' "B ") "2 "3 "7 !4 !" !!

Departmrnt of Computer Science and Engineering

Progra%s ,rogram for implementation of >onts ,rogram for implementation of Table ,rogram for implementation of List ,rogram for addition of t:o noDs in ja$a script ,rogram for implementation of S:itch case ,rogram to find ma*imum of % noDs ,rogram for Entering a pass:ord ,rogram for implementation of Thambola ,rogram for >actorial ,rogram for &ddition of t:o Matrices ,rogram for #nline E*am ,rogram for Bubble Sort ,rogram for implementation of Linear Search ,rogram for displaying an image on mouse clic9 ,rogram for E*hibiting Blending Effect ,rogram Code To E*hibit C ray Effect ,rogram Code To E*hibit Shado: Effect ,rogram Code To E*hibit Wa$e Effect ,rogram for implementing &nimation ,rogram To display bac9ground Sound ,rogram To add $ideo file 0sing .a$aScript sort gi$en array in ascending and descending order+

Page no !% !' !2 !3 %4 %% %B %3 '4 '! '3 B% BB B7 )" )% )B )2 )7 2! 2)

Balaji Institute of Technology and Science

2"

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI#

,r!te a scr!0t for select!on sort. PRO*RA# JhtmlK JheadK JtitleK Sort JEtitleK Jscript languageNO.a$ascriptOK $ar b=i=j=tempU $ar aU aNWXU document+:rite-OEnter arrayO1U for-iN4UiJBUiFF1 V bNprompt-Oenter array elementNO=O4O1U aWiXNparseInt-b1U Y for-iN4UiJBUiFF1 V document+:rite-OJbrKOFaWiX1U Y document+:rite-OJbrKSorted array isO1U Balaji Institute of Technology and Science 2!

WT Lab Manual for-iN4UiJBUiFF1 V for-jN4UjJBUjFF1 V if-aWiXJaWjX1 V tempNaWiXU aWiXNaWjXU aWjXNtempU Y Y Y for-iN4UiJBUiFF1 V

Departmrnt of Computer Science and Engineering

document+:rite-OJbrKOFaWiX1U Y JEscriptK JEheadKJEhtmlK

Balaji Institute of Technology and Science

2%

WT Lab Manual OUTPUT

Departmrnt of Computer Science and Engineering

Enter array ' ! B Sorted array is ! ' B

Balaji Institute of Technology and Science

2'

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,r!te Ja/a Scr!0t t(at !n0)ts t(ree !ntegers fro% t(e )ser an+ o)t0)ts t(e!r s)%5 a/erage5 largest. Use alert +!alog &o: to +!s0la- res)lts. PRO*RA# JhtmlK JheadK JtitleK M&C JEtitleK

Jscript languageNOja$ascriptOK $ar a=b=c=n"=n!=n%=m"=m!=sum=a$gU aNprompt-Oenter "st noNO=O O1U bNprompt-Oenter !nd noNO=O O1U cNprompt-Oenter %rd noNO=O O1U n"NparseInt-a1U n!NparseInt-b1U n%NparseInt-c1U sumNn"Fn!Fn%U a$gNsumE%U m"NMath+ma*-n"=n!1U m!NMath+ma*-n%=m"1U alert-Othe sum isN OFsum1U alert-Othe a$g isN OFm!1U alert-Othe ma* no is OFm!1U JEscriptK JEheadKJEhtmlK

Balaji Institute of Technology and Science

2B

WT Lab Manual OUTPUT

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

2)

WT Lab Manual AI#

Departmrnt of Computer Science and Engineering

,r!te a scr!0t to wr!te wor+ e?)!/alent of a c(ec2 a%o)nt. PRO*RA# JhtmlK JAE&DK JSC8I,T L&/(0&(ENO.a$aScriptOK $ar chec9flag N OfalseOU

function chec9-field1 V if -chec9flag NN OfalseO1 V for -i N 4U i J field+lengthU iFF1 V fieldWiX+chec9ed N trueUY chec9flag N OtrueOU return O0nchec9 &llOU Y else V for -i N 4U i J field+lengthU iFF1 V fieldWiX+chec9ed N falseU Balaji Institute of Technology and Science 22

WT Lab Manual Y chec9flag N OfalseOU return OChec9 &llOU Y Y

Departmrnt of Computer Science and Engineering

JEscriptK JEAE&DK JB#DLK JcenterK Jform nameNmyform actionNOO methodNpostK JtableK JtrKJtdK JbKLour >a$orite Scripts ` LanguagesJEbKJbrK Jinput typeNchec9bo* nameNlist $alueNO"OK.a$aJbrK Jinput typeNchec9bo* nameNlist $alueNO!OK.a$aScriptJbrK Jinput typeNchec9bo* nameNlist $alueNO%OK&S,JbrK Jinput typeNchec9bo* nameNlist $alueNO'OKATMLJbrK Jinput typeNchec9bo* nameNlist $alueNOBOKSMLJbrK JbrK JinputtypeNbutton $alueNOChec9 &llO JEtdKJEtrK JEtableK Balaji Institute of Technology and Science 23 onClic9NOthis+$alueNchec9-this+form+list1OK

WT Lab Manual JEformK JEcenterK JEbodyK JEhtmlK

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

27

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# Pract!ce wr!t!ng css r)les PRO*RA# JhtmlK JheadK JtitleKstyle sheetsJEtitleK Jstyle typeNIte*tEcssIKJEheadK emVbac9groundcolor?]3444ffUcolorN:hiteY h"Vfont family?Lucida consoleUcolor?redY pVfont si<e?%4ptY spVcolor?redY JEstyleK JEheadK JbodyK Jh!classNIspIKtestingJEh!K JemKJh"KtestJEh"KJEemK JemKtest"JEemK JpKtest!JEpK JEbodyK JEhtmlK

Balaji Institute of Technology and Science

34

WT Lab Manual OUTPUT testing test test1 test!

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

3"

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,r!te a we& 0age t(at +!s0la-s gra+es of a st)+ent. PRO*RA# JhtmlK JheadK JtitleK ST0DE/T (8&DES JEtitleK Jscript languageNOja$a scriptOK $ar a=n=b=c=sumN4=a$gN4U bNprompt-Oenter student noNO=O4O1U nNparseInt-b1U :hile-nJN"41 V aNprompt-Oenter ur gradeNO=O4O1U cNparseInt-a1U sumNsumFcU nFFU Y a$gNsumEnU document+:rite-Oa$g isOFa$g1U JEscriptK JEheadKJEhtmlK

Balaji Institute of Technology and Science

3!

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,r!te a f)nct!on t(at res0on+s to a cl!c2 an-w(ere on t(e 0age. PRO*RA# JhtmlK JbodyK JcenterK WJa hrefNOEO onMouse#$erNOdocument+bgColorNT(reenTOK(8EE/JEaKX WJa hrefNOEO onMouse#$erNOdocument+bgColorNT8edTOK8EDJEaKX WJa hrefNOEO onMouse#$erNOdocument+bgColorNTorangeTOKorangeJEaKX WJa hrefNOEO onMouse#$erNOdocument+bgColorNTgreenTOKbrightgreenJEaKX WJa hrefNOEO onMouse#$erNOdocument+bgColorNTseagreenTOKseagreenJEaKX JEcenterK JEbodyK JEhtmlK

Balaji Institute of Technology and Science

3%

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

3'

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for implementation of >onts PRO*RA# JhtmlK JheadK JtitleK>ont TestJEtitleK JEheadK JbodyK JbrKJbKJfont faceNITimes /e: 8omanIK>ont Chec9JEfontKJEbK JbrKJiKJfont faceNITimes /e: 8omanIKJfont si<eN'KJfont colorNIblac9IK&ll #perationsJEfontKJEiK JbrKJiKJbKBold italic chec9JEbKJEiK JEbodyK JEhtmlK

OUTPUT 8ont C(ec2 All Operations Bold italic check

Balaji Institute of Technology and Science

3B

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for creating a table PRO*RA# JZ code to sho: the use of tablesK JhtmlK JheadK JtitleK 0sing TablesJEtitleK JEheadK JbodyKJcenterK Jtable borderNO"O :idthNOBO heightNOBO bordercolorNOredOK JcaptionK Student Mar9s JEcaptionK JtheadK JtrK JthK/ameJEthK JthKMar9sJEthK JEtrK JEtheadK JtbodyK JtrK JtdKalo9JEtdK JtdK74JEtdK JEtrK JtrK Balaji Institute of Technology and Science 3)

WT Lab Manual JtdKanujJEtdK JtdK74JEtdK JEtrK JtrK JtdKan9ushJEtdK JtdK74JEtdK JEtrK JEtbodyK JtfootK JtrK

Departmrnt of Computer Science and Engineering

JthK&$g Mar9sJEthK JthK74JEthK JEtrK JEtfootK JEbodyK JEhtmlK

Balaji Institute of Technology and Science

32

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

33

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for implementation of List PRO*RA# JhtmlK JbodyK JulK JliKlist"JEliK JEulKJulK JliK(necJEliK JolK JliKITJEliK JliKCSEJEliK JliKECEJEliK JEolKJEolKJEulKJEbodyKJEhtmlK OUTPUT b list" b (nec "+ IT !+ CSE %+ ECE

Balaji Institute of Technology and Science

37

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# Write a ja$a script to compute addition of t:o numbers PRO*RA# JhtmlK JheadK JtitleK .a$a Script &dd JEtitleK Jscript languageNOja$a scriptOK alert-O&ddition Pn of t:o numbersZO1U $ar num"= num!=n"=n!=sumU num"N:indo:+ prompt-OEnter the first numberO=O4O1U num!N:indo:+ prompt-OEnter the second numberO=O4O1U n"NparseInt-num"1U n!NparseInt-num!1U sumNn"Fn!U alert-Osum isOFsum1U JEscriptK JEheadK JbodyK

JEbodyK JEhtmlK Balaji Institute of Technology and Science 74

WT Lab Manual OUTPUT

Departmrnt of Computer Science and Engineering

A++!t!on of .A an+ "A 3 HA

Balaji Institute of Technology and Science

7"

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for implementation of S:itch case PRO*RA# JhtmlK JheadK JtitleK 0SI/( TAE SWITCA ST&TEME/T JEtitleK Jscript language NO.a$ascriptOK $ar numNtrueU$ar choice=ch=ft=lt=headU choiceN:indo:+prompt-O 0/DE8LI/ED ++++++O=O"O1U ch NparseInt-choice1U s:itch-ch1 V case "? ftNOJbKOU ltNOJEbKOU headNOJh"K This is in B#LD JEh"KOU brea9U case !? ftNOJiKOU ltNOJEiKOU headNOJh"K This is in IT&LICS JEh"KOU brea9U Balaji Institute of Technology and Science 7! "+ B#LD !+ IT&LICS %+

WT Lab Manual case %? ftNOJuKOU ltNOJEuKOU

Departmrnt of Computer Science and Engineering

headNOJh"K This is in 0/DE8LI/ED JEh"KOU brea9U default ? numNfalseU brea9U Y if-numNNtrue1 document+:rite-headFftFO Jh"KTe*t JEh"KOFlt1U else document+:rite-O Jh"KWrong Choice Z try again+O1U JEscriptK JEheadK JbodyK JEbodyK JEhtmlK OUTPUT

Balaji Institute of Technology and Science

7%

WT Lab Manual T(!s !s !n UNDERLINED Te:t

Departmrnt of Computer Science and Engineering

AI# Progra% to f!n+ %a:!%)% of H noIs PRO*RA# JhtmlK JheadK JtitleK Ma*imum of /umber JEtitleK Jscript language NO.a$ascriptOK $ar a=b=c=a"=b"=c"=ma*"=ma*!U aN:indo:+prompt-OEnter first number KKO=O4O1U bN:indo:+prompt-OEnter second number KKO=O"O1U cN:indo:+prompt-OEnter third number KKO=O!O1U

a"NparseInt-a1U b"NparseInt-b1U c"NparseInt-c1U ma*"NMath+ma*-a=b1U ma*!NMath+ma*-ma*"=c1U document+:rite-OJbrKJbrKJbrKJcenterKJh"KMa*imum number of OFa"FO= OFb"FO and OFc"FO is OFma*!1U JEscriptK JbodyKJEbodyK JEhtmlK Balaji Institute of Technology and Science 7'

WT Lab Manual

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

7B

WT Lab Manual

Departmrnt of Computer Science and Engineering

O)t0)t

#a:!%)% n)%&er of @5 . an+

!s

Balaji Institute of Technology and Science

7)

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for Entering a pass:ord PRO*RA# JhtmlK JheadK JtitleKe$ent clic9ingJEtitleK Jscript languageNOja$ascriptOK function pass:ordChec9-1 V $ar p:Nole+p:+$alueU $ar cp:Nole+cp:+$alueU if-p:ZNcp:1 :indo:+alert-O8e enter your pass:ordO1U Y JEscriptKJEheadK

JcenterK Jbody font si<eNB bgcolorNOcyanO leftmarginN"!4 topmarginN"!4 K J>orm idNOoleO onSubmitNOpass:ordChec9-this1OK

Balaji Institute of Technology and Science

72

WT Lab Manual

Departmrnt of Computer Science and Engineering

,&SSW#8D?`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`n bspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbs pU `nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU Jinput typeNpass:ord nameNOp:Osi<eN"4 ma* lengthN3 K JEinputK JbrKJbrKJbrK C#/>I8M ,&SSW#8D`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`nbspU`n bspU`nbspU`nbspU`nbspU Jinput typeNpass:ord nameNOcp:Osi<eN"4 ma* lengthN3 K JEinputK JbrKJbrKJbrK Jinput typeNOsubmitO nameNOsubO $alueNOsubmitOK `nbspU `nbspU Jinput typeNOresetO nameNOrtO $alueNOresetOKJEcenter JEbodyK JEhtmlK

Balaji Institute of Technology and Science

73

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

77

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for implementation of Thambola PRO*RA# JhtmlK JheadK JtitleK . Script JEtitleK Jscript languageNOja$ascriptOK $ar $alueU alert-OthambolaO1U document+:riteln-OJtable borderN" :idthNB4Q alignNcenterK O1U

document+:riteln-OJcaptionK Thambola JEcaptionKJtrKO1U

for-$ar iN"UiJN"BUiFF1 V

$alueNMath+floor-"FMath+random-1^"441U document+:riteln-OJtdKOF$alueFOJEtdKO1U if-iQBNN4 1 document+:riteln-OJEtrKJtrKO1U Y document+:riteln-OJEtrKJEtableKO1U

Balaji Institute of Technology and Science

"44

WT Lab Manual JEscriptK JEheadK JEhtmlK

Departmrnt of Computer Science and Engineering

#0T,0T

Balaji Institute of Technology and Science

"4"

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for >actorial PRO*RA# JhtmlK JheadKJuKJcenterKJbK8EC08SI5E >&CT#8I&LJEbKJbrKJEuK JtitleK factorial JEtitleK Jscript langNOja$ascriptOK $ar i=fact=a=nU aNprompt-Oenter any noO=O4O1U nNparseInt-a1U for -iN"UiJNnUiFF1 V resNfact-i1U document+:riteln-OJbrK TAE >&CT#8I&L #> OFiFO Y function fact-*1 V if-*NN4 \\ *NN"1 return-"1U else return-*^fact-* "11U Balaji Institute of Technology and Science "4! IS KK OFres1U

WT Lab Manual Y JEscriptK JEheadK JEhtmlK #0T,0T

Departmrnt of Computer Science and Engineering

RECURSIVE 8ACTORIAL TAE >&CT#8I&L #> " IS KK " TAE >&CT#8I&L #> ! IS KK ! TAE >&CT#8I&L #> % IS KK ) TAE >&CT#8I&L #> ' IS KK !' TAE >&CT#8I&L #> B IS KK "!4

Balaji Institute of Technology and Science

"4%

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for addition of t:o matrices PRO*RA# JhtmlK JheadK JtitleK&rray &dditionJEtitleK Jscript languageNOja$ascriptOK $ar a=n=i=j=b=cU aNWWX=WXXU bNWWX=WXXU cNWWX=WXXU nNparseInt-:indo:+prompt-OEnter the array si<eO=O O11U :indo:+alert-OEnter the elements in the first arrayO1U for -iN4UiJnUiFF1 V for -jN4UjJnUjFF1 V aWiXWjXNparseInt-:indo:+prompt-OEnter the elementO=OO11U Y Y :indo:+alert-OEnter the elements in the second arrayO1U for -iN4UiJnUiFF1 V Balaji Institute of Technology and Science "4'

WT Lab Manual

Departmrnt of Computer Science and Engineering for -jN4UjJnUjFF1 V bWiXWjXNparseInt-:indo:+prompt-OEnter the elementO=OO11U Y Y document+:riteln-OJbrKThe elements of Ist Matri* are++O1U for -iN4UiJnUiFF1 V for -jN4UjJnUjFF1 V document+:riteln-aWiXWjX1U Y Y document+:riteln-OJbrKThe elements of IInd Matri* are++O1U for -iN4UiJnUiFF1 V for -jN4UjJnUjFF1 V document+:riteln-bWiXWjX1U Y Y for -iN4UiJnUiFF1 V

Balaji Institute of Technology and Science

"4B

WT Lab Manual

Departmrnt of Computer Science and Engineering for -jN4UjJnUjFF1 V cWiXWjXNaWiXWjXFbWiXWjXU Y Y document+:riteln-OJbrKThe addition of matrices is?O1U for -iN4UiJnUiFF1 V for -jN4UjJnUjFF1 V

document+:riteln-cWiXWjX1U Y Y JEscriptK JEheadK JEhtmlK #0T,0T

Balaji Institute of Technology and Science

"4)

WT Lab Manual

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

"42

WT Lab Manual

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

"43

WT Lab Manual

Departmrnt of Computer Science and Engineering

The elements of Ist Matri* are++ " " " " The elements of IInd Matri* are++ " " " " The addition of matrices is? ! ! ! !

Balaji Institute of Technology and Science

"47

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for #nline E*am PRO*RA# JhtmlK JheadK JtitleK E*am JEtitleK Jscript languageNO.a$ascriptOK function e*am-form1 V $ar iN4U if-form+oneW!X+chec9ed1 iNiF"U if-form+threeW4X+chec9ed1 iNiF"U if-form+fourW4X+chec9ed1 iNiF"U if-form+fi$eW"X+chec9ed1 iNiF"U :indo:+alert-Oscore isOFi1U Y JEscriptK JEheadK JbodyK Balaji Institute of Technology and Science ""4

WT Lab Manual

Departmrnt of Computer Science and Engineering

Jform onSubmitNOe*am-this1OK JcenterK#nline e*amJEcenterK J,K Jh"K"1Which is platform independent languageJEh"K Jinput typeNOradioO nameNOoneO $alueNOcFFOK JlabelKcFFJElabelK Jinput typeNOradioO nameNOoneO $alueNOcOK JlabelKcJElabelK Jinput typeNOradioO nameNOoneO $alueNOja$aOK JlabelK.a$aJElabelK Jinput typeNOradioO nameNOoneO $alueNObasicOK JlabelKB&SICJElabelK JE,K J,K Jh"K!1Which is class of all class in ja$aJEh"K Jinput typeNOradioO nameNOthreeO $alueNOobjectOK JlabelK#bjectJElabelK Jinput typeNOradioO nameNOthreeO $alueNOmathOK JlabelKMathJElabelK Jinput typeNOradioO nameNOthreeO $alueNOsystemOK JlabelKSystemJElabelK Jinput typeNOradioO nameNOthreeO $alueNOgraphicOK JlabelK(raphicJElabelK Balaji Institute of Technology and Science """

WT Lab Manual JE,K J,K

Departmrnt of Computer Science and Engineering

Jh"K%1>rame pac9age is in :hich pac9ageJEh"K Jinput typeNOradioO nameNOfourO $alueNOa:tOK JlabelK&WTJElabelK Jinput typeNOradioO nameNOfourO $alueNOappletOK JlabelK&ppletJElabelK Jinput typeNOradioO nameNOfourO $alueNOlangOK JlabelKLangJElabelK Jinput typeNOradioO nameNOfourO $alueNOs:ingOK JlabelKS:ingJElabelK JE,K J,K Jh"K'1.a$a does not supportJEh"K Jinput typeNOradioO nameNOfi$eO $alueNOinheritanceOK JlabelKInheritanceJElabelK Jinput typeNOradioO nameNOfi$eO $alueNOmultiple inheritanceOK JlabelKMultiple inheritanceJElabelK Jinput typeNOradioO nameNOfi$eO $alueNOpolymorphismOK JlabelK,olymorphismJElabelK Jinput typeNOradioO nameNOfi$eO $alueNOencryptionOK JlabelKEncryptionJElabelK JE,K J,KJcenterK ""!

Balaji Institute of Technology and Science

WT Lab Manual

Departmrnt of Computer Science and Engineering

Jinput typeNOsubmitO $alueNOsubmitOK Jinput typeNOresetO nameNOresetO $alueNOresetOK JEcenterK OUTPUT #nline e*am .',(!c( !s 0latfor% !n+e0en+ent lang)age cFF c .a$a B&SIC JEpK JEbodyKJEhtmlK

"',(!c( !s class of all class !n 9a/a #bject Math System (raphic

H'8ra%e 0ac2age !s !n w(!c( 0ac2age &WT &pplet Lang S:ing

J'Ja/a +oes not s)00ort Inheritance Multiple inheritance ,olymorphism


su#mit reset

Encryption

Balaji Institute of Technology and Science

""%

WT Lab Manual AI# ,rogram for Bubble Sort PRO*RA# JhtmlK JheadK JtitleKBubble SortJEtitleK Jscript languageNO.a$aScriptOK $ar aNW%=B=!=7="=3X $ar t=nN)U for- $ar iN4UiJnUiFF1 V for-$ar jN4UjJn "UjFF1 V if-aWjXKaWjF"X1 V tNaWjXU aWjXNaWjF"XU aWjF"XNtU Y Y Y

Departmrnt of Computer Science and Engineering

document+:rite-OJh"KThe sorted order is?`nbspUO1U for-iN4UiJnUiFF1 Balaji Institute of Technology and Science ""'

WT Lab Manual V document+:rite-aWiX1U

Departmrnt of Computer Science and Engineering

document+:rite-O`nbspU`nbspUO1U Y JEscriptK JEheadK Jbody topmarginNB4 leftmarginN"44KJEbodyK JEhtmlK #0T,0T T(e sorte+ or+er !s3 . " H = K

Balaji Institute of Technology and Science

""B

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for implementation of Linear Search PRO*RA# JhtmlK JheadK JtitleK&rray &dditionJEtitleK Jscript languageNOja$ascriptOK $ar flagN4=a=n=9ey=i $ar aNne: &rray-"41U nNparseInt-:indo:+prompt-OEnter the si<e of the arrayO=OO11U :indo:+alert-OEnter the elements in the arrayO1U for -iN4UiJnUiFF1 V aWiXNparseInt-:indo:+prompt-OEnter the element O=OO11U Y 9eyNparseInt-:indo:+prompt-OEnter the searching elementO=OO11U for -iN4UiJnUiFF1 V if -aWiXNN9ey1 V flagN"U :indo:+alert-O element is found at location OF-iF"11U Y Balaji Institute of Technology and Science "")

WT Lab Manual Y if -flagNN41 :indo:+alert-O element is not

Departmrnt of Computer Science and Engineering

foundO1UEEdocument+:riteln-OThe element is not foundO1U JEscriptK JEheadK Jbody bgcolorNOpin9OK JEbodyK JEhtmlK

Balaji Institute of Technology and Science

""2

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

""3

WT Lab Manual

Departmrnt of Computer Science and Engineering

Balaji Institute of Technology and Science

""7

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram for displaying an image on mouse clic9 PRO*RA#

JhtmlK JheadK JtitleKonclic9 imageJEtitleK JEheadK Jbody leftmarginN"44 topmarginN"44 bgcolorNO]""""""OK Jimg nameNOimagesO :idthN!44 heightN!44 srcN"+gif OKJpK JformKJh%K Jinput typeNObuttonO onClic9NOdocument+images+srcNT"+gifT OK Jinput typeNObuttonO $alueNOimage!O onClic9NOdocument+images+srcNT!+gifT OK Jinput typeNObuttonO onClic9NOdocument+images+srcNT%+gifT OK JEformKJEh%KJEbodyKJEhtmlK $alueNOimage%O $alueNOimage"O

Balaji Institute of Technology and Science

"!4

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

"!"

WT Lab Manual

Departmrnt of Computer Science and Engineering

AI# ,rogram Code To E*hibit Blending Effect PRO*RA# JhtmlK JheadK JtitleK . Script JEtitleK Jscript languageNOja$ascriptOK function blend#ut-1 V EEte*tInput+filters-OblendTransO1+apply-1U EEte*tInput+style+$isibilityNOhiddenOU te*tInput+filters-OblendTransO1+play-1U Y JEscriptK JEheadK JbodyK Jdi$ idNOte*tInputO onmouseo$erNOblend#ut-1O styleNO:idth?"44U filter?blendTrans-durationNB1OK Jh"K JEdi$K JEbodyK JEhtmlK M0LTI MEDI& JEh"K

Balaji Institute of Technology and Science

"!!

WT Lab Manual

Departmrnt of Computer Science and Engineering

OUTPUT

Balaji Institute of Technology and Science

"!%

WT Lab Manual

Departmrnt of Computer Science and Engineering

VIVA ?)est!ons > Answers "+ ,(at !s t(e +!fference &etween 0roce+)ral an+ o&9ect7or!ente+ 0rogra%sL a1 In procedural program= programming logic follo:s certain procedures and the instructions are e*ecuted one after another+ In ##, program= unit of program is object= :hich is nothing but combination of data and code+ b1 In procedural program= data is e*posed to the :hole program :hereas in ##,s program= it is accessible :ith in the object and :hich in turn assures the security of the code+ !+ ,(at are Enca0s)lat!on5 In(er!tance an+ Pol-%or0(!s%L Encapsulation is the mechanism that binds together code and data it manipulates and 9eeps both safe from outside interference and misuse+ Inheritance is the process by :hich one object ac6uires the properties of another object+ ,olymorphism is the feature that allo:s one interface to be used for general class actions+ %+ ,(at !s t(e +!fference &etween Ass!gn%ent an+ In!t!al!4at!onL &ssignment can be done as many times as desired :hereas initiali<ation can be done only once+ '+ ,(at !s OOPsL #bject oriented programming organi<es a program around its data= i+ e+ = objects and a set of :ell defined interfaces to that data+ &n object oriented program can be characteri<ed as data controlling access to code+ B+ ,(at are Class5 Constr)ctor an+ Pr!%!t!/e +ata t-0esL Class is a template for multiple objects :ith similar features and it is a blue print for objects+ It defines a type of object according to the data the object can hold and the operations the object can perform+ Constructor is a special 9ind of method that determines ho: an object is initiali<ed :hen created+ ,rimiti$e data types are 3 types and they are? byte= short= int= long= float= double= boolean= char+ )+ ,(at !s an O&9ect an+ (ow +o -o) allocate %e%or- to !tL #bject is an instance of a class and it is a soft:are unit that combines a structured set of data :ith a set of operations for inspecting and manipulating that data+ When an object is created using ne: operator= memory is allocated to it+ 2+ ,(at !s t(e +!fference &etween constr)ctor an+ %et(o+L Constructor :ill be automatically in$o9ed :hen an object is created :hereas method has to be called e*plicitly+ 3+ ,(at are %et(o+s an+ (ow are t(e- +ef!ne+L Methods are functions that operate on instances of classes in :hich they are defined+ #bjects can communicate :ith each other using methods and can call methods in other classes+ Method definition has four parts+ They are name of the method= type of object or primiti$e type the method returns= a list of parameters and the body of the method+ & methodDs signature is a combination of the first three parts mentioned abo$e+ 7+ ,(at !s t(e )se of &!n an+ l!& !n JD;L Bin contains all tools such as ja$ac= applet$ie:er= a:t tool= etc+= :hereas lib contains &,I and all pac9ages+ "4+ ,(at !s cast!ngL Casting is used to con$ert the $alue of one type to another+ ""+ 1ow %an- wa-s can an arg)%ent &e 0asse+ to a s)&ro)t!ne an+ e:0la!n t(e%L &n argument can be passed in t:o :ays+ They are passing by $alue and passing by reference+ ,assing by $alue? This method copies the $alue of an argument into the formal parameter of the subroutine+ ,assing by reference? In this method= a reference to an argument -not the $alue of the argument1 is passed to the parameter+ Balaji Institute of Technology and Science "!'

WT Lab Manual

Departmrnt of Computer Science and Engineering

"!+ ,(at !s t(e +!fference &etween an arg)%ent an+ a 0ara%eterL While defining method= $ariables passed in the method are called parameters+ While using those methods= $alues passed to those $ariables are called arguments+ "%+ ,(at are +!fferent t-0es of access %o+!f!ersL public? &ny thing declared as public can be accessed from any:here+ pri$ate? &ny thing declared as pri$ate canDt be seen outside of its class+ protected? &ny thing declared as protected can be accessed by classes in the same pac9age and subclasses in the other pac9ages+ default modifier ? Can be accessed only to classes in the same pac9age+ "'+ ,(at !s f!nal5 f!nal!4e$' an+ f!nall-L final ? final 9ey:ord can be used for class= method and $ariables+ & final class cannot be subclassed and it pre$ents other programmers from subclassing a secure class to in$o9e insecure methods+ & final method canDt be o$erridden+ & final $ariable canDt change from its initiali<ed $alue+ finali<e-1 ? finali<e-1 method is used just before an object is destroyed and can be called just prior to garbage collection+ finally ? finally= a 9ey :ord used in e*ception handling= creates a bloc9 of code that :ill be e*ecuted after a tryEcatch bloc9 has completed and before the code follo:ing the tryEcatch bloc9+ The finally bloc9 :ill e*ecute :hether or not an e*ception is thro:n+ >or e*ample= if a method opens a file upon e*it= then you :ill not :ant the code that closes the file to be bypassed by the e*ception handling mechanism+ This finally 9ey:ord is designed to address this contingency+ "B+ ,(at !s UNICODEL 0nicode is used for internal representation of characters and strings and it uses ") bits to represent each other+ ")+ ,(at !s *ar&age Collect!on an+ (ow to call !t e:0l!c!tl-L When an object is no longer referred to by any $ariable= ja$a automatically reclaims memory used by that object+ This is 9no:n as garbage collection+ System+ gc-1 method may be used to call it e*plicitly+ "2+ ,(at !s f!nal!4e$' %et(o+L finali<e -1 method is used just before an object is destroyed and can be called just prior to garbage collection+ "3+ ,(at are Trans!ent an+ Volat!le #o+!f!ersL Transient? The transient modifier applies to $ariables only and it is not stored as part of its objectDs ,ersistent state+ Transient $ariables are not seriali<ed+ 5olatile? 5olatile modifier applies to $ariables only and it tells the compiler that the $ariable modified by $olatile can be changed une*pectedly by other parts of the program+ "7+ ,(at !s %et(o+ o/erloa+!ng an+ %et(o+ o/err!+!ngL Method o$erloading? When a method in a class ha$ing the same method name :ith different arguments is said to be method o$erloading+ Method o$erriding ? When a method in a class ha$ing the same method name :ith same arguments is said to be method o$erriding+ !4+ ,(at !s +!fference &etween o/erloa+!ng an+ o/err!+!ngL a1 In o$erloading= there is a relationship bet:een methods a$ailable in the same class :hereas in o$erriding= there is relationship bet:een a superclass method and subclass method+ b1 #$erloading does not bloc9 inheritance from the superclass :hereas o$erriding bloc9s inheritance from the superclass+ c1 In o$erloading= separate methods share the same name :hereas in o$erriding= subclass method replaces the superclass+ d1 #$erloading must ha$e different method signatures :hereas o$erriding must ha$e same signature+ !"+ ,(at !s %eant &- In(er!tance an+ w(at are !ts a+/antagesL Inheritance is the process of inheriting all the features from a class+ The ad$antages of inheritance are Balaji Institute of Technology and Science "!B

WT Lab Manual

Departmrnt of Computer Science and Engineering

reusability of code and accessibility of $ariables and methods of the super class by subclasses+ !!+ ,(at !s t(e +!fference &etween t(!s$' an+ s)0er$'L this-1 can be used to in$o9e a constructor of the same class :hereas super-1 can be used to in$o9e a super class constructor+ !%+ ,(at !s t(e +!fference &etween s)0erclass an+ s)&classL & super class is a class that is inherited :hereas sub class is a class that does the inheriting+ !'+ ,(at %o+!f!ers %a- &e )se+ w!t( to07le/el classL public= abstract and final can be used for top le$el class+ !B+ ,(at are !nner class an+ anon-%o)s classL Inner class ? classes defined in other classes= including those defined in methods are called inner classes+ &n inner class can ha$e any accessibility including pri$ate+ &nonymous class ? &nonymous class is a class defined inside a method :ithout a name and is instantiated and declared in the same place and cannot ha$e e*plicit constructors+ !)+ ,(at !s a 0ac2ageL & pac9age is a collection of classes and interfaces that pro$ides a high le$el layer of access protection and name space management+ !2+ ,(at !s a reflect!on 0ac2ageL ja$a+ lang+ reflect pac9age has the ability to analy<e itself in runtime+ !3+ ,(at !s !nterface an+ !ts )seL Interface is similar to a class :hich may contain methodDs signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it+ Interfaces are useful for? a1Declaring methods that one or more classes are e*pected to implement b1Capturing similarities bet:een unrelated classes :ithout forcing a class relationship+ c1Determining an objectDs programming interface :ithout re$ealing the actual body of the class+ !7+ ,(at !s an a&stract classL &n abstract class is a class designed :ith implementation gaps for subclasses to fill in and is deliberately incomplete+ %4+ ,(at !s t(e +!fference &etween Integer an+ !ntL a1 Integer is a class defined in the ja$a+ lang pac9age= :hereas int is a primiti$e data type defined in the .a$a language itself+ .a$a does not automatically con$ert from one to the other+ b1 Integer can be used as an argument for a method that re6uires an object= :hereas int can be used for calculations+ %"+ ,(at !s a clonea&le !nterface an+ (ow %an- %et(o+s +oes !t conta!nL It is not ha$ing any method because it is a T&((ED or M&8GE8 interface+ %!+ ,(at !s t(e +!fference &etween a&stract class an+ !nterfaceL a1 &ll the methods declared inside an interface are abstract :hereas abstract class must ha$e at least one abstract method and others may be concrete or abstract+ b1 In abstract class= 9ey :ord abstract must be used for the methods :hereas interface :e need not use that 9ey:ord for the methods+ c1 &bstract class must ha$e subclasses :hereas interface canDt ha$e subclasses+ %%+ Can -o) (a/e an !nner class !ns!+e a %et(o+ an+ w(at /ar!a&les can -o) accessL Les= :e can ha$e an inner class inside a method and final $ariables can be accessed+ %'+ ,(at !s t(e +!fference &etween Str!ng an+ Str!ng B)fferL a1 String objects are constants and immutable :hereas StringBuffer objects are not+ b1 String class supports constant strings :hereas StringBuffer class supports gro:able and modifiable strings+ Balaji Institute of Technology and Science "!)

WT Lab Manual

Departmrnt of Computer Science and Engineering

%B+ ,(at !s t(e +!fference &etween Arra- an+ /ectorL &rray is a set of related data type and static :hereas $ector is a gro:able array of objects and dynamic+ %)+ ,(at !s t(e +!fference &etween e:ce0t!on an+ errorL The e*ception class defines mild error conditions that your program encounters+ E*ceptions can occur :hen trying to open the file= :hich does not e*ist= the net:or9 connection is disrupted= operands being manipulated are out of prescribed ranges= the class file you are interested in loading is missing+ The error class defines serious error conditions that you should not attempt to reco$er from+ In most cases it is ad$isable to let the program terminate :hen such an error is encountered+ %2+ ,(at !s t(e +!fference &etween 0rocess an+ t(rea+L ,rocess is a program in e*ecution :hereas thread is a separate path of e*ecution in a program+ %3+ ,(at !s %)lt!t(rea+!ng an+ w(at are t(e %et(o+s for !nter7t(rea+ co%%)n!cat!on an+ w(at !s t(e class !n w(!c( t(ese %et(o+s are +ef!ne+L Multithreading is the mechanism in :hich more than one thread run independent of each other :ithin the process+ :ait -1= notify -1 and notify&ll-1 methods can be used for inter thread communication and these methods are in #bject class+ :ait-1 ? When a thread e*ecutes a call to :ait-1 method= it surrenders the object loc9 and enters into a :aiting state+ notify-1 or notify&ll-1 ? To remo$e a thread from the :aiting state= some other thread must ma9e a call to notify-1 or notify&ll-1 method on the same object+ %7+ ,(at !s t(e class an+ !nterface !n 9a/a to create t(rea+ an+ w(!c( !s t(e %ost a+/antageo)s %et(o+L Thread class and 8unnable interface can be used to create threads and using 8unnable interface is the most ad$antageous method to create threads because :e need not e*tend thread class here+ '4+ ,(at are t(e states assoc!ate+ !n t(e t(rea+L Thread contains ready= running= :aiting and dead states+ '"+ ,(at !s s-nc(ron!4at!onL Synchroni<ation is the mechanism that ensures that only one thread is accessed the resources at a time+ '!+ ,(en -o) w!ll s-nc(ron!4e a 0!ece of -o)r co+eL When you e*pect your code :ill be accessed by different threads and these threads may change a particular data causing data corruption+ '%+ ,(at !s +ea+loc2L When t:o threads are :aiting each other and canDt precede the program is said to be deadloc9+ ''+ ,(at !s +ae%on t(rea+ an+ w(!c( %et(o+ !s )se+ to create t(e +ae%on t(rea+L Daemon thread is a lo: priority thread :hich runs intermittently in the bac9 ground doing the garbage collection operation for the ja$a runtime system+ setDaemon method is used to create a daemon thread+ 'B+ Are t(ere an- glo&al /ar!a&les !n Ja/a5 w(!c( can &e accesse+ &- ot(er 0art of -o)r 0rogra%L /o= it is not the main method in :hich you define $ariables+ (lobal $ariables is not possible because concept of encapsulation is eliminated here+ ')+ ,(at !s an a00letL &pplet is a dynamic and interacti$e program that runs inside a :eb page displayed by a ja$a capable bro:ser+ '2+ ,(at !s t(e +!fference &etween a00l!cat!ons an+ a00letsL a1&pplication must be run on local machine :hereas applet needs no e*plicit installation on local machine+ b1&pplication must be run e*plicitly :ithin a ja$a compatible $irtual machine :hereas applet loads and runs itself automatically in a ja$a enabled bro:ser+ d1&pplication starts e*ecution :ith its main method :hereas applet starts e*ecution :ith its init Balaji Institute of Technology and Science "!2

WT Lab Manual

Departmrnt of Computer Science and Engineering

method+ e1&pplication can run :ith or :ithout graphical user interface :hereas applet must run :ithin a graphical user interface+ '3+ 1ow +oes a00let recogn!4e t(e (e!g(t an+ w!+t(L 0sing get,arameters-1 method+ '7+ ,(en +o -o) )se co+e&ase !n a00letL When the applet class file is not in the same directory= codebase is used+ B4+ ,(at !s t(e l!fec-cle of an a00letL init-1 method Can be called :hen an applet is first loaded start-1 method Can be called each time an applet is started+ paint-1 method Can be called :hen the applet is minimi<ed or ma*imi<ed+ stop-1 method Can be used :hen the bro:ser mo$es off the appletDs page+ destroy-1 method Can be called :hen the bro:ser is finished :ith the applet+ B"+ 1ow +o -o) set sec)r!t- !n a00letsL using setSecurityManager-1 method B!+ ,(at !s an e/ent an+ w(at are t(e %o+els a/a!la&le for e/ent (an+l!ngL &n e$ent is an e$ent object that describes a state of change in a source+ In other :ords= e$ent occurs :hen an action is generated= li9e pressing button= clic9ing mouse= selecting a list= etc+ There are t:o types of models for handling e$ents and they are? a1 e$ent inheritance model and b1 e$ent delegation model B%+ ,(at are t(e a+/antages of t(e %o+el o/er t(e e/ent7!n(er!tance %o+elL The e$ent delegation model has t:o ad$antages o$er the e$ent inheritance model+ They are? a1It enables e$ent handling by objects other than the ones that generate the e$ents+ This allo:s a clean separation bet:een a componentDs design and its use+ b1It performs much better in applications :here many e$ents are generated+ This performance impro$ement is due to the fact that the e$ent delegation model does not ha$e to be repeatedly process unhandled e$ents as is the case of the e$ent inheritance+ B'+ ,(at !s so)rce an+ l!stenerL source ? & source is an object that generates an e$ent+ This occurs :hen the internal state of that object changes in some :ay+ listener ? & listener is an object that is notified :hen an e$ent occurs+ It has t:o major re6uirements+ >irst= it must ha$e been registered :ith one or more sources to recei$e notifications about specific types of e$ents+ Second= it must implement methods to recei$e and process these notifications+ BB+ ,(at !s a+a0ter classL &n adapter class pro$ides an empty implementation of all methods in an e$ent listener interface+ &dapter classes are useful :hen you :ant to recei$e and process only some of the e$ents that are handled by a particular e$ent listener interface+ Lou can define a ne: class to act listener by e*tending one of the adapter classes and implementing only those e$ents in :hich you are interested+ >or e*ample= the MouseMotion&dapter class has t:o methods= mouseDragged-1and mouseMo$ed-1+ The signatures of these empty are e*actly as defined in the MouseMotionListener interface+ If you are interested in only mouse drag e$ents= then you could simply e*tend MouseMotion&dapter and implement mouseDragged-1 + B)+ ,(at !s %eant &- controls an+ w(at are +!fferent t-0es of controls !n A,TL Controls are components that allo: a user to interact :ith your application and the &WT supports the follo:ing types of controls? Labels= ,ush Buttons= Chec9 Bo*es= Choice Lists= Lists= Scrollbars= Te*t Components+ These controls are subclasses of Component+ B2+ ,(at !s t(e +!fference &etween c(o!ce an+ l!stL & Choice is displayed in a compact form that re6uires you to pull it do:n to see the list of a$ailable choices and only one Balaji Institute of Technology and Science "!3

WT Lab Manual

Departmrnt of Computer Science and Engineering

item may be selected from a choice+ & List may be displayed in such a :ay that se$eral list items are $isible and it supports the selection of one or more list items+ B3+ ,(at !s t(e +!fference &etween scroll&ar an+ scroll0aneL & Scrollbar is a Component= but not a Container :hereas Scrollpane is a Conatiner and handles its o:n e$ents and perform its o:n scrolling+ B7+ ,(at !s a la-o)t %anager an+ w(at are +!fferent t-0es of la-o)t %anagers a/a!la&le !n 9a/a A,TL & layout manager is an object that is used to organi<e components in a container+ The different layouts are a$ailable are >lo:Layout= BorderLayout= CardLayout= (ridLayout and (ridBagLayout+ )4+ 1ow are t(e ele%ents of +!fferent la-o)ts organ!4e+L >lo:Layout? The elements of a >lo:Layout are organi<ed in a top to bottom= left to right fashion+ BorderLayout? The elements of a BorderLayout are organi<ed at the borders -/orth= South= East and West1 and the center of a container+ CardLayout? The elements of a CardLayout are stac9ed= on top of the other= li9e a dec9 of cards+ (ridLayout? The elements of a (ridLayout are of e6ual si<e and are laid out using the s6uare of a grid+ (ridBagLayout? The elements of a (ridBagLayout are organi<ed according to a grid+ Ao:e$er= the elements are of different si<e and may occupy more than one ro: or column of the grid+ In addition= the ro:s and columns may ha$e different si<es+ )"+ ,(!c( conta!ners )se a Bor+er la-o)t as t(e!r +efa)lt la-o)tL Windo:= >rame and Dialog classes use a BorderLayout as their layout+ )!+ ,(!c( conta!ners )se a 8low la-o)t as t(e!r +efa)lt la-o)tL ,anel and &pplet classes use the >lo:Layout as their default layout+ )%+ ,(at are wra00er classesL Wrapper classes are classes that allo: primiti$e types to be accessed as objects+ )'+ ,(at are Vector5 1as(ta&le5 L!n2e+L!st an+ En)%erat!onL 5ector ? The 5ector class pro$ides the capability to implement a gro:able array of objects+ Aashtable ? The Aashtable class implements a Aashtable data structure+ & Aashtable inde*es and stores objects in a dictionary using hash codes as the objectDs 9eys+ Aash codes are integer $alues that identify objects+ Lin9edList? 8emo$ing or inserting elements in the middle of an array can be done using Lin9edList+ & Lin9edList stores each object in a separate lin9 :hereas an array stores object references in consecuti$e locations+ Enumeration? &n object that implements the Enumeration interface generates a series of elements= one at a time+ It has t:o methods= namely hasMoreElements-1 and ne*tElement-1+ AasMoreElemnts-1 tests if this enumeration has more elements and ne*tElement method returns successi$e elements of the series+ )B+ ,(at !s t(e +!fference &etween set an+ l!stL Set stores elements in an unordered :ay but does not contain duplicate elements= :hereas list stores elements in an ordered :ay but may contain duplicate elements+ ))+ ,(at !s a strea% an+ w(at are t(e t-0es of Strea%s an+ classes of t(e Strea%sL & Stream is an abstraction that either produces or consumes information+ There are t:o types of Streams and they are? Byte Streams? ,ro$ide a con$enient means for handling input and output of bytes+ Character Streams? ,ro$ide a con$enient means for handling input ` output of characters+ Byte Streams classes? &re defined by using t:o abstract classes= namely InputStream and #utputStream+ Character Streams classes? &re defined by using t:o abstract classes= namely 8eader and Writer+ Balaji Institute of Technology and Science "!7

WT Lab Manual

Departmrnt of Computer Science and Engineering

)2+ ,(at !s t(e +!fference &etween Rea+erM,r!ter an+ In0)tStrea%MO)t0)t Strea%L The 8eaderEWriter class is character oriented and the InputStreamE#utputStream class is byte oriented+ )3+ ,(at !s an IMO f!lterL &n IE# filter is an object that reads from one stream and :rites to another= usually altering the data in some :ay as it is passed from one stream to another+ )7+ ,(at !s ser!al!4at!on an+ +eser!al!4at!onL Seriali<ation is the process of :riting the state of an object to a byte stream+ Deseriali<ation is the process of restoring these objects+ 24+ ,(at !s JDBCL .DBC is a set of .a$a &,I for e*ecuting SML statements+ This &,I consists of a set of classes and interfaces to enable programs to :rite pure .a$a Database applications+ 2"+ ,(at are +r!/ers a/a!la&leL a1 .DBC #DBC Bridge dri$er b1 /ati$e &,I ,artly .a$a dri$er c1 .DBC /et ,ure .a$a dri$er d1 /ati$e ,rotocol ,ure .a$a dri$er 2!+ ,(at !s t(e +!fference &etween JDBC an+ ODBCL a1 #BDC is for Microsoft and .DBC is for .a$a applications+ b1 #DBC canDt be directly used :ith .a$a because it uses a C interface+ c1 #DBC ma9es use of pointers :hich ha$e been remo$ed totally from .a$a+ d1 #DBC mi*es simple and ad$anced features together and has comple* options for simple 6ueries+ But .DBC is designed to 9eep things simple :hile allo:ing ad$anced capabilities :hen re6uired+ e1 #DBC re6uires manual installation of the #DBC dri$er manager and dri$er on all client machines+ .DBC dri$ers are :ritten in .a$a and .DBC code is automatically installable= secure= and portable on all platforms+ f1 .DBC &,I is a natural .a$a interface and is built on #DBC+ .DBC retains some of the basic features of #DBC+ 2%+ ,(at are t(e t-0es of JDBC Dr!/er #o+els an+ e:0la!n t(e%L There are t:o types of .DBC Dri$er Models and they are? a1 T:o tier model and b1 Three tier model T:o tier model? In this model= .a$a applications interact directly :ith the database+ & .DBC dri$er is re6uired to communicate :ith the particular database management system that is being accessed+ SML statements are sent to the database and the results are gi$en to user+ This model is referred to as clientEser$er configuration :here user is the client and the machine that has the database is called as the ser$er+ Three tier model? & middle tier is introduced in this model+ The functions of this model are? a1 Collection of SML statements from the client and handing it o$er to the database= b1 8ecei$ing results from database to the client and c1 Maintaining control o$er accessing and updating of the abo$e+ 2'+ ,(at are t(e ste0s !n/ol/e+ for %a2!ng a connect!on w!t( a +ata&ase or (ow +o -o) connect to a +ata&aseLa1 Loading the dri$er ? To load the dri$er= Class+ for/ame-1 method is used+ Class+ for/ame-Isun+ jdbc+ odbc+ .dbc#dbcDri$erI1U When the dri$er is loaded= it registers itself :ith the ja$a+ s6l+ Dri$erManager class as an a$ailable database dri$er+ b1 Ma9ing a connection :ith database? To open a connection to a gi$en database= Dri$erManager+ getConnection-1 method is used+ Connection con N Dri$erManager+ getConnection -Ijdbc?odbc?somedbI= HuserI= Hpass:ordI1U c1 E*ecuting SML statements ? To e*ecute a SML 6uery= ja$a+ s6l+ statements class is used+ createStatement-1 method of Connection to obtain a ne: Statement object+ Statement stmt N con+ createStatement-1U & 6uery that returns data can be e*ecuted using the e*ecuteMuery-1 method of Statement+ This method e*ecutes the statement and returns a Balaji Institute of Technology and Science "%4

WT Lab Manual

Departmrnt of Computer Science and Engineering

ja$a+ s6l+ 8esultSet that encapsulates the retrie$ed data? 8esultSet rs N stmt+ e*ecuteMuery-ISELECT ^ >8#M some tableI1U d1 ,rocess the results ? 8esultSet returns one ro: at a time+ /e*t-1 method of 8esultSet object can be called to mo$e to the ne*t ro:+ The getString-1 and get#bject-1 methods are used for retrie$ing column $alues? :hile-rs+ ne*t-11 V String e$ent N rs+ getString-Ie$entI1U #bject count N -Integer1 rs+ get#bject-IcountI1U 2B+ ,(at t-0e of +r!/er +!+ -o) )se !n 0ro9ectL .DBC #DBC Bridge dri$er -is a dri$er that uses nati$e-C language1 libraries and ma9es calls to an e*isting #DBC dri$er to access a database engine1+ 2)+ ,(at are t(e t-0es of state%ents !n JDBCL Statement? to be used createStatement-1 method for e*ecuting single SML statement ,reparedStatement c To be used preparedStatement-1 method for e*ecuting same SML statement o$er and o$er+ CallableStatement c To be used prepareCall-1 method for multiple SML statements o$er and o$er+ 22+ ,(at !s store+ 0roce+)reL Stored procedure is a group of SML statements that forms a logical unit and performs a particular tas9+ Stored ,rocedures are used to encapsulate a set of operations or 6ueries to e*ecute on database+ Stored procedures can be compiled and e*ecuted :ith different parameters and results and may ha$e any combination of inputEoutput parameters+ 23+ 1ow to create an+ call store+ 0roce+)resL To create stored procedures? Create procedure procedurename -specify in= out and in out parameters1 BE(I/ &ny multiple SML statementU E/DU To call stored procedures? CallableStatement csmt N con+ prepareCall-IVcall procedure name-R=R1YI1U csmt+ register#ut,arameter-column no+ = data type1U csmt+ setInt-column no+ = column name1 csmt+ e*ecute-1U 27+ ,(at !s ser/letL Ser$lets are modules that e*tend re6uestEresponse oriented ser$ers= such as ja$a enabled :eb ser$ers+ >or e*ample= a ser$let might be responsible for ta9ing data in an ATML order entry form and applying the business logic used to update a companyDs order database+ 34+ ,(at are t(e classes an+ !nterfaces for ser/letsL There are t:o pac9ages in ser$lets and they are ja$a*+ ser$let and 3"+ ,(at !s t(e +!fference &etween an a00let an+ a ser/letL a1 Ser$lets are to ser$ers :hat applets are to bro:sers+ b1 &pplets must ha$e graphical user interfaces :hereas ser$lets ha$e no graphical user interfaces+ 3!+ ,(at !s t(e +!fference &etween +oPost an+ +o*et %et(o+sL a1 do(et-1 method is used to get information= :hile do,ost-1 method is used for posting information+ b1 do(et-1 re6uests canDt send large amount of information and is limited to !'4 !BB characters+ Ao:e$er= do,ost-1re6uests passes all of its data= of unlimited length+ c1 & do(et-1 re6uest is appended to the re6uest 08L in a 6uery string and this allo:s the e*change is $isible to the client= :hereas a do,ost-1 re6uest passes directly o$er the soc9et connection as part of its ATT, re6uest body and the e*change are in$isible to the client+ 3%+ ,(at !s t(e l!fe c-cle of a ser/letL Each Ser$let has the same life cycle? a1 & ser$er loads and initiali<es the ser$let by init -1 method+ b1 The ser$let handles <ero or more clientDs re6uests through ser$ice-1 method+ c1 The ser$er remo$es the ser$let through destroy-1 method+ 3'+ ,(o !s loa+!ng t(e !n!t$' %et(o+ of ser/letL Web ser$er Balaji Institute of Technology and Science "%"

WT Lab Manual

Departmrnt of Computer Science and Engineering

3B+ ,(at are t(e +!fferent ser/ers a/a!la&le for +e/elo0!ng an+ +e0lo-!ng Ser/letsL a1 .a$a Web Ser$er b1 .8un g1 &pache Ser$er h1 /etscape Information Ser$er i1 Web Logic 3)+ 1ow %an- wa-s can we trac2 cl!ent an+ w(at are t(e-L The ser$let &,I pro$ides t:o :ays to trac9 client state and they are? a1 0sing Session trac9ing and b1 0sing Coo9ies+ 32+ ,(at !s sess!on trac2!ng an+ (ow +o -o) trac2 a )ser sess!on !n ser/letsL Session trac9ing is a mechanism that ser$lets use to maintain state about a series re6uests from the same user across some period of time+ The methods used for session trac9ing are? a1 0ser &uthentication occurs :hen a :eb ser$er restricts access to some of its resources to only those clients that log in using a recogni<ed username and pass:ord+ b1 Aidden form fields fields are added to an ATML form that are not displayed in the clientDs bro:ser+ When the form containing the fields is submitted= the fields are sent bac9 to the ser$er+ c1 08L re:riting e$ery 08L that the user clic9s on is dynamically modified or re:ritten to include e*tra information+ The e*tra information can be in the form of e*tra path information= added parameters or some custom= ser$er specific 08L change+ d1 Coo9ies a bit of information that is sent by a :eb ser$er to a bro:ser and :hich can later be read bac9 from that bro:ser+ e1 AttpSession places a limit on the number of sessions that can e*ist in memory+ This limit is set in the session+ ma*residents property+ 33+ ,(at !s Ser/er7S!+e Incl)+es $SSI'L Ser$er Side Includes allo:s embedding ser$lets :ithin ATML pages using a special ser$let tag+ In many ser$lets that support ser$lets= a page can be processed by the ser$er to include output from ser$lets at certain points inside the ATML page+ This is accomplished using a special internal SSI/CL0DE= :hich processes the ser$let tags+ SSI/CL0DE ser$let :ill be in$o9ed :hene$er a file :ith an+ shtml e*tension is re6uested+ So ATML files that include ser$er side includes must be stored :ith an + shtml e*tension+ 37+ ,(at are coo2!es an+ (ow w!ll -o) )se t(e%L Coo9ies are a mechanism that a ser$let uses to ha$e a client hold a small amount of state information associated :ith the user+ a1 Create a coo9ie :ith the Coo9ie constructor? public Coo9ie-String name= String $alue1 b1 & ser$let can send a coo9ie to the client by passing a Coo9ie object to the addCoo9ie-1 method of AttpSer$let8esponse? public $oid AttpSer$let8esponse+ addCoo9ie-Coo9ie coo9ie1 c1 & ser$let retrie$es coo9ies by calling the getCoo9ies-1 method of AttpSer$let8e6uest? public Coo9ieW X AttpSer$let8e6uest+ getCoo9ie-1+ 74+ Is !t 0oss!&le to co%%)n!cate fro% an a00let to ser/let an+ (ow %an- wa-s an+ (owL Les= there are three :ays to communicate from an applet to ser$let and they are? a1 ATT, Communication-Te*t based and object based1 b1 Soc9et Communication c1 8MI Communication 7"+ ,(at !s connect!on 0ool!ngL With ser$lets= opening a database connection is a major bottlenec9 because :e are creating and tearing do:n a ne: connection for e$ery page re6uest and the time ta9en to create connection :ill be more+ Creating a connection pool is an ideal approach for a complicated ser$let+ With a connection pool= :e can duplicate only the resources :e need to duplicate rather than the entire ser$let+ & connection pool can also intelligently manage the si<e of the pool and ma9e sure each connection remains $alid+ & number of connection pool pac9ages are currently a$ailable+ Some li9e DbConnectionBro9er are freely a$ailable from .a$a E*change Balaji Institute of Technology and Science "%!

WT Lab Manual

Departmrnt of Computer Science and Engineering

Wor9s by creating an object that dispenses connections and connection Ids on re6uest+ The Connection,ool class maintains a Aastable= using Connection objects as 9eys and Boolean $alues as stored $alues+ The Boolean $alue indicates :hether a connection is in use or not+ & program calls getConnection-1 method of the Connection,ool for getting Connection object it can useU it calls returnConnection-1 to gi$e the connection bac9 to the pool+ 7!+ ,(- s(o)l+ we go for !nterser/let co%%)n!cat!onL Ser$lets running together in the same ser$er communicate :ith each other in se$eral :ays+ The three major reasons to use interser$let communication are? a1 Direct ser$let manipulation allo:s to gain access to the other currently loaded ser$lets and perform certain tas9s -through the Ser$letConte*t object1 b1 Ser$let reuse allo:s the ser$let to reuse the public methods of another ser$let+ c1 Ser$let collaboration re6uires to communicate :ith each other by sharing specific information -through method in$ocation1 7%+ Is !t 0oss!&le to call ser/let w!t( 0ara%eters !n t(e URLL Les+ Lou can call a ser$let :ith parameters in the synta* as -R,aram" N *** \\ m! N yyy1+ 7'+ ,(at !s Ser/let c(a!n!ngL Ser$let chaining is a techni6ue in :hich t:o or more ser$lets can cooperate in ser$icing a single re6uest+ In ser$let chaining= one ser$letDs output is piped to the ne*t ser$letDs input+ This process continues until the last ser$let is reached+ Its output is then sent bac9 to the client+ 7B+ 1ow +o ser/lets (an+le %)lt!0le s!%)ltaneo)s re?)estsL The ser$er has multiple threads that are a$ailable to handle re6uests+ When a re6uest comes in= it is assigned to a thread= :hich calls a ser$ice method -for e*ample? do(et-1= do,ost-1 and ser$ice-11 of the ser$let+ >or this reason= a single ser$let object can ha$e its ser$ice methods called by many threads at once+ 7)+ ,(at !s t(e +!fference &etween TCPMIP an+ UDPL TC,EI, is a t:o :ay communication bet:een the client and the ser$er and it is a reliable and there is a confirmation regarding reaching the message to the destination+ It is li9e a phone call+ 0D, is a one :ay communication only bet:een the client and the ser$er and it is not a reliable and there is no confirmation regarding reaching the message to the destination+ It is li9e a postal mail+ 72+ ,(at !s Inet a++ressL E$ery computer connected to a net:or9 has an I, address+ &n I, address is a number that uni6uely identifies each computer on the /et+ &n I, address is a %! bit number+ 73+ ,(at !s Do%a!n Na%!ng Ser/!ce$DNS'L It is $ery difficult to remember a set of numbers-I, address1 to connect to the Internet+ The Domain /aming Ser$ice-D/S1 is used to o$ercome this problem+ It maps one particular I, address to a string of characters+ >or e*ample= :::+ mascom+ com implies com is the domain name reser$ed for 0S commercial sites= moscom is the name of the company and ::: is the name of the specific computer= :hich is mascomDs ser$er+ 77+ ,(at !s URLL 08L stands for 0niform 8esource Locator and it points to resource files on the Internet+ 08L has four components? http?EE:::+ address+ com?34Einde*+html= :here http protocol name= address I, address or host name= 34 port number and inde*+html file path+ "44+ ,(at !s R#I an+ ste0s !n/ol/e+ !n +e/elo0!ng an R#I o&9ectL 8emote Method In$ocation -8MI1 allo:s ja$a object that e*ecutes on one machine and to in$o9e the method of a .a$a object to e*ecute on another machine+ The steps in$ol$ed Balaji Institute of Technology and Science "%%

WT Lab Manual

Departmrnt of Computer Science and Engineering

in de$eloping an 8MI object are? a1 Define the interfaces b1 Implementing these interfaces c1 Compile the interfaces and their implementations :ith the ja$a compiler d1 Compile the ser$er implementation :ith 8MI compiler e1 8un the 8MI registry f1 8un the application "4"+ ,(at !s R#I arc(!tect)reL 8MI architecture consists of four layers and each layer performs specific functions? a1 &pplication layer contains the actual object definition+ b1 ,ro*y layer consists of stub and s9eleton+ c1 8emote 8eference layer gets the stream of bytes from the transport layer and sends it to the pro*y layer+ d1 Transportation layer responsible for handling the actual machine to machine communication+ "4!+ w(at !s Un!castRe%oteO&9ectL &ll remote objects must e*tend 0nicast8emote#bject= :hich pro$ides functionality that is needed to ma9e objects a$ailable from remote machines+ "4%+ E:0la!n t(e %et(o+s5 re&!n+$' an+ loo2)0$' !n Na%!ng classL rebind-1 of the /aming class-found in ja$a+ rmi1 is used to update the 8MI registry on the ser$er machine+ /aming+ rebind-I&ddSe$erI= &ddSer$erImpl1U loo9up-1 of the /aming class accepts one argument= the rmi 08L and returns a reference to an object of type &ddSer$erImpl+ "4'+ ,(at !s a Ja/a BeanL & .a$a Bean is a soft:are component that has been designed to be reusable in a $ariety of different en$ironments+ "4B+ ,(at !s a Jar f!leL .ar file allo:s to efficiently deploying a set of classes and their associated resources+ The elements in a jar file are compressed= :hich ma9es do:nloading a .ar file much faster than separately do:nloading se$eral uncompressed files+ The pac9age ja$a+ util+ <ip contains classes that read and :rite jar files+ "4)+ ,(at !s BD;L BDG= Bean De$elopment Git is a tool that enables to create= configure and connect a set of set of Beans and it can be used to test Beans :ithout :riting a code+ "42+ ,(at !s JSPL .S, is a dynamic scripting capability for :eb pages that allo:s .a$a as :ell as a fe: special tags to be embedded into a :eb file -ATMLECML= etc1+ The suffi* traditionally ends :ith +jsp to indicate to the :eb ser$er that the file is a .S, files+ .S, is a ser$er side technology you canDt do any client side $alidation :ith it+ The ad$antages are? a1 The .S, assists in ma9ing the ATML more functional+ Ser$lets on the other hand allo: outputting of ATML but it is a tedious process+ b1 It is easy to ma9e a change and then let the .S, capability of the :eb ser$er you are using deal :ith compiling it into a ser$let and running it+ "43+ ,(at are JSP scr!0t!ng ele%entsL .S, scripting elements lets to insert .a$a code into the ser$let that :ill be generated from the current .S, page+ There are three forms? a1 E*pressions of the form JQN e*pression QK that are e$aluated and inserted into the output= b1 Scriptlets of the formthat are inserted into the ser$letDs ser$ice method= and c1 Declarations of the form JQZ Code QKthat are inserted into the body of the ser$let class= outside of any e*isting methods+ "47+ ,(at are JSP D!rect!/esL & .S, directi$e affects the o$erall structure of the ser$let class+ It usually has the follo:ing form?JQa directi$e attributeNI$alueI QK Ao:e$er= you can also combine multiple attribute settings for a single directi$e= as follo:s?JQa directi$e attribute"NI$alue"d attribute !NI$alue!d + + + attribute/ NI$alue/I QK There are t:o main types of directi$e? page= :hich lets to do things li9e Balaji Institute of Technology and Science "%'

WT Lab Manual

Departmrnt of Computer Science and Engineering

import classes= customi<e the ser$let superclass= and the li9eU and include= :hich lets to insert a file into the ser$let class at the time the .S, file is translated into a ser$let ""4+ ,(at are Pre+ef!ne+ /ar!a&les or !%0l!c!t o&9ectsL To simplify code in .S, e*pressions and scriptlets= :e can use eight automatically defined $ariables= sometimes called implicit objects+ They are re6uest= response= out= session= application= config= pageConte*t= and page+ """+ ,(at are JSP ACTIONSL .S, actions use constructs in CML synta* to control the beha$ior of the ser$let engine+ Lou can dynamically insert a file= reuse .a$aBeans components= for:ard the user to another page= or generate ATML for the .a$a plugin+ &$ailable actions include? jsp?include Include a file at the time the page is re6uested+ jsp?useBean >ind or instantiate a .a$aBean+ jsp?set,roperty Set the property of a .a$aBean+ jsp?get,roperty Insert the property of a .a$aBean into the output+ jsp?for:ard >or:ard the re6uester to a ne:page+ .sp? plugin (enerate bro:ser specific code that ma9es an #B.ECT or EMBED ""!+ 1ow +o -o) 0ass +ata $!ncl)+!ng Ja/aBeans' to a JSP fro% a ser/letL -"1 8e6uest Lifetime? 0sing this techni6ue to pass beans= a re6uest dispatcher -using either HincludeI or for:ardI1 can be called+ This bean :ill disappear after processing this re6uest has been completed+ Ser$let? re6uest+ set&ttribute-ItheBeanI= myBean1U 8e6uestDispatcher rd N getSer$letConte*t-1+ get8e6uestDispatcher-Ithepage+ jspI1U rd+ for:ard-re6uest= response1U .S, ,&(E?Jjsp? useBean idNItheBeanI scopeNIre6uestI classNI+ + + + + I EK-!1 Session Lifetime? 0sing this techni6ue to pass beans that are rele$ant to a particular session -such as in indi$idual user login1 o$er a number of re6uests+ This bean :ill disappear :hen the session is in$alidated or it times out= or :hen you remo$e it+ Ser$let? AttpSession session N re6uest+ getSession-true1U session+ put5alue-ItheBeanI= myBean1U E^ Lou can do a re6uest dispatcher here= or just let the bean be $isible on the ne*t re6uest ^E .S, ,age?Jjsp?useBean idNItheBeanI scopeNIsessionI classNI+ + + I EK %1 &pplication Lifetime? 0sing this techni6ue to pass beans that are rele$ant to all ser$lets and .S, pages in a particular app= for all users+ >or e*ample= I use this to ma9e a .DBC connection pool object a$ailable to the $arious ser$lets and .S, pages in my apps+ This bean :ill disappear :hen the ser$let engine is shut do:n= or :hen you remo$e it+ Ser$let? (etSer$letConte*t-1+ set&ttribute-ItheBeanI= myBean1U .S, ,&(E?Jjsp?useBean idNItheBeanI scopeNIapplicationI classNI+ + + I EK ""%+ 1ow can I set a coo2!e !n JSPL response+ setAeader-ISet Coo9ieI= Hcoo9ie stringI1U To gi$e the response object to a bean= :rite a method set8esponse -AttpSer$let8esponse response1 to the bean= and in jsp file?JQ bean+ set8esponse -response1U QK ""'+ 1ow can I +elete a coo2!e w!t( JSPL Say that I ha$e a coo9ie called Hfoo= I that I set a :hile ago ` I :ant it to go a:ay+ I simply? JQ Coo9ie 9illCoo9ie N ne: Coo9ie-IfooI= null1U GillCoo9ie+ set,ath-IEI1U 9illCoo9ie+ setMa*&ge-41U response+ addCoo9ie-9illCoo9ie1U QK ""B+ 1ow are Ser/lets an+ JSP Pages relate+L .S, pages are focused around ATML -or CML1 :ith .a$a codes and .S, tags inside them+ When a :eb ser$er that has .S, support is as9ed for a .S, page= it chec9s to see if it has already compiled the page into a ser$let+ Thus= .S, pages become ser$lets and are transformed into pure .a$a and then compiled= loaded into the ser$er and e*ecuted+ Balaji Institute of Technology and Science "%B

WT Lab Manual

Departmrnt of Computer Science and Engineering

Serlets ,(at !s t(e +!fference &etween a00l!cat!on ser/er an+ we& ser/erL &n :eb ser$er is responsible for handling Attp8e6uests :hereas appliation ser$er is responsible for handling business logics+ app ser$er has inbuilt features li9e load balancing= clusters= caches and resource ` connection pooling etc+ 0sing application ser$er :e can de$elop reusable components+ &pplication ser$er is a super set of :eb ser$er+ :eb ser$er is used for e*ecuting .S, and ser$lets+ application ser$er is used for e*ecuting E.B related components

D!fference &etween t(row an+ t(rows 3 thro: to e*plicitly thro: an e*ception thro:s means that e*ception :ill be handled by the called method+ .. D!fference &etween 1as(Ta&le an+ (as(#a0 L 777777777777777777777777777777777777777777777 Both pro$ide 9ey $alue access to data+ The Aashtable is one of the original collection classes in .a$a+ AashMap is part of the ne: Collections >rame:or9= added :ith .a$a != $"+!+ The 9ey difference bet:een the t:o is that access to the Aashtable is synchroni<ed on the table :hile access to the AashMap isnTt+ Lou can add it= but it isnTt there by default+ &nother difference is that iterator in the AashMap is fail safe :hile the enumerator for the Aashtable isnTt+ If you change the map :hile iterating= youTll 9no:+ &nd= a third difference is that AashMap permits null $alues in it= :hile Aashtable doesnTt+ >or ne: code=:e can use AashMap. ".,(at !s t(e +!ffrence &etween Arra-5Vector5Arr-L!st5L!n2e+L!st an+ 1as(Ta&le w!t( 1as(#a0 w!t( e:a%0le an+ s!t)at!onL #nce u create an array -of any type1 u canTt change the array si<e at dynamically+ it is not synchroni<ed+ 0 can $ie: a 5ector as a dynamic array+ 0 can resi<e the $ector at runtime+ u can addEremo$e element in a $ector at runtime+ it is synchroni<ed+ &n &rrayList is a class :hich implements the List interface+ This again is resi<able and u ha$ all the functionalities of a list+ it is not synchroni<ed+ /ote ? u can ha$ duplicates in &rrayList+ & Lin9edList is an implementation of the calssical TCT lin9ed list+ 0 can get= remo$e or insert elements+ AashTable is basically a 9ey $alue pair collection+ Gey has to be uni6ue+ 0 can do get and put+ AashMap is implementation of Map interface+ 0 can ha$ null 9ey or null $alue+ 0se :hen u re6uire constant time performance.

Balaji Institute of Technology and Science

"%)

WT Lab Manual

Departmrnt of Computer Science and Engineering

H.,(at !s t(e +!fference &etween ser/let > C*IL 77777777777777777777777777777777777777777777777 >or each ATT, re6uest= C(I :ill start a ne: process+ :hereas in ser$let= init-1 method :ill be called= only :hen first ATT, re6uest came+ for subse6uent re6uest an thread is created+ J.,(at !s t(e +!fference &etween t(e Serl/et s!ngle t(rea+e+ %o+el an+ %)lt! t(rea+e+ %o+elL 77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777 Typically= a ser$let class is instantiated the first time it is in$o9ed+ The same instance :ill be used o$er se$eral client re6uests= so all members that are declared in that ser$let are shared accross clients+ That is :hat is meant by multi threaded model= multiple clients that access the same instance+ There are situations :here you :ant to protect your ser$let member $ariables from being modified by different clients+ In this case= you can ha$e your ser$let implement the mar9er interface SingleThreadModel+ E$ery time a client ma9es a re6uest to a ser$let that implements this interface= the engine :ill create a ne: instance of the ser$let+ >or performance reasons= the engine can also maintain a instance pool= handing out instances as they are needed+ #r it could also seriali<e client re6uests= e*ecuting one after another+ =. 1ow to rea+ en/!ron%ent /ar!a&les fro% a Ser/letL A3Ser$letConfig+get,arameter-String param[name1 N3 ,(at !s t(e +!fference &etween an Interface an+ an A&stract classL A3 &n &bstract class declares ha$e at least one instance method that is declared abstract :hich :ill be implemented by the subclasses+ &n abstract class can ha$e instance methods that implement a default beha$ior+ &n Interface can only declare constants and instance methods= but cannot implement default beha$ior. &ll memeber $ariable of an interface is Opublic static finalO N3 ,(at !s t(e 0)r0ose of gar&age collect!on !n Ja/a5 an+ w(en !s !t )se+L A? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused+ & .a$a object is subject to garbage collection :hen it becomes unreachable to the program in :hich it is used+ N3 Descr!&e s-nc(ron!4at!on !n res0ect to %)lt!t(rea+!ng. A3 With respect to multithreading= synchroni<ation is the capability to control the access of multiple threads to shared resources+ Without synchoni<ation= it is possible for one thread to modify a shared $ariable :hile another thread is in the process of using or updating same shared $ariable+ This usually leads to significant errors+

Balaji Institute of Technology and Science

"%2

WT Lab Manual N3 E:0la!n +!fferent wa- of )s!ng t(rea+L

Departmrnt of Computer Science and Engineering

A3 The thread could be implemented by using runnable interface or by inheriting from the Thread class+ The former is more ad$antageous= Tcause :hen you are going for multiple inheritance++the only interface can help+ N3 ,(at are 0ass &- reference an+ 0ass&- /al)eL A? ,ass By 8eference means the passing the address itself rather than passing the $alue+ ,assby 5alue means passing a copy of the $alue to be 0asse+. N3 ,(at !s 1as(#a0 an+ #a0L A? Map is Interface and Aashmap is class that implements that+ N3 D!fference &etween 1as(#a0 an+ 1as(Ta&leL A3 The AashMap class is roughly e6ui$alent to Aashtable= e*cept that it is unsynchroni<ed and permits nulls+ -AashMap allo:s null $alues as 9ey and $alue :hereas Aashtable doesnt allo:1+ AashMap does not guarantee that the order of the map :ill remain constant o$er time+ AashMap is non synchroni<ed and Aashtable is synchroni<ed+ N3 D!fference &etween Vector an+ Arra-L!stL A3 5ector is synchroni<ed :hereas arraylist is not+ N3 D!fference &etween Sw!ng an+ AwtL A3 &WT are hea$y :eight componenets+ S:ings are light :eight components+ Aence s:ing :or9s faster than &WT+ N3 ,(at !s t(e +!fference &etween a constr)ctor an+ a %et(o+L A? & constructor is a member function of a class that is used to create objects of that class+ It has the same name as the class itself= has no return type= and is in$o9ed using the ne: operator+ & method is an ordinary member function of a class+ It has its o:n name= a return type -:hich may be $oid1= and is in$o9ed using the dot operator+ N3 ,(at !s an IteratorsL A3 Some of the collection classes pro$ide tra$ersal of their contents $ia a ja$a+util+Iterator interface+ This interface allo:s you to :al9 a collection of objects= operating on each object in turn+ 8emember :hen using Iterators that they contain a snapshot of the collection at the time the Iterator :as obtainedU generally it is not ad$isable to modify the collection itself :hile tra$ersing an Iterator. Balaji Institute of Technology and Science "%3

WT Lab Manual

Departmrnt of Computer Science and Engineering

N3 ,(at !s an a&stract classL A3 &bstract class must be e*tendedEsubclassed -to be useful1+ It ser$es as a template+ & class that is abstract may not be instantiated -ie= you may not call its constructor1= abstract class may contain static data+ &ny class :ith an abstract method is automatically abstract itself= and must be declared as such+ & class may be declared abstract e$en if it has no abstract methods+ This pre$ents it from being instantiated+ N3 ,(at !s stat!c !n 9a/aL A3 Static means one per class= not one for each object no matter ho: many instance of a class might e*ist+ This means that you can use them :ithout creating an instance of a class+Static methods are implicitly final= because o$erriding is done based on the type of the object= and static methods are attached to a class= not an object+ & static method in a superclass can be shado:ed by another static method in a subclass= as long as the original method :as not declared final+ Ao:e$er= you canTt o$erride a static method :ith a nonstatic method+ In other :ords= you canTt change a static method into an instance method in a subclass+ N3 ,(at !s f!nalL A3 & final class canTt be e*tended ie+= final class may not be subclassed+ & final method canTt be o$erridden :hen its class is inherited+ Lou canTt change $alue of a final $ariable -is a constant1+ N3 ,(at !f t(e %a!n %et(o+ !s +eclare+ as 0r!/ateL A3 The program compiles properly but at runtime it :ill gi$e OMain method not public+O message. N3 ,(at !f t(e stat!c %o+!f!er !s re%o/e+ fro% t(e s!gnat)re of t(e %a!n %et(o+L A? ,rogram compiles+ But at runtime thro:s an error O/oSuchMethodErrorO+ N3 ,(at !f I wr!te stat!c 0)&l!c /o!+ !nstea+ of 0)&l!c stat!c /o!+L A3 ,rogram compiles and runs properly+ N3 ,(at !f I +o not 0ro/!+e t(e Str!ng arra- as t(e arg)%ent to t(e %et(o+L A? ,rogram compiles but thro:s a runtime error O/oSuchMethodErrorO+

Balaji Institute of Technology and Science

"%7

WT Lab Manual

Departmrnt of Computer Science and Engineering

N3 ,(at !s t(e f!rst arg)%ent of t(e Str!ng arra- !n %a!n %et(o+L A3 The String array is empty+ It does not ha$e any element+ This is unli9e CECFF :here the first element by default is the program name+ N3 If I +o not 0ro/!+e an- arg)%ents on t(e co%%an+ l!ne5 t(en t(e Str!ng arra- of #a!n %et(o+ w!ll &e e%0t- of n)llL A3 It is empty+ But not null. N3 1ow can one 0ro/e t(at t(e arra- !s not n)ll &)t e%0t-L A3 ,rint args+length+ It :ill print 4+ That means it is empty+ But if it :ould ha$e been null then it :ould ha$e thro:n a /ull,ointerE*ception on attempting to print args+length+ N3 ,(at en/!ron%ent /ar!a&les +o I nee+ to set on %- %ac(!ne !n or+er to &e a&le to r)n Ja/a 0rogra%sL A3 CL&SS,&TA and ,&TA are the t:o $ariables. N3 Can an a00l!cat!on (a/e %)lt!0le classes (a/!ng %a!n %et(o+L A3 Les it is possible+ While starting the application :e mention the class name to be run+ The .5M :ill loo9 for the Main method only in the class :hose name you ha$e mentioned+ Aence there is not conflict amongst the multiple classes ha$ing main method+ N3 Can I (a/e %)lt!0le %a!n %et(o+s !n t(e sa%e classL A3 /o the program fails to compile+ The compiler says that the main method is already defined in the class+ N3 Do I nee+ to !%0ort 9a/a.lang 0ac2age an- t!%eL ,(- L A3 /o+ It is by default loaded internally by the .5M+ N3 Can I !%0ort sa%e 0ac2ageMclass tw!ceL ,!ll t(e JV# loa+ t(e 0ac2age tw!ce at r)nt!%eL A? #ne can import the same pac9age or same class multiple times+ /either compiler nor .5M complains abt it+ &nd the .5M :ill internally load the class only once no matter ho: many times you import the same class+

Balaji Institute of Technology and Science

"'4

WT Lab Manual

Departmrnt of Computer Science and Engineering

N3 ,(at are C(ec2e+ an+ UnC(ec2e+ E:ce0t!onL A3 & chec9ed e*ception is some subclass of E*ception -or E*ception itself1= e*cluding class 8untimeE*ception and its subclasses+ Ma9ing an e*ception chec9ed forces client programmers to deal :ith the possibility that the e*ception :ill be thro:n+ eg= I#E*ception thro:n by ja$a+io+>ileInputStreamTs read-1 methode 0nchec9ed e*ceptions are 8untimeE*ception and any of its subclasses+ Class Error and its subclasses also are unchec9ed+ With an unchec9ed e*ception= ho:e$er= the compiler doesnTt force client programmers either to catch the e*ception or declare it in a thro:s clause+ In fact= client programmers may not e$en 9no: that the e*ception could be thro:n+ eg= StringInde*#ut#fBoundsE*ception thro:n by StringTs char&t-1 methode Chec9ed e*ceptions must be caught at compile time+ 8untime e*ceptions do not need to be+ Errors often cannot be+ N3 ,(at !s O/err!+!ngL A3 When a class defines a method using the same name= return type= and arguments as a method in its superclass= the method in the class o$errides the method in the superclass+ When the method is in$o9ed for an object of the class= it is the ne: definition of the method that is called= and not the method definition from superclass+ Methods may be o$erridden to be more public= not more pri$ate+ N3 ,(at are +!fferent t-0es of !nner classesL A? /ested top le$el classes= Member classes= Local classes= &nonymous classes Neste+ to07le/el classes If you declare a class :ithin a class and specify the static modifier= the compiler treats the class just li9e any other top le$el class+ &ny class outside the declaring class accesses the nested class :ith the declaring class name acting similarly to a pac9age+ eg= outer+inner+ Top le$el inner classes implicitly ha$e access only to static $ariables+There can also be inner interfaces+ &ll of these are of the nested top le$el $ariety+ #e%&er classes Member inner classes are just li9e other member methods and member $ariables and access to the member class is restricted= just li9e methods and $ariables+ This means a public member class acts similarly to a nested top le$el class+ The primary difference bet:een member classes and nested top le$el classes is that member classes ha$e access to the specific instance of the enclosing class+ Local classes 7 Local classes are li9e local $ariables= specific to a bloc9 of code+ Their $isibility is only :ithin the bloc9 of their declaration+ In order for the class to be useful beyond the declaration bloc9= it :ould need to implement a more publicly a$ailable interface+Because local classes are not members= the modifiers public= protected= pri$ate= and static are not usable+

Balaji Institute of Technology and Science

"'"

WT Lab Manual

Departmrnt of Computer Science and Engineering

Anon-%o)s classes 7 &nonymous inner classes e*tend local inner classes one le$el further+ &s anonymous classes ha$e no name= you cannot 0ro/!+e a constr)ctor. N3 ,(at !s t(e +!fference &etween +eclar!ng a /ar!a&le an+ +ef!n!ng a /ar!a&leL A3 In declaration :e just mention the type of the $ariable and itTs name+ We do not initiali<e it+ But defining means declaration F initiali<ation+ e+g String sU is just a declaration :hile String s N ne: String -OabcdO1U #r String s N OabcdOU are both definitions. N3 ,(at !s t(e +efa)lt /al)e of an o&9ect reference +eclare+ as an !nstance /ar!a&leL A? null unless :e define it e*plicitly. N3 ,(at t-0e of 0ara%eter 0ass!ng +oes Ja/a s)00ortL A3 .a$a supports both pass by $alue as :ell as pass by reference. N3 Pr!%!t!/e +ata t-0es are 0asse+ &- reference or 0ass &- /al)eL A3 ,rimiti$e data types are passed by $alue+ N3 O&9ects are 0asse+ &- /al)e or &- referenceL A3 #bjects are al:ays passed by reference+ Thus any modifications done to an object inside the called method :ill al:ays reflect in the caller method+ N3 ,(at !s ser!al!4at!onL A? Seriali<ation is a mechanism by :hich you can sa$e the state of an object by con$erting it to a byte stream+ N3 ,(!c( %et(o+s of Ser!al!4a&le !nterface s(o)l+ I !%0le%entL A3 T(e ser!al!4a&le !nterface !s an e%0t- !nterface5 !t +oes not conta!n an- %et(o+s. So we +o not !%0le%ent an- %et(o+s. N3 ,(at !s t(e co%%on )sage of ser!al!4at!onL A? Whene$er an object is to be sent o$er the net:or9= objects need to be seriali<ed+ Moreo$er if the state of an object is to be sa$ed= objects need to be serila<ed. N3 ,(at !s E:ternal!4a&le !nterfaceL A3 E*ternali<able is an interface :hich contains t:o methods readE*ternal and :riteE*ternal+ These methods gi$e you a control o$er the seriali<ation mechanism+ Thus if your class Balaji Institute of Technology and Science "'!

WT Lab Manual

Departmrnt of Computer Science and Engineering

implements this interface= you can customi<e the seriali<ation process by implementing these methods. N3 ,(at (a00ens to t(e o&9ect references !ncl)+e+ !n t(e o&9ectL A3 The seriali<ation mechanism generates an object graph for seriali<ation+ Thus it determines :hether the included object references are seriali<able or not+ This is a recursi$e process+ Thus :hen an object is seriali<ed= all the included objects are also seriali<ed along:ith the original object+ N3 ,(at one s(o)l+ ta2e care of w(!le ser!al!4!ng t(e o&9ectL A3 #ne should ma9e sure that all the included objects are also seriali<able+ If any of the objects is not seriali<able then it thro:s a /otSeriali<ableE*ception. N3 ,(at (a00ens to t(e stat!c f!el+s of a class +)r!ng ser!al!4at!onL Are t(ese f!el+s ser!al!4e+ as a 0art of eac( ser!al!4e+ o&9ectL A3 Les the static fields do get seriali<ed+ If the static field is an object then it must ha$e implemented Seriali<able interface+ The static fields are seriali<ed as a part of e$ery object+ But the commonness of the static fields across all the instances is maintained e$en after seriali<ation. N3 Does Ja/a 0ro/!+e an- constr)ct to f!n+ o)t t(e s!4e of an o&9ectL A3 /o there is not si<eof operator in .a$a+ So there is not direct :ay to determine the si<e of an object directly in .a$a+ W 8ecei$ed from Sandesh SadhaleX T#, N3 ,(at are wra00er classesL A3 .a$a pro$ides speciali<ed classes corresponding to each of the primiti$e data types+ These are called :rapper classes+ They are e+g+ Integer= Character= Double etc+ N3 ,(- +o we nee+ wra00er classesL A3 It is sometimes easier to deal :ith primiti$es as objects+ Moreo$er most of the collection classes store objects and not primiti$e data types+ &nd also the :rapper classes pro$ide many utility methods also+ Because of these resons :e need :rapper classes+ &nd since :e create instances of these classes :e can store them in any of the collection classes and pass them around as a collection+ &lso :e can pass them around as method parameters :here a method e*pects an object+

Balaji Institute of Technology and Science

"'%

WT Lab Manual N3 ,(at are c(ec2e+ e:ce0t!onsL

Departmrnt of Computer Science and Engineering

A3 Chec9ed e*ception are those :hich the .a$a compiler forces you to catch+ e+g+ I#E*ception are chec9ed E*ceptions+ N3 ,(at are r)nt!%e e:ce0t!onsL &? 8untime e*ceptions are those e*ceptions that are thro:n at runtime because of either :rong input data or because of :rong business logic etc+ These are not chec9ed by the compiler at compile time+ N3 ,(at !s t(e +!fference &etween error an+ an e:ce0t!onL A3 &n error is an irreco$erable condition occurring at runtime+ Such as #ut#fMemory error+ These .5M errors and you can not repair them at runtime+ While e*ceptions are conditions that occur because of bad input etc+ e+g+ >ile/ot>oundE*ception :ill be thro:n if the specified file does not e*ist+ #r a /ull,ointerE*ception :ill ta9e place if you try using a null reference+ In most of the cases it is possible to reco$er from an e*ception -probably by gi$ing user a feedbac9 for entering proper $alues etc+1+ N3 1ow to create c)sto% e:ce0t!onsL A3 Lour class should e*tend class E*ception= or some more specific type thereof. N3 ,(at (a00ens to an )n(an+le+ e:ce0t!onL A3 #ne can not do anytihng in this scenarion+ Because .a$a does not allo: multiple inheritance and does not pro$ide any e*ception interface as :ell+ N3 1ow +oes an e:ce0t!on 0er%eate t(ro)g( t(e co+eL A3 &n unhandled e*ception mo$es up the method stac9 in search of a matching When an e*ception is thro:n from a code :hich is :rapped in a try bloc9 follo:ed by one or more catch bloc9s= a search is made for matching catch bloc9+ If a matching type is found then that bloc9 :ill be in$o9ed+ If a matching type is not found then the e*ception mo$es up the method stac9 and reaches the caller method+ Same procedure is repeated if the caller method is included in a try catch bloc9+ This process continues until a catch bloc9 handling the appropriate type of e*ception is found+ If it does not find such a bloc9 then finally the program terminates+ N3 ,(at are t(e +!fferent wa-s to (an+le e:ce0t!onsL A3 There are t:o :ays to handle e*ceptions= "+ By :rapping the desired code in a try bloc9 follo:ed by a catch bloc9 to catch the e*ceptions+ and Balaji Institute of Technology and Science "''

WT Lab Manual

Departmrnt of Computer Science and Engineering

!+ List the desired e*ceptions in the thro:s clause of the method and let the caller of the method hadle those e*ceptions+ .. 1ow can -o) ac(!e/e #)lt!0le In(er!tance !n Ja/aL .a$aTs interface mechanism can be used to implement multiple inheritance= :ith one important difference from cFF :ay of doing MI? the inherited interfaces must be abstract+ This ob$iates the need to choose bet:een different implementations= as :ith interfaces there are no implementations+ H. Searc(!ng a Str!ng String string N OaStringOU EE >irst occurrence+ int inde* N string+inde*#f-TST1U EE " EE Last occurrence+ inde* N string+lastInde*#f-TiT1U EE ' EE /ot found+ inde* N string+lastInde*#f-T<T1U EE " J. Connect!ng to a Data&ase an+ Str!ngs 1an+l!ng Constructing a String If you are constructing a string :ith se$eral appends= it may be more efficient to construct it using a StringBuffer and then con$ert it to an immutable String object+ StringBuffer buf N ne: StringBuffer-OInitial Te*tO1U EE Modify int inde* N "U buf+insert-inde*= OabcO1U buf+append-OdefO1U EE Con$ert to string String s N buf+toString-1U (etting a Substring from a String int start N "U int end N 'U String substr N OaStringO+substring-start= end1U EE Str =. ,(at !s a trans!ent /ar!a&leL & transient $ariable is a $ariable that may not be seriali<ed+ If you donTt :ant some field not to be seriali<ed= you can mar9 that field transient or static+ @. ,(at !s t(e +!fference &etween Ser!al!4al&le an+ E:ternal!4a&le !nterfaceL When you use Seriali<able interface= your class is seriali<ed automatically by default+ But you can o$erride :rite#bject-1 and read#bject-1t:o methods to control more comple* object seraili<ation process+ When you use E*ternali<able interface= you ha$e a complete control o$er your classTs seriali<ation process+ Balaji Institute of Technology and Science "'B

WT Lab Manual

Departmrnt of Computer Science and Engineering

C. 1ow %an- %et(o+s !n t(e E:ternal!4a&le !nterfaceL There are t:o methods in the E*ternali<able interface+ Lou ha$e to implement these t:o methods in order to ma9e your class e*ternali<able+ These t:o methods are readE*ternal-1 and :riteE*ternal-1+ K. 1ow %an- %et(o+s !n t(e Ser!al!4a&le !nterfaceL There is no method in the Seriali<able interface+ The Seriali<able interface acts as a mar9er= telling the object seriali<ation tools that your class is seriali<able+ . 1ow to %a2e a class or a &ean ser!al!4a&leL By implementing either the ja$a+io+Seriali<able interface= or the ja$a+io+E*ternali<able interface+ &s long as one class in a classTs inheritance hierarchy implements Seriali<able or E*ternali<able= that class is seriali<able+ .A. ,(at !s t(e ser!al!4at!onL The seriali<ation is a 9ind of mechanism that ma9es a class or a bean persistence by ha$ing its properties or fields and state information sa$ed and restored to and from storage+ ... ,(at are s-nc(ron!4e+ %et(o+s an+ s-nc(ron!4e+ state%entsL Synchroni<ed methods are methods that are used to control access to an object+ & thread only e*ecutes a synchroni<ed method after it has ac6uired the loc9 for the methodTs object or class+ Synchroni<ed statements are similar to synchroni<ed methods+ & synchroni<ed statement can only be e*ecuted after a thread has ac6uired the loc9 for the object or class referenced in the synchroni<ed statement+ .". ,(at !s s-nc(ron!4at!on an+ w(- !s !t !%0ortantL With respect to multithreading= synchroni<ation is the capability to control the access of multiple threads to shared resources+ Without synchroni<ation= it is possible for one thread to modify a shared object :hile another thread is in the process of using or updating that objectTs $alue+ This often causes dirty data and leads to significant errors+ .H. ,(at !s t(e 0)r0ose of f!nal!4at!onL The purpose of finali<ation is to gi$e an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected+ .J. ,(at classes of e:ce0t!ons %a- &e ca)g(t &- a catc( cla)seL & catch clause can catch any e*ception that may be assigned to the Thro:able type+ This includes the Error and E*ception types+

Balaji Institute of Technology and Science

"')

WT Lab Manual

Departmrnt of Computer Science and Engineering

.=. ,(at !s t(e +!fference &etween t(e Rea+erM,r!ter class (!erarc(- an+ t(e In0)tStrea%MO)t0)tStrea% class (!erarc(-L The 8eaderEWriter class hierarchy is character oriented= and the InputStreamE#utputStream class hierarchy is byte oriented+ .@. ,(at (a00ens w(en a t(rea+ cannot ac?)!re a loc2 on an o&9ectL If a thread attempts to e*ecute a synchroni<ed method or synchroni<ed statement and is unable to ac6uire an objectTs loc9= it enters the :aiting state until the loc9 becomes a$ailable+ .C. ,(at restr!ct!ons are 0lace+ on %et(o+ o/err!+!ngL #$erridden methods must ha$e the same name= argument list= and return type+ The o$erriding method may not limit the access of the method it o$errides+ The o$erriding method may not thro: any e*ceptions that may not be thro:n by the o$erridden method+ .K. ,(at restr!ct!ons are 0lace+ on %et(o+ o/erloa+!ngL T:o methods may not ha$e the same name and argument list but different return types+ . . 1ow +oes %)lt!t(rea+!ng ta2e 0lace on a co%0)ter w!t( a s!ngle CPUL Ja/a Inter/!ew N)est!ons ; Subscribe to >8EE ` E*clusi$e career resources at The operating systemTs tas9 scheduler allocates e*ecution time to multiple tas9s+ By 6uic9ly s:itching bet:een e*ecuting tas9s= it creates the impression that tas9s e*ecute se6uentially+ "A. 1ow !s !t 0oss!&le for two Str!ng o&9ects w!t( !+ent!cal /al)es not to &e e?)al )n+er t(e OO o0eratorL The NN operator compares t:o objects to determine if they are the same object in memory+ It is possible for t:o String objects to ha$e the same $alue= but located indifferent areas of memory+ ".. 1ow are t(!s$' an+ s)0er$' )se+ w!t( constr)ctorsL this-1 is used to in$o9e a constructor of the same class+ super-1 is used to in$o9e a superclass constructor+ "". ,(at class allows -o) to rea+ o&9ects +!rectl- fro% a strea%L The #bjectInputStream class supports the reading of objects from input streams+ "H. ,(at !s t(e Reso)rceB)n+le classL The 8esourceBundle class is used to store locale specific resources that can be loaded by a program to tailor the programTs appearance to the particular locale in :hich it is being run+ "J. ,(at !nterface %)st an o&9ect !%0le%ent &efore !t can &e wr!tten to a strea% as an o&9ectL &n object must implement the Seriali<able or E*ternali<able interface before it can be :ritten to a stream as an object+ "=. ,(at !s Ser!al!4at!on an+ +eser!al!4at!onL Seriali<ation is the process of :riting the state of an object to a byte stream+ Deseriali<ation is the process of restoring these objects+ Balaji Institute of Technology and Science "'2

WT Lab Manual

Departmrnt of Computer Science and Engineering

"@. ,(at are t(e O&9ect an+ Class classes )se+ forL The #bject class is the highest le$el class in the .a$a class hierarchy+ The Class class is used to represent the classes and interfaces that are loaded by a .a$a program+ "C. Can -o) wr!te Ja/a co+e for +eclarat!on of %)lt!0le !n(er!tance !n Ja/a L Class C e*tends & implements B V Y "K. ,(at +o -o) %ean &- %)lt!0le !n(er!tance !n CPP L Ja/a Inter/!ew N)est!ons Multiple inheritance is a feature in CFF by :hich one class can be of different types+ Say class teaching&ssistant is inherited from t:o classes say teacher and Student+ " . ,r!te t(e Ja/a co+e to +eclare an- constant $sa- gra/!tat!onal constant' an+ to get !ts /al)e. Class &BC V static final float (8&5IT&TI#/&L[C#/ST&/T N 7+3U public $oid getConstant-1 V system+out+println-O(ra$itational[Constant? O F (8&5IT&TI#/&L[C#/ST&/T1U Y Y HA. ,(at are t(e +!sa+/antages of )s!ng t(rea+sL DeadLoc9+ H.. *!/en two ta&les St)+ent$SID5 Na%e5 Co)rse' an+ Le/el$SID5 le/el' wr!te t(e SNL state%ent to get t(e na%e an+ SID of t(e st)+ent w(o are ta2!ng co)rse O H an+ at fres(%an le/el. SELECT Student+name= Student+SID >8#M Student= Le$el WAE8E Student+SID N Le$el+SID &/D Le$el+Le$el N OfreshmanO &/D Student+Course N %U H". ,(at +o -o) %ean &- /!rt)al %et(o+sL $irtual methods are used to use the polymorhism feature in CFF+ Say class & is inherited from class B+ If :e declare say fuction f-1 as $irtual in class B and o$erride the same function in class & then at runtime appropriate method of the class :ill be called depending upon the type of the object+ HH. ,(at +o -o) %ean &- stat!c %et(o+sL By using the static method there is no need creating an object of that class to use that method+ We can directly call that method on that class+ >or e*ample= say class & has static function f-1= then :e can call f-1 function as &+f-1+ There is no need of creating an object of class &+ Balaji Institute of Technology and Science "'3

WT Lab Manual

Departmrnt of Computer Science and Engineering

HJ. ,(at +o %ean &- 0ol-%or0(!s%5 !n(er!tance5 enca0s)lat!onL Pol-%or(!s%? is a feature of ##,l that at run time depending upon the type of object the appropriate method is called+ In(er!tance? is a feature of ##,L that represents the Ois aO relationship bet:een different objects-classes1+ Say in real life a manager is a employee+ So in ##,L manger class is inherited from the employee class+ Enca0s)lat!on? is a feature of ##,L that is used to hide the information+ H=. ,(at are t(e a+/antages of OOPLL #bject oriented programming languages directly represent the real life objects+ The features of ##,L as inhreitance= polymorphism= encapsulation ma9es it po:erful+ H@. 1ow %an- %et(o+s +o ) !%0le%ent !f !%0le%ent t(e Ser!al!4a&le InterfaceL The Seriali<able interface is just a Omar9erO interface= :ith no methods of its o:n to implement+ HC. Are t(ere an- ot(er Q%ar2erQ !nterfacesL ja$a+rmi+8emote ja$a+util+E$entListener HK. ,(at !s t(e +!fference &etween !nstanceof an+ !sInstanceL instanceof is used to chec9 to see if an object can be cast into a specified type :ithout thro:ing a cast class e*ception+ isInstance-1 determines if the specified object is assignment compatible :ith the object represented by this Class+ This method is the dynamic e6ui$alent of the .a$a language instanceof operator+ The method returns true if the specified #bject argument is nonnull and can be cast to the reference type represented by this Class object :ithout raising a ClassCastE*ception+ It returns false other:ise+ H . w(- +o -o) create !nterfaces5 an+ w(en #UST -o) )se oneL Lou :ould create interfaces :hen you ha$e t:o or more functionalities tal9ing to each other+ Doing it this :ay help you in creating a protocol bet:een the parties in$ol$ed+ JA. ,(atQs t(e +!fference &etween t(e OO o0erator an+ t(e e?)als$' %et(o+L ,(at test +oes O&9ect.e?)als$' )se5 an+ w(-L The NN operator :ould be used= in an object sense= to see if the t:o objects :ere actually the same object+ This operator loo9s at the actually memory address to see if it actually the same object+ The e6uals-1 method is used to compare the $alues of the object respecti$ely+ This is used in a higher le$el to see if the object $alues are e6ual+ #f course the the e6uals-1 method :ould be o$erloaded in a meaningful :ay for :hate$er object that you Balaji Institute of Technology and Science "'7

WT Lab Manual

Departmrnt of Computer Science and Engineering

:ere :or9ing :ith+ Y J". *!/en a te:t f!le5 !n0)t.t:t5 0ro/!+e t(e state%ent re?)!re+ to o0en t(!s f!le w!t( t(e a00ro0r!ate IMO strea% to &e a&le to rea+ an+ 0rocess t(!s f!le. JH. Na%e fo)r %et(o+s e/er- Ja/a class w!ll (a/e. public String toString-1U public #bject clone-1U public boolean e6uals-1U public int hashCode-1U JJ. ,(at +oes t(e Ra&stractR 2e-wor+ %ean !n front of a %et(o+L A classL &bstract 9ey:ord declares either a method or a class+ If a method has a abstract 9ey:ord in front of it= it is called abstract method+&bstract method has no body+ It has only arguments and return type+ &bstract methods act as placeholder methods that are implemented in the subclasses+ &bstract classes canTt be instantiated+If a class is declared as abstract=no objects of that class can be created+If a class contains any abstract method it must be declared as abstract+ J=. Does Ja/a (a/e +estr)ctorsL /o garbage collector does the job :or9ing in the bac9ground J@. Are constr)ctors !n(er!te+L Can a s)&class call t(e 0arentQs class constr)ctorL ,(enL Lou cannot inherit a constructor+ That is= you cannot create a instance of a subclass using a constructor of one of itTs superclasses+ #ne of the main reasons is because you probably donTt :ant to o$eride the superclasses constructor= :hich :ould be possible if they :ere inherited+ By gi$ing the de$eloper the ability to o$erride a superclasses constructor you :ould erode the encapsulation abilities of the language+ J . Does Ja/a (a/e RgotoRL /o =A. ,(at +oes t(e Rf!nalR 2e-wor+ %ean !n front of a /ar!a&leL A %et(o+L A classL >I/&L for a $ariable ? $alue is constant >I/&L for a method ? cannot be o$erridden >I/&L for a class ? cannot be deri$ed +

Balaji Institute of Technology and Science

"B4

WT Lab Manual

Departmrnt of Computer Science and Engineering

Reference Boo2s
"+ Web ,rogramming= building Internet applications= Chris Bates !nd edition= WILEL Dreamtech !+ The complete 8eference .a$a ! >ifth Edition by ,atric9 /aughton and Aerbert Schildt+ TMA %+ .a$a Ser$er ,ages ;Aans Bergsten= S,D #D8eilly '+ Internet and World Wide Web ; Ao: to program by Dietel and /ieto ,AIE,earson Education &sia+ B+ .a9arta Struts Coo9boo9= Bill Siggel9o:= S , D #D8eilly+

Balaji Institute of Technology and Science

"B"

S-ar putea să vă placă și