(FAQ) For Core & Advance Java


1houghi íor Irogrammcrs.
''$t ta|ts a |aaa t|mt ta aaatrstaaa aat||aa.''
- Edward Dahlbcrg. Amcrican auihor and criiic (:¬oo -:¬) -
Mainiaincd By ~:¡i:£ ´iur£× ~:¡i:£ ´iur£× ~:¡i:£ ´iur£× ~:¡i:£ ´iur£×

(FAQ) For Core & Advance Java









(FAQ) For Core & Advance Java
/ Paar (a~ !×»× P~a.~×..e~; / Paar (a~ !×»× P~a.~×..e~; / Paar (a~ !×»× P~a.~×..e~; / Paar (a~ !×»× P~a.~×..e~;
1o usc ihis book docs rcquirc a prior basic knowlcdgc oí Ja·a and Ad·ancc
Ja·a. 1his book is siricily íor rcícrcncc purposc. And ii gi·cs ihc basic
dcíiniiion oí Corc Ja·a and icchnology rclaicd io Ja·a.

viס 1; 1.;:àe viס 1; 1.;:àe viס 1; 1.;:àe viס 1; 1.;:àe
1his book coniains all aspcci oí Ja·a programming languagc.
Part I prcscnis ihc dcíiniiion oí Corc Ja·a. Ii bcgins will classcs and ihcn
on io daia iypcs. conirol siaicmcnis and classcs. Iari I also dcíincs ihc
cxccpiion handing dcíiniiion. muliiihrcading packagcs and inicríacc.
Part II cxamincs ihc Ja·a Bcan and ii propcriics and bcha·ior.
Part III is whcrc ihc ad·ancc Ja·a dcíiniiion siaris. Ii includcs Ja·a
Scr·lcis JSI. Siruis and EJB and SOL.

1.¡e.;:a. c( ji:; Paar; 1.¡e.;:a. c( ji:; Paar; 1.¡e.;:a. c( ji:; Paar; 1.¡e.;:a. c( ji:; Paar;
1his book is inicndcd íor ihc quick rcícrcncc íor ihc dcíiniiion oí Ja·a and
Ad·ancc Ja·a. Ii is wriiicn in inicnsion íor ihc programmcr io prcparc íor
iis Inicr·icw. 1hc dcíiniiions pro·idcd in ihis book arc commonly askcd in
ihis indusiry.

ra..e.¡; ra..e.¡; ra..e.¡; ra..e.¡;
Ií you ha·c any commcnis/suggcsiions ihcn plcasc mail mc on my
íollowing ID as ii shall hclp mc in building ihis book.

Edition: :.o

MithiI ShukIa

(FAQ) For Core & Advance Java
rcP£ !/\/ rcP£ !/\/ rcP£ !/\/ rcP£ !/\/

Vhat is thc diffcrcncc bctwccn thc !DK i.ce cvcnt modcI and thc
cvcnt-dcIcgation modcI introduccd with !DK i.i?

1hc JDK :.o: c·cni modcl uscs an c·cni inhcriiancc or bubbling approach.
In ihis modcl. componcnis arc rcquircd io handlc ihcir own c·cnis. Ií ihcy
do noi handlc a pariicular c·cni. ihc c·cni is inhcriicd by (or bubblcd up io)
ihc componcni's coniaincr. 1hc coniaincr ihcn ciihcr handlcs ihc c·cni or
ii is bubblcd up io iis coniaincr and so on. uniil ihc highcsi-lc·cl coniaincr
has bccn iricd.
In ihc c·cni-dclcgaiion modcl. spcciíic objccis arc dcsignaicd as c·cni
handlcrs íor GUI componcnis. 1hcsc objccis implcmcni c·cni-lisicncr
inicríaccs. 1hc c·cni-dclcgaiion modcl is morc cííicicni ihan ihc c·cni-
inhcriiancc modcl bccausc ii climinaics ihc proccssing rcquircd io suppori
ihc bubbling oí unhandlcd c·cnis.

Vhat is thc diffcrcncc bctwccn !RE and !AVA?

1hcy arc íunciionally cqui·alcni. wiih minor diíícrcnccs in ihc handling oí
dcíauli ClassIaih and opiions supporicd. 1o rcducc coníusion. ihc jrc
command was rcmo·cd in JDK :.:. Insicad ihcrc is a "ja·a" command in
boih bin and jrc/bin.
jrc.c×c is ihc ja·a launchcr ihai comcs wiih ihc Ja·a Runiimc
En·ironmcni. Ii ignorcs ihc CLASSIA1H cn·ironmcni sciiing in ía·or oí
iis own inicrnally gcncraicd dcíauli and whaic·cr is supplicd on ihc cmd
linc using -cp or -classpaih. Ii's inicndcd io bc a bii simplcr íor ihosc who
arc only c·cr running Ja·a programs. noi dc·cloping ihcm.
java.c×c is ihc ja·a launchcr ihai comcs wiih ihc JDK. Ii uscs ihc
CLASSIA1H cn·ironmcni sciiing as a siariing poini and ihcn iacks on iis
own inicrnally gcncraicd cnirics.
1hcy boih scr·c ihc samc purposc and ihai's io siari a Ja·a VM.
ha·c ii run a Ja·a applicaiion. ihcn icrminaic. 1hc sourcc íor jrc.cxc is
pro·idcd in ihc JDK. 1hc sourcc io ja·a.cxc is pro·idcd only in ihc JDK
Sourcc disiribuiion.

Vhat is cIass?
A cIass is a icmplaic íor an objcci

(FAQ) For Core & Advance Java
CIass is a logical consiruci upon which ihc cniirc Ja·a languagc is buili
bccausc ii dcíincs ihc shapc and naiurc oí o|]«cí.
Notc: 1hc class íorms a basis íor objcci-oricnicd-programming in Ja·a.
Any conccpi you whish io implcmcni in Ja·a program musi bc
cncapsulaicd wiihin a class
A cIass is simply a collcciion oí o|]«cí, m«íno(. and (uíu.

Notc: A class cannoi bc dcclarcd as pri·aic or proiccicd.

Is a cIass a SubcIass of itscIf?

Ycs. A cIass is a SubcIass oí iisclí.

If vou dcfinc an inncr cIass at thc samc IcvcI as thc cncIosing cIass
instancc variabIcs, can inncr cIass acccss thosc instancc variabIcs?

Ycs. ií you dcíinc an inncr cIass ai ihc samc lc·cl as ihc cnclosing class`
insiancc ·ariablcs. ihc inncr class can acccss ihosc insiancc ·ariablcs - no
maiicr whai ihcir acccss conirol.

Vhat happcns whcn vou dcfinc an inncr cIass within a mcthod? Can it
acccss instancc variabIcs and aIso thc IocaI variabIcs of a mcthod?

Ií you dcíinc an inncr cIass wiihin a mcihod. ihc inncr class can acccss ihc
cnclosing class` insiancc ·ariablcs and also ihc local ·ariablcs and
paramcicr íor ihai mcihod.

Can vou dcfinc a cIass without namc?

Ycs. you can dcíinc an anonvmous cIass - a class wiihoui a namc.

Can anonvmous cIasscs havc const?

Anonvmous cIasscs cannoi ha·c consi

(FAQ) For Core & Advance Java
If vour inncr cIass is not dcfincd as static can vou crcatc ncw instanccs
of this cIass from a non-static mcthod?

Ycs. ií your inncr cIass is noi dcíincd as .íuí:c you can only crcaic ncw
insianccs oí ihis class írom a non-.íuí:c mcihod.

Vhat is an objcct?
An Objcct is a soíiwarc unii ihai combincs a siruciurcd sci oí daia wiih a
sci oí opcraiions íor inspcciing and manipulaiing ihai daia.

Docs !ava providc standard Itcrator functions for inspccting a
coIIcction of objccts?

Sort of; thc Enumcration intcrfacc in java.utiI packagc providcs a
barc-boncs framcwork for stcpping oncc through a coIIcction of
1hc Enumcraiion inicríacc in ja·a.uiil packagc lcis you cxiraci clcmcnis
onc by onc írom a collcciion wiihoui nccding io know how ihc collcciion is
implcmcnicd. 1hc inicríacc compriscs oí jusi : mcihods
• HasMorcElcmcnis().
• NcxiElcmcni().

Is !ava objccts pointcrs?

No. Ja·a objccis arc objccis (iypcd coniaincrs íor daia plus associaicd
mcihods). bui ihc Ja·a languagc lcis you acccss objccis only ·ia rcícrcnccs.

Vhat is thc diffcrcncc bctwccn java cIass fiIc and c×c?

Iollowing arc ihc diíícrcncc bciwccn ja·a class íilc and cxc.

.cIass FiIc E×c FiIc
.class íilc is noihing bui an byic codc
which is iransíormcd by Ja·a during
ihc compilc iimc.
Exc is an comprcsscd sclí cxccuii·c
binary íilc
In ordcr io ialk (or communicaic)
wiih opcraiing sysicm ii nccds JVM
Ii docsn`i nccd and iranslaior io ialk
wiih opcraiing sysicm as Opcraiing
(FAQ) For Core & Advance Java
as an iranslaior as opcraiing sysicm
docsn`i undcrsiand byic codc.
sysicm only undcrsiand binary codc
which Exc pro·idcs io ii
.class íilc arc slowcr in cxccuiing Exc arc íasicr in cxccuiion.
.class íilc associaics wiih Ja·a
Exc arc associaicd wiih C/C++

Do constructor havc rcturn tvpc? If vou trv to rcturn what wiII

Consiruciors nc·cr rciurn a ·aluc. Ií you do spcciíy a rciurn ·aluc. ihc
JVM will inicrprci your inicndcd consirucior as a mcihod.

Docs !VM caII an objcct`s constructor whcn vou cIonc thc objcct?

1hc JVM docs noi call an objcci`s consirucior whcn you clonc ihc objcci.

If I c×tcnd/SubcIass a cIass, do constructors inhcrit?

Consirucior dcclaraiions arc noi mcmbcrs. 1hcy arc nc·cr inhcriicd and
ihcrcíorc arc noi subjcci io hiding or o·crriding. 1hc dcíauli consirucior is
noi inhcriicd.

Vhat wiII happcn if vou don`t givc dircct caII to constructor via.
supcr() or this()jwith or without args j¯?

Ií. in a consirucior. you do noi makc a dircci call io .up«· or ín:. (wiih or
wiihoui args) ¯noic. musi bc on ihc íirsi linc¯ ihcn .up«· (no args) will íirsi
bc in·okcd ihcn any codc in ihc consirucior will bc cxccuicd.

How arc this() and supcr() uscd with constructors?

this () is uscd io in·okc a consirucior oí ihc samc class.
supcr () is uscd io in·okc a Supcrclass consirucior.

(FAQ) For Core & Advance Java
Vhat modificrs mav bc uscd with a top-IcvcI cIass?

A iop-lc·cl class may bc public. absiraci. or íinal.

Vhat is dcfauIt acccss modificr in java?
1hc dcíauli acccss modiíicr in ja·a is íricndly¨. i.c.
Dcíauli -- Ií no acccss lc·cl is spcciíicd. ihcn any class in ihc samc packagc
as ihc dcclaring class will bc pcrmiiicd acccss.

Vhat arc Vrappcr cIasscs?

1hc Vrappcr cIasscs arc classcs ihai allow primiii·c iypcs io bc acccsscd
as objccis.

Vhat is thc LocaIc cIass?

1hc LocaIc cIass is uscd io iailor program ouipui io ihc con·cniions oí a
pariicular gcographic. poliiical. or culiural rcgion.

Vhat is thc Dictionarv cIass?

1hc Dictionarv cIass is an absiraci class ihai pro·idcs ihc capabiliiy io
siorc kcy-·aluc pairs.
Notc: 1hc Dictionarv cIass is o|.o}«í«. You should implcmcni Map
inicríacc io obiain kcy/·aluc íunciionaliiy.

Vhat is instancc variabIc?

1hc daia dcíincd by ihc class arc rcícrrcd io as an insiancc ·ariablc or
mcmbcr ·ariablc.
1hc daia. or ·ariablc. dcíincd wiihin a class arc callcd as insiancc ·ariablc

public class classnamc
ini i·o. // Insiancc ·ariablc
(FAQ) For Core & Advance Java
Siring s·¨Hcllo¨. // Insiancc Variablc
Char c ·`c`. // Insiancc Variablc

Vhat is cIass variabIc?

1hc ·ariablcs. which arc dcíincd wiih ihc siaiic modiíicr. arc callcd as class
public class classnamc
siaiic ini x·¨o¨. // class Variablc
• 1hc runiimc sysicm allocaics class ·ariablcs oncc pcr class
rcgardlcss oí ihc numbcr oí insianccs crcaicd oí ihai class.
• You can acccss class ·ariablcs ihrough an insiancc or ihrough ihc
class iisclí.

Vhat is instancc mcthod?

Insiancc mcihods opcraic on ihc currcni objcci's insiancc ·ariablcs bui also
ha·c acccss io ihc class ·ariablcs.

Vhat is cIass mcthod?

Class mcihods. arc ihc mcihod ihai cannoi acccss ihc insiancc ·ariablcs
dcclarcd wiihin ihc class (unlcss ihcy crcaic a ncw objcci and acccss ihcm
ihrough ihc objcci). Also. class mcihods can bc in·okcd on ihc class. you
don'i nccd an insiancc io call a class mcihod.

Vhat is thc diffcrcncc bctwccn an instancc variabIc and a cIass

An insiancc ·ariablc rcprcscnis a scparaic daia iicm íor cach insiancc oí a
class. whcrcas a class ·ariablc rcprcscnis a singlc daia iicm sharcd by ihc
class and all iis insianccs.
(FAQ) For Core & Advance Java

Is it pcrfcctIv IcgaI to havc two diffcrcnt instancc variabIcs with thc
samc namc?

Ii is pcríccily lcgal io ha·c iwo diíícrcni insiancc ·ariablcs wiih ihc samc
namc ií ihcy arc dcíincd in diíícrcni classcs whcrc onc class inhcriis írom
ihc oihcr.

Vhat is thc diffcrcncc bctwccn an instancc mcthod and a cIass

An insiancc mcihod has this insiancc rcícrcncc. whcrcas a class mcihod
docs noi.

Can vou makc a mcthod in a subcIass morc privatc than it is dcfincd in
thc SupcrcIass?

You cannoi makc a mcihod in a subclass morc pri·aic ihan ii is dcíincd in
ihc Supcrclass. ihough you can makc ii morc public.

Vhat docs it mcan that a mcthod or ficId is "static"?

Static ·ariablcs and mcihods arc insianiiaicd only oncc pcr class. In oihcr
words ihcy arc class ·ariablcs. noi insiancc ·ariablcs. Ií you changc ihc
·aluc oí a siaiic ·ariablc in a pariicular objcci. ihc ·aluc oí ihai ·ariablc
changcs íor all insianccs oí ihai class.
Siaiic mcihods can bc rcícrcnccd wiih ihc namc oí ihc class raihcr ihan ihc
namc oí a pariicular objcci oí ihc class (ihough ihai works ioo). 1hai's how
library mcihods likc Sysicm.oui.priniln() work. ouí is a siaiic íicld in ihc
ja·a.lang.Sysicm class.

Is a static variabIcs vaIuc IikcIv to bc garbagc coIIcctcd oncc objccts
of thc cIass arc no Iongcr pointing to it? Docs it dcstrov itscIf at anv
point or must that bc donc c×pIicitIv?

(FAQ) For Core & Advance Java
No - ihc siaiic ·ariablcs arc cííccii·cly linkcd io ihc Class objcci raihcr
ihan any insianccs. ihcy will only bc garbagc collccicd whcn ihc Class is
garbagc collccicd. 1his will only happcn ií ihc ClassLoadcr. which loadcd
ihc Class. is garbagc collccicd.

Vhat is a transicnt variabIc?

A transicnt ·ariablc is a ·ariablc ihai may noi bc scrializcd.

Can onc writc anothcr main mcthod in thc main, which is thc cntrv
point in java?

Ycs. onc can wriic anoihcr main mcihod in main which is cniry poini íor
any ja·a program cxccpi Scr·lci and applci. ihc rcason is Ja·a jusi looks
ihc main as an cniry poini io siari running ihc ja·a íilc and whai c·cr
insidc ii bc ii a main mcihod JVM ircais ii as any oihcr o·crloadcd mcihod.

Vhv docsn't finaI prcvcnt mv objcct from changing?

You ha·c codc likc ihis

finaI StringBuffcr s = ncw StringBuffcr ("don't changc mc");
// ...
s.appcnd(", but I did");

And ihc ncw ·aluc oí s is "don'i changc mc. bui I did". 1hc rcason is ihai
ihc "íinal" modiíicr makcs ihc rcícrcncc ·ariablc (hcrc. s) íinal. noi ihc
objcci ihai s poinis io. Ii mcans ihai ihc rcícrcncc ·ariablc cannoi bc
changcd io poini io somc oihcr SiringBuíícr. 1hc siaic oí ihc SiringBuíícr
can siill bc modiíicd by calling mcihods on ii or dirccily assigning io iis
public íiclds.

1hc righi way io ihink aboui íinal is ihai ii prc·cnis you assigning io ihai
pariicular ·ariablc. 1hc only way io makc ihc íiclds oí an objcci consiani
(unchanging) is io makc all iis daia íiclds pri·aic. and noi pro·idc any sci
mcihods íor ihcm. only gci mcihods. E·cn ihai won'i siop oihcr objccis oí
ihc samc class adjusiing ii.

(FAQ) For Core & Advance Java

Vhat is thc diffcrcncc bctwccn finaI, finaIIv and finaIizc?
• FinaI: Iinal is uscd whcn onc wani io makc ihc ·alucs in ihc ·ariablc
or ihc objccis consiani ihrough oui ihc program.
• FinaIIv: Iinally is uscd along wiih ihc trv catch block
• FinaIizc: Iinalizc is uscd io call ihc garbagc collccior

Is it possibIc to ovcrridc a finaI mcthod?

Ob·iously. ii is noi possiblc io o·crridc a finaI mcihod.

If an objcct is garbagc coIIcctcd, can it bccomc rcachabIc again?

Oncc an objcci is garbagc collccicd. ii ccascs io cxisi. Ii can no longcr
bccomc rcachablc again.

Vhcn is thc finaIIv cIausc of a trv-catch-finaIIv statcmcnt c×ccutcd?

1hc finaIIv clausc oí ihc iry-caich-íinally siaicmcni is always cxccuicd
unlcss ihc ihrcad oí cxccuiion icrminaics or an cxccpiion occurs wiihin ihc
cxccuiion oí ihc íinally clausc.

Vhat happcns if a trv-catch-finaIIv statcmcnt docs not havc a catch
cIausc to handIc an c×ccption that is thrown within thc bodv of thc
trv statcmcnt?

1hc cxccpiion propagaics up io ihc ncxi highcr lc·cl iry-caich siaicmcni
(ií any) or rcsulis in ihc program's icrminaiion.

How docs a trv statcmcnt dctcrminc which catch cIausc shouId bc
uscd to handIc an c×ccption?

Vhcn an cxccpiion is ihrown wiihin ihc body oí a iry siaicmcni. ihc caich
clauscs oí ihc iry siaicmcni arc cxamincd in ihc ordcr in which ihcy appcar.
(FAQ) For Core & Advance Java
1hc íirsi caich clausc ihai is capablc oí handling ihc cxccpiion is cxccuicd.
1hc rcmaining caich clauscs arc ignorcd.

Can an objcct's finaIizc () mcthod bc invokcd whiIc it is rcachabIc?

An objcci's finaIizc () mcihod cannoi bc in·okcd by ihc garbagc collccior
whilc ihc objcci is siill rcachablc. Howc·cr an objcci's íinalizc () mcihod
may bc in·okcd by oihcr objccis.

Vhat wiII happcn whcn vou sct vour objcct rcfcrcncc to nuII?

Sciiing your objcci rcícrcncc io nuII makcs ii a candidaic íor garbagc

Vhich is thc basc cIass for aII c×ccption?
All cxccpiions arc subclasscs oí ihc class ja·a.lang.1hrowablc

Vhat vou wouId do in ordcr to prcvcnt vour objcct to bc garbagc
coIIcctcd oncc it Ioscs it`s rcfcrcncc?

Ií you wani io pcríorm somc iask whcn your objcci is aboui io bc garbagc
collccicd. you can o·crridc ihc ja·a.lang.Objcci mcihod callcd J:nu}::«().
1his mcihod is dcclarcd as proiccicd. docs noi rciurn a ·aluc. and ihrows a
1hrowablc objcci. i.c. p·oí«cí«( .o:( J:nu}::«() ín·ou. Tn·ouu|}«.

How manv timc can finaIizc() can invoIc on a objcct?

1hc JVM only in·okcs finaIizc() oncc pcr objcci. Sincc ihis is ihc casc. do
noi rcsurrcci an objcci in íinalizc as whcn ihc objcci is íinalizcd again iis
J:nu}::«() mcihod will noi bc callcd. Insicad you should crcaic a clonc oí ihc
objcci ií you musi bring ihc objcci back io liíc.

How docs !ava passcs it`s mcthod paramctcr?

Rcmcmbcr ihai Ja·a passcs mcihod paramcicrs by ·aluc and noi by
rcícrcncc. Ob·iously ihcn. anyihing ihai happcns io a primiii·c daia iypc
(FAQ) For Core & Advance Java
insidc a mcihod docs noi aíícci ihc original ·aluc in ihc calling codc. Also.
any rcassignmcni oí objcci rcícrcnccs insidc a mcihod has no cíícci on ihc
objccis passcd in.

Vhat is thc diffcrcncc bctwccn throw and throws in !AVA?
1hc throws clausc is uscd whcn dcclaring mcihods and indicaics ihai
undcr ccriain circumsianccs ihc mcihod will gcncraic a spcciíic cxccpiion.

Ior cxamplc. ihc dcclaraiion.

pubIic String mvMcthod(BooIcan statc)
throws IOE×ccption ¦

indicatcs that thc mcthod mvMcthod() can gcncratc an IOE×ccption.

1hc throw siaicmcni is uscd in ihc body oí ihc mcihod io aciually gcncraic
ihc cxccpiion undcr a spcciíic condiiion.

Ior Examplc.

if (tcstCondition)
throw ncw IOE×ccption(mcssagc);

So thc compIctc codc for thc mcthod couId Iook Iikc:

pubIic String mvMcthod(BooIcan statc)
throws IOE×ccption
String mcssagc = "Anothcr IOE×ccption";
String rcturnVaIuc = "HcIIo VorId!";

throw ncw IOEXccption(mcssagc)

rcturn rcturnVaIuc;
(FAQ) For Core & Advance Java

Undcr what circumstancc wiII vou writc a mcthod with throws

In·oking a mcihod which dcclarcs ii ihrows cxccpiions is noi possiblc
unlcss ciihcr ihc codc is placcd in a iry-caich. or ihc calling mcihod
dcclarcs ihai ii ihrows ihc cxccpiions. i.c. chcckcd cxccpiions musi bc
caughi or rcihrown. Ií ihc iry-caich approach is uscd. ihcn ihc iry-caich
musi copc wiih all oí ihc cxccpiions which a mcihod dcclarcs ii ihrows.

Vhat cIasscs of c×ccptions mav bc thrown bv a throw statcmcnt?

A ihrow siaicmcni may ihrow any cxprcssion ihai may bc assigncd io ihc
1hrowablc iypc.

Vhat cIasscs of c×ccptions mav bc caught bv a catch cIausc?

A caich clausc can caich any cxccpiion ihai may bc assigncd io ihc
1hrowablc iypc. 1his includcs ihc Error and Exccpiion iypcs.

Vhat is unchcckcd c×ccption?

RuniimcExccpiion and iis subclasscs arc uncn«c}«( cxccpiions.

Notc: Unchcckcd cxccpiions do noi ha·c io bc caughi. And All Errors arc

How manv bits arc uscd to rcprcscnt Unicodc, ASCII, UTF-ic, and
UTF-s charactcrs?

Unicodc rcquircs :o biis and ASCII rcquirc biis. Alihough ihc ASCII
characicr sci uscs only biis. ii is usually rcprcscnicd as s biis. U1I-s
rcprcscnis characicrs using s. :o. and :s bii paiicrns. U1I-:o uscs :o-bii
and largcr bii paiicrns.

(FAQ) For Core & Advance Java
Vhat arc thc rangc of ASCII charactcrs?

ASCII characicrs arc all íound in ihc rangc \uoooo` io \uooíí`

Vhat is thc dcfauIt vaIuc for anv cIass variabIc or instancc variabIc
dccIarcd as a char?

1hc dcíauli ·aluc íor any class ·ariablc or insiancc ·ariablc dcclarcd as a
char is \uoooo`.

Is thc tcrnarv opcrator writtcn × : v ? z or × ? v : z ?

Ii is wriiicn x' y . z.

Vhat is thc diffcrcncc bctwccn >> and >>>?

>> is righi shiíi kccp ihc sign. >>> is righi shiíi don`i kccp ihc sign.

Vhat is thc diffcrcncc bctwccn cquaIs() and ¨==¨?

1hc «quu}.() mcihod (dcíincd ai ihc highcsi` lc·cl as a mcihod oí Objcci) is
uscd io icsi ihc ·aluc oí an objcci. 1hc ·· opcraior is uscd io icsi ihc
objcci rcícrcnccs ín«m.«}.«..

Docs fIoating mcthods givc c×ccption?

Di·iding an inicgcr by o is illcgal and would causc Ja·a io ihrow an
d·:ínm«í:c1rc«pí:on. Iloaiing-poini numbcrs ha·c ·alucs íor iníiniiy and
noi-a-numbcr. so using ariihmciic opcraiors on íloaiing poini numbcrs
nc·cr rcsulis in an cxccpiion.

Vhat is thc diffcrcncc bctwccn Intcgcr and int in !ava?

Inicgcr is a class dcíincd in ihc ja·a.lang packagc. whcrcas ini is a primiii·c
daia iypc dcíincd in ihc Ja·a languagc

(FAQ) For Core & Advance Java
Do !ava fIoating-point opcrations throw anv c×ccption if undcrfIow or
ovcrfIow occurs?

No. Ja·a's íloaiing-poini opcraiions do noi ihrow any cxccpiion. O·crílows
rcsuli in IOSI1IVE_INIINI1Y or NEGA1IVE_INIINI1Y. dcpcnding
on signs. Undcrílows rcsuli in o.o oí ihc propcr sign.

How do I rcad a hc× string into an int?

ini i · Inicgcr.·alucOí (myHcxSiring. :o).iniValuc().

Vhat is idcntificr?

An idcntificr is an unlimiicd-lcngih scqucncc oí Ju.u }«íí«·. and Ju.u (:v:í..
ihc íirsi oí which musi bc a Ja·a lciicr. Ja·a lciicrs includc _ and s. Digiis
includc o-¬.

Is thcrc anv Iimit to thc Icngth of an idcntificr?

Ycs. o:.:·: characicrs is ihc maximum possiblc idcniiíicr lcngih. bccausc
idcniiíicr namcs musi bc rcprcscniablc in Ja·a class íilcs.

How do I comparc two Strings?

1hc comparison using "· ·" on objccis. such as Sirings. is asking.
"Do ihcsc iwo objccis ha·c ihc samc rcícrcncc'" Do ihcy ha·c ihc samc
addrcss. and hcncc arc ihc samc objcci' Vhai you rcally wani io do is ask.
"Do ihcsc iwo Sirings ha·c ihc samc ¨conicnis¨'"
Comparc Siring conicnis wiih any oí ihc íollowing.

ií (s:.cquals(s:) )
ií (s:.cqualsIgnorcCasc(s:) )
ií (s:.siarisViih(s:) )
ií (s:.cndsViih(s:) )
ií (s:.rcgionMaichcs(s:_oíísci. s:. s:_oíísci. lcngih) )
ií (s:.comparc1o(s:) < o)
(FAQ) For Core & Advance Java

(1hcrc arc oihcr ways. ioo.)
Noic ihai you can do ihis wiih liicrals.

ií ("applc".cquals(s:) ) ...

Ií you comparc ihcsc ihc oihcr way round. likc ihis.

ií ( s:.cquals("applc") ) ...

and s: is null. you will gci a null poinicr cxccpiion.

Vhat is thc "sub string trap"?

1hc "substring trap" is ihc namc íor a misiakc ihai is all ioo casy io makc
whcn using ihc subsiring() mcihod oí class Siring. 1hc mcihod signaiurc
public Siring subsiring(ini bcginIndcx. ini cndIndcx)

1hc namc "cndIndcx" suggcsis ihai is ihc indcx whcrc ihc Subsiring cnds.

Bui in íaci. ihc subsiring cxicnds only io ihc characicr ai posiiion
(cndIndcx-:)! Ii sccms io bc donc ihis way so ihai s.subsiring(o.s.lcngih())
is cqual io s. Ií so. ihc namc oí ihc sccond paramcicr should bc somcihing
likc cndInxLcssOnc or Lcngih. Bui noi ihc coníusing and mislcading
cndIndcx. Bcwarc oí ihc subsiring irap.

Vhat is thc diffcrcncc bctwccn thc String and StringBuffcr cIasscs?

Siring objccis arc consianis. SiringBuíícr objccis arc noi.

Vhv is thc cIass String dcsigncd to bc immutabIc (i.c. rcad onIv)?

Iirsily. an immuiablc objcci is ihrcad-saíc and you don'i nccd io worry
aboui daia consisicncy whilc acccsscd by many ihrcads.
Sccondly. sincc ii is immuiablc. ii is noi ncccssary io synchronizc iis
mcihods. 1hus. somc rcad-only mcihods (such as gciChars) arc íasicr in
Siring ihan SiringBuíícr.
(FAQ) For Core & Advance Java

Vhat wiII happcn whcn vou run foIIowing codc?

íinal SiringBuíícr s · ncw SiringBuíícr("don'i changc mc").
// ...
s.appcnd(". bui I did").

Assumc ihai codc is wriiicn in main

Vhat wiII bc thc rcturn tvpc for c×prcssion of tvpc if() and whiIc()?

1hc cxprcssion íor an ií¨ and whilc¨ siaicmcni mu.í |« u Boo}«un.

Vhat wiII bc thc rcturn tvpc for switch statcmcnt?

1hc cxprcssion in a swiich siaicmcni mu.í |« un :ní o· u cnu·.

Vhat is Arrav?
An arrav is a group oí likcd-iypcd ·ariablcs ihai arc rcícrrcd io by common

If arravs arc objccts, whv can't I usc a Icngth mcthod to dctcrminc an
arrav's sizc?

Such a mcthod wouId bc rcasonabIc, but it docsn't c×ist; thc !ava
Ianguagc dcsigncrs dccidcd to c×posc arrav Icngth Iikc a pubIic finaI
instancc variabIc rathcr than a mcthod.
In Ja·a. arrays arc objccis. 1hcy arc crcaicd dynamically írom ccniral.
sysicm-managcd mcmory likc oihcr objccis. ihcy all ha·c Objcci as a
Supcrclass. and you can in·okc any Objcci mcihod on ihcm. Gi·cn ihis. ii
would ha·c bccn possiblc io dcíinc a lcngih insiancc mcihod on arrays io
rciurn ihc lcngih oí ihc array. much likc ihc lcngih mcihod in class Siring.
Howc·cr. ihc Ja·a languagc dcsigncrs chosc io rcprcscni lcngih simply
as an insiancc ·ariablc oí ihc array. 1hc lcngih ·ariablc is always acccssiblc
bui nc·cr changcablc. iis ·aluc is íixcd ai ihc iimc ihc array is crcaicd.

(FAQ) For Core & Advance Java

How can vou acccss thc first and Iast vaIucs in an arrav?

Iirsi clcmcni in an array is ai indcx o and lasi clcmcni is ai }«nvín-:. }«nvín
is a spccial array ·ariablc (noi a mcihod. so don`i nccd round brackcis aíicr

Vhat is Inhcritancc?

Inhcritancc is ihc way a Subclass uscs mcihod dcíiniiions and ·ariablcs
írom a Supcrclass jusi as ií ihcy bclongcd io ihc Subclass iisclí.

Is it possibIc to dccIarc an inhcritcd mcthod abstract?

Ycs. Ii is possiblc io dcclarc an inhcriicd mcihod u|.í·ucí.
Notc: bui ihcn ihcrc is no way io gci io ihc bcha·ior in ihc hicrarchy
abo·c ihc u|.í·ucí dcclaraiion

Vhat is PoIvmorphism?

PoIvmorphism (írom a Grcck. mcaning many íorms¨) is a ícaiurc ihai
allows onc inicríacc io bc uscd íor a gcncral class oí aciions.
c.g. Considcr ihc siack (which is a lasi-in íirsi-oui lisi). You mighi ha·c a
program ihai rcquircs ihrcc iypcs oí siacks. Onc siack is uscd íor inicgcr
·alucs. onc íor íloaiing-poini ·alucs. and onc íor characicrs. 1hc algoriihm
ihai implcmcnis cach siack is samc. c·cn ihough daia bcing siorcd diíícrs.
In non-objcci-oricnicd languagc. you would bc rcquircd io crcaic ·
diíícrcni scis oí siack rouiinc. wiih cach sci using diíícrcni namcs.
Howc·cr. bccausc oí polymorphism. in Ja·a you can spcciíy a gcncral sci oí
siack rouiincs ihai all sharc ihc samc namc.
Morc. Gcncral. ihc conccpi oí Iolymorphism is oíicn cxprcsscd by ihc
¨Onc Intcrfacc, MuItipIc mcthods¨

Vhat is an ovcrIoadcd mcthod?

(FAQ) For Core & Advance Java
An ovcrIoadcd mcthod. siricily spcaking. is morc ihan onc mcihod. ii is
iwo or morc scparaic mcihods (dcíincd or inhcriicd) in ihc samc class ihai
sharc ihc samc namc.

Vhat docs it mcan to ovcrridc a mcthod?

Insicad oí inhcriiing a mcihod írom iis Supcrclass. a Subclass can o·crridc
ihc mcihod by pro·iding iis own dcíiniiion íor ii.

Vhat is thc diffcrcncc bctwccn ovcrIoading and ovcrriding?

OvcrIoading occurs whcn two or morc mcthods usc thc samc namc
(but diffcrcnt paramctcr Iists), such that both mcthods arc avaiIabIc
sidc bv sidc in thc samc cIass; ovcrriding occurs whcn a SubcIass
mcthod and a SupcrcIass mcthod usc thc samc namc (and matching
paramctcr Iists), such that thc SubcIass mcthod bIocks out thc
SupcrcIass mcthod.
O·crloading and o·crriding arc oíicn coníuscd wiih cach oihcr. bccausc
ihc words arc similar and bccausc ihcy boih in·ol·c a singlc mcihod namc
rcícrring io poicniially sc·cral diíícrcni mcihod implcmcniaiions. 1hcrc
arc clcar diíícrcnccs. howc·cr. whcn you comparc ihc iwo poini by poini. as
donc in 1ablc.

OvcrIoading vcrsus Ovcrriding
OvcrIoading Ovcrriding
Rclaiionship bciwccn mcihods
a·ailablc in ihc samc class
Rclaiionship bciwccn a Supcrclass
mcihod and a Subclass mcihod
Docs noi block inhcriiancc írom
ihc Supcrclass
Blocks inhcriiancc írom ihc
Scparaic mcihods sharc
(o·crload) ihc samc namc
Subclass mcihod rcplaccs
(o·crridcs) ihc Supcrclass mcihod
Diíícrcni mcihod signaiurcs Samc mcihod signaiurc
May ha·c diíícrcni rciurn iypcs Musi ha·c maiching rciurn iypcs
May ha·c diíícrcni dcclarcd
Musi ha·c compaiiblc dcclarcd

(FAQ) For Core & Advance Java
Ior cxamplc. boih ihc O|]«cí c}u.. and ihc Sí·:nv c}u.. (an immcdiaic
Subclass oí Objcci) in ihc ja·a.lang packagc dcíinc an cquals mcihod.
In Objcct : public boolcan cquals(Objcci obj) ¦ /¨ ... ¨/ ·
In String : public boolcan cquals(Objcci obj) ¦ /¨ ... ¨/ ·
1his is a casc oí o·crriding. sincc Siring is a Subclass oí Objcci. and
Siring's cquals mcihod has ihc samc namc. paramcicr iypcs. and rciurn
iypc as Objcci's cquals mcihod. Now supposc ihai a Siring: class
dcíincd cquals io iakc a Siring: paramcicr insicad oí an Objcci paramcicr.
In Objcct : public boolcan cquals(Objcci obj)
Hypoihciical Stringe: public boolcan cquals(Siring: sir)
1his would bc a casc oí o·crloading. 1hc Siring: class would ha·c iwo
alicrnaic cquals mcihods. onc dcíincd in Siring: and onc inhcriicd írom
Objcci. E·cn ihough ihc iwo mcihods ha·c ihc samc namc and rciurn iypc.
ihcy diíícr in whai paramcicr ihcy iakc.

Docs Cocrcion of argumcnts happcn with ovcrIoading, as wcII as

Cocrcion oí argumcnis only happcns wiih o·crloading. noi o·crriding.

Vhat is an abstract mcthod?

An abstract mcthod is a mcthod that dcfincs aII aspccts of itscIf
c×ccpt what codc it c×ccutcs.
An absiraci mcihod dcclarcs ihc mcihod's namc. paramcicr iypcs. rciurn
iypc. and cxccpiions. bui docs noi pro·idc an implcmcniaiion íor ihc
mcihod. You dcclarc an absiraci mcihod wiih ihc absiraci kcyword and
wiih a scmicolon in placc oí ihc mcihod body. Ior cxamplc.
public absiraci ·oid drawIigurc(). // absiraci mcihod dcclaraiion

Vhat is an abstract cIass?

An abstract cIass is a cIass dcsigncd with impIcmcntation gaps for
SubcIasscs to fiII in.
An absiraci class is dclibcraicly incomplcic. Ii dcíincs a skclcion ihai
·arious Subclasscs can ílcsh oui wiih cusiomizcd implcmcniaiion dciails.
1hc diíícrcni conc·«í« (nonabsiraci) Subclasscs pro·idc a íamily oí ·arianis.
An absiraci class musi bc cxpliciily dcclarcd wiih ihc absiraci kcyword.
(FAQ) For Core & Advance Java
Noic ihai dcclaring a class as absiraci suííiccs io makc ii absiraci÷ii nccd
noi c·cn ha·c any absiraci mcihods. By dcclaring ii absiraci. you signal
ihai ii is íunciionally incomplcic. and you cnsurc ihai no onc can crcaic
insianccs oí ihai class.

Can an abstract cIass bc finaI as wcII as static?

An absiraci class may noi bc dcclarcd as íinal.
And Absiraci class cannoi bc siaiic bccausc siaiic mcihods cannoi bc

Vhat is an intcrfacc?

In thc !ava Ianguagc, an intcrfacc is Iikc a strippcd-down cIass: it
spccifics a sct of mcthods that an instancc must handIc, but it omits
inhcritancc rcIations and mcthod impIcmcntations.
Objcci-oricnicd programming is somciimcs modclcd as communicaiion
bciwccn objccis÷onc objcci ialks io. or scnds a mcssagc io. anoihcr objcci
by in·oking a mcihod on ii. In ihis ·icw. an inicríacc in ihc Ja·a languagc
spcciíics ihc barc minimum onc objcci nccds io know in ordcr io ialk io
anoihcr objcci. a sci oí absiraci mcihods . ihai is. mcihods minus any
implcmcniaiion iníormaiion. Iui anoihcr way. an inicríacc spcciíics how
you can ialk io an objcci. bui says noihing aboui whai kind oí objcci will
handlc your mcssagcs. Ii is ihc job oí onc or morc class insianccs io pro·idc
ihc subsiancc bchind an inicríacc's promisc.
Inicríaccs arc a closc cousin io classcs. and ihcir dcclaraiions rcscmblc
class dcclaraiions. Ior cxamplc. ihc Runnablc inicríacc in ihc ja·a.lang
packagc (JDK :.o.: and :.:) is dcclarcd as íollows.
/¨ in Runnablc.ja·a (JDK :.o.: and :.:). ¨/
public inicríacc Runnablc ¦
public absiraci ·oid run().
An inicríacc can also coniain siaiic íinal ·ariablcs. callcd class consianis.
scc Tn« Ju.u 1unvuuv« Sp«c:J:cuí:on. pp. :so-:ss. íor dciails.
Inicríaccs pro·idc ihc minimal lc·cl oí dcpcndcncc bciwccn inicraciing
objccis. An inicríacc íocuscs purcly on ihc rolc an objcci plays÷whai
scr·iccs ii can pro·idc÷wiihoui making any rcsiriciions on ihc class oí ihc

(FAQ) For Core & Advance Java

How is an abstract cIass diffcrcnt from an intcrfacc?

AIthough thc !ava Ianguagc makcs a cIcar distinction bctwccn
abstract cIasscs and intcrfaccs, in practicc thc diffcrcncc is oftcn a
mattcr of dcgrcc and intcnt.
Absiraci classcs íill in ihc widc rangc bciwccn concrcic classcs and
inicríaccs. 1ablc lisis sc·cral disiinciions ihai arc rclc·ani io iypical uscs.

Concrctc CIass vcrsus Abstract CIass vcrsus Intcrfacc
Concrcic Class Absiraci Class Inicríacc
Spcciíics ihc íull
sci oí mcihods íor
an objcci
Spcciíics ihc íull
sci oí mcihods íor
an objcci
Spcciíics a subsci oí
mcihods íor an objcci
Implcmcnis all oí
iis mcihods
Implcmcnis nonc.
somc. or all oí iis
Implcmcnis nonc oí iis
Can ha·c insianccs
Can'i ha·c
Can`i ha·c insianccs
Can ha·c
Musi ha·c
Subclasscs. usclcss
wiihoui ihcm
Can`i ha·c Subclasscs. musi
ha·c classcs ihai implcmcni
ii. usclcss wiihoui ihcm

Ií ihc diíícrcnccs in 1ablc abo·c lca·c you unccriain aboui whcihcr io usc
an absiraci class or an inicríacc. onc íurihcr íacior may bc dccisi·c. A class
can ha·c only onc immcdiaic Supcrclass. bui ii can implcmcni any numbcr
oí inicríaccs. An absiraci class. c·cn onc wiih all absiraci mcihods. iics iis
Subclasscs io a pariicular inhcriiancc hicrarchy. An inicríacc. in conirasi.
lca·cs an objcci írcc io implcmcni oihcr inicríaccs as nccdcd by ihc objcci's
Bcyond ihc dciails. ii is imporiani io undcrsiand how classcs and
inicríaccs diíícr in spirii. Classcs gcncrally spcciíy ihc íull idcniiiy oí an
objcci. who/whai ii is (iis parcniagc). whai rolcs ii can pcríorm (iis
·ocabulary oí mcihods). and how ii spcciíically pcríorms ihosc rolcs (iis
implcmcnicd bcha·ior). Inicríaccs spcciíy nciihcr parcniagc nor bcha·ior
íor an objcci÷ihcy íocus cxclusi·cly on ihc rolc(s) an objcci can play.
Considcr. íor cxamplc. ihc Obscr·cr inicríacc and Obscr·ablc class in
ihc ja·a.uiil packagc. An Obscr·cr objcci is onc ihai cxpccis io bc noiiíicd
(FAQ) For Core & Advance Java
whcn an objcci ii is waiching (an Obscr·ablc objcci) changcs siaic. An
Obscr·ablc objcci is onc ihai knows how io rcgisicr. unrcgisicr. and noiiíy
a collcciion oí Obscr·cr objccis.
1hc Obscr·cr inicríacc (JDK :.o.: and :.:) dcíincs a singlc mcihod.
/* in Obscrvcr.java (!DK i.c.e and i.i): */
pubIic intcrfacc Obscrvcr ¦
void updatc(ObscrvabIc o, Objcct arg);
Bccausc Obscrvcr is an intcrfacc, vou can dcfinc whatcvcr cIass vou
Iikc to impIcmcnt thc intcrfacc. For instancc, vou couId dcfinc a
Button SubcIass to scrvc as an Obscrvcr:
pubIic cIass ObscrvcrButton c×tcnds Button
impIcmcnts java.utiI.Obscrvcr ¦
pubIic static void updatc(ObscrvabIc o, Objcct arg) ¦
// ... rcspond to notification
1his indcpcndcncc oí inicríaccs írom any class hicrarchy is a nuv« bcncíii.
In conirasi. Obscr·ablc is dcíincd as a class. which rcsiricis all
Obscr·ablc objccis io bclong io ihc Obscr·ablc class or io a Subclass oí
Obscr·ablc. Unlikc Obscr·crBuiion abo·c. you simply cannoi dcíinc an
Obscr·ablcBuiion as a Subclass oí Buiion. Ja·a allows only singlc class
inhcriiancc. which íorccs you io choosc bciwccn onc or ihc oihcr
inhcriiancc hicrarchy.

Vhat is thc CoIIcctions API?

1hc Collcciions AII is a sci oí classcs and inicríaccs ihai suppori
opcraiions on collcciions oí objccis.

Vhat is thc Map intcrfacc?

1hc Map intcrfacc rcplaccs ihc JDK :.: Diciionary class and is uscd
associaic kcys wiih ·alucs.

Vhat is thc Sct intcrfacc?

(FAQ) For Core & Advance Java
1hc Sct intcrfacc pro·idcs mcihods íor acccssing ihc clcmcnis oí a íiniic
maihcmaiical sci. Scis do noi allow duplicaic clcmcnis.

Vhat is thc List intcrfacc?

1hc List intcrfacc pro·idcs suppori íor ordcrcd collcciions oí objccis.

Vhat is an Itcrator intcrfacc?

1hc Iicraior inicríacc is uscd io sicp ihrough ihc clcmcnis oí a Collcciion.

Vhat is thc HashMap?

HaspMap pro·idcs kcy-·aluc acccss io daia jusi likc a mini daiabasc. Ii is
also pari oí ncw Collcciions Iramcwork. addcd wiih Ja·a :. ·:.:

Vhat is HashtabIc?

Hashiablc ioo pro·idcs kcy-·aluc acccss io ihc daia. Ii was onc oí ihc
original Collcciion classcs in Ja·a

Vhat is thc diffcrcncc bctwccn HashMap and HashtabIc?

Now in ihc abo·c : dcíiniiion you mighi ha·c noiicc ihc dcíiniiion arc ·cry
idcniical and you wani io know ihai arc ihcrc any diíícrcncc bciwccn
HashMap and Hashiablc.
Ycs. Iollowing arc ihc diíícrcncc bciwccn HashMap and Hashiablc

HashMap HashtabIc
Acccss io ihc HashMap isn'i
syncronizcd. You can add ii. bui ii
isn'i ihcrc by dcíauli.
Vhilc acccss io ihc Hashiablc is
synchronizcd on ihc iablc
Anoihcr diíícrcncc is ihai Iicraior in
ihc HashMap is íail-saíc
Vhilc ihc cnumcraior íor ihc
Hashiablc isn'i.
HashMap pcrmiis null ·alucs in ii Hashiablc docsn'i.

(FAQ) For Core & Advance Java
Docs thc !ava Ianguagc providc virtuaI mcthods, Iikc C++?

Ycs; but !ava mcthods arc virtuaI bv dcfauIt, whcrcas C++ mcthods
arc not.
"Viriual mcihods" arc an csscniial ingrcdicni íor iaking ad·aniagc oí
inhcriiancc and polymorphism. Bccausc oí inhcriiancc and polymorphism.
an objcci rcícrcncc nccd noi maich cxacily ihc class oí ihc objcci ii rcícrs
io. ihc objcci can bclong io a Subclass oí ihc rcícrcncc's iypc. Ior cxamplc.
ihc a1hrcad rcícrcncc bclow is oí iypc 1hrcad. bui ihc objcci ii rcícrs io is
oí iypc My1hrcad. a Subclass oí 1hrcad.
cIass MvThrcad c×tcnds Thrcad ¦ /* ... ovcrridcs start() ... */ ]
cIass E×ampIc ¦
// ...
Thrcad aThrcad = ncw MvThrcad();
// ...
Vhcn you in·okc a mcihod on a rcícrcncc. such as ihc anObjcci.siari()
cxprcssion abo·c. ihc ·iriual machinc musi know which class io usc íor
looking up ihc mcihod dcíiniiion. ihcrc arc iwo basic choiccs.
.íuí:c |:n(:nv÷usc compilc-iimc iypc oí objcci rcícrcncc
(ynum:c |:n(:nv÷usc run-iimc iypc oí rcícrrcd-io objcci
In ihc prcccding 1hrcad cxamplc. dynamic binding will pick up ihc
o·crriddcn siari mcihod in ihc My1hrcad Subclass. whcrcas siaiic binding
would íind only ihc 1hrcad ·crsion. Irogrammcrs íamiliar wiih C++ will
rccognizc ihis disiinciion as ihc diíícrcncc bciwccn non·iriual and ·iriual
In ihc Ja·a languagc and Viriual Machinc. dynamic binding is ihc
dcíauli. and ihcrc is no ·iriual kcyword. 1hc disiinciion bciwccn dynamic
and siaiic binding is gcncrally cxprcsscd in icrms oí whcihcr a mcihod can
bc o·crriddcn.

Vhat arc packagcs?

A packagc is a collcciion oí classcs and inicríaccs ihai pro·idcs a high-
lc·cl laycr oí acccss proicciion and namc spacc managcmcni.

(FAQ) For Core & Advance Java
Vhcrc wouId vou dcfinc thc packagc cIass?

packagc siaicmcni musi comc íirsi in íilc and musi bc íollowcd by any
impori siaicmcnis.

Vhv is cIonc mcthod protcctcd?

1hc clonc mcihod is proiccicd. so an objcci can only rcqucsi a clonc oí
anoihcr objccis which is ciihcr in ihc samc packagc or which ii inhcriis
írom. (i.c. siandard mcaning oí proiccicd).

Docs !VM caII an objcct`s constructor whcn vou cIonc thc objcct?

1hc JVM docs noi call an objcci`s consirucior whcn you clonc ihc objcci.

Vhat arc diffcrcnt tvpcs of Strcams/Rcadcrs/Vritcrs?
Strcams dcal in byics.
Rcadcrs/Vritcrs dcal in unicodc.
Low-lc·cl Sircams ÷ IilcInpuiSircam. IilcOuipuiSircam (byic
rcads/wriics ÷ no primaii·c)
BuíícrSircams ÷ BuíícrInpuiSircam ÷ siis bciwccn low-lc·cl
and high lc·cl sircams ÷ rcduccs I/O o·crhcad
High-Lc·cl Iilicr Sircam ÷ DaiaInpuiSircam ÷ can only chain
io low-lc·cl sircams (primaii·c rcads/wriics)

Vhat arc Proccsscs?

A Proccss is. in csscncc. a program ihai is cxccuiing. 1hus p·oc«..-|u.«(
muliiiasking is ihc ícaiurc ihai allows your compuicr io run iwo or morc
program concurrcnily.

(FAQ) For Core & Advance Java
Vhat is Thrcad?

Thrcad is a smallcsi unii oí dispaichablc codc. 1his mcans ihai a singlc
program can pcríorm iwo or morc iask simuliancously.

Vhat is thc Iifc cvcIc of thrcad?

A ihrcad has a liíc cyclc. Crcaiing a ncw 1hrcad insiancc puis ihc ihrcad
inio ihc "ncw ihrcad" siaic. Vhcn ihc .íu·í() mcihod is in·okcd. ihc ihrcad
is ihcn "ali·c" and "runnablc". A ihrcad ai ihis poini will rcpond io ihc
mcihod :.d}:.«() by rciurning iruc.
Noic. A Ja·a program runs uniil ihc only ihrcads lcíi running arc dacmon

Vhcn docs thrcad wiII continuc to rcturn truc to isAIivc()?

1hc ihrcad will coniinuc io rciurn iruc io :.d}:.«() uniil ii is "dcad". no
maiicr whcihcr ii is "runnablc" or "noi runnablc".

Vhat is thc prioritv numbcrs for thrcads faIIs bctwccn?

1hc prioriiy numbcrs íor ihrcads íalls bciwccn ihc rangc oí
1hrcad.MIN_IRIORI1Y and 1hrcad.MAX_IRIORI1Y.

• 1hc dcíauli ihrcad prioriiy is 1hrcad.NORM_IRIORI1Y and
• Ncw ihrcads iakc on ihc prioriiy oí ihc ihrcad ihai spawncd ihcm.

Vhat docs notifvaII() mcthod do?

1hc noí:Jyd}}() mcihod wakcs up all ihrcads waiiing io rcacquirc ihc
moniior íor ihc objcci

(FAQ) For Core & Advance Java
Vhat arc thc rcasons for a thrcad to bc "aIivc" and "not runnabIc"?

• 1hc ihrcad is noi ihc highcsi prioriiy ihrcad and so is noi ablc io gci
CIU iimc
• 1hc ihrcad has bccn pui io slccp using ihc .}««p() mcihod (oí class
1hrcad. 1hrows 1ní«··upí«(1rc«pí:on)
• 1hc ihrcad has bccn suspcndcd using ihc .u.p«n(() mcihod
• 1hc ihrcad is waiiing on a condiiion bccausc somconc in·okcd uu:í()
íor ihc ihrcad
• 1hc ihrcad has cxpliciily yicldcd conirol by in·oking y:«}(()

Vhat is thc diffcrcncc bctwccn thrcad and proccss?

Iroccss 1hrcad
Rcquircs morc O·crhcads Rcquircs lcss O·crhcads
Iroccss arc hca·ywcighi iasks ihai
rcquircs ihcir own scparaic addrcss
1hrcads arc lighiwcighi iasks ihai
sharcs samc addrcss spacc and
coopcraii·cly sharc samc
hca·ywcighi proccsscs
Inicrproccss communicaiion is
cxpcnsi·c and limiicd
Inicrihrcad communicaiion is
Conicxi swiiching írom onc proccss
io ihc ncxi is cosily.
Conicxi swiiching írom onc ihrcad
io ihc ncxi is low cosi.

How do I spccifv thc pausc timcs in mv programs using thrcads?

Onc can pcríorm a pausc iimcs in program by using onc oí ihc .}««p
mcthod ini ihc ircad class.

Vhat is Thrcad svnchronization?

Thrcad svnchronization pro·idcs ihc iool íor cnsuring ihai diíícrcni
ihrcads iakc propcr iurns whcn using sharcd rcsourccs.

(FAQ) For Core & Advance Java
How docs svnchronizcd kcvword works?

1hc svnchronizcd kcyword marks poriions oí ihc codc as bclonging io a
monitor: a proiccicd poriion oí ihc codc ihai cníorccs a onc-ihrcad-ai-a-
iimc policy

How manv thrcads can I crcatc?

By dcíauli. on a Vin·: machinc. cach ("naii·c") ihrcad gcis onc MB
(:o:+¨:o:+ byics) oí siack spacc rcscr·cd íor ii. 1hc mcmory is noi
aciually allocaicd. Ií ii wcrc. you'd run oui oí mcmory wiih dozcns oí
ihrcads insicad oí hundrcds. 1hc spacc nccds io bc rcscr·cd as a
coniiguous block oí mcmory so ihc siack can grow as nccdcd.

Vhat is thc diffcrcncc bctwccn run and RunnabIc?

run (): run() is ihc cniry poini oí ihc ihrcad i.c. you will dcíinc ihc codc
ihai consiiiuics ihc ncw ihrcad. Ii is imporiani io undcrsiand ihai run ()
can call oihcr mcihods. uscs oihcr classcs. and dcclarcs ·ariablcs. jusi likc
ihc main ihrcad can. 1hc only diíícrcncc is ihai run () csiablishcs ihc cniry
poini íor anoihcr. concurrcni ihrcad oí cxccuiion wiih in your program.
1his ihrcad will cnd whcn run () rciurn.

RunnabIc: RunnabIc inicríacc is ihc casicsi way io crcaic ihc ihrcad. onc
jusi nccd io implcmcni ihis inicríacc. Runnablc absiracis a unii oí
cxccuiablc codc. Onc can consiruci a ihrcad on any objcci ihai implcmcnis
Runnablc. a class nccds only implcmcni a singlc mcihod call run (). which
is dcclarc likc ihis.
Iublic ·oid run ()

Vhat is thc diffcrcncc bctwccn start () and run () mcthod, if no
RunnabIc intcrfacc is uscd, run mcthod is ovcrriddcn insidc thc

1hc purposc oí ihc siari () mcihod is io lci ihc JVM know ihai ihc ihrcad is
rcady io run. So. whcn wc call i.siari (). JVM auiomaiically calls ihc run()
mcihod oí ihc ihrcad i. In ihrcading. run () mcihod oí ihc ihrcad should noi
bc callcd dirccily.
Vhai happcns ií wc call run () dirccily'
(FAQ) For Core & Advance Java
In ihis casc. ihc run mcihod would bc callcd jusi likc any ordinary mcihod
NO1 as ihc run () mcihod in a 1hrcad class. 1hcrc won`i bc any compilc
iimc crrors. bui ii won`i bc cxccuicd likc a ihrcad.

Vhv is thrcad svnchronization important for muItithrcadcd

1hrcad synchronizaiion pro·idcs ihc iool íor cnsuring ihai diíícrcni
ihrcads iakc propcr iurns whcn using sharcd rcsourccs.

Vhat is a monitor?

A monitor is a onc-ihrcad-ai-a-iimc sci oí codc-blocks and mcihods ihai
also pro·idcs a waii-noiiíy scr·icc.

Vhat do I usc thc vicId mcthod for?

You invokc yicld in onc thrcad to givc othcr thrcads morc chanccs to
Any plaiíorm ihai runs morc ihan onc ihrcad pcr proccssor nccds io
ha·c a ihrcad schcdulcr a siraicgy íor dcciding whcn io swiich cxccuiion
írom onc ihrcad io anoihcr. and which oihcr ihrcad io swiich io. A ihrcad
schcdulcr iypically rcgulaics iurn iaking according io ihrcad siaics and
ihrcad prioriiics. 1hc íollowing ihrcad siaics disiinguish among which
ihrcads arc cxccuiing or ha·c ihc poicniial io cxccuic.
running: currcnily cxccuiing
runnabIc: rcady io run. bui noi currcnily cxccuiing
waiting: noi rcady io run. musi íirsi bc rcacii·aicd by a iimcr. a
noiiíicaiion. or a rcsumc mcihod in·ocaiion
dcad: íinishcd cxccuiing. cannoi run anymorc

How arc Obscrvcr and ObscrvabIc uscd?

Objccis ihai Subclass ihc Obscr·ablc class mainiains a lisi oí obscr·crs.
Vhcn an Obscr·ablc objcci is updaicd ii in·okcs ihc updaic () mcihod oí
cach oí iis obscr·crs io noiiíy ihc obscr·crs ihai ii has changcd siaic.
Objccis ihai obscr·c Obscr·ablc objccis implcmcni ihc Obscr·cr inicríacc.
(FAQ) For Core & Advance Java

Vhat rcstrictions arc pIaccd on thc vaIucs of cach casc of a switch

During compilaiion. ihc ·alucs oí cach casc oí a swiich siaicmcni musi
c·aluaic io a ·aluc ihai can bc promoicd io an ini ·aluc.

Vhat is an objcct's Iock and which objccts havc Iocks?

An objcci's lock is a mcchanism ihai is uscd by muliiplc ihrcads io obiain
synchronizcd acccss io ihc objcci. A ihrcad may cxccuic a synchronizcd
mcihod oí an objcci only aíicr ii has acquircd ihc objcci's lock. All objccis
and classcs ha·c locks. A class's lock is acquircd on ihc class's Class objcci.

How docs muItithrcading takc pIacc on a computcr with a singIc

1hc opcraiing sysicm's iask schcdulcr allocaics cxccuiion iimc io muliiplc
iasks. By quickly swiiching bciwccn cxccuiing iasks. ii crcaics ihc
imprcssion ihai iasks cxccuic scqucniially.

Vhat is thc highcst-IcvcI cvcnt cIass of thc cvcnt-dcIcgation modcI?

1hc ja·a.uiil.E·cniObjcci class is ihc highcsi-lc·cl class in ihc c·cni-
dclcgaiion class hicrarchy

Vhat is thc purposc of thc Runtimc cIass?

1hc purposc oí ihc Runiimc class is io pro·idc acccss io ihc Ja·a runiimc

For which statcmcnts docs it makc scnsc to usc a IabcI?

1hc only siaicmcnis íor which ii makcs scnsc io usc a labcl arc ihosc
siaicmcnis ihai can cnclosc a brcak or coniinuc siaicmcni.

(FAQ) For Core & Advance Java

Vhat is thc purposc of a statcmcnt bIock?

A siaicmcni block is uscd io organizc a scqucncc oí siaicmcnis as a singlc
siaicmcni group.

Can an anonvmous cIass bc dccIarcd as impIcmcnting an intcrfacc and
c×tcnding a cIass?

An anonymous class may implcmcni an inicríacc or cxicnd a Supcrclass.
bui may noi bc dcclarcd io do boih.

Vhv arc thc mcthods of thc Math cIass static?

Mcihods oí Maih class arc siaiic so ihcy can bc in·okcd as ií ihcy arc a
maihcmaiical codc library.

Vhat is thc diffcrcncc bctwccn vicIding and sIccping?

Vhcn a iask in·okcs iis vicId () mcihod. ii rciurns io ihc rcady siaic.
Vhcn a iask in·okcs iis sIccp () mcihod. ii rciurns io ihc waiiing siaic.

How docs !ava handIc intcgcr ovcrfIows and undcrfIows?

Ii uscs ihosc low ordcr byics oí ihc rcsuli ihai can íii inio ihc sizc oí ihc
iypc allowcd by ihc opcraiion.

Vhat cIass of c×ccptions is gcncratcd bv thc !ava run-timc svstcm?

1hc Ja·a runiimc sysicm gcncraics RuniimcExccpiion and Error

Vhat happcns whcn vou invokc a thrcad's intcrrupt mcthod whiIc it is
sIccping or waiting?

(FAQ) For Core & Advance Java
Vhcn a iask's inicrrupi () mcihod is cxccuicd. ihc iask cnicrs ihc rcady
siaic. 1hc ncxi iimc ihc iask cnicrs ihc running siaic an
InicrrupicdExccpiion is ihrown.

Vhat arc ordcr of prcccdcncc and associativcIv, and how arc thcv

Ordcr oí prcccdcncc dcicrmincs ihc ordcr in which opcraiors arc c·aluaicd
in cxprcssions. Associaii·cly dcicrmincs whcihcr an cxprcssion is
c·aluaicd lcíi-io-righi or righi-io-lcíi

Vhat is a compiIation unit?

A compilaiion unii is a Ja·a sourcc codc íilc.

Vhcn a thrcad bIocks on I/O, what statc docs it cntcr?

A ihrcad cnicrs ihc waiiing siaic whcn ii blocks on I/O.

Vhat is thc diffcrcncc bctwccn thc Rcadcr/Vritcr cIass hicrarchv
and thc InputStrcam/OutputStrcam cIass hicrarchv?

1hc Rcadcr/Vriicr class hicrarchy is characicr-oricnicd. and ihc
InpuiSircam/OuipuiSircam class hicrarchy is byic-oricnicd.

Vhat statc docs a thrcad cntcr whcn it tcrminatcs its proccssing?

Vhcn a ihrcad icrminaics ii`s proccssing. ii cnicrs ihc dcad siaic.

How do I crcatc and usc sockcts in !AVA?

Crcaic Sockct insianccs on ihc clicni sidc by spcciíying a hosi and pori
numbcr io conncci io. and crcaic S«·.«·Soc}«í insiancc on ihc scr·cr sidc by
spcciíying a pori numbcr ai which io waii íor ncw conncciions.

(FAQ) For Core & Advance Java
How do I crcatc a nonbIocking scrvcr sockct in !AVA?

You can`t c×actIv but vou can achicvc thc samc affcct bv using a
scparatc thrcad to run S«·.«·Soc}«í'. ucc«pí mcthod.
A Scr·crSockci insiancc has only onc way io waii íor incoming
conncciions. by in·oking ihc Scr·crSockci acccpi mcihod. 1hc acccpi
mcihod is dcíincd io bc a blocking call. which mcans ihai ihc ihrcad
in·oking ii will block íurihcr cxccuiion uniil ihc conncciion is madc. Ií you
wani io a·oid blocking somc ccniral ihrcad in your scr·cr codc. you should
ha·c a scparaic ihrcad run ucc«pí mcihod.

Vhat is thc advantagc of thc cvcnt-dcIcgation modcI ovcr thc carIicr
cvcnt-inhcritancc modcI?

1hc c·cni-dclcgaiion modcl has iwo ad·aniagcs o·cr ihc c·cni-inhcriiancc
modcl. Iirsi. ii cnablcs c·cni handling io bc handlcd by objccis oihcr ihan
ihc oncs ihai gcncraic ihc c·cnis (or ihcir coniaincrs). 1his allows a clcan
scparaiion bciwccn a componcni's dcsign and iis usc. 1hc oihcr ad·aniagc
oí ihc c·cni-dclcgaiion modcl is ihai ii pcríorms much bciicr in
applicaiions whcrc many c·cnis arc gcncraicd. 1his pcríormancc
impro·cmcni is duc io ihc íaci ihai ihc c·cni-dclcgaiion modcl docs noi
ha·c io rcpcaicdly proccss unhandlcd c·cnis. as is ihc casc oí ihc c·cni-
inhcriiancc modcl.

Vhat is thc RcsourccBundIc cIass?

1hc RcsourccBundlc class is uscd io siorc localc-spcciíic rcsourccs ihai can
bc loadcd by a program io iailor ihc program's appcarancc io ihc pariicular
localc in which ii is bcing run.

Vhat is thc instanccof kcvword?

1hc instanccof kcyword is a iwo-argumcni opcraior ihai icsis whcihcr ihc
run-iimc iypc oí iis íirsi argumcni is assignmcni compaiiblc wiih iis
sccond argumcni.

(FAQ) For Core & Advance Java
Vhat arc thc IcgaI opcrands of thc instanccof opcrator?

1hc lcíi opcrand is an objcci rcícrcncc or null ·aluc and ihc righi opcrand
is a class. inicríacc. or array iypc.

Vhat modificrs can bc uscd with a IocaI inncr cIass?

A local inncr class may bc íinal or absiraci.

Can a nativc mcthod havc a bodv, or cvcn a sct of braccs?

A nuí:.« mcihod docs noi ha·c a body. or c·cn a sci oí braccs. c.g. public
naii·c ·oid mcihod().

A nativc mcthod bc abstract and throw an c×ccptions?

A nuí:.c mcihod cannoi bc absiraci.
A nuí:.« mcihod can ihrow cxccpiions.

Vhat docs "dcprccatcd" mcan? I got this in a compiIcr crror

"Dcprccaicd" mcans you arc using an oldcr AII. which Sun has rcplaccd
wiih a ncwcr onc (usually io íollow morc consisicni naming con·cniions).
Dcprccaicd mcihods arc noi rccommcndcd íor usc. 1hcy arc supporicd in
ihc shori icrm. bui your codc should bc updaicd wiih ihc ncw. 1o updaic
your codc. compilc your old codc using ja·ac's "-dcprccaiion" opiion io lisi
dcprccaicd mcihods. ihcn íind ihc
rcplaccmcni mcihods in ihc currcni H1ML AII documcniaiion íor ihc old
dcprccaicd mcihods.
As an cxamplc oí a dcprccaicd AII. Componcni.sizc () was rcplaccd by
Componcni.gciSizc ().

Scc also
Dcprccaicd Mcihods
Dcprccaicd mcihods in ihc :.: AV1
(FAQ) For Core & Advance Java

How can I obtain a pcrformancc profiIc of mv !AVA program?

In !DK (i.c.e and i.i) vou can usc thc !ava intcrprctcr`s ÷p·oJ options;
this crcatcs a java.prof fiIc with information about which mcthods caII
which, and how much timc is spcnt c×ccuting a givcn mcthod.

1hc JDK implcmcniaiions oí JVM supporis simplc pcríormancc
moniioring oí Ja·a mcihods. Ií you spcciíy ihc ÷p·oJ opiions whcn running
ihc ja·a commands (cg. Ja·a ÷proí MyClass). ihc ·iriual machinc cxccuics
your codc and produccs a íilc namc ja·a.proí whcn ii cxiis. (Siaring wiih
ihc JDK :.: you can also spcciíy an alicrnaii·c namc íor ihc proíilc íilc
using ÷prof:<fiIc>, c.g. java ÷prof:MvCIass.prof MvCIass) ja·a.proí is a
plian icxi íilc ihai you can inspcci wiih ihc icxi cdiior. or proccss íurihcr
wiih scparaic analysis program.
1hc ja·a.proí íilc primarily iraccs mcihod cxccuiion. Ii rcporis on which
mcihods in·okcd wiih oihcr mcihods. how many iimcs. and how long cach
mcihod spcni cxccuiing. 1his iníormaiion is organizcd by pairs oí mcihods.
ihc callcc (ihc mcihod whosc cxccuiion is mcasurcd) and ihc callcr (mcihod
in·oking ihc callcc). Ior cach callcc-callcr pair. ja·a.proí pro·idcs a linc
wiih íour íiclds. as shown in iablc bcllow.

Count Thc numbcr of timcs this caIIcr invokcd this caIIcc
CaIIcc Thc mcthod whosc c×ccution timc is rcportcd.
CaIIcr Thc mcthods that invokc caIIcc
Timc Thc totaI timc (in miIIiscconds) that thc caIIcc spcnt
c×ccuting whcn invokcd bv this caIIcr.

How io usc ihis iníormaiion in ja·a.proí is bcsi sccn in ihc cxamplc bcllow.
Considcr ihc íollowing sourcc íilc. Ioo.ja·a. as a siariing poini.

class Ioo
·oid m:()

·oid m:()
(FAQ) For Core & Advance Java

·oid m·()


public siaiic ·oid main (Siring args¯¯)
Ioo íoo · ncw Ioo ().
íor (ini i·o.i<:ooooo . ++i )
Aíicr compiling Ioo.ja·a . run ja·a ÷proí Ioo and ihcn inspcci ihc
rcsuliing ja·a.proí íilc. Onc pass on a :+o Mhz Sun UliraSparc (using JDK
:.:.:) producing ihc íollowing daia.

Couni callcc callcr iimc
:ooooo Ioo.m·()V Ioo.m:()V ::s
:ooooo Ioo.m:()V Ioo.m:()V +¬o
:ooooo Ioo.m·()V Ioo.m:()V ::¬
:ooooo Ioo:()V Ioo.main(¯Lja·a/lang/Siring.)V :·:
:ooooo Ioo·()V Ioo.main(¯Lja·a/lang/Siring.)V ::o
:ooooo Ioo:()V Ioo.main(¯Lja·a/lang/Siring.)V ::+:
. . . . . . . . .
. . . . . . . . .

Irom ihcsc lincs. you can cxiraci iníormaiion íor spcciíic callcc-callcr pairs.
or írom combinaiions oí such pairs. 1hc m· mcihod. íor insiancc. was
in·okcd a ioial oí +ooooo iimcs. :ooooo iimcs by m:. :ooooo iimcs by m:.
and :ooooo iimcs by main. As anoihcr cxamplc. ihc m: mcihod spcni a
ioial :·: milliscconds in :ooooo in call by m:.

(FAQ) For Core & Advance Java
Notc: Irom JDK :.o.: onwards ii aiicmpis io íormai ihc iníormaiion in
ja·a.proí using aligncd columns. iruncaiing mcihod namcs ií ncccssary.
1his madc ihc daia somcwhai casicr io ·icw. bui ai ihc cxpcnsc oí
somcihing rcmo·ing crucial paris oí ihc mcihod namcs. 1hc JDK :.:
simply prcscnis ihc íull iníormaiion. and lca·cs any íormaiiing work io bc
handlcd scparaicly.

Using thc RcfIcction API, a program can onc dctcrminc a cIass`
acccssibIc ficIds, mcthods and constructors at runtimc?

Ycs. Using ihc Rcílcciion AII. a program can dcicrminc a class` acccssiblc
íiclds. mcihods and consiruciors ai runiimc.

(FAQ) For Core & Advance Java
!/\/P£/w !/\/P£/w !/\/P£/w !/\/P£/w
Vhat is !avaBcan?

Ja·aBcan is a poriablc. plaiíorm-indcpcndcni componcni modcl wriiicn in
ihc Ja·a programming languagc. dc·clopcd in collaboraiion wiih indusiry
lcadcrs. Ii cnablcs dc·clopcrs io wriic rcusablc componcnis oncc and run
ihcm anywhcrc -- bcncíiiing írom ihc plaiíorm-indcpcndcni powcr oí Ja·a
icchnology. Ja·aBcan acis as a Bridgc bciwccn propriciary componcni
modcls and pro·idcs a scamlcss and powcríul mcans íor dc·clopcrs io build
componcnis ihai run in Acii·cX coniaincr applicaiions.

Vhat is a Bcan? Vhv isn't a Bcan an AppIct?
Ja·aBcans componcnis. or Bcans. arc rcusablc soíiwarc componcnis ihai
can bc manipulaicd ·isually in a buildcr iool. Bcans can bc combincd io
crcaic iradiiional applicaiions. or ihcir smallcr wcb-oricnicd brcihrcn.
applcis. In addiiion. applcis can bc dcsigncd io work as rcusablc Bcans.
Indi·idual Bcans will íunciion quiic diíícrcnily. bui iypical uniíying
ícaiurcs ihai disiinguish a Bcan arc.
• Introspcction: Enablcs a buildcr iool io analyzc how a Bcan works
• Customization: Enablcs a dc·clopcr io usc an app buildcr iool io
cusiomizc ihc appcarancc and bcha·ior oí a Bcan
• Evcnts: Enablcs Bcans io communicaic and conncci iogcihcr
• Propcrtics: Enablc dc·clopcrs io cusiomizc and program wiih Bcans
• Pcrsistcncc: Enablcs dc·clopcrs io cusiomizc Bcans in an app
buildcr. and ihcn rciric·c ihosc Bcans. wiih cusiomizcd ícaiurcs
iniaci. íor íuiurc usc.
Notc: Gcncrally ihc bcan should siorc ihc siaic oí any cxposcd propcriy.
Sclccicd inicrnal ·ariablc may also bc sa·cd. Bcan should noi. howc·cr.
siorc poinicr io cxicrnal bcan.
Vhat is ScriaIization?
ScriaIization. allows you io sa·c and rcsiorc ihc siaic oí an objcci.
(FAQ) For Core & Advance Java
ScriaIization is ihc proccss oí wriiing ihc siaic oí an objcci io a byic
sircam. 1his is uscíul whcn you wani io sa·c ihc siaic oí your program io a
pcrsisicni sioragc arca. such as íilc. Ai a laicr iimc. you may rcsiorc ihc
siaic oí an objcci by using ihc proccss oí dcscrializaion.
Notc: Only objcci ihai implcmcnis ihc Scrializablc inicríacc can bc sa·cd
and rcsiorc by scrializaiion íaciliiics. Thc ScriaIizabIc intcrfacc dcfincs
no mcmbcrs. Ii simply uscd io indicaic ihai ihc class mighi bc scrializcd.
Vhat is E×tcrnaIizabIc?
1hc Ja·a íaciliiaics íor scrializaiion and dcscrializaion ha·c bccn dcsigncd
so ihai much oí ihc work io sa·c and rcsiorc ihc siaic oí an objcci occurs
auiomaiically. Howc·cr. ihcrc arc cascs in which programmcr may nccd io
ha·c conirol o·cr ihcsc proccss. Ior Examplc. ii may bc dcsirablc io usc
comprcssion or cncrypiion icchniquc. 1hc E×tcrnaIizabIc inicríacc is
dcsign íor ihis siiuaiion.
1hc Exicrnalizablc inicríacc dcíincs ihcsc : mcihods.
• ·oid rcadExicrnal(ObjcciInpui :nSí·«um) ihrows IOExccpiion.
• ·oid wriicExicrnal(ObjcciOuipui ouíSí·«um) ihrows IOExccpiion
In ihcsc mcihods. :nSí·«um is ihc byic sircam íor which ihc objcci is io bc
rcad. and ouíSí·«um is ihc byc sircam io which ihcsc objccis is io bc
Vhv arc componcnt architccturcs uscfuI?
Dc·clopcrs arc iurning io crcaiing componcnis raihcr ihan monoliihic
applicaiions io írcc ihcmscl·cs írom slow. cxpcnsi·c applicaiion
dc·clopmcni. and io build up a poriablc and rcusablc codc basc. 1his
cnablcs dc·clopcrs io quickly aiiack ncw markci opporiuniiics. ncw joini
dc·clopmcni opporiuniiics. and ncw ways io scll smallcr packagcs oí

(FAQ) For Core & Advance Java
Is !AVABEAN compIctc componcnt architccturc?
Ja·aBcans is a complcic componcni modcl. Ii supporis ihc siandard
componcni archiicciurc ícaiurcs oí propcriics. c·cnis. mcihods. and
pcrsisicncc. In addiiion. Ja·aBcans pro·idcs suppori íor inirospcciion (io
allow auiomaiic analysis oí a Ja·aBcans componcni) and cusiomizaiion (io
makc ii casy io coníigurc a Ja·aBcans componcni).

Vhv a componcnt architccturc for thc !ava pIatform?
Ja·aBcans brings ihc cxiraordinary powcr oí ihc Ja·a plaiíorm io
componcni dc·clopmcni. oíícring ihc idcal cn·ironmcni íor a dc·clopcr
who wanis io cxicnd ihc conccpi oí rcusablc componcni dc·clopmcni
bcyond onc plaiíorm and onc archiicciurc io cmbracc c·cry plaiíorm and
c·cry archiicciurc in ihc indusiry.
Arc thcrc !avaBcans componcnts avaiIabIc that I can buv todav?
Ycs. A largc numbcr oí companics. boih largc and small. ha·c announccd
ihcir plans io dcli·cr Ja·aBcans-bascd producis including Corcl.
EnicrpriscSoíi. Gcmsionc. IBM. JScapc. KeA Soíiwarc. KL Group. Loius
Dc·clopmcni. No·cll. IroioVicw Dc·clopmcni. Roguc Va·c. and Siingray
Soíiwarc among many oihcrs. Coniaci ihcsc companics íor iníormaiion on
produci a·ailabiliiy. Many morc companics ha·c adopicd ihc Ja·aBcans
componcni modcl. iakc a look ai ihc Ja·aBcans Dircciory.
Vhat is thc rcIationship bctwccn Sun's !FC and !avaBcans?
1hc JIC (Ja·a Ioundaiion Classcs) is bascd upon ihc AV1 (Absiraci
Vindowing 1oolkii). which has bccn pari oí ihc Ja·a plaiíorm írom ihc
bcginning. JIC cííccii·cly adds a richcr sci oí ·isual clcmcnis íor building
Ja·aBcans componcnis and applicaiions. Scc ihc JIC wcb siic íor morc
Vhat arc thc sccuritv impIications for downIoading Bcans ovcr thc
Ja·aBcans docs noi add any sccuriiy ícaiurcs io ihc Ja·a plaiíorm. Raihcr.
Ja·aBcans componcnis ha·c íull acccss io ihc broad rangc oí sccuriiy
(FAQ) For Core & Advance Java
ícaiurcs ihai arc pari oí ihc Ja·a plaiíorm. Ja·aBcans componcnis can bc
uscd io build a rangc oí diíícrcni kinds oí soluiions írom íull-ílcdgcd Ja·a
dcskiop applicaiions io wcb-bascd Applcis.
Docs thc icc% Purc !ava program cnsurc compatibiIitv and
intcropcrabiIitv bctwccn Bcans?
1hc :oo° Iurc Ja·a Iniiiaii·c is a program dcsigncd spcciíically íor
dc·clopcrs oí Ja·a soíiwarc. Sun will pro·idc icsiing and markciing
suppori spcciíically íor Ja·aBcans componcnis in ihc íirsi halí oí :¬¬s.
ViII thc BcanBo× cvoIvc to bccomc a "compIctc" dcvcIopmcnt
1hc BcanBox. pro·idcd as pari oí ihc BDK. is pro·idcd as a icsi coniaincr
io allow Ja·aBcans dc·clopcrs io icsi and c·aluaic Bcan bcha·ior. 1hough
Sun will rclcasc addiiional ·crsions oí ihc BcanBox as a pari oí íuiurc BDK.
wc inicnd io kccp ii iruc io iis original mission as ouilincd abo·c.
Vhat arc thc Propcrtv in !avaBcans and how mav tvpcs of Propcrtics
arc thcrc in !avaBcans?
A propcriy is a singlc public aiiribuic. Iropcriics can bc rcad/wriic. rcad-
only or wriic-only. 1hcrc arc sc·cral iypcs oí propcriics.
• SimpIc Propcrtv: A S:mp}« P·op«·íy rcprcscnis a singlc ·aluc and
can bc dcíincd wiih a pair oí gci/sci mcihods
• Indc×cd Propcrtv: An 1n(«r P·op«·íy rcprcscnis an array oí
·alucs. A propcriy clcmcni gci/sci mcihod iakcs an Inicgcr indcx
• Bound Propcrtv: A Boun( p·op«·íy noiiíics oihcr objccis whcn
ii`s ·aluc changcs. Ii iimc ii ·aluc changcs. ihc propcriy íircs
IropcriyChangc c·cni. which coniains propcriy namc. old and
ncw ·aluc. and
• Constraincd Propcrtv: An objcci wiih a Con.í·u:ní P·op«·í:«.
allows ihc oihcr objccis io ·cio a consiraincd propcriy ·aluc
changc. Consiraincd Iropcriy lisicncrs can Vcto a changc by
ihrowing a IropcriyVcioExccpiion. VcotabIcChangcListcncr:
(FAQ) For Core & Advance Java
Ii is an Inicríacc íor rccci·ing changcd bcan consiraincd propcriy
Notc: In gcncral Consiraini Iropcriy should also bc bound
Vhat is BcanInfo Intcrfacc?
Each Bcan class may ha·c a BcanInío class. which cusiomizcs how ihc Bcan
is io appcar in a buildcr. 1hc BcanInío can dcíinc propcriics. mcihods.
c·cnis. wiih display namcs and shori hclp.
How do u prcvcnt scIcct ficId from bcing pcrsistcncc (Savcd) whcn
impIcmcnts ScriaIizabIc intcrfacc?
1hc íiclds in any insiancc oí a Bcan. which implcmcnis S«·:u}::u|}«. will
auiomaiically bc sa·cd. You nccd do noihing clsc. You can prc·cni sclccicd
íiclds írom bcing sa·cd by marking ihcm iransicni or siaiic. iransicni and
siaiic ·ariablcs arc noi sa·cd.
Vhat is thc Scopc of thc Bcan with rcspcct to pagc, rcqucst, scssion
and appIication?
Pagc: Bcan wiih pagc scopc arc acccssiblc only wiihin ihc pagc whcrc ihcy
whcrc crcaicd. Rcícrcncc io an objcci wiih ihc pagc scopc will bc rclcascd
whcn ihc rcsponsc is scni back io ihc clicni or ihc rcqucsi is íorwardcd io
anoihcr rcsourcc.
Rcqucst: Bcans wiih rcqucsi scopc arc acccssiblc only wiihin pagcs
proccssing ihc samc rcqucsi ihai ihc objcci was crcaicd in. Rcícrcncc io ihc
objcci will bc rclcascd aíicr ihc rcqucsi proccsscd complcicly.
Scssion: Bcans wiih scssion scopc arc acccssiblc only wiihin pagcs
proccssing rcqucsi ihai arc in ihc samc scssion as ihc onc in which ihc bcan
was crcaicd.
Notc: Ii is illcgal io dcíinc an objcci wiih scssion scopc írom wiihin a pagc
dirccii·c has an aiiribuic scssion·íalsc¨
AppIication: Bcans wiih applicaiion scopc arc acccssiblc wiih in ihc pagcs
proccssing rcqucsis ihai arc in ihc samc applicaiion spacc as ihc pagc in
(FAQ) For Core & Advance Java
which ihcy arc crcaicd. Rcícrcnccs io ihc objcci will bc rclcascd whcn ihc
runiimc cn·ironmcni rcclaims ihc ScrvIctContc×t.

(FAQ) For Core & Advance Java
´£P\L£j ´£P\L£j ´£P\L£j ´£P\L£j

Vhat is thc diffcrcncc bctwccn URI and URL?

Iollowing arc ihc diíícrcncc bciwccn URI and URL
• A URI is a siring uscd io idcniiíy a Inicrnci rcsourcc.
• A URL is such siring in a íormai oí a spcciíic proiocol likc HTTP-
onc wiih a schcmc pari (c.g. hiip). domain oí ihc scr·cr. a paih. and
possiblc a qucry siring paramcicr.
• Iinally. a URI path is ihc pari oí ihc URL ihai idcniiíics ihc
rcsourcc wiihin a spcciíic scr·cr. i.c. jusi a paih pari. URL paih
mighi ha·c bccn ihc bciicr namc. bui URL paih is ihc icrm uscd in
ihc Scr·lci Spcciíicaiion.

Vhat is thc diffcrcncc bctwccn URL instancc and URL Conncction

A URL insiancc rcprcscnis ihc locaiion oí a rcsourcc. and
URLConncction insiancc rcprcscnis a link íor acccssing or communicaiion
wiih rcsourcc ai ihai locaiion.

Vhat arc ScrvIcts and how can thcv hcIp in dcvcIoping Vcb
"S«·.}«í. arc modulcs ihai cxicnd rcqucsi/rcsponsc-oricnicd scr·crs. such
as Ja·a-cnablcd wcb scr·crs. Ior cxamplc. a Scr·lci mighi bc rcsponsiblc
íor iaking daia in an H1ML ordcr-cniry íorm and applying ihc busincss
logic uscd io updaic a company's ordcr daiabasc. S«·.}«í. arc io scr·crs
whai applcis arc io browscrs. Unlikc applcis. howc·cr. S«·.}«í. ha·c no
graphical uscr inicríacc. S«·.}«í. can bc cmbcddcd in many diíícrcni scr·crs
bccausc ihc Scr·lci AII. which you usc io wriic S«·.}«í, assumcs noihing
aboui ihc scr·cr's cn·ironmcni or proiocol. S«·.}«í. ha·c bccomc mosi
widcly uscd wiihin H11I scr·crs. many wcb scr·crs suppori ihc Scr·lci

(FAQ) For Core & Advance Java
Vhat is thc Advantagc of using ScrvIcts ovcr CGI programming?
Scr·lcis arc only insianiiaicd whcn ihc clicni íirsi acccsscs ihc program.
1hai is. ihc íirsi iimc any clicni hiis ihc URL ihai is uscd io acccss ihc
Scr·lci. ihc Scr·lci cnginc insianiiaics ihai objcci. All subscqucni acccsscs
arc donc io ihai insiancc. 1his kccps ihc rcsponsc iimc oí Scr·lcis lowcr
ihan ihai oí CGI programs. which musi bc run oncc pcr hii. Also. bccausc a
Scr·lci is insianiiaicd only oncc. all acccsscs arc pui ihrough ihai onc
objcci. 1his hclps in mainiaining objccis likc inicrnal Conncciion Iooling
or uscr scssion iracking and lois oí oihcr ícaiurcs.
Hcrc arc a ícw morc oí ihc many applicaiions íor Scr·lcis.
:. Allowing collaboraiion bciwccn pcoplc. A Scr·lci can handlc muliiplc
rcqucsis concurrcnily. and can synchronizc rcqucsis. 1his allows Scr·lcis
io suppori sysicms such as on-linc conícrcncing.
:. Iorwarding rcqucsis. Scr·lcis can íorward rcqucsis io oihcr scr·crs and
Scr·lcis. 1hus Scr·lcis can bc uscd io balancc load among sc·cral scr·crs
ihai mirror ihc samc conicni. and io pariiiion a singlc logical scr·icc o·cr
sc·cral scr·crs. according io iask iypc or organizaiional boundarics.

Vhat is thc !akarta Projcct?
1hc goal oí ihc Ju}u·íu P·o]«cí is io pro·idc commcrcial-qualiiy scr·cr
soluiions bascd on ihc Ja·a Ilaiíorm ihai is dc·clopcd in an opcn and
coopcraii·c íashion. 1hc ílagship produci. Tomcuí. is a world-class
implcmcniaiion oí ihc Ja·a Scr·lci :.: and Ju.uS«·.«· Puv«.
:.:Spcciíicaiions. 1his implcmcniaiion will bc uscd in ihc Apachc Vcb
Scr·cr as wcll as in oihcr wcb scr·crs and dc·clopmcni iools.

Vhcrc can I find a good tutoriaI and morc dctaiIs on ScrvIcts?
1hcsc URL's arc a good siariing poini ií you wani io know morc aboui
(FAQ) For Core & Advance Java
Scr·lci Links
Dc·cloping Applicaiions using Scr·lcis (1uiorial)

How do I gct authcntication with mvScrvIct?
Many popular Scr·lci cngincs oíícr Scr·lci auihcniicaiion and ihc AII has
a call HiipScr·lciRcqucsi.gciUscrNamc() which is rcsponsiblc íor
rciurning ihc uscrnamc oí an auihcniicaicd uscr. Ior morc dciails plcasc
·isii ihis pagc

Vhat is Config?

Namc uscd io siorc Scr·lciConíig in IagcConicxi namc iablc

Vhat is ScrvIctConfig?

pu|}:c :ní«·Juc« S«·.}«íConJ:v

ScrvIctConfig is an Inicríacc. which allows Scr·lcis io gci iniiializaiion
1his inicríacc dcíincs an objcci ihai a Scr·lci cnginc gcncraics io coníigurc
a Scr·lci and allow ihc Scr·lci io obiain a rcícrcncc io iis Scr·lciConicxi
inicríacc. Each Scr·lciConíig objcci ihc Scr·lci rccci·cs is uniquc io ihai
1hc Mcihods oí Scr·lciConíig arc.

• gctInitParamctcr
pu|}:c Sí·:nv v«í1n:íPu·um«í«·(Sí·:nv num«):
1his mcihod rciurns a Siring coniaining ihc ·aluc oí ihc Scr·lci`s namcd
iniiializaiion paramcicr. or null ií ihis paramcicr docs noi cxisi.
• gctInitParamctcrNamcs
pu|}:c 1num«·uí:on v«í1n:íPu·um«í«·Aum«.():
1his mcihod rciurns an cnumcraiion oí Siring objccis coniaining ihc
namcs oí ihc iniiializaiion paramcicrs íor ihc calling Scr·lci. Ií ihc calling
Scr·lci has no iniiializaiion paramcicrs. gciIniiIaramcicrNamcs rciurns an
cmpiy cnumcraiion.
(FAQ) For Core & Advance Java
• gctScrvIctContc×t
pu|}:c S«·.}«íConí«rí v«íS«·.}«íConí«rí():
Rciurns ihc Scr·lciConicxi objcci íor ihis Scr·lci.

Vhat is thc conccpt of Contc×t? E.g. InitiaIContc×t, ScssionContc×t,
PagcContc×t and ScrvIctContc×t.

Contc×t (in gcncral) rcprcscnis an cn·ironmcni. or siaic wiihin which
somcihing cxisis. 1hc conicxi objcci allows ihc cncloscd objcci io
communicaic wiih iis cn·ironmcni.

Ior cxamplc. whcn you run a Scr·lci. ii has a mcihod call as ihc
Scr·lciConicxi. 1his conicxi allows ihc Scr·lci io kccp irack oí iis
cn·ironmcni io which ii bclongs.

Vhat is PagcContc×t?

A PagcContc×t insiancc pro·idcs acccss io all ihc namcspaccs associaicd
wiih a JSI pagc. pro·idcs acccss io sc·cral pagc aiiribuics. as wcll as a
laycr abo·c ihc implcmcniaiion dciails.
1hc IagcConicxi pro·idcs a numbcr oí íaciliiics io ihc pagc/componcni
auihor and pagc implcmcnicr. including.
• A singlc AII io managc ihc ·arious scopcd namcspaccs
• A numbcr oí con·cnicncc AII's io acccss ·arious public objccis
• A mcchanism io obiain ihc JspVriicr íor ouipui
• A mcchanism io managc scssion usagc by ihc pagc
• A mcchanism io cxposc pagc dirccii·c aiiribuics io ihc scripiing
• Mcchanisms io íorward or includc ihc currcni rcqucsi io oihcr
acii·c componcnis in ihc applicaiion
• A mcchanism io handlc crrorpagc cxccpiion proccssing

Vhat is ScrvIctContc×t?

pu|}:c :ní«·Juc« S«·.}«íConí«rí

(FAQ) For Core & Advance Java
ScrvIctContc×t is a caicgorizaiion oí Scr·lcis undcr onc paih and possibly
onc applicaiion. Ií onc Vcbscr·cr is uscd íor morc ihan onc applicaiions
ihai arc kcpi in diíícrcni locaiions Scr·lci conicxi has io bc dcíincd in wcb
scr·cr coníiguraiion and ihcn wcb scr·cr picks up ihc corrcci paih and
applicaiion íor ihai pariicular applicaiion or Scr·lci.

1hc Mcihods oí Scr·lciConicxi arc.
• gctAttributc
pu|}:c O|]«cí v«ídíí·:|uí«(Sí·:nv num«):
Rciurns an objcci ihai is known io ihc Scr·lci conicxi objcci by a gi·cn
namc. or null ií ihcrc is no such objcci associaicd wiih ihc namc. 1his
mcihod allows acccss io addiiional iníormaiion aboui ihc Scr·lci cnginc
noi alrcady pro·idcd by oihcr mcihods in ihis inicríacc.
You should usc ihc samc naming con·cniions íor aiiribuics as íor packagc
namcs. Namcs maiching ja·a.¨. ja·ax.¨. and sun.¨ arc rcscr·cd íor
dcíiniiion by ihis spcciíicaiion or ihc rcícrcncc implcmcniaiion.
• gctAttributcNamcs
pu|}:c 1num«·uí:on v«ídíí·:|uí«Aum«.():
Rciurns an cnumcraiion oí ihc aiiribuic namcs prcscni in ihc calling
Scr·lci conicxi objcci.
• gctContc×t
pu|}:c S«·.}«íConí«rí v«íConí«rí(Sí·:nv u·:puín):
Rciurns ihc Scr·lci conicxi objcci ihai coniains Scr·lcis and rcsourccs íor
a pariicular URI paih. or null ií a conicxi cannoi bc pro·idcd íor ihc paih.
1hc íormai oí ihc URI paih is /dir/dir/fiIcnamc.c×t.
Ior sccuriiy. ihc Scr·lci conicxi objcci ihai a sandboxcd or oihcrwisc
rcsiricicd Scr·lci has acccss io should rciurn null io ihis mcihod call.
• gctMajorVcrsion
pu|}:c :ní v«íMu]o·!«·.:on():
Rciurns ihc major ·crsion oí ihc Scr·lci AII ihai ihis Scr·lci cnginc
supporis. All :.: compliani implcmcniaiions musi rciurn ihc inicgcr : io
ihis mcihod.
• gctMinorVcrsion
pu|}:c :ní v«íM:no·!«·.:on():
Rciurns ihc minor ·crsion oí ihc Scr·lci AII ihai ihis Scr·lci cnginc
supporis. All :.: compliani implcmcniaiions musi rciurn ihc inicgcr : io
ihis mcihod.
• gctMimcTvpc
pu|}:c Sí·:nv v«íM:m«Typ«(Sí·:nv J:}«):
(FAQ) For Core & Advance Java
Rciurns ihc MIME iypc oí ihc spcciíicd íilc. or null ií noi known. 1hc
MIME iypc is dcicrmincd according io ihc coníiguraiion oí ihc Scr·lci
• gctRcaIPath
pu|}:c Sí·:nv v«íR«u}Puín(Sí·:nv puín):
Applics alias rulcs io ihc spcciíicd ·iriual paih in URL paih íormai. ihai is.
/dir/dir/fiIcnamc.c×t. Rciurns a Siring rcprcscniing ihc corrcsponding
rcal paih in ihc íormai ihai is appropriaic íor ihc machinc (including ihc
propcr paih scparaiors) ihai ihc Scr·lci cnginc is running on.
Rciurns null ií ihc iranslaiion could noi bc pcríormcd íor any rcason.
• gctRcsourcc
pu|}:c 1R1 v«íR«.ou·c«(Sí·:nv u·:puín):
Rciurns an URL objcci io a rcsourcc known io ihc Scr·lci conicxi objcci
locaicd ai ihc gi·cn URL paih (oí íormai /dir/dir/fiIcnamc.c×t). 1hc
Scr·lci cnginc musi implcmcni whaic·cr URLSircamHandlcrs arc
ncccssary io acccss ihc gi·cn conicxi. Ií ihcrc is no rcsourcc known io a
Scr·lci conicxi íor a pariicular paih. gctRcsourcc rciurns null.
1his mcihod docs noi íill ihc samc purposc as ihc gctRcsourcc mcihod in
ja·a.lang.Class. 1hc mcihod in ja·a.lang.Class looks up rcsourccs bascd on
Class class loadcr. 1his allows ihc scr·cr io makc conicni ·isiblc io any
Scr·lci írom any sourcc wiihoui rcgard io class loadcrs. locaiion. and so on.
• gctRcsourccAsStrcam
pu|}:c 1npuíSí·«um v«íR«.ou·c«d.Sí·«um(Sí·:nv u·:puín):
Rciurns an InputStrcam objcci ihai rcícrs io conicni known io ihc Scr·lci
conicxi objcci ai ihc spcciíicd ihc URL. or null ií ihc scr·lci conicxi objcci
is noi íound. 1hc URL paih is oí ihc íorm /dir/dir/fiIcnamc.c×t.
1his mcihod is a con·cnicni way io obiain an URL objcci írom ihc
gctRcsourcc mcihod and opcn a sircam. Noic ihai mcia-iníormaiion such
as conicni lcngih and conicni iypcs arc losi whcn you usc
• gctRcqucstDispatchcr
pu|}:c R«qu«.íD:.puícn«· v«íR«qu«.íD:.puícn«·(Sí·:nv u·:puín):
Rciurns a RcqucstDispatchcr objcci íor ihc spcciíicd URL ií ihc conicxi
knows oí an acii·c sourcc (such as a Scr·lci. JSI pagc. CGI scripi. cic.) oí
conicni íor ihc pariicular paih. or null oihcrwisc. 1hc íormai oí ihc URL
paih is /dir/dir/fiIcnamc.c×t). 1hc Scr·lci cnginc implcmcnis whaic·cr
íunciionaliiy is nccdcd io wrap ihc iargci paih wiih a rcqucsi dispaichcr
objcci ihai can pcríorm rcqucsi íorwarding and programmaiic scr·cr sidc
• gctScrvcrInfo
(FAQ) For Core & Advance Java
pu|}:c Sí·:nv v«íS«·.«·1nJo():
Rciurns a Siring objcci coniaining ai lcasi ihc namc and ·crsion oí ihc
scr·lci cnginc.
• Iog
pu|}:c .o:( }ov(Sí·:nv m.v):
pu|}:c .o:( }ov(Sí·:nv m.v, Tn·ouu|}« í):
pu|}:c .o:( }ov(1rc«pí:on «rc«pí:on, Sí·:nv m.v): // dcprccaicd
Vriics ihc spcciíicd mcssagc io ihc log íilc íor ihis Scr·lci conicxi objcci.
1hc log wriiicn io is spcciíic io ihc Scr·lci cnginc. bui is usually an c·cni
log. Vhcn ihis mcihod is callcd wiih an cxccpiion. ihc siack iracc should
bc includcd in ihc log.
• sctAttributc
pu|}:c .o:( .«ídíí·:|uí«(Sí·:nv num«, O|]«cí o):
Binds ihc objcci you spcciíy io ihc namc you spcciíy in ihc Scr·lci conicxi
Aiiribuic namcs should íollow ihc samc con·cniion as packagc namcs.
Namcs bcginning wiih ihc prcíixcs ja·a.¨. ja·ax.¨. and sun.¨ arc rcscr·cd
íor dcíiniiion by ihis spcciíicaiion or ihc rcícrcncc implcmcniaiion
• rcmovcAttributc
pu|}:c .o:( ·«mo.«díí·:|uí«(Sí·:nv num«):
Rcmo·cs an aiiribuic írom ihc spcciíicd Scr·lci conicxi objcci.

Vhat is thc scopc of PAGE, REOUEST, SESSION, and

Pagc scopc. (1his is ihc dcíauli) ihc namcd rcícrcncc rcmains a·ailablc in
ihis IagcConicxi uniil ihc rciurn írom ihc currcni Scr·lci.scr·icc()
Rcqucst scopc. 1hc namcd rcícrcncc rcmains a·ailablc írom ihc
Scr·lciRcqucsi associaicd wiih ihc Scr·lci ihai uniil ihc currcni rcqucsi is
Scssion scopc: (Only ·alid ií ihis pagc pariicipaics in a scssion). 1hc
namcd rcícrcncc rcmains a·ailablc írom ihc HiipScssion (ií any) associaicd
wiih ihc Scr·lci uniil ihc HiipScssion is in·alidaicd.
AppIication scopc. Namcd rcícrcncc rcmains a·ailablc in ihc
Scr·lciConicxi uniil ii is rcclaimcd.

(FAQ) For Core & Advance Java
Vhat is a Scssion Objcct?
Scssion Objcct is uscd io mainiain uscr scssion rclaicd iníormaiion on ihc
Scr·cr sidc. You can siorc. rciric·c and rcmo·c iníormaiion írom a Scssion
objcci according io your program logic. A scssion objcci crcaicd íor cach
uscr pcrsisis on ihc scr·cr sidc. ciihcr uniil uscr closcs ihc browscr or uscr
rcmains idlc íor ihc scssion cxpiraiion iimc. which is coníigurc ablc on cach

How to crcatc Scssion objcct and usc it for storing information?
1his codc will gci ihc Scssion conicxi íor ihc currcni uscr and placc ·alucs
oí couní1 and couní. inio My1(«ní:J:«·1 and My1(«ní:J:«·. rcspccii·cly
HiipScssion scssion · rcq.gciScssion(iruc). //Crcaiing a Scssion insiancc
scssion.puiValuc ("MyIdcniiíicr:".couni:). // Sioring Valuc inio scssion
scssion.puiValuc ("MyIdcniiíicr:". couni:).
scssion.gciValuc(MyIdcniiíicr:). // Irinis ·aluc oí Couni
scssion.rcmo·cValuc(MyIdcniiíicr:). // Rcmo·ing Valucírom Scssion

Vhat is thc Ma× amount of information that can bc savcd in a Scssion
As such ihcrc is no limii on ihc amouni oí iníormaiion ihai can bc sa·cd in
a Scssion Objcci. Only ihc RAM a·ailablc on ihc scr·cr machinc is ihc
limiiaiion. 1hc only limii is ihc Scssion ID lcngih (Idcniiíicr). which
should noi cxcccd morc ihan +K. Ií ihc daia io bc siorc is ·cry hugc. ihcn
ii's prcícrrcd io sa·c ii io a icmporary íilc onio hard disk. raihcr ihan
sa·ing ii in scssion. Inicrnally ií ihc amouni oí daia bcing sa·cd in Scssion
cxcccds ihc prcdcíincd limii. mosi oí ihc scr·crs wriic ii io a icmporary
cachc on Hard disk.

(FAQ) For Core & Advance Java
Vhat arc Cookics and how to usc thcm?
A Cookic is a bii oí iníormaiion scni by ihc Vcb scr·cr ihai can laicr bc
rcad back írom ihc browscr. Limiiaiions íor cookics arc. browscrs arc only
rcquircd io acccpi :o cookics pcr siic. ·oo ioial pcr uscr. and ihcy can limii
cach cookic's sizc io +o¬o byics (+K).
Cookic cookic = ncw Cookic("uscrId", "es!c4"); //Crcating ncw
rcsponsc.addCookic (cookic); // scnding cookic to thc browscr
// Printing out aII Cookics
Cookicj¯ cookics = rcqucst.gctCookics();
if (cookics != nuII)
for (int i=c; i<cookics.Icngth; i++)
String namc = cookicsji¯.gctNamc();
String vaIuc = cookicsji¯.gctVaIuc();
Notc: Ií you wani io sa·c spccial characicrs likc "·". or spaccs. in ihc ·aluc
oí ihc cookic. ihcn makc surc you URL cncodc ihc ·aluc oí ihc cookic.
bcíorc crcaiing a cookic. Ilcasc rcícr io IAO on URLEncodc and

How to confirm that uscr's browscr acccptcd thc Cookic?
1hcrc's no dircci AII io dirccily ·criíy ihai uscr's browscr acccpicd ihc
cookic. Bui ihc quick alicrnaii·c would bc. aíicr scnding ihc rcquircd daia
io ihc uscrs browscr. rcdircci ihc rcsponsc io a diíícrcni Scr·lci. which
would iry io rcad back ihc cookic. Ií ihis Scr·lci is ablc io rcad back ihc
cookic. ihcn ii was succcssíully sa·cd. clsc uscr had disablcd ihc opiion io
acccpi cookics.

(FAQ) For Core & Advance Java
How can I dcIctc or sct ma× duration for which Cookic c×ists?
You can sci ihc maximum agc oí a cookic wiih ihc coo}:«..«íMurdv«(:ní
.«con(.) mcihod. Hcrc arc diíícrcni opiions io ihis mcihod.
Zcro mcans io dclcic ihc cookic
A posiii·c ·aluc is ihc maximum numbcr oí scconds ihc cookic will li·c.
bcíorc ii cxpircs
A ncgaii·c ·aluc mcans ihc cookic will noi bc siorcd bcyond ihis browscr
scssion (dclcicd on browscr closc)
Hcrc is a samplc codc io dclcic cookic.
privatc void dcIctcCookic(String cookicNamc)
Cookicj¯ cookics =rcqucst.gctCookics();
if (cookics != nuII)
for (int i=c; i<cookics.Icngth; i++)
if (cookicsji¯.gctNamc().cquaIs(cookicNamc));

Vhat arc thc ScrvIcts EquivaIcnts of CGI for commonIv rcqucstcd

SERVER_NAME rcqucsi.gciScr·crNamc().
SERVER_IRO1OCOL rcqucsi.gciIroiocol().
SERVER_IOR1 rcqucsi.gciScr·crIori()
REOUES1_ME1HOD rcqucsi.gciMcihod()
IA1H_INIO rcqucsi.gciIaihInío()
(FAQ) For Core & Advance Java
IA1H_1RANSLA1ED rcqucsi.gciIaih1ranslaicd()
SCRII1_NAME rcqucsi.gciScr·lciIaih()
DOCUMEN1_ROO1 rcqucsi.gciRcalIaih("/")
OUERY_S1RING rcqucsi.gciOucrySiring()
REMO1E_HOS1 rcqucsi.gciRcmoicHosi()
REMO1E_ADDR rcqucsi.gciRcmoicAddr()
AU1H_1YIE rcqucsi.gciAuih1ypc()
REMO1E_USER rcqucsi.gciRcmoicUscr()
CON1EN1_1YIE rcqucsi.gciConicni1ypc()
CON1EN1_LENG1H rcqucsi.gciConicniLcngih()
H11I_ACCEI1 rcqucsi.gciHcadcr("Acccpi")
H11I_USER_AGEN1 rcqucsi.gciHcadcr("Uscr-Agcni")
H11I_REIERER rcqucsi.gciHcadcr("Rcícrcr")
Ior morc dciails on ihcsc ·ariablcs plcasc rcícr io ihis URL

How can gct thc cntirc URL of thc currcnt ScrvIct?

1hc íollowing codc íragmcni will rciurn ihc cniirc URL oí ihc currcni

String currcntFiIc = rcqucst.gctRcqucstURI();
if (rcqucst.gctOucrvString() != nuII)
currcntFiIc = currcntFiIc + '?' + rcqucst.gctOucrvString();

URL currcntURL = ncw URL(rcqucst.gctSchcmc(),
rcqucst.gctScrvcrNamc(), rcqucst.gctScrvcrPort(),


(FAQ) For Core & Advance Java
Vhat arc diffcrcnt mcthods in HttpScrvIct? AIso what arc advantagcs
of Gct and POST mcthods?
1hcrc arc iwo main mcihods by which daia can bc scni io Scr·lcis. 1hcy
arc GET and POST. Hcrc is ihc o·cr ·icw oí samplc Scr·lci and ihc
mcihods you should bc o·cr riding.
pubIic abstract svnchronizcd cIass HttpScrvIct
c×tcndsGcncricScrvIct impIcmcnts ScriaIizabIc
protcctcd void doGct (HiipScr·lciRcqucsi rcqucsi.
HiipScr·lciRcsponsc rcsponsc) throws ScrvIctE×ccption,
// If from thc Form, thc data is submittcd using GET
mcthod thcn this mcthod
// is caIIcd. AIso bv dcfauIt whcn this ScrvIct is caIIcd from
Browscr thcn this
// mcthod is caIIcd.
protcctcd void doPost (HiipScr·lciRcqucsi rcqucsi.
HiipScr·lciRcsponsc rcsponsc) throws ScrvIctE×ccption,
// If from thc Form, thc data is submittcd using PUT
mcthod thcn this mcthod is caIIcd
protcctcd void scrvicc (HiipScr·lciRcqucsi rcqucsi.
HiipScr·lciRcsponsc rcsponsc) throws ScrvIctE×ccption,
// If vou must rcspond to rcqucsts madc bv a cIicnt that is
not using thc
// HTTP protocoI, vou must usc scrvicc(). You normaIIv
shouId ncvcr nccd
// to ovcrridc this mcthod, c×ccpt in spcciaI cascs.
(FAQ) For Core & Advance Java
Ií you wani io suppori doIosi and doGci írom samc Scr·lci. ihcn in ciihcr
doIosi or doGci mcihod. which c·cr is gciiing callcd. call ihc oihcr
mcihod using
doGct (rcqucst, rcsponsc) or doPost (rcqucst, rcsponsc)
Ior sccuriiy rcasons ií ihc daia bcing submiiicd is scnsiii·c. ihcn ii's
always prcícrrcd io usc IOS1 mcihod so ihai ihc clcmcnis don'i shown up
in URL. Vhcn submiiicd using GE1. ihcn all ihc clcmcnis gci appcndcd
io ihc URL. Ií uscr clicks on a ihird pariy click and ií ihai scr·cr is logging
ihc Rcícrrcr. ihcn ihc cniirc URL is sa·cd. which will also ha·c scnsiii·c
Also ihcrc arc limiiaiions oí amouni oí daia ihai can bc scni ihrough GE1
mcihod. Somc Opcraiing Sysicms ha·c a limiiaiion on ihc Max Lcngih oí
ihc URL. which is normally ::: characicrs. Ií scnding ihrough GE1
mcihod. ihcn ihc ioial lcngih oí ihc iargci Scr·lci namc and all paramcicrs
daia nccd io bc lcss ihan ::: characicrs.

How do I scnd cmaiI from mv ScrvIct?
You ha·c iwo opiions. 1hc íirsi is io wriic or íind a Simplc Mail 1ransícr
Iroiocol (SM1I) implcmcniaiion in Ja·a. 1hc sccond is io usc ihc
Ja·aMail AII. You can download ihc Ja·aMail írom ihis siic ai
Ior a samplc implcmcniaiion oí scnding E-mail using Ja·aMail plcasc rcícr
io ihis Ariiclc on Scnding E-mail using Ja·aMail.

How do I pass argumcnts to thc ScrvIcts?
Argumcnis io ihc Scr·lcis can bc passcd ai iwo lc·cls.
Vhcn a clicni is in·oking ihc Scr·lci. ihc clicni can pass ihc argumcnis as
pari oí a URL in íorm oí namc/·aluc pairs in ihc qucry siring poriion oí
ihc URL. Ií a iag is uscd in ihc himl languagc io in·okc ihc Scr·lci. ihc
argumcnis can bc passcd ihrough ihc param namc consiruci.
<scr·lci codc·"scr·lci_namc". siock_symbol·"JCOM">
(FAQ) For Core & Advance Java
1hc scr·cr adminisiraior/wcb-masicr can pass argumcnis io ihc Scr·lci ai
loading/iniiializaiion iimc by spcciíying ihc argumcnis as pari oí ihc
scr·cr coníiguraiion. Ior 1omcai and JSVDK. cdii ihc coní/wcb.xml íilc.

1hc rcquircd argumcni (paramcicr: in ihis cxamplc) can bc rciric·cd ihis
way. gctScrvIctContc×t().gctAttributc( "paramatcri")

How can I sct CIass Path for mv ScrvIcts?
Ior dc·cloping Scr·lcis. jusi makc surc ihai ihc JAR íilc coniaining
ja·ax.scr·lci.¨ is in your CLASSIA1H. and usc your normal dc·clopmcni
Ior morc dciails on sciiing ClassIaih. plcasc rcícr io ihis URL.

Can vou providc a sampIc ScrvIct?
Hcrc's ihc samplc implcmcniaiion oí a Scr·lci io prini "Vclcomc io ihc
Vorld oí Scr·lcis." on io ihc browscr
cIass HcIIoScrvIct c×tcnds HttpScrvIct
* HandIc thc HTTP GET mcthod bvbuiIding a simpIc wcb pagc.
pubIic void doGct (HttpScrvIctRcqucstrcqucst,
HttpScrvIctRcsponsc rcsponsc) throws ScrvIctE×ccption,

String titIc= "HcIIo ScrvIct";

//sct contcnt tvpc and othcr rcsponsc hcadcr ficIds first

(FAQ) For Core & Advance Java
// thcn writc thc data of thc rcsponsc
out =rcsponsc.gctVritcr();
out.printIn("<Hi>"+ titIc + "</Hi>");
out.printIn("<P>VcIcomc to thc VorId of ScrvIcts.");

How do I gct thc namc of thc currcntIv c×ccuting script?
Usc rcq.gciRcqucsiURI() or rcq.gciScr·lciIaih(). 1hc íormcr rciurns ihc
paih io ihc scripi including any cxira paih iníormaiion íollowing ihc namc
oí ihc Scr·lci. ihc laiicr sirips ihc cxira paih inío. Ior cxamplc.

URL hiip.//www.ja·asoíi.com/scr·lcis/HclloScr·lci/jdaia/uscrinío'pagciypc·s·epagcnum·+
GctRcqucstURI scr·lcis/HclloScr·lci/jdaia/uscrinío
gctScrvIctPath scr·lcis/HclloScr·lci/
gctPathInfo /jdaia/uscrinío
gctOucrvString pagciypc·s·epagcnum·+

Can vou providc mc with a List of ISP's who support ScrvIcts?
Hcrc is ihc lisi oí all ISI's who supporis Scr·lcis.

Vhat arc URL Encoding and URL Dccoding?
URL cncoding is ihc mcihod oí rcplacing all ihc spaccs and oihcr cxira
characicrs inio ihcir corrcsponding Hcx Characicrs and
(FAQ) For Core & Advance Java
Dccoding is ihc rc·crsc proccsscs con·criing all Hcx Characicrs back
ihcir normal íorm.
Ior Examplc considcr ihis URL. /Scr·lcisDircciory/Hcllo'scr·lci/
Vhcn Encodcd using
URLEncodcr.cncodc("/Scr·lcisDircciory/Hcllo'scr·lci/") ihc ouipui is


1his can bc dccodcd back using

Vhat arc Good Vavs of Dcbugging a ScrvIct? Arc IDE`s, which
support ScrvIct Dcbugging?
1hcrc is couplc oí Mcihods. Iirsily scr·crs likc JRun and oihcrs pro·idc
scparaic logs whcrc all your Irini rcqucsis will prini ihcir daia inio. Ior
cxamplc rcqucsis io Sysicm.oui. and Sysicm.crr go io diíícrcni log íilcs. Ií
ihis ícaiurcs is noi a·ailablc or you wani io mainiain logs scparaicly íor
cach modulc ihcn you can crcaic a siaiic class callcd "LogVriicr" and call
ihc mcihod insidc ihc iry caich loop. 1hc prini mcihod in Log Vriicr will
bc wriiing io a cusiom dcíincd Log Iilc.
¦ ....
catch(E×ccption c×p)
LogVritcr.print("Dcbug : Thc foIIowing crror occurrcd at function
.... ')
Also Inprisc JBuildcr supporis Scr·lci Dcbugging. Ilcasc rcícr io JBuildcr
documcniaiion íor dciails.
Jusi as a Cauiionary noic. írom your Scr·lcis. nc·cr call Svstcm.c×it(c).
1hc rcsulis mighi bc unprcdiciablc. Mighi closc down ihc cniirc Scr·cr.
(FAQ) For Core & Advance Java
1hc Bcsi way is io caich ihc Exccpiion and ciihcr scnd rcsuliani Error
Iagc io ihc Browscr or Vriic io a Log íilc and iníorm uscr aboui ihc

If I put a ScrvIct on cach scrvcr, and thcrc is a static variabIc in this
ScrvIct, Do aII thc instanccs on thcsc scrvcrs sharc thc samc static
variabIc? If not, how can I sharc data among thcsc instanccs?
No you can`i. bccausc on c·cry scr·cr ihcrc is a diíícrcni VM running and a
siaiic ·ariablc is sharcd only bciwccn class insianccs in ihc samc VM.

How can I crcatc Imagcs DircctIv on fIv and scnd thcm to browscr,
aIso at run timc if I want to writc tc×t on top of a imagc, how to do

You can gci ihc laicsi imagc gcncraior packagc írom

How can I upIoad FiIc using a ScrvIct?
You can show a Browsc and Upload Iorm buiion using ihc íollowing codc.
<IORM ENC1YIE·"muItipart/form-data" mcihod·posi
<INIU1 1YIE·"íilc" NAME·"currcniíilcnamc">
<INIU1 1YIE·"submii" VALUE·"upload">
Ii's casy io usc a rcadily a·ailablc class. which you can casily cmbcd inio
your Scr·lci íor uploading ihc íilc. Download ihc mosi widcly uscd Iilc
Upload packagc wriiicn by Jason Hunicr ai ihis URL

How can I prcvcnt Browscr from Caching thc Pagc Contcnt?
Bcíorc scnding ihc daia io ihc browscr. wriic ihc íollowing siaicmcnis.
(FAQ) For Core & Advance Java
rcsponsc.sctDatcHcadcr ("E×pircs", c);
1his also hclps in casc. whcn uscr should noi bc ablc io scc prc·ious pagcs
by clicking ihc Back buiion in ihc browscr. (Vhcnc·cr scnsiii·c
iníormaiion is bcing displaycd and somconc clsc comcs back io machinc
and irics io scc whai daia was cnicrcd)

How can I rcducc thc numbcr of writcs to CIicnt from ScrvIct?
Ii is always rccommcndcd io usc SiringBuíícr insicad oí Siring c·cn íor
concaicnaiing a group oí Sirings. A·oid wriiing c·cry small siring io
clicni. Insicad siorc all ihc sirings inio a SiringBuíícr and aíicr suííicicni
daia is a·ailablc. scnd ihis daia io Browscr.
Hcrc is somc samplc codc.
PrintVritcr out =rcs.gctVritcr();
StringBuffcr sb = ncw StringBuffcr();

//concatcnatc aII tc×t to StringBuffcr
sb.appcnd("<htmI><bodv>............. </bodv></htmI>")


Can vou comparc thc Pcrformancc of ScrvIcts, !SP and PcrI Pagcs?
Scr·lcis will bc íasicr ihan Icrl CGI. bccausc ihc Icrl cxccuiablc nccds io
launch as a scparaic proccss. ihcn Icrl nccds io iniiializc iisclí. ihcn load in
ihc scripi. ihcn compilc ihc scripi. and íinally cxccuic ihc scripi. Hcrc arc
somc Bcnchmarks íor running Scr·lcis and Icrl undcr similar condiiions
hiip.//www.objcxccl.com/workingja·a.him4Vcb Scr·cr Bcnchmarks

Vhat is RcqucstDispatchcr?

RcqucstDispatchcr is an Inicríacc ihai dcíincs an objcci ihai rccci·cs
rcqucsis írom ihc clicni and scnds ihcm io any rcsourcc (such as a Scr·lci.
(FAQ) For Core & Advance Java
H1ML íilc. or JSI íilc) on ihc scr·cr. 1hc Scr·lci coniaincr crcaics ihc
RcqucsiDispaichcr objcci. which is uscd as a wrappcr around a scr·cr
rcsourcc locaicd ai a pariicular paih or gi·cn by a pariicular namc.
1his inicríacc is inicndcd io wrap Scr·lcis. bui a Scr·lci coniaincr
can crcaic RcqucsiDispaichcr objccis io wrap any iypc oí rcsourcc.
RcqucsiDispaichcr has : mcihods.
• íorward ().
forward (Scr·lciRcqucsi rcqucsi. Scr·lciRcsponsc rcsponsc)
Iorwards a rcqucsi írom a scr·lci io anoihcr rcsourcc (Scr·lci. JSI
íilc. or H1ML íilc) on ihc scr·cr.
• includc ().
incIudc (Scr·lciRcqucsi rcqucsi. Scr·lciRcsponsc rcsponsc)
Includcs ihc conicni oí a rcsourcc (Scr·lci. JSI pagc. H1ML íilc) in
ihc rcsponsc.

Vhat is thc diffcrcncc bctwccn Forward Tag and IncIudc Tag?

1hc Diíícrcncc bciwccn Iorward 1ag and Includc 1ag arc.

Forward Tag IncIudc Tag
1hc <jsp.íorward> clcmcni
íorwards ihc rcqucsi objcci
coniaining ihc clicni rcqucsi
iníormaiion írom onc JSI pagc io
anoihcr rcsourcc.

1hc <jsp.includc> clcmcni allows
you io includc ciihcr a .íuí:c or
(ynum:c ·«.ou·c« in a JSI pagc.

1hc iargci rcsourcc can
bc an H1ML íilc. anoihcr JSI pagc.
or a Scr·lci. as long as ii is in ihc
samc applicaiion conicxi as ihc
íorwarding JSI pagc.

Ií ihc rcsourcc is siaiic. iis conicni is
includcd in ihc calling JSI pagc. Ií
ihc rcsourcc is dynamic. ii acis on a
rcqucsi and scnds back a rcsuli ihai
is includcd in ihc JSI pagc.

1hc lincs in ihc sourcc JSI pagc
aíicr ihc <jsp.íorward> clcmcni arc
noi proccsscd.
Vhcn ihc includc aciion is íinishcd.
ihc JSI coniaincr coniinucs
proccssing ihc rcmaindcr oí ihc JSI

<jsp.íorward pagc·"/scr·lci/login"

pagc·"scripis/login.jsp" />

(FAQ) For Core & Advance Java

Vhat thc diffcrcncc bctwccn rcsponsc.scndRcdircct () and a

Ií you usc RcqucsiDispaichcr. you can íorward ihc HiipRcqucsi and
HiipRcsponsc objccis io ihc dcsiinaiion Scr·lci or JSI pagc. Vhcrcas
whcn you usc scndRcdircci. ihc rcqucsi and rcsponsc objccis arc losi and ii
is a mcrc rcdircciion io anoihcr pagc.
Vhcn ihc Scr·lci rcdirccis io oihcr rcsourcc (scndRcDircci ()) ihc rcqucsi
objcci docs noi rcach ihc sccond sourcc dirccily. 1hc scr·cr scnds an ·o:
mcssagc io ihc clicni saying ihai ihc rcsourcc is mo·cd io anoihcr URL
and ihc clicni should acccss írom ihcrc. 1hc boiiom linc ihc liíc cyclc oí
iniiial rcqucsi objcci icrminaics wiih cnd oí íirsi Scr·lci.

In rcqucsi dispaichcr íorward mcihod ihc rcqucsi objcci is íorwardcd io
ihc sccond rcsourcc ihus mainiaining ihc objcci bindings wiih oui round
irip io ihc clicni on ihc nciwork. Scr·lcis/JSI`s usc íorward mcchanism io
dclcgaic iask among ihc rcsourccs.

How can I caII anothcr ScrvIct or !SP from thc currcnt ScrvIct?
Onc oí ihc prc·ious mcihods íor calling anoihcr Scr·lci was
HcIIoScrvIct hcIIo =
Irom Scr·lci AII ·crsion :.:. ihis mcihod has bccn dcprccaicd and ihc
prcícrrcd mcihod is io usc RcqucsiDispaichcr. Hcrc is ihc syniax íor ii.
rcq. rcs).

(FAQ) For Core & Advance Java
How can I writc Data submittcd from an AppIct into a diffcrcnt fiIc,
and thcn show uscrs this data on a ncw pagc?
1his is onc oí ihc mosi írcqucnily uscd icchniqucs. Irom your applci crcaic
a URL conncciion ihc Scr·lci and scnd ihc daia using DaiaOuipuiSircam.
Ncxi in ihc Scr·icc mcihod oí Scr·lci. using IriniVriicr. crcaic a ncw
Iilc. On ihc oihcr cnd Applci will bc waiiing íor rcsponsc írom Scr·lci.
using DaiaInpuiSircam. Iass ihc ncwly crcaicd íilcnamc io ihc Applci.
Ncxi Irom applci call ShowDocumcni () mcihod wiih ncw URL.
Hcrc is somc samplc codc.
URL tickctscrvIct = ncw URL
(¨http://jcom.com/scrvIct/TickctscrvIct ");
URLConncction scrvIctConncction =

// inform thc conncction that wc wiII scnd output and
acccpt input

// Don't usc a cachcd vcrsion of URL conncction.
scrvIctConncction.sctUscCachcs (faIsc);
scrvIctConncction.sctDcfauItUscCachcs (faIsc);

// Spccifv thc contcnt tvpc that wc wiII scnd binarv data
scrvIctConncction.sctRcqucstPropcrtv ("Contcnt-Tvpc",

// scnd thc studcnt objcct to thc scrvIct using
ObjcctOutputStrcam tickctOutputToScrvIct = ncw

// scriaIizc thc objcct , and scnding it to scrvIct ,
tickctData shouId impIcmcnt ScriaIizabIc intcrfacc.
tickctOutputToScrvIct .writcObjcct(tickctData);
tickctOutputToScrvIct .fIush();
tickctOutputToScrvIct .cIosc();
(FAQ) For Core & Advance Java
//rcading back data from ScrvIct
ObjcctInputStrcam inputFromScrvIct = ncw
Vcctor rcsuItantData = (Vcctor)
String fiIcnamc = (String) rcsuItantData.cIcmcntAt(c)
//Showing thc ncwIv crcatcd pagc from AppIct
URL("http://www.jcom.com/profiIcs/" + fiIcnamc));

Vhat arc AppIication Scrvcrs, ScrvIct Engincs and Vcb Scrvcrs? How
arc thcv diffcrcnt from cach othcr?
Iniiially whcn ihc Vcb siaricd. ihcn all ii nccdcd was a Scr·cr. which
would rccci·c ihc rcqucsis íor H1ML pagcs or imagcs and scnd ihcm back
io browscrs. 1hcsc arc callcd Vcb Scr·crs. 1hcn camc rcqucsis íor
handling high capaciiy Daiabasc 1ransaciions. supporiing laicsi Ja·a
Icaiurcs likc Scr·lcis. JSI. and EJB cic. 1hcsc rcqucsis arc handlcd by
Applicaiion Scr·crs. Normally íor an Applicaiion Scr·cr io cxisis and
proccss clicni rcqucsis. ihcy nccd io comc ihrough Vcb scr·crs. All ihc
siaiic daia likc plain H1ML pagcs cic arc placcd on Vcb Scr·cr. All ihc
Dynamic Conicni gcncraiors likc Scr·lcis. EJB arc placcd on applicaiion
Scr·lci Engincs arc similar io Applicaiion scr·crs. cxccpi ihcy suppori
only Scr·lcis and JSI.
Hcrc is ihc Scr·lci Engincs. which suppori Scr·lcis and JSI.
hiip.//ja·a.sun.com/producis/scr·lci/indusiry.himl. Also hcrc is a ihc lisi
oí All Vcb Scr·crs and Applicaiion Scr·crs.
Applicaiions scr·crs arc jusi anoihcr lc·cl abo·c Scr·lci Engincs.
Normally all ihc Applicaiion scr·crs suppori Scr·lcis and JSI. Noi all
Scr·lci Engincs suppori oihcr ícaiurcs likc EJB cic. which Applicaiion
Scr·crs suppori.

(FAQ) For Core & Advance Java
Vhat arc thc Latcst DcvcIopmcnts to ScrvIcts API?
Ilcasc ·isii ihis URL.

Is thcrc a MaiIing List for ScrvIcts rcIatcd discussions?
Hcrc is ihc archi·c oí all ihc Scr·lcis Inicrcsi discussions mainiaincd by
Ja·aSoíi hiip.//archi·cs.ja·a.sun.com/archi·cs/scr·lci-inicrcsi.himl. You
can also subscribc io ihis mailing lisi írom hcrc.

How do I downIoad a binarv fiIc from a ScrvIct or !SP?
Usc ihc Normal Anchor 1ag írom your pagc. 1his is ihc mosi prcícrrcd
way and lci ihc browscr handlc ihc downloading pari. Hcrc is ihc Simplc
syniax íor ii.
<a hrcí·"Iroíilcr.zip">DownloadIilc Irom Hcrc</a>
Anoihcr way io ha·c Scr·lci do ihis would bc. pro·idc a link io Scr·lci.
and írom ihc Scr·lci call ihis íunciion.

How do I know whcn uscr Scssion has c×pircd or rcmovcd?
Dcíinc a class. say ScssionTimcoutIndicator. which implcmcnis
java×.scrvIct.http.HttpScssionBindingListcncr. Crcaic a
ScssionTimcoutIndicator objcci and add ii io ihc uscr scssion. Vhcn ihc
scssion is rcmo·cd. ScssionTimcoutIndicator.vaIucUnbound () will bc
callcd by ihc Scr·lci cnginc. You can implcmcni vaIucUnbound () io do
ihc rcquircd opcraiion.

(FAQ) For Core & Advance Java
How can I rcad data from anv URL, which can bc a binarv data or
from a Activc Scrvcr Pagc?
Using ihc Iollowing codc. you can download any daia. Ií ii connccis io
ciihcr Scr·lci or ASI pagc. ihcn ihc daia gcis proccsscd ií rcquircd and ihc
rcsuliani daia is rcad back.
//Conncct to anv URL, can bc to a imagc, to a static URL Iikc
www.vahoo.com or anv ASP pagc.
URL urI = ncw URL

URLConncction conncction = urI.opcnConncction();
InputStrcam strcam = conncction.gctInputStrcam();
BuffcrcdInputStrcam in = ncw BuffcrcdInputStrcam(strcam);
FiIcOutputStrcam fiIc = ncw FiIcOutputStrcam("rcsuIt.t×t");
BuffcrcdOutputStrcam out = ncw BuffcrcdOutputStrcam(fiIc);

//Rcading Data from thc abovc URL
int i; whiIc ((i = in.rcad()) != -i)¦

How can I strcss tcst mv ScrvIcts?
You can usc onc oí ihc íollowing producis io sircss icsi your Scr·lcis.
• Scr·lciKillcr Irom Allairc Soíiwarc (Ircc Iroduci)
• E-Load írom RSV Soíiwarc
• Vcb Applicaiion Sircss 1ool Irom Microsoíi
• JMcicr írom Apachc

Vhat is ScrvIct Chaining?
ScrvIct Chaining is ihc proccss oí chaining ihc ouipui oí onc Scr·lci io
anoihcr Scr·lci.
You nccd io coníigurc your Scr·lci cnginc Ja·a Vcb scr·cr. JRun. JScr· ...
íor ihis proccss io work.
(FAQ) For Core & Advance Java

Ior cxamplc io coníigurc JRun íor Scr·lci chaining.
• Sclcci ihc JSE scr·icc (JRun Scr·lci cnginc) io acccss io ihc JSE
Scr·icc Coníig pancl. You ha·c jusi io dcíinc a ncw mapping rulc
whcrc you dcíinc your chaining Scr·lci.
• Lci's say /scrvIcts/TickctChainScrvIct íor ihc ·iriual paih and a
comma scparaicd lisi oí Scr·lcis as CustomcrScrvIct
• So whcn you in·okc a rcqucsi likc
http://javacommcrcc.com/scrvIcts/chainScrvIct. inicrnally ihc
Scr·lci CustomcrScrvIct will bc in·okcd íirsi and iis rcsulis will bc
pipcd inio ihc Scr·lci TickctScrvIct.
1hc CusiomcrScr·lci Scr·lci codc should look likc.
pubIic cIass CustomcrScrvIctc×tcnds HttpScrvIct ¦

pubIic void doGct (HttpScrvIctRcqucst rcqucst,
HttpScrvIctRcsponscrcsponsc) ¦
PrintVritcr out =rcs.gctVritcr();
out.printIn("Customcr Namc : Tom");
1ickciScr·lci has io do is io opcn an inpuisircam io ihc rcqucsi objcci and
rcad ihc daia inio a BuíícrcdRcadcr objcci.
BuffcrcdRcadcr b = ncwBuffcrcdRcadcr( ncw
InputStrcamRcadcr(rcq.gctInputStrcam() ) );
String data = b.rcadLinc();
Hcrc in daia ·ariablc you would gci "CusiomcrNamc. 1om"
Notc: Noi many Scr·lci Engincs suppori Scr·lci Chaining. Also ii has
bccn rcmo·cd írom ihc siandard spcciíicaiions íor Scr·lcis.

(FAQ) For Core & Advance Java
How can I makc surc Uscr had Ioggcd in whcn acccssing Sccurc
Ai ihc bcginning oí cach pagc. placc ihis small codc. or includc ii in a
common íilc
HttpScssion scssion =rcqucst.gctScssion(truc);
if (scssion.gctVaIuc("uscrnamc") == nuII) ¦

// Go ahcad and show thc pagc
In LoginIagc.jsp oncc ihc uscr has pro·idcd ihc corrcci logon crcdcniials.

Vhv am I not sccing thc most rcccnt updatcs madc to thc ScrvIct,
from mv browscr cvcn though I am cIicking on Rcfrcsh button and
rcmovcd aII thc cachc from thc browscr?
1his is aciually onc oí ihc ícaiurcs oí Scr·lci. 1hc íirsi iimc whcn ihc
Scr·lci is in·okcd. ihcn ihc inii () mcihod is callcd. So oncc ihc Scr·lci is
loadcd inio mcmory. ii will siay ihcrc uniil ihc scr·cr is rcsiaricd.
According io spccs. ihc Scr·lci should rcload iisclí whcn ihcrc is a changc
in ihc codc or ncw class is crcaicd. Bui bccausc oí pcríormancc rcasons.
mosi oí ihc Scr·crs don'i chcck íor ihis opiion and ihc samc old Scr·lci is
uscd which is currcnily in mcmory íor all rcqucsis. In JRun. you nccd io
click on ihc rcsiari buiion. Any oihcr soluiions io ihis onc arc wclcomc.
1omcai rcquircs you io jusi rcplacc ihc VAR íilc.

(FAQ) For Core & Advance Java
How can I Iimit thc numbcr of SimuItancous conncctions to thc samc
1his opiion is coníigurablc on ihc Scr·cr. Ior cxamplc JRun allows you io
sciup ihc numbcr oí maximum concurrcni conncciions írom Admin opiion.

Vhat is Scrvcr Sidc Push and how is it impIcmcntcd and whcn is it
Scrvcr Sidc push is uscíul whcn daia nccds io changc rcgularly on ihc
clicnis applicaiion or browscr. wiihoui inicr·cniion írom clicni. Siandard
cxamplcs mighi includc apps likc Siock's 1rackcr. Currcni Ncws cic. As
such scr·cr cannoi conncci io clicni's applicaiion auiomaiically. 1hc
mcchanism uscd is. whcn clicni íirsi connccis io Scr·cr. (Eiihcr ihrough
login cic.). and ihcn Scr·cr kccps ihc 1CI/II conncciion opcn.
Ii's noi always possiblc or ícasiblc io kccp ihc conncciion io Scr·cr opcn.
So anoihcr mcihod uscd is. io usc ihc siandard H11I proiocols ways oí
rcírcshing ihc pagc. which is normally supporicd by all browscrs.
1his will rcírcsh ihc pagc in ihc browscr auiomaiically and loads ihc ncw
daia c·cry : scconds.

Vhat ScrvIct cngincs support sharing of scssion data across muItipIc
Ioad-baIanccd wcb scrvcrs, aIso rcfcrrcd to as CIustcring?
1hc íollowing scr·crs suppori Clusicring.
• IBM Vcbsphcrc. Vcbsphcrc Applicaiion Scr·cr Ad·anccd Ediiion.
by cnabling pcrsisicni scssions
• BEA VcbLogic
• JRun ·.o (Irom ·crsion ·.o)

(FAQ) For Core & Advance Java
Vhat is thc diffcrcncc bctwccn MuItipIc Instanccs of Browscr and
MuItipIc Vindows? How docs this affcct Scssions?
Irom ihc currcni Browscr window. ií wc opcn a ncw Vindow. ihcn ii
rcícrrcd io as Muliiplc Vindows. Scssions propcriics arc mainiaincd across
all ihcsc windows. c·cn ihough ihcy arc opcraiing in muliiplc windows.

Insicad. ií wc opcn a ncw Browscr. by ciihcr doublc clicking on ihc
Browscr Shoricui icon or shoricui link. ihcn wc arc crcaiing a ncw
Insiancc oí ihc Browscr. 1his is rcícrrcd io as Muliiplc Insianccs oí
Browscr. Hcrc cach Browscr window is considcrcd as diíícrcni clicni. So
Scssions arc noi mainiaincd across ihcsc windows.

Vhat mcthods do u usc in ScrvIcts ÷ AppIct Communication?
All ihc Scr·lcis musi implcmcni ihc Scr·lci Inicríacc. Scr·lci Inicríacc
dcclarcs · mcihods
inii ( )
scr·icc ( ) and
dcsiroy ( ).
Vhat arc thc Tvpcs of ScrvIcts?
1hcrc arc : iypcs oí Scr·lcis
• HiipScr·lci.
• GcncricScr·lci.
Vhat is thc Diffcrcncc bctwccn HttpScrvIct and GcncricScrvIct?
GcncricScrvIct: GcncricScr·lci has scr·icc () aimcd io handlc rcqucsis.
HttpScrvIct: HiipScr·lci cxicnds GcncricScr·lci and add suppori íor
doGci (). doIosi () mcihods (H11I :.o). plus doIui (). doOpiion ().
doDclcic (). do1racc () mcihods (H11I :.:). Boih ihcsc classcs arc
(FAQ) For Core & Advance Java
Vhat is thc Diffcrcncc bctwccn doGct and doPost?
doGct (): doGci () mcihod is limiicd wiih :k oí daia io bc scni. and
doPost (): doIosi () mcihod docsn'i ha·c ihis limiiaiion. A rcqucsi siring
íor doGci () looks likc ihc íollowing.
Vhat arc thc tvpcs of Scssion Tracking?
Sincc H11I is .íuí«}«.. p·oíoco}, which mcans ihai ihc cach rcqucsi is
indcpcndcni oí ihc prc·ious onc. how c·cr somc applicaiion nccds io sa·c
ihc siaic iníormaiion so ihai iníormaiion can bc collccicd írom sc·cral
inicraciion across browscr. hcrc is whcrc Scssion bccomc imporiani whcn
you arc cxchanging any iníormaiion wiih ihc ·isiior and don`i wani io losc
iníormaiion. Scssion Tracking is mosi con·cnicni and casy way oí uniquc
clicni idcniiíicaiion.
Vhat is Cookics? Vhv is Cookics uscd?
Cookics arc liiilc (up io +k) picccs oí icxi siorcd by ihc scr·cr on a clicni's
machinc. Cookics arc a siandard way io siorc pcrsisicni scssion iníormaiion
(shopping cari. clicni's prcícrcnccs. cic.). E·cry Vcb scr·cr supporis
cookics. and morc ihan so° clicnis kccp cookics opiions iurncd on in ihcir
Inicrnci browscrs.
If mv browscr docs not support Cookic, and mv scrvcr scnds a cookic
instancc what wiII happcn?
Noihing will happcn cxccpi you will bc unablc io rcad ihis cookic írom a
Can u usc !avaScript in ScrvIcts?
You can includc Ja·aScripi codc io ihc Scr·lci rcsponsc (i.c. io ihc
gcncraicd pagc). bui noi io ihc Scr·lci codc iisclí.
(FAQ) For Core & Advance Java
Vhat is thc Capacitv thc doGct can scnd to Scrvcr?
doGci () mcihod is limiicd wiih :k oí daia io bc scni.
Vhat arc thc Tvpcs of ProtocoI uscd in HttpScrvIct?
HiipScr·lci class was dc·clopcd íor usagc wiih H11I and H11IS

How wiII vou pass thc VaIucs from HTML pagc to ScrvIct?
1hc ·aluc can bc passcd io Scr·lci ·ia. ¨action¨ siaicmcni in <Form> iag.

How do vou invokc ScrvIcts?
1o in·okc Scr·lcis. howc·cr. ihcrc is a common con·cniion. usc a URL oí
ihc íorm
Notc: Thc URL rcfcrs to a ScrvIct.

How can bc ScrvIct Thrcad safc?

1hc Scr·lci can bc 1rcad saíc by implcmcniing ¨SingIcThrcadModcI¨

How do I conncct to a databasc from mv ScrvIct?
Ja·a includcs suppori íor daiabascs. using Ja·a Daiabasc Connccii·iiy
(JDBC). Mosi modcrn daiabascs oíícr JDBC dri·crs. which allow you io
conncci any Ja·a applicaiion (including Scr·lcis) dirccily io your daiabasc.
Ií your daiabasc ·cndor docs noi oíícr a JDBC dri·cr. and ihcrc arc no
ihird pariy dri·crs a·ailablc. you may likc io usc a JDBC bridgc. Ior
cxamplc. ihc ODBC-JDBC bridgc allows you io conncci io any ODBC daia
sourcc. which many daiabascs suppori. Also ihcrc arc íour iypc oí Dri·crs
(FAQ) For Core & Advance Java
Tvpc I : JDBC-ODBC bridgc
Tvpc II : naii·c-AII parily Ja·a
icchnology-cnablcd dri·cr
Tvpc III : nci-proiocol íully Ja·a icchnology-cnablcd dri·cr
Tvpc IV : naii·c-proiocol íully Ja·a icchnology-cnablcd dri·cr
Ior a lisi oí currcnily a·ailablc Daiabasc dri·crs. plcasc ·isii ihis pagc
Crcaiing a ncw Conncciion io Daiabasc írcqucnily could slow down your
applicaiion. Mosi oí ihc projccis usc a conccpi callcd Conncction PooIing.
Hcrc whcn ihc Scr·lci siaris up. in ihc inii mcihod. a pool oí conncciions
arc crcaicd and arc siorcd in mcmory (Mosily in a Vccior). Vhcn a
iransaciion wiih Daiabasc is rcquircd. ihcn ihc ncxi írcc a·ailablc
conncciion is rciric·cd írom ihis Vccior and uscd íor ihai purposc. Oncc iis
work is donc. ii is again pui back inio Conncciions Iool. indicaiing ii is
a·ailablc. 1oday mosi oí ihc JDBC Dri·crs suppori ihis ícaiurc and ha·c
inbuili conncciion pooling mcchanisms.
PIcasc Notc: Ií you arc crcaiing your own Conncciion Iooling. ii is
sirongly rccommcndcd io closc all ihc opcn conncciions in ihc dcsiroy
mcihod. Elsc ihcrc arc chanccs oí your daia gciiing corrupicd.

Vhat is Mctadata and whv shouId I usc it?

Mctadata ('daia aboui daia') is iníormaiion aboui onc oí iwo ihings.
Daiabasc iníormaiion (ja·a.sql.DaiabascMciaDaia). or
Iníormaiion aboui a spcciíic RcsuliSci (ja·a.sql.RcsuliSciMciaDaia).

Usc DaiabascMciaDaia io íind iníormaiion aboui your daiabasc. such as iis
capabiliiics and siruciurc. Usc RcsuliSciMciaDaia io íind iníormaiion
aboui ihc rcsulis oí an SOL qucry. such as sizc and iypcs oí columns

Vhat is a "dirtv rcad"?

Ouiic oíicn in daiabasc proccssing. wc comc across ihc siiuaiion whcrcin
onc iransaciion can changc a ·aluc. and a sccond iransaciion can rcad ihis
·aluc bcíorc ihc original changc has bccn commiiicd or rollcd back. 1his is
(FAQ) For Core & Advance Java
known as a dirtv rcad sccnario bccausc ihcrc is always ihc possibiliiy ihai
ihc íirsi iransaciion may rollback ihc changc. rcsuliing in ihc sccond
iransaciion ha·ing rcad an in·alid ·aluc.
Vhilc you can casily command a daiabasc io disallow (:·íy ·«u(.. ihis
usually dcgradcs ihc pcríormancc oí your applicaiion duc io ihc incrcascd
locking o·crhcad. Disallowing diriy rcads also lcads io dccrcascd sysicm

Vhat's a bcttcr approach for cnabIing thrcad-safc ScrvIcts and !SP`s?
SingIcThrcadModcI Intcrfacc or Svnchronization?

Alihough ihc Singlc1hrcadModcl icchniquc is casy io usc. and works wcll
íor low ·olumc siics. ii docs noi scalc wcll. Ií you aniicipaic your uscrs io
incrcasc in ihc íuiurc. you may bc bciicr oíí implcmcniing cxplicii
synchronizaiion íor your sharcd daia. 1hc kcy howc·cr. is io cííccii·cly
minimizc ihc amouni oí codc ihai is synchronizcd so ihai you iakc
maximum ad·aniagc oí muliiihrcading.
Also. noic ihai Singlc1hrcadModcl is prciiy rcsourcc inicnsi·c írom ihc
scr·cr's pcrspccii·c. 1hc mosi scrious issuc howc·cr is whcn ihc numbcr oí
concurrcni rcqucsis cxhausis ihc Scr·lci insiancc pool. In ihai casc. all ihc
un-scr·iccd rcqucsis arc qucucd uniil somcihing bccomcs írcc - which
rcsulis in poor pcríormancc. Sincc ihc usagc is non-dcicrminisiic. ii may
noi hclp much c·cn ií you did add morc mcmory and incrcascd ihc sizc oí
ihc insiancc pool.

Can thcrc bc morc than onc instancc of a ScrvIct at onc timc?

Ii is imporiani io noic ihai ihcrc can bc morc ihan onc insiancc oí a gi·cn
Scr·lci class in ihc Scr·lci coniaincr. Ior cxamplc. ihis can occur whcrc
ihcrc was morc ihan onc Scr·lci dcíiniiion ihai uiilizcd a spcciíic Scr·lci
class wiih diíícrcni iniiializaiion paramcicrs. 1his can also occur whcn a
Scr·lci implcmcnis ihc Singlc1hrcadModcl inicríacc and ihc coniaincr
crcaics a pool oí Scr·lci insianccs io usc.
I havc a gIobaI variabIc in a ScrvIct cIass. Vhat wiII happcn to this
gIobaI variabIc if two rcqucsts hit on thc samc timc?

Vhai will happcn is an uníorcsccablc c·cni.
1hc bcsi way io csiablish a dcíauli occurrcncc (ihc Scr·lci handlcs a
rcqucsi ai a iimc) is io synchronizc ihc acccss io ihc global ·ariablc or
(FAQ) For Core & Advance Java
alicrnaii·cly io crcaic a Scr·lci ihai implcmcnis ihc Singlc1hrcadModcl

(FAQ) For Core & Advance Java
!/\/´£P\£P P/H£´ !/\/´£P\£P P/H£´ !/\/´£P\£P P/H£´ !/\/´£P\£P P/H£´

Vhat is "!avaScrvcr Pagcs" TcchnoIogv?

!avaScrvcr Pagcs , a pari oí ihc !eEE plaiíorm. is a icchnology spcciíicd
by Sun Microsysicms as a con·cnicni way oí gcncraiing dynamic conicni
in pagcs ihai arc ouipui by a Vcb applicaiion. 1his icchnology. which is
closcly couplcd wiih Ja·a Scr·lci icchnology. allows you io includc Ja·a
codc snippcis and calls io cxicrnal Ja·a componcnis wiihin ihc H1ML
codc (or oihcr markup codc. such as XML) oí your Vcb pagcs. Ja·aScr·cr
Iagcs (JSI) icchnology works niccly as a íroni-cnd íor busincss logic and
dynamic íunciionaliiy cncapsulaicd in Ja·aBcans and Enicrprisc Ja·aBcan
A JSI pagc is iranslaicd inio a Ja·a Scr·lci bcíorc bcing cxccuicd. and ii
proccsscs H11I rcqucsis and gcncraics rcsponscs similarly io any oihcr
Scr·lci. JSI icchnology oíícrs a morc con·cnicni way io codc ihc Scr·lci.
1ranslaiion usually occurs "on dcmand"--ihai is. as ihc applicaiion is run.
1hc JSI iranslaior is iriggcrcd by ihc .jsp íilc namc cxicnsion in a URL.
How do !SP pagcs work?
A JSI cnginc inicrprcis iags. and gcncraics ihc conicni rcquircd - íor
cxamplc. by calling a bcan. acccssing a daiabasc wiih ihc JDBC AII or
including a íilc. Ii ihcn scnds ihc rcsulis back in ihc íorm oí an H1ML (or
XML) pagc io ihc browscr. 1hc logic ihai gcncraics ihc conicni is
cncapsulaicd in iags and bcans proccsscd on ihc scr·cr.

Docs !SP tcchnoIogv rcquirc thc usc of othcr !ava pIatform APIs?
JSI pagcs arc iypically compilcd inio Ja·a plaiíorm Scr·lci classcs.
As a rcsuli. JSI pagcs rcquirc a Ja·a ·iriual machinc ihai supporis
ihc Ja·a plaiíorm Scr·lci spcciíicaiion.
How is a !SP pagc invokcd and compiIcd?
Iagcs buili using JSI icchnology is iypically implcmcnicd using a
iranslaiion phasc i.c. pcríormcd oncc. ihc íirsi iimc ihc pagc is callcd. 1hc
(FAQ) For Core & Advance Java
pagc is compilcd inio a Ja·a Scr·lci class and rcmains in scr·cr mcmory. so
subscqucni calls io ihc pagc ha·c ·cry íasi rcsponsc iimcs.
Vhat docs !SP pagc sourcc Iook Iikc?
Hcrc is a simplc cxamplc.
<HEAD><TITLE>Thc VcIcomc Uscr !SP</TITLE></HEAD>
<% String uscr=rcqucst.gctParamctcr("uscr"); %>
<Hs>VcIcomc <%= (uscr==nuII) ? "" : uscr %>!</Hs>
<P><B> Todav is <%= ncw java.utiI.Datc() %>. Havc a nicc dav! :-
<B>Entcr namc:</B>
<INPUT TYPE="tc×t" NAME="uscr" SIZE=ia>
<INPUT TYPE="submit" VALUE="Submit namc">
1his JSI pagc will producc somcihing likc ihc íollowing ouipui ií ihc uscr
inpuis ihc namc "Amy".
Vclcomc Amy!
1oday is Vcd May o¬ :·.+:.:· ID1 :oo·. Ha·c a nicc day! .-)

Vhv do I nccd !SP tcchnoIogv if I aIrcadv havc ScrvIcts?
JSI pagcs arc compilcd inio Scr·lcis. so ihcorciically you could wriic
Scr·lcis io suppori your wcb-bascd applicaiions. Howc·cr. JSI
icchnology was dcsigncd io simpliíy ihc proccss oí crcaiing pagcs by
scparaiing wcb prcscniaiion írom wcb conicni. In many applicaiions.
ihc rcsponsc scni io ihc clicni is a combinaiion oí icmplaic daia and
dynamically gcncraicd daia. In ihis siiuaiion. ii is much casicr io
work wiih JSI pagcs ihan io do c·cryihing wiih Scr·lcis.
(FAQ) For Core & Advance Java
Vhat arc somc othcr rcsourccs for gcncraI information and tvpicaI
qucstions about !avaScrvcr Pagcs tcchnoIogv?
Sun Microsvstcms Vcb sitcs:
hiip.//ja·a.sun.com/producis/jsp/ (gcncral iníormaiion and
hiip.//ja·a.sun.com/producis/jsp/docs.himl (JSI bcginncr's guidc)
Oihcr gcncral iníormaiion and "frcqucntIv askcd qucstions" siics.
hiip.//www.jspin.com/ (JSI gcncral indcx)
hiip.//www.ja·asoíi.com/producis/jsp/íaq.himl (IAO lisi)
hiip.//www.jguru.com/jguru/íaq/íaqpagc.jsp'namc·JSI (IAO lisi)
1o add yoursclí io Sun's jsp-inicrcsi mailing lisi. scnd c-mail io
lisiscr·(ja·a.sun.com wiih ihc íollowing in ihc body oí ihc mcssagc.
subscribc jsp-inicrcsi
you·}u.ínum« you·J:·.ínum«
1o unsubscribc. scnd cmail wiih ihc íollowing in ihc body oí ihc mcssagc.
signoíí jsp-inicrcsi
Vhat arc thc diffcrcnt attributcs in !SP?
1hcrc arc :: diíícrcni dirccii·cs in JSI ihcy arc lisicd bcllow
• 1hc import Aiiribuic
Usc oí ihc import aiiribuic iakcs onc oí ihc íollowing iwo íorms.
<°( pagc impori·"ja·a.uiil.¨.corcscr·lcis.¨" °>

• 1hc contcntTvpc Aiiribuic
Usc oí ihc contcntTvpc aiiribuic iakcs onc oí ihc íollowing iwo íorms.
<°( pagc conicni1ypc·"MIME-1ypc" °>
<°( pagc conicni1ypc·"MIME-1ypc. charsci·Characicr-Sci" °>
Ior cxamplc. ihc dirccii·c
<°( pagc conicni1ypc·"icxi/plain" °>
has ihc samc cíícci as ihc scripilci
<° rcsponsc.sciConicni1ypc("icxi/plain"). °>

• 1hc isThrcadSafc Aiiribuic
(FAQ) For Core & Advance Java
1hc is1hrcadSaíc aiiribuic iakcs onc oí ihc íollowing iwo íorms.
<°( pagc is1hrcadSaíc·"iruc" °> <°!-- Dcíauli --°>
<°( pagc is1hrcadSaíc·"íalsc" °>

• 1hc scssion Aiiribuic
Usc oí scssion aiiribuic iakcs onc oí ihc íollowing iwo íorms.
<°( pagc scssion·"iruc" °> <°-- Dcíauli --°>
<°( pagc scssion·"íalsc" °>

• 1hc buffcr Aiiribuic
Usc oí buffcr aiiribuic iakcs onc oí iwo íorms.
<°( pagc buíícr·".::«kb" °>
<°( pagc buíícr·"nonc" °>

• 1hc autofIush Aiiribuic
Usc oí autofIush aiiribuic iakcs onc oí ihc íollowing iwo íorms.
<°( pagc auioílush·"iruc" °> <°-- Dcíauli --°>
<°( pagc auioílush·"íalsc" °>
A ·aluc oí íalsc is illcgal whcn also using buíícr·"nonc".

• 1hc c×tcnds Aiiribuic
1hc c×tcnds aiiribuic indicaics ihc supcrclass oí ihc Scr·lci ihai will bc
gcncraicd íor ihc JSI pagc and iakcs ihc íollowing íorm.
<°( pagc cxicnds·"puc}uv«.c}u.." °>
Notc: Usc ihis aiiribuic wiih cxircmc cauiion sincc ihc scr·cr may bc
using acusiom supcrclass alrcady.

• 1hc info Aiiribuic
1hc info aiiribuic dcíincs a siring ihai can bc rciric·cd írom ihc Scr·lci
by mcans oí ihc gciScr·lciInío mcihod. Usc oí inío iakcs ihc íollowing
<°( pagc inío·"Som« M«..uv«" °>

• 1hc crrorPagc Aiiribuic
1hc crrorPagc aiiribuic spcciíics a JSI pagc ihai should proccss any
cxccpiions(i.c.. somcihing oí iypc 1hrowablc) ihrown bui noi caughi in
ihc currcni pagc. Ii is uscd as íollows.
<°( pagc crrorIagc·"R«}uí:.« 1R1" °>

• 1hc isErrorPagc Aiiribuic
(FAQ) For Core & Advance Java
1hc isErrorPagc aiiribuic indicaics whcihcr or noi ihc currcni pagc
can aci as ihc crror pagc íor anoihcr JSI pagc. Usc oí isErrorIagc iakcs
onc oí ihc íollowing iwo íorms.
<°( pagc isErrorIagc·"iruc" °>
<°( pagc isErrorIagc·"íalsc" °> <°!-- Dcíauli --°>

• 1hc Ianguagc Aiiribuic
Ai somc poini. ihc Ianguagc aiiribuic is inicndcd io spcciíy ihc
undcrlying programming languagc bcing uscd. as bclow.
<°( pagc languagc·"cobol" °>
Notc: Ior now. don`i boihcr wiih ihis aiiribuic sincc ja·a is boih ihc
dcíauli and ihc only lcgal choicc.
Vhat is Buffcr and what is it thcv usc in !SP?

1hc Buffcr aiiribuic spcciíics ihc sizc oí ihc buíícr uscd by ihc ouí ·ariablc.
which is oí iypc JspVriicr (a subclass oí IriniVriicr). Usc oí ihis aiiribuic
iakcs onc oí iwo íorms.
<°( pagc buíícr·".::«kb" °>
<°( pagc buíícr·"nonc" °>
Scr·crs can usc a largcr buíícr ihan you spcciíy. bui noi a smallcr onc. Ior
<°( pagc buíícr·"·:kb" °> mcans ihc documcni conicni should bc
buíícrcd and noi scni io ihc clicni uniil ai lcasi ·: kilobyics ha·c bccn
accumulaicd or ihc pagc is complcicd. 1hc dcíauli buíícr sizc is scr·cr
spcciíic. bui musi bc ai lcasi s kilobyics. Bc cauiious aboui iurning oíí
buíícring. doing so rcquircs JSI cnirics ihai sci hcadcrs or siaius codcs io
appcar ai ihc iop oí ihc íilc. bcíorc any H1ML conicni.

Arc !avaScrvcr Pagcs intcropcrabIc with ScrvIcts?
Ycs. JSI pagcs arc íully inicropcrablc wiih Scr·lcis. JSI pagcs can includc
ouipui írom a Scr·lci or íorward io a Scr·lci. and Scr·lcis can includc
ouipui írom a JSI pagc or íorward io a JSI pagc. Iollowing arc somc
<jsp:incIudc pagc="/scrvIct/MvScrvIct" fIush="truc" />
<jsp:forward pagc="/scrvIct/MvScrvIct" />
How do I caII a !ava Bcan (or othcr !ava cIass) from a !SP pagc?
(FAQ) For Core & Advance Java
Usc ihc siandard JSI uscBcan iag io cxccuic a Ja·aBcan or oihcr Ja·a class
írom a JSI pagc. Ior cxamplc.
<jsp:uscBcan id="pagcBcan" cIass="mvbcans.NamcBcan"
scopc="pagc" />
1his cxamplc crcaics an insiancc. pagcBcan. oí ihc mybcans.NamcBcan
class (ihrough a no-argumcni consirucior). 1hc pagcBcan insiancc can bc
uscd ihroughoui ihc currcni JSI pagc--ihai is iis "scopc". (Oihcr possiblc
scopcs arc rcqucsi. scssion. and applicaiion.)
Laicr in ihc pagc. you can usc pagcBcan as in ihc íollowing cxamplc.
HcIIo <%= pagcBcan.gctNcwNamc() %> !
1hc jsp. uscBcan iag is onc oí many siandard JSI "aciion" iags using ciihcr
oí ihc íollowing gcncral íorms.
<jsp: tagnamc ... />
<jsp: tagnamc ... >
</jsp: tagnamc>
How do I caII an Entcrprisc !ava Bcan (E!B) from a !SP pagc?
1hc kcy sicps rcquircd íor a JSI pagc io in·okc an EJB arc ihc íollowing.
:) Impori ihc EJB packagc íor ihc bcan homc and rcmoic inicríaccs inio
cach JSI pagc ihai makcs EJB calls. (In a JSI pagc. usc a pagc dirccii·c íor
:) Usc JNDI io look up ihc EJB homc inicríacc.
·) Crcaic ihc EJB rcmoic objcci írom ihc homc.
+) In·okc busincss mcihods on ihc rcmoic objcci.
NOTE: 1hc abo·c sicps arc pcríormcd by scriplci codc.

(FAQ) For Core & Advance Java
Is thcrc !avaScrvcr Pagcs functionaIitv for incIuding anothcr pagc or
forwarding to anothcr pagc?
Ycs. ihcrc arc sc·cral opiions íor ihis.
:) You can usc ihc includc dirccii·c. known as a "siaiic includc". as
<%m incIudc fiIc="/jsp/uscrinfopagc.jsp" %>
1his cííccii·cly copics ihc pagc sourcc oí uscriníopagc.jsp inio ihc calling
JSI pagc ai iranslaiion iimc. and ihcrcíorc incrcascs ihc sizc oí ihc calling
pagc. (1his is similar in naiurc io a 4includc in ihc C languagc.)
:) You can usc ihc jsp.includc aciion. known as a "dynamic includc". as
<jsp:incIudc pagc="/jsp/uscrinfopagc.jsp" fIush="truc" />
1his includcs ouipui írom uscriníopagc.jsp wiihin ihc ouipui oí ihc calling
pagc. iaking cíícci during runiimc insicad oí during iranslaiion. and is
handlcd ihrough ihc rcqucsi dispaichcr. Ii docs noi incrcasc ihc sizc oí ihc
calling pagc. oihcr ihan io add a call io ihc rcqucsi dispaichcr. (NO1E.
ílush·"iruc" is always rcquircd in JSI :.o and JSI :.: cn·ironmcnis. bui
noi in JSI :.: cn·ironmcni)
·) You can usc ihc jsp.íorward aciion. as íollows.
<jsp:forward pagc="/jsp/uscrinfopagc.jsp" />
Insicad oí simply including ihc conicni oí uscriníopagc.jsp wiihin ihc
conicni oí ihc calling pagc. ihis command aciually siops ihc proccssing oí
ihc calling pagc and iransícrs cxccuiion io uscriníopagc.jsp. Anyihing in
ihc ouipui buíícr írom ihc calling pagc ihai has noi alrcady bccn displaycd
(ílushcd) in ihc browscr will bc clcarcd wiihoui bcing displaycd.
NOTE: A jsp.íorward also íorwards ihc original H11I rcqucsi objcci io
ihc iargci pagc. 1his diíícrs írom H11I rcsponsc objcci
scndRcdircci(Siring URL) íunciionaliiy. which iriggcrs ihc browscr io go
io a spcciíicd URL wiihoui íorwarding ihc original rcqucsi objcci.
(FAQ) For Core & Advance Java
Can I forward to or incIudc a ScrvIct or HTML pagc instcad of a !SP
Ycs. in any JSI cn·ironmcni running in a Scr·lci :.: or highcr
cn·ironmcni. you can spcciíy a Scr·lci class or an H1ML pagc as wcll as a
JSI pagc in any pagc íorward (jsp.íorward) or dynamic includc (jsp.includc)
siaicmcni. as in ihc íollowing cxamplcs.
<jsp:incIudc pagc="/scrvIcts/MvScrvIct" fIush="truc" />
<jsp:forward pagc="/htmI/mvfiIc.htm" />
How can I pass HTTP rcqucst paramctcrs bctwccn pagcs whcn
forwarding to or incIuding anothcr pagc?
You can usc ihc jsp.param iag in conjunciion wiih jsp.includc or
jsp.íorward. as in ihc íollowing cxamplc.
<jsp:incIudc pagc="/tcmpIatcs/uscrinfopagc.jsp" fIush="truc" >
<jsp:param namc="uscrnamc" vaIuc="Smith" />
<jsp:param namc="uscrcmpno" vaIuc="os!c" />
Alicrnaii·cly. you can usc URL rcwriiing in ihc jsp.includc or jsp.íorward.
as in ihc íollowing cxamplc.
s!c" fIush="truc" />
ShouId !SP pagcs bc uscd to proccss or dispIav binarv data?
No--JSI icchnology is dcsigncd íor ouipui and display oí icxi. noi binary
daia. Ior onc ihing. ihcrc arc no mcihods íor wriiing raw daia inio ihc JSI
ouipui wriicr (a ja·ax.scr·lci.jsp.JspVriicr insiancc). Ior anoihcr. during
cxccuiion ihc JSI coniaincr prcscr·cs sourcc codc whiic spacc. including
such ihings as iabs. carriagc rciurns. or lincíccds. Such whiic spacc in
binary daia may causc crrors or problcms in ihc daia.
(FAQ) For Core & Advance Java
1o handlc binary daia such as graphics or daia BLOB. wc rccommcnd ihai
you usc Scr·lcis insicad.
Vhat arc !SP "impIicit objccts"?
JSI icchnology makcs a·ailablc io any JSI pagc a sci oí :mp}:c:í o|]«cí..
1hcsc arc Ja·a class insianccs ihai arc crcaicd auiomaiically by ihc JSI
mcchanism and ihai allow inicraciion wiih ihc undcrlying Scr·lci
A·ailablc implicii objccis includc ihc íollowing.
• pagc. 1his rcprcscnis ihc JSI pagc and is an insiancc oí ihc pagc
implcmcniaiion class ihai was gcncraicd by ihc JSI iranslaior.
• rcqucst. 1his rcprcscnis an H11I rcqucsi and is an insiancc oí
• rcsponsc: 1his rcprcscnis an H11I rcsponsc and is an insiancc oí
• scssion: 1his rcprcscnis an H11I scssion and is an insiancc oí
• appIication: 1his rcprcscnis ihc scr·lci conicxi íor ihc Vcb
applicaiion and is an insiancc oí ja·ax.scr·lci.Scr·lciConicxi.
• out: 1his is an objcci ihai is uscd io wriic ouipui io ihc browscr
írom ihc JSI pagc and is an insiancc oí ja·ax.scr·lci.jsp.JspVriicr.
• config: Rcprcscnis ihc Scr·lci Coníig íor ihis pagc
• c×ccption: rcprcscnis ihc uncaughi 1hrowablc ihai rcsulis írom a
call io ihc crror pagc.
• pagcContc×t: Rcprcscnis ihc pagc conicxi íor ihc JSI
Ior cxamplc. you could usc ihc íollowing codc io gci ihc uscr namc írom
ihc H11I rcqucsi objcci.
<% String uscr=rcqucst.gctParamctcr("uscr"); %>
Vhich dcpIovmcnt dcscriptor cIcmcnt is uscd to scIcct thc
authcntication mcchanism (for c×ampIc BASIC or CLIENT_CERT)
for a Vcb AppIication?
uuín-m«íno( is uscd as an dcscripior clcmcni io sclcci ihc auihcniicaiion
mcchanism íor an Vcb Applicaiion.
(FAQ) For Core & Advance Java
Vhat arc "custom tag Iibrarics¨?
Siandard Ja·aScr·cr Iagcs icchnology. bcginning wiih ihc JSI :.:
spcciíicaiion. allows dc·clopcrs and ·cndors io crcaic cusiom JSI iag
librarics. A Tag Iibrarv dcíincs a collcciion oí cusiom aciions. 1hc iags can
bc uscd dirccily by dc·clopcrs in manually coding a JSI pagc. or
auiomaiically by Ja·a dc·clopmcni iools. Any siandard iag library is
poriablc bciwccn diíícrcni JSI :.: and highcr coniaincr implcmcniaiions.

Vhat is Tag HandIcr?

Tag HandIcr is similar io Ja·aBcans in ihai ihcy arc spccializcd yci
rcusablc componcnis wiih public acccssor/muiaior mcihods.

Vhat arc thc Main mcthods of Tag Intcrfacc?

1hcrc arc o main mcihods oí iag inicríacc.
• doSiari1ag () rciurn iypc. ini
• doEnd1ag () rciurn iypc. ini
• gciIarcni () rciurn iypc. 1ag
• rclcasc () rciurns ·oid
• sciIagcConicxi () rciurn iypc. ·oid
• sciIarcni (1ag) rciurn ·oid
Notc: Ii is good praciicc io implcmcni all ihc mcihods oí ihc inicríacc
c·cn ihough ihcy don`i do anyihing.

Vhich arc thc int vaIucs that doStartTag () can rcturn?

1hcrc arc · ini ·alucs ihai doSiari1ag () can rciurn.
• EVAL_BODY_INCLUDE: Mcans includc ihc body bciwccn ihc
opcning and closing iags inio ihc cxisiing ouipui sircam. bui do noi
crcaic a ncw BodyConicni objcci.
• EVAL_BODY_TAG: Mcans crcaic a ncw Body Conicni objcci and
proccss ihc body oí ihc iag.
• SKIP_BODY: Mcans do noi proccss ihc body oí ihc iag ií ihcrc is
(FAQ) For Core & Advance Java

How do I usc !avaBcans componcnts (Bcans) from a !SP pagc?

1hc JSI spcciíicaiion includcs siandard iags íor bcan usc and manipulaiion.
1hc uscBcan iag crcaics an insiancc oí a spcciíic Ja·aBcans class. Ií ihc
insiancc alrcady cxisis. ii is rciric·cd. Oihcrwisc. ii is crcaicd. 1hc
sciIropcriy and gciIropcriy iags lci you manipulaic propcriics oí ihc
gi·cn objcci. 1hcsc iags arc dcscribcd in morc dciail in ihc JSI
spcciíicaiion and iuiorial.

How can I usc !avaScrvcr Pagcs for databasc acccss?

In a iypical sccnario íor a daiabasc applicaiion. a JSI pagc will call a
Ja·aBcan or Enicrprisc Ja·aBcan and ihc bcan will dirccily or indirccily
acccss ihc daiabasc. gcncrally ihrough JDBC.
How do I sct thc contcnt tvpc (for GIobaIization) in a !SP pagc?
1hc globalizaiion conicni iypc oí a JSI pagc can bc sci ciihcr .íuí:cu}}y
(during iranslaiion) or (ynum:cu}}y (ai runiimc).
You can sci ihc conicni iypc siaiically using a JSI pagc dirccii·c. A
pagc dirccii·c passcs insiruciions io ihc JSI cnginc íor usc during
iranslaiion and runiimc. In addiiion io conicni iypc. ihis mighi includc
such ihings as ihc languagc oí ihc pagc (Ja·a or SOLJ). classcs io impori.
buíícr sizc. and many oihcr iypcs oí insiruciions.
Hcrc is an cxamplc.
<%m pagc ... contcntTvpc="tc×t/htmI" %>
Or ihc íollowing io also spcciíy ihc characicr sci.
<%m pagc ... contcntTvpc="tc×t/htmI; charsct=UTF-s" %>
Eiihcr oí ihc prcccding would ha·c cíícci during iranslaiion as wcll as
(FAQ) For Core & Advance Java
You can sci ihc conicni iypc dynamically by calling (in a JSI scripilci) ihc
sciConicni1ypc() mcihod oí ihc H11I rcsponsc objcci. as in ihc íollowing
Or ihc íollowing io also spcciíy ihc characicr sci.
String s = "EUC!IS"; // or anv othcr dvnamic vaIuc
rcsponsc.sctContcntTvpc("tc×t/htmI; charsct=" + s);
Eiihcr oí ihc prcccding would ha·c cíícci during runiimc only. In ihcsc
cascs. ihc JSI pagc iisclí cannoi coniain globalizaiion characicrs oihcr ihan
ihosc in ihc JVM's dcíauli cncoding. bccausc ihcy could noi bc inicrprcicd
during iranslaiion. Also noic ihai hcrc you can usc runiimc cxprcssions in
sciiing ihc ·alucs. you cannoi usc runiimc cxprcssions in a pagc dirccii·c
bccausc ihc dirccii·c also has cíícci during iranslaiion. and runiimc
cxprcssions cannoi bc calculaicd during iranslaiion.
NOTE. A sciConicni1ypc () call should bc as closc as possiblc io ihc iop oí
ihc JSI pagc. prcccding any buíícr ílushing.
How can I cnabIc Iogging for a !SP appIication?
You can cnablc logging íor a JSI by using applicaiion.log () (ihc log ()
mcihod oí ihc JSI implicii applicaiion objcci). 1his siaris logging io ihc
Scr·lci cnginc log íilc.

How can I vcrifv that a probIcm is rcaIIv in thc !SP containcr, as
opposcd to somc othcr componcnt?
Vriic cqui·alcni codc in a Scr·lci. Ií ihc problcm siill occurs in a Scr·lci.
ihcn ihc rooi causc is in ihc Scr·lci coniaincr or undcrlying laycrs. noi in
ihc JSI coniaincr.

(FAQ) For Core & Advance Java
In mv !SP pagc I want to providc dvnamic path substitution of HREF
vaIuc in anchor tag.... Instcad of giving Iikc foIIowing: Iink to tcst !SP
i want to substitutc thc vaIuc for HREF through a string...

Usc <a hrcf="<%= vaIuc %>">Link</a>
I am scnding form post variabIcs thru HttpURLConncction cIass thcn
I am not abIc to acccss c×isting scssion. How can I scnd variabIcs as
form post without brcaking mv c×isting scssion?

Includc ihc jscssionid wiih ihc ·aluc oí scssion.gciId () as a hiddcn
paramcicr in ihc íorm you wani io posi.
Vhat is Oucrv String?
H11I pro·idcs ihc capabiliiy io appcnd cxira iníormaiion io ihc cnd oí ihc
URL. 1his bii oí iníormaiion in ihc URL is rcícrrcd io as Oucrv String
and is ílaggcd by a qucsiion mark (') ai ihc bcginning. 1hc qucry siring
comc in ihc íorm oí
num«1 can bc any namc you assign ii so ihai ihc rcqucsi can rcícr back io
rciric·c any ·aluc in placc oí .u}u«1

Notc: ¨&¨ in thc abovc string works as a dcIimitcr.
Vhat barc thc diffcrcnt wavs of proccssing thc Oucrv String?
gctParamctcr ( String namc)
1hc gci paramcicr will rciurn only singlc ·aluc írom ihc qucry siring
irrcspccii·c oí muliiplc ·alucs passcd io ii.
gctParamctcrVaIucs (String namc)
(FAQ) For Core & Advance Java
Ior ihc paramcicr coniaining muliiplc ·alucs. calling ihc
gciIaramcicrValucs (somc_namc) mcihod will rciurn a String array oí
all possiblc ·alucs.
gctParamctcrNamcs ()
1hc gciIaramcicrNamcs () will rciurn an 1num«·uí:on O|]«cí ihai lci
you loop o·cr all ihc paramcicr namcs ihai whcrc passcd in ihc rcqucsi.
1hcy arc uscd whcn namcs/·alucs arc passcd in íorm oí FORM íicld. in
which casc gciIaramcicr (Siring namc) and gciIaramcicrValucs (Siring
namc) arc incííccii·c
NOTE: whcn you arc using gciIaramcicrNamcs () makc surc you
impori ihc java.utiI.Enumcration¨ API.
How docs onc cncodc thc URL vaIucs?
E×ampIc. http://IocaIhost:scsc/!SP/Chaptcr-c
1hc URL cncoding i.c. rcmo·ing ihc cscapc scqucncc likc °.o` (is an
ASCII rcprcscnis spacc) in ihc abo·c URL can bc achic·cd by : ways onc
Inscriing ihc appropriaic cscapc scqucncc in ihc URL and or
Onc can usc ihc URLEncodcr. which is íound in JDK:.o. 1his call
can bc íound in ¨java.nct¨ packagc and coniains only onc mcihod
cncodc (String s).
Vhat arc thc diffcrcnt wavs to Iink !SP to HTML, !SP and ScrvIcts?
1hcrc arc : ways io link JSI io H1ML. JSI. Scr·lcis
<jsp:forward pagc = rcIativcURLspcc> or
<jsp:forward pagc =URLspcc>
<jsp:param namc =¨namc¨ vaIuc=¨vaIuc¨ />
(FAQ) For Core & Advance Java
rcsponsc.scndRcdircct (String Iocation);
NOTE: ihc <jsp.íorward> iag allows you io íorward only io anoihcr
rcsourcc oí ihc samc applicaiion ihcrcíorc. ií you ha·c an applicaiion
rooicd ai /mvAppIn, and anoihcr applicaiion is rooicd in samc scr·cr
bui ai /storcAppIn, ihcn any JSI in ihc applicaiion /mvAppIn can
íorward only io rcsourccs dcíincd in /mvAppIn. Aiicmpiing io íorward
a pagc in /mvAppIn io any rcsourcc in /storcAppIn will rcsuli an
Vhat is thc svnta× for !avaScrvcr Pagcs tcchnoIogv?
1hc syniax card and rcícrcncc can bc ·icwcd or downloadcd írom
Can I crcatc XML pagcs using !SP tcchnoIogv?
Ycs. ihc JSI spcciíicaiion docs suppori crcaiion oí XML documcnis.
Ior simplc XML gcncraiion. ihc XML iags may bc includcd as siaiic
icmplaic poriions oí ihc JSI pagc. Dynamic gcncraiion oí XML iags
occurs ihrough bcan componcnis or cusiom iags ihai gcncraic XML
ouipui. Scc ihc whiic papcr D«.«}op:nv AM1 So}uí:on. u:ín Ju.uS«·.«·
Puv«. T«cnno}ovy (IDI) íor dciails.
Can I gcncratc and manipuIatc !SP pagcs using XML tooIs?
1hc JSI :.: spcciíicaiion dcscribcs a mapping bciwccn JSI pagcs and XML
documcnis. 1hc mapping cnablcs ihc crcaiion and manipulaiion oí JSI
pagcs using XML iools.

Is it possibIc to pass in anv Objcct othcr than a String to a tagIib?

Ycs. jusi usc an cxprcssion. 1hc ·aluc oí ihc cxprcssion will noi bc
con·cricd io a siring in ihis casc.

1akc ihc íollowing cxamplc

(FAQ) For Core & Advance Java
<c×ampIc:tag namc="datc" datc="<%= ncw java.utiI.Datc()%>" />

In ihc abo·c cxamplc ihc corrcsponding iag handlcr will ha·c ihc sciDaic
mcihod callcd wiih a ja·a.uiil.Daic objcci passcd in as a paramcicr.

1his is pcríccily ·alid in your JSI assuming ihc iag's ·aluc can bc a
runiimc cxprcssion. 1hc 1LD íor ihis iag library would ha·c io includc ihc
íollowing snippci.




How do I havc mv !SP pagc output a binarv fiIc?
1hc oííicial and corrcci answcr io ihis qucsiion is ihai you don'i ouipui a
binary íilc using a JSI pagc. In playing wiih codc. howc·cr. I íound you
could wriic oui a binary íilc.

1his cxamplc shows how io pull in an cxicrnal binary sourcc and rcdircci ii
using a JSI íilc. 1hc codc uscs rcsponsc.gciOuipuiSircam() insicad oí ihc
oui implicii objcci. Ha·ing shown you ihis cxamplc. I musi also warn you
noi usc a JSI pagc ihis way. Ií you wani io usc Scr·lciOuipuiSircam you
should usc a scr·lci insicad. Vhilc ihis codc cxamplc works in 1omcai ·.:.
I suspcci ii will noi work in oihcr JSI coniaincrs.

Ja·aScr·cr Iagcs arc. oí coursc. bascd upon scr·lcis. In a scr·lci you can
dccidc ií you wani io producc binary ouipui using an OuipuiSircam or icxi
ouipui using a IriniVriicr. In ihc currcni spcciíicaiions. JSI is buili io
producc icxi ouipui so your coniaincr auiomaiically iransíorms your JSI
pagc inio a scr·lci using ihc IriniVriicr. 1hc rub is you can only usc
rcsponsc.gciOuipuiSircam() oncc on a gi·cn HiipScr·lciRcsponsc. 1his
mcans using rcsponsc.gciOuipuiSircam() will pui you in conílici wiih JSI
Scr·lci codc. So dcpcnding on iiming you mighi gci a
IllcgalSiaicExccpiion sincc ihc JSI Scr·lci has alrcady crcaicd ihc oui
(FAQ) For Core & Advance Java
implicii objcci. 1iming wisc. your coniaincr will iniiializc ihc oui implicii
objcci in ihc scr·icc() mcihod in ihc Scr·lci. In ihc codc snippci bclow wc
bcai ihc JSI coniaincr's iniiializaiion oí ihc oui objcci. Vc pay ihc pricc.
howc·cr. oí now bcing unablc io usc ihc oui implicii objcci.

Kccp in mind ihc codc cxamplc is noi mcani íor produciion usc bui raihcr
as iraining iool. Ii shows how JSI handlcs icxi and binary ouipui. 1hc
codc snippci has a sccondary bcncíii oí also showing how io usc sircam
objccis io iransícr daia bciwccn iwo diíícrcni poinis.

Codc E×ampIc:

<%m pagc buffcr="ick" autoFIush="faIsc" %>
<%m pagc import="java.nct.*" %>
<%m pagc import="java.io.*" %>
/* i) Notc: Vhcn vou turn off thc buffcr, thcn !SP wouId havc
grabbcd thc first rcfcrcncc to thc rcsponsc.gctOutputStrcam() to
work with thc output, and this codc wiII causc an c×ccption. Trv it as
a nicc iIIustration whv this shouId bc donc as a scrvIct rathcr than a
!SP pagc! */

/*e) Now tcII thc dcstination browscr what tvpc of fiIc to c×pcct. */

/*s) Now usc thc URL cIass to point to a fiIc and thcn crcatc a
InputStrcam objcct to transfcr thc fiIc with */
URL I_fiIc = ncw URL("http://www.jspinsidcr.com/Iogo.jpg");
URLConncction I_conncct_to_fiIc = I_fiIc.opcnConncction();
InputStrcam I_fiIc_strcam = I_conncct_to_fiIc.gctInputStrcam();

/*or s) RcpIacc thc prcvious s Iincs with thc foIIowing if vou want to
rcad a fiIc IocaIIv off a disk.*/
//FiIcInputStrcam I_fiIc_strcam = ncw

/*4) Crcatc a Buffcrcd strcam objcct to makc rcading thc fiIc morc
cfficicnt */
BuffcrcdInputStrcam I_input_buffcr = ncw

(FAQ) For Core & Advance Java
/*a) Gct a handIc to thc ScrvIct output strcam objcct */
OutputStrcam I_jsp_strcam = rcsponsc.gctOutputStrcam();

/*c) Vrap a buffcr around our ScrvIct Output strcam */
BuffcrcdOutputStrcam I_hoIdfiIc = ncw

/*!) Ioop through and rcad thc binarv data from thc sourcc and push
it into our output strcam. */
int Ii_Ioop;
whiIc ((Ii_Ioop = I_input_buffcr.rcad()) != -i)
¦ I_hoIdfiIc.writc(Ii_Ioop); ]

/*s) fIush thc fiIc into thc jsp strcam */


How do I find mv crror whcn I gct an intcrnaI ScrvIct crror?

Vhcn you rccci·c an crror írom a JSI pagc. ii isn'i ·cry uscr íricndly. In
íaci. íor mosi ncw programmcrs ii's almosi usclcss and can causc much
írusiraiion. Error pagcs. howc·cr. can yicld quiic a bii oí iníormaiion.
Icrhaps mosi imporianily. ihcy can rc·cal ihc cxaci locaiion oí ihc crror.

Ior cxamplc. an crror írom ihc 1omcai coniaincr looks likc ihis.
IntcrnaI ScrvIct Error:

cic morc crror icxi

(FAQ) For Core & Advance Java
Root causc:


Morc Error 1racc lisiing

1hc nccdcd daia is siorcd in ihc "rooi causc" scciion oí ihc crror. 1his
crror iracc shows whcrc ihc crror happcncd in ihc compilcd Scr·lci. Ai
íirsi glancc. ii docsn'i say much aboui whcrc in ihc JSI pagc ihc crror
occurrcd. bui iakc a closcr look. 1hc crror iclls you which linc ihc problcm
happcncd wiihin ihc Scr·lci ihai was crcaicd írom your JSI pagc. You can
·icw your compilcd Scr·lci and ihcn maich up ihc rcsulis io your JSI pagc.
In ihc abo·c cxamplc. I ha·c a Null poinicr in my build Rcpori íunciion
locaicd on linc :: oí ihc compilcd Scr·lci.

1hc íilc namc whcrc I can íind ihc linc in qucsiion is callcd.
_My_JSI_Iagc_ooo:íGcncral_ooo:íLisi_ jsp_:.ja·a.

Dcpcnding on your JSI coniaincr. ihc Scr·lci ja·a íilcs may bc siorcd in
diíícrcni placcs. Icríorm a íilc scarch io íind ihc íilc in qucsiion. In my casc
íor 1omcai. ihc íilcs arc siorcd in

Dcpcnding on your JSI coniaincr you may gci oihcr iníormaiion ihai can
bc hclpíul. 1omcai lca·cs noics wiihin ihc Scr·lci io show whcrc in ihc
original JSI pagc ihc codc originaicd. Ior insiancc. in ihis cxamplc I can
íind ihc íollowing commcni jusi bcíorc my crror. // bcgin
1his iclls mc io go io my \jsp_projcci\Rcpori_Gcncral_Lisi.jsp pagc.

(FAQ) For Core & Advance Java
Iinally. ihc crror iracc shows ihc lasi ícw logical sicps ihc program iook
írom mcihod io mcihod. 1his iníormaiion may bc imporiani in ·criíying
ihai ihc JSI Scr·lci is cxccuiing in ihc ordcr inicndcd íor ihc dcsign.

Viih ihis iníormaiion you arc on your way io íinding whcrc in your logic
you ha·c a problcm. Ok. noi íun. bui ai lcasi now you can usc ihc crror
pagc rcsulis íor morc ihan scrap papcr!
How do I sct a cookic and usc thc cookic's vaIuc on anothcr !SP?
Cookics arc liiilc picccs oí iníormaiion. undcr +k. ihai arc siorcd wiih ihc
clicni's browscr and laicr scni back io ihc scr·cr. Cookics arc mosi
commonly uscd íor scssion iracking. cspccially wiih JSI. Sciiing a simplc
cookic is quiic casy wiih JSI. 1akc íor cxamplc ihc íollowing JSI.
Vc will call ihis scicookic.jsp and ii will sci a cookic dcscribing a
uscrnamc. 1hc Cookic objcci's consirucior iakcs iwo paramcicrs. a Siring
namc íor ihc cookic. and a Siring ·aluc.
<hcad><iiilc>Sciiing ihc cookic.</iiilc></hcad>

Siring cookicNamc · "uscrnamc".
Siring cookicValuc · "miihil".
Cookic cookic · ncw Cookic(cookicNamc. cookicValuc).
<p>1hc cookic <°· cookicNamc °> has bccn sci wiih ihc ·aluc oí
<°· cookicValuc °>.</p>
<p><a hrcí·"scccookic.jsp">Click hcrc io scc all ihc currcni

Oncc a cookic has bccn sci. ihc ·aluc is a·ailablc íor oihcr pagcs io usc.
Hcrc is anoihcr JSI scccookic.jsp ihai rcads and displays all oí ihc cookics
sci íor ihc scssion.

<hcad><iiilc>Sccing ihc cookic.</iiilc></hcad>
(FAQ) For Core & Advance Java
<p>Hcrc is a lisi oí ihc currcni cookics.</p>
<iablc bordcr·":">

Cookic¯¯ cookics · rcqucsi.gciCookics().
íor(ini i·o.i<cookics.lcngih.i++) ¦ °>
<ir><id><°· cookics¯i¯.gciNamc() °></id>
<id><°· cookics¯i¯.gciValuc() °></id></ir>
<° · °>
Usc ihc abo·c iwo pagcs iogcihcr. and wc arc passing iníormaiion wiih
Iirsi Browsc io scicookic.jsp you will bc iníormcd ihc ncw cookic is sci.

Ncxi browsc io scccookic.jsp. you will scc somcihing similar io ihc
(FAQ) For Core & Advance Java

1hc cookic sci írom scicookic.jsp is displaycd along wiih anoihcr cookic
namcd jscssionid. 1hc JSI coniaincr io kccp ihc clicni`s scssion is using ihc
sccond cookic.
In gcncral a browscr is rcsponsiblc íor acccpiing up io :o cookics
írom a wcb scr·cr. and ihcy should cach bc undcr +k in sizc. Cookics may
also spcciíy ihcir liícspan on ihc clicni's compuicr and domains ihc cookic's
iníormaiion should bc shown. Consuli ihc Cookic objcci's documcniaiion
íor morc dciails.
ja·ax.scr·lci.hiip.Cookic objcci's documcniaiion

How can I impIcmcnt a thrcad-safc !SP pagc?

You can makc your JSI ihrcad-saíc by ha·ing ihcm implcmcni ihc
SingIcThrcadModcI inicríacc.
Adding ihc dirccii·c docs ihis.

<%m pagc isThrcadSafc="faIsc" %>
Viihin your JSI pagc.
Viih ihis. insicad oí a singlc insiancc oí ihc Scr·lci gcncraicd íor your JSI
pagc loadcd in mcmory. you will ha·c N insianccs oí ihc Scr·lci loadcd and
iniiializcd. wiih ihc scr·icc mcihod oí cach insiancc cííccii·cly
synchronizcd. You can iypically conirol ihc numbcr oí insianccs (N) ihai
arc insianiiaicd íor all Scr·lcis implcmcniing S:nv}«Tn·«u(Mo(«} ihrough
ihc admin scrccn íor your JSI cnginc.

(FAQ) For Core & Advance Java
´jP(j´ ´jP(j´ ´jP(j´ ´jP(j´

Vhat Is Struts and Vhv ShouId I Carc?

A Strut is an applicaiion dc·clopmcni íramcwork ihai is dcsigncd íor and
uscd wiih ihc popular !eEE (Ja·a :. Enicrprisc Ediiion) plaiíorm. Ii cuis
iimc oui oí ihc dc·clopmcni proccss and makcs dc·clopcrs morc producii·c
by pro·iding ihcm a scrics oí iools and componcnis io build applicaiions
wiih. Ii is non-propriciary and works wiih ·iriually any J:EE-compliani
applicaiion scr·cr. Siruis íalls undcr ihc Jakaria subprojcci oí ihc Apachc
Soíiwarc Ioundaiion and comcs wiih an Opcn Sourcc liccnsc (mcaning ii
has no cosi and iis uscrs ha·c írcc acccss io all iis inicrnal sourcc codc).

In addiiion io hclping you work íasicr and ha·ing no cosi. Siruis also hclps
makc your cnd work producis bciicr. 1hc main rcason íor ihis. io quoic
írom Eric Raymond's "1hc Caihcdral and ihc Bazaar" (a classic book on
opcn sourcc dc·clopmcni). is ihai "(g)i·cn a largc cnough bcia-icsicr and
co-dc·clopcr basc. almosi c·cry problcm will bc characicrizcd quickly and
ihc íix ob·ious io somconc." In oihcr words. so many pcoplc arc using and
dc·cloping Siruis ihai bugs arc íound and gci íixcd quickly.

Arc Struts a Vcb AppIication 'Framcwork'?

1hc diciionary calls a íramcwork "A siruciurc íor supporiing or cnclosing
somcihing clsc. cspccially a skclcial suppori uscd as ihc basis íor somcihing
bcing consirucicd." 1his pcríccily dcscribcs Siruis÷a collcciion oí Ja·a
codc dcsigncd io hclp you build solid applicaiions whilc sa·ing iimc. Ii
pro·idcs ihc basic skclcion and plumbing. you íocus on ihc layoui and look
oí cach room.

Inicrcsiingly. ihc diciionary oíícrs an alicrnaii·c dcíiniiion oí a íramcwork.
"A sci oí assumpiions. conccpis. ·alucs. and praciiccs ihai consiiiuics a way
oí ·icwing rcaliiy." 1his dcscribcs Siruis as wcll÷ii's a way oí looking ai
ihings. Siruis sa·c you iimc by allowing you io ·icw complcx applicaiions
as a scrics oí basic componcnis. !:«u.. dcí:on C}u..«.. and Mo(«} compon«ní..

"... And Framcworks Arc Important Bccausc?"
Using a íramcwork mcans ihai you don'i ha·c io spcnd iimc building your
cniirc applicaiion. You can íocus on coding ihc busincss logic and ihc
prcscniaiion laycr oí ihc applicaiion÷noi ihc o·crhcad picccs likc íiguring
(FAQ) For Core & Advance Java
oui how io capiurc uscr inpui or íiguring oui how io gcncraic drop-down
boxcs on a Vcb pagc.

Using a íramcwork also hclps you cncodc bcsi praciiccs. 1hc íramcwork
dc·clopcrs ha·c pui a loi oí ihoughi inio ihc bcsi approachcs io applicaiion
building÷why rcin·cni ihis yoursclí'

Anoihcr bcncíii oí using a íramcwork is ihai ii allows your codc (ai lcasi in
ihc casc oí Siruis) io bc highly plaiíorm indcpcndcni. Ior cxamplc. ihc
samc Siruis codc should work undcr 1omcai on an old Vindows machinc
as runs using Vcblogic on Linux or Solaris in produciion. And ihis can bc
accomplishcd wiihoui c·cn rccompiling in many cascs÷ihc samc Vcb
applicaiion (or ". war" íilc) can simply bc copicd írom onc scr·cr io anoihcr.

Anoihcr cxircmcly imporiani bcncíii÷cspccially ií you'rc rclaii·cly ncw io
Vcb dc·clopmcni÷is ihai ii gi·cs you a placc io siari. Any dc·clopcr will
icll you ii's casicr io iakc a basic applicaiion and modiíy ii ihan ii is io build
somcihing írom scraich. 1his ícaiurc oí Siruis can sa·c you days or wccks
oí planning and dc·clopmcni.

1oday. I crcaic ·iriually noihing írom scraich. Almosi no onc who is an
cxpcricnccd dc·clopcr docs. In íaci. somc oí ihc grcaicsi succcsscs in
soíiwarc dc·clopmcni wcrc bascd on ihis cxaci idca. Ior cxamplc. in :¬¬:
whcn Linus 1or·alds bcgan building ihc opcraiing sysicm ihai ioday is
Linux. hc bcgan wiih ihc opcraiing sysicm Minix. Hc goi a copy oí ihc
Minix sourcc codc. lookcd ii o·cr in dciail. and uscd ii as ihc basis íor
Linux. And whilc ihc íirsi launch oí Linux coniaincd nonc oí ihc original
Minix codc. Linus surcly wcni íurihcr. íasicr bccausc hc had ii io siari

How Docs Struts Vork?

Struts arc bascd on ihc iimc-pro·cn ModcI-Vicw-ControIIcr (MVC)
dcsign paiicrn. 1hc MVC paiicrn is widcly rccognizcd as bcing among ihc
mosi wcll-dc·clopcd and maiurc dcsign paiicrns in usc. By using ihc MVC
dcsign paiicrn. proccssing is brokcn inio ihrcc disiinci scciions apily
namcd ihc Modcl. ihc Vicw. and ihc Conirollcr. 1hcsc arc dcscribcd in ihc
íollowing subscciions.

(FAQ) For Core & Advance Java
ModcI Componcnts
ModcI componcnts pro·idc a "modcl" oí ihc busincss logic or daia bchind
a Siruis program. Ior cxamplc. in a Siruis applicaiion ihai managcs
cusiomcr iníormaiion. ii may bc appropriaic io ha·c a "Cusiomcr" Modcl
componcni ihai pro·idcs program acccss io iníormaiion aboui cusiomcrs.

Ii's ·cry common íor Modcl componcnis io pro·idc inicríaccs io daiabascs
or back-cnd sysicms. Ior cxamplc. ií a Siruis applicaiion nccds io acccss
cmploycc iníormaiion ihai is kcpi in an cnicrprisc HR iníormaiion sysicm.
ii mighi bc appropriaic io dcsign an "Employcc" Modcl componcni ihai
acis as an inicríacc bciwccn ihc Siruis applicaiion and ihc HR iníormaiion

Modcl componcnis arc gcncrally siandard Ja·a classcs. 1hcrc is no
spcciíically rcquircd íormai íor a Modcl componcni. so ii may bc possiblc io
rcusc Ja·a codc wriiicn íor oihcr projccis.

Vicw Componcnts
Vicw componcnts arc ihosc picccs oí an applicaiion ihai prcscni
iníormaiion io uscrs and acccpi inpui. In Siruis applicaiions. ihcsc
corrcspond io Vcb pagcs.

Vicw componcnis arc uscd io display ihc iníormaiion pro·idcd by Modcl
componcnis. Ior cxamplc. ihc "Cusiomcr" Modcl componcni discusscd
abo·c would nccd a Vicw componcni io display iis iníormaiion. Usually.
ihcrc will onc or morc Vicw componcnis íor cach Vcb pagc in a Siruis

Vicw componcnis arc gcncrally buili using Ja·aScr·cr Iagc (JSI) íilcs.
Siruis pro·idc a largc numbcr oí "JSI Cusiom 1ags" (somciimcs rcícrrcd
io as Siruis 1ags). which cxicnd ihc normal capabiliiics oí JSI and simpliíy
ihc dc·clopmcni oí Vicw componcnis.

ControIIcr Componcnts
ControIIcr componcnts coordinaic acii·iiics in ihc applicaiion. 1his may
mcan iaking daia írom ihc uscr and updaiing a daiabasc ihrough a Modcl
componcni. or ii may mcan dcicciing an crror condiiion wiih a back-cnd
sysicm and dircciing ihc uscr ihrough spccial crror proccssing. Conirollcr
componcnis acccpi daia írom ihc uscrs. dccidc which Modcl componcnis
nccd io bc updaicd. and ihcn dccidc which Vicw componcni nccds io bc
callcd io display ihc rcsulis.
(FAQ) For Core & Advance Java

Onc oí ihc major coniribuiions oí Conirollcr componcnis is ihai ihcy allow
ihc dc·clopcr io rcmo·c much oí ihc crror handling logic írom ihc JSI
pagcs in ihcir applicaiion. (Aíicr all. ií crrors in proccssing occur. ihc
Conirollcr componcni íorwards io an crror-proccssing Vicw componcni.
noi ihc primary rcsulis Vicw componcni.) 1his can signiíicanily simpliíy
ihc logic in ihc pagcs and makc ihcm casicr io dc·clop and mainiain.

Conirollcr componcnis in Siruis arc Ja·a classcs and musi bc buili using
spcciíic rulcs. 1hcy arc usually rcícrrcd io as "Aciion classcs."

Bottom Linc Bcncfits of MVC

Rcmcmbcr ihc carlicr dcíiniiion ihai dcscribcd a Iramcwork as "A sci oí
assumpiions. conccpis. ·alucs. and praciiccs ihai consiiiuics a way oí
·icwing rcaliiy'" 1his is onc oí ihc mosi powcríul bcncíiis oí ihc MVC
paiicrn. Ii allows dc·clopcrs io ihink aboui (and dcsign) complcx
applicaiions as a scrics oí rclaii·cly simplc Modcl. Vicw. and Conirollcr
componcnis. ihis lcads io bciicr. morc consisicni. and morc casily
mainiainablc dcsigns. In addiiion. ii hclps a·oid ihc common piiíall oí
ha·ing cach dc·clopcr on a projcci choosc a diíícrcni approach íor his or
hcr work.
Casc Siudy. Righi Hand Managcr Soíiwarc
Vhcn Dcnnis Doublcday and oihcrs on his icam ai Righi Hand Managcr
Soíiwarc (hiip.//www.righihandmanagcr.com) wcrc c·aluaiing how ihcy
wanicd io do dc·clopmcni. ihcy dccidcd righi away ihai ihcy wanicd io usc
a íramcwork ihai was bascd on ihc MVC dcsign paiicrn. 1his dccision
camc aíicr ha·ing buili a numbcr oí applicaiions bascd on ihc J:EE
plaiíorm. On prc·ious projccis. Dcnnis says "Vc spcni a loi oí iimc
dc·cloping ihc rc-usablc íramcwork and noi our applicaiion."
In oihcr words. ihcy wanicd a íramcwork ihai allowcd ihcm io íocus on
building ihcir applicaiion wiihoui spcnding a loi oí iimc wriiing ihc codc
ihai organizcd and coordinaicd proccssing. Aíicr rc·icwing a numbcr oí
oihcr compciing íramcworks. ihcy sciilcd on Siruis íor a numbcr oí
rcasons÷somc icchnical and somc noi. 1hc Siruis icchnical ícaiurcs ihai
wcrc imporiani io ihcm wcrc.
Siruis pcríorm wcll.
Siruis ha·c a sound archiicciural modcl wiih a modcsi lcarning
Siruis arc ·cry solid and siablc.
(FAQ) For Core & Advance Java
Siruis ha·c a sirong sci oí cusiom iag librarics ihai simpliíy JSI
Siruis arc ·cry compciiii·c icchnically. bui io my mind ihc biggcsi
ad·aniagcs Siruis has o·cr compciing icchnologics arc praciical.
1hcsc ícaiurcs arc.
Ongoing dc·clopmcni by a largc numbcr oí commiiicd
Knowlcdgcablc and rcsponsi·c projcci lcadcrship
Gcncrally quick (and somciimcs ncar-insiani) problcm rcsoluiion ·ia
ihc Siruis mailing lisi or archi·cs
Acccss io sourcc codc
Sirong conncciion io and commiimcni io íuiurc inicgraiion wiih
íorward-looking icchnologics likc JS1L and Ja·a Scr·cr Iaccs
Incrcasing mind sharc ihai wc cxpcci will makc ii casicr io hirc ncw
dc·clopcrs alrcady íamiliar wiih ihc íramcwork"

So. ihc qucsiion ariscs. should you considcr adopiing Siruis'
Oí coursc. your answcr dcpcnds on your pariicular circumsianccs and
cn·ironmcni. bui hcrc arc somc criicria io considcr.
Arc you using ihc J:EE plaiíorm (ihai is. dc·cloping applicaiions
using J:EE-compliani scr·crs such as Vcblogic Scr·cr. Vcbsphcrc
Applicaiion Scr·cr. Jakaria 1omcai. JBoss. iIlanci. and so íorih)' Ií
ihc answcr io ihis is ycs. Siruis is likcly worih considcring.
Do your dc·clopcrs ha·c Ja·a cxpcriisc' Alihough ihis isn'i a "makc
or brcak" criicrion. ii hclps. Ií your dc·clopcrs don'i ha·c Ja·a
cxpcricncc bui you arc dcdicaicd io mo·ing io Ja·a anyway. Siruis
may aciually makc ihc iransiiion casicr.
Arc you building applicaiions ihai nccd io work in a Vcb browscr'
1his is ihc nichc ihai Siruis íills. Ií ihc answcr is no. Siruis isn'i íor
you. Unlcss you ha·c rcquircmcnis íor browscr-bascd applicaiion
dcli·cry. Siruis won'i add much ·aluc.
Arc you building applicaiions now ihai usc Ja·aScr·cr Iagcs (JSI)'
Ií so. you should dcíiniicly bc looking ai Siruis. Ii may incrcasc your
dc·clopcr producii·iiy signiíicanily.
Is your dc·clopmcni icam considcring building a "cusiom
íramcwork" íor building Vcb-bascd applicaiions' Ií so. ask ihcm io
jusiiíy noí using Siruis. Anyihing ihcy would build on ihcir own
would likcly noi undcrgo ncar ihc amouni oí icsiing and
dc·clopmcni ihai Siruis has. Vhilc ihcy may ha·c good rcasons íor
noi using ii. Siruis should dcíiniicly bc on ihcir radar.
(FAQ) For Core & Advance Java

1o summarizc. Struts is an applicaiion "Framcwork" íor building Vcb-
bascd applicaiions in Ja·a using ihc !eEE plaiíorm. Siruis makc dc·clopcrs
morc producii·c by gi·ing ihcm prc-buili componcnis io asscmblc
applicaiions írom. Siruis was buili using indusiry bcsi praciiccs including
ihc MVC dcsign paiicrn and ii can bc dcploycd in a widc rangc oí
Ií you arc using ihc Ja·a/J:EE plaiíorm÷and cspccially ií you arc
currcnily dc·cloping applicaiions using Ja·aScr·cr Iagcs (JSI)÷you
should bc considcring Siruis íor ihc dc·clopmcni oí browscr-bascd

(FAQ) For Core & Advance Java
´rL ´rL ´rL ´rL

Vhat is SOL?

Siruciurcd Oucry Languagc (SOL) is an intcrnationaI standard, which is
an EngIish Iikc non-proccduraI Ianguagc.
SOL is an ANSI siandard. which is uscd io rciric·c and updaic daia in a
1hc purposc oí SOL is io pro·idc an inicríacc io a rclaiional daiabasc by
using SOL siaicmcnis as insiruciions io ihc daiabasc.

Vhat is cmbcddcd SOL? And statc it`s uscs.

1hc icrm cmbcddcd SOL¨ rcícrs io SOL siaicmcnis placcd wiihin an
applicaiion program is callcd hosi program and ihc languagc ihai is wriiicn
is callcd hosi languagc.
1hc uscs oí cmbcddcd SOL arc.
• Ior largc and complcx qucrics and applicaiions SOL¨Ilus will noi bc
cnough and uscíul.
• Embcddcd SOL pro·idcs an cn·ironmcni io usc high-lc·cl languagcs
io allow io conncci daiabasc and io run SOL commands.
• Embcddcd SOL is a mcihod oí combining ihc daiabasc manipulaiion
capabiliiics oí SOL and a high lc·cl languagc likc C/C++ or ja·a.
• Embcddcd SOL allows io uscr io cxccuic an SOL siaicmcni írom an
applicaiion program.

Vhat is DBMS?

A Databasc Managcmcnt Svstcm (DBMS) is a collcciion oí programs
ihai cnablcs uscrs io crcaic and mainiain daiabasc.
DBMS has · pillars.
Dcfining: Dcíining a daiabasc in·ol·cs spcciíying ihc daia iypcs. siruciurc
and consirainis íor ihc daia io bc siorcd in ihc daiabasc.
Constructing: Consiruciing ihc daiabasc is ihc proccss oí sioring ihc daia
iisclí or sum sioragc mcdium ihai is conirollcd by DBMS.
ManipuIating: Manipulaiing a daiabasc includcs such íunciions as
qucrying ihc daiabasc io rciric·c spcciíic daia. updaiing daiabasc io rcílcci
changcs in ihc miniworld. and gcncraiing rcporis írom ihc daia.

(FAQ) For Core & Advance Java
Vhat is RDBMS?

A RcIationaI Databasc Managcmcnt Svstcm siorcs iníormaiion in ihc
íorm oí iablcs and ihcn links or rclaics ihosc iablcs io pro·idc answcr io
uscrs` di·crsc qucsiions.

Vhat is thc diffcrcncc bctwccn DBMS and RDBMS?

1hcrc is a big diíícrcncc bciwccn DBMS and RDBMS.
• Ior a DBMS io bc iiilcd as a RDBMS. ii musi íollow ihc Boycc
Codd's rulcs (ailcasi o oí ::)
• Inicgriiy rulcs cannoi bc applicd on a DBMS (Viihoui a ·cry
complcx piccc oí codc) whilc an RDBMS pro·idcs Irimary/Iorcign
Kcy consirainis íor ihis purposc. Viih ihc hclp oí ihcsc consirainis
wc arc ablc io csiablish a rclaiion bciwccn : or morc iablcs or c·cn
daiabascs and ihus ihis known as Rclaiional DBMS.
• DBMS arc íor smallcr organizaiions wiih small amouni oí daia.
whcrc sccuriiy oí ihc daia is noi oí major conccrn. Also DBMS arc
noi ncccssarily clicni scr·cr bascd sysicms. Viih DBMS. onc can
dc·clop a complcic applicaiion. siariing írom proccssing inpuis io
gcncraiing ouipui. Vhcrc as
RDBMS arc dcsigncd io iakc carc oí largc amounis oí daia and also
ihc sccuriiy oí ihis daia. 1hcy arc also clicni scr·cr bascd sysicms.
1o crcaic a complcic applicaiion. onc rcquircs clicni soíiwarc likc
VB. Dc·clopcr :ooo and JAVA

Vhat is Databasc?

A Databasc is ihc collcciion oí iablcs. ·icws. indcxcs. iriggcrs. siorc
proccdurcs. and oihcr objccis.

Vhat arc TabIcs?

TabIcs arc ihc corc oí Daiabasc. 1hcy siorc ihc daia groupcd in ihc íorm
oí rocs and columns likc sprcadshcci. Each rows rcprcscnis a rccord. and
cach columns is an aiiribuic or íicld.

(FAQ) For Core & Advance Java
Vhat is RcIation?

A TabIc callcd an Entitv¨ or RcIation¨ which is collcciion oí columns
and rows

Vhat arc Primarv kcvs, Forcign kcv, Rcfcrcncc Kcv, Uniquc Kcv?

Primarv Kcv: Onc or morc columns oí ihc iablc ihai idcniiíy ihc row
cxclusi·cly insidc ihc iablc íorm ihc primary kcy.
Forcign Kcv: 1hc íorcign kcy is a column or a combinaiion oí columns
uscd io csiablish a link bciwccn iwo iablcs.
Rcfcrcncc Kcv:
Uniquc Kcv: In addiiion io primary kcy. wc can crcaic consirainis oí iypc
UNIOUE in oihcr columns. Vhcn ihis iypc oí consirainis is crcaicd. ihc
SOL Scr·cr crcaics an cxclusi·c indcx in ordcr io assurc ihai daia is noi

Vhat NormaIization? And what arc i
NF, e
NF, and srd NF with
NormaIization: Ao·mu}::uí:on a logical daiabasc dcsign in·ol·cs using
íormal mcihods io scparaic ihc daia inio muliiplc. rclaicd iablcs. A grcaicr
numbcr oí narrow iablcs (wiih ícwcr columns) arc characicrisiic oí a
normalizcd daiabasc. A ícw widc iablcs (wiih morc columns) arc
characicrisiic oí a non-normalizcd daiabasc.
Normalizaiion is a scrics oí rulcs ihai wc apply on ihc prcliminary daiabasc
dcsign io climinaic rcdundancy and inconsisicncy. Simply siaicd.
normalizaiion is ihc proccss oí rcmo·ing rcdundani daia írom rclaiional
iablcs by spliiiing a rclaiional iablc inio smallcr iablcs. 1hc ncw iablcs can
bc rccombincd by a naiural join (using SOL) io rccrcaic ihc original iablc
wiihoui crcaiing any spurious or rcdundani daia. 1hough ihcrc can bc
morc siagcs io normalizing a daiabasc. ihc basic proccss in·ol·cs ihrcc
1hc ad·aniagcs oí Normalizaiion arc.
• Iasicr soriing and Indcx crcaiion.
• A largcr numbcr oí clusicrcd Indcxcs.
(FAQ) For Core & Advance Java
• Narrowcr and morc compaci Indcxcs.
• Icwcr Indcxcs pcr iablc. which impro·cs ihc pcríormancc oí
INSER1. UIDA1E. and DELE1E siaicmcnis.
• Icwcr null ·alucs and lcss opporiuniiy íor inconsisicncy. which
incrcasc daiabasc compacincss.
Notc: As normalizaiion incrcascs. so do ihc numbcr and complcxiiy oí
joins rcquircd rciric·ing daia. 1oo many complcx rclaiional joins bciwccn
ioo many iablcs can hindcr pcríormancc. Rcasonablc normalizaiion oíicn
includcs ícw rcgularly cxccuicd qucrics ihai usc joins in·ol·ing morc ihan
íour iablcs.
NF: Rclaiion should ha·c no non-aiomic i.c. (ii should ha·c simplc.
indi·isiblc ·alucs) aiiribuics or ncsicd rclaiions.
Notc: 1hc ·alucs oí any aiiribuic in ihc iuplc musi bc a singlc ·aluc írom
ihc domain oí ihai aiiribuics.
NF: Ior rclaiions whcrc primary kcy consirains muliiplc aiiribuics. no
non-kcy aiiribuic should bc íunciionally dcpcndcni on a pari oí primary
NF: Rclaiion should noi ha·c a non-kcy aiiribuics (or by a sci oí non-
kcy aiiribuics.) i.c. ihcrc should bc no iransiii·c dcpcndcncy oí a non-kcy
aiiribuic on ihc primary kcy.
Propcrtv_ID¬(pk) Countrv_Namc(ck) Lot¬(ck) Arca(ck) Pricc Ta×_Ratc
Propcrtv_ID¬(pk) Countrv_Namc Lot¬ Arca Pricc
(FAQ) For Core & Advance Java
Countrv_Namc (pk) Ta×_Ratc
Propcrtv_ID¬(pk) Countrv_Namc Lot¬ Arca
Arca Pricc



(pk) : primarv kcv (ck) : candidatc kcv.

Vhat arc Vicws?

A Vicw is a ·iriual iablc wiih iis conicnis dcíincd by a qucry io ihc
daiabasc. 1hc Vicw is noi a physical iablc. bui a sci oí insiruciions ihai
rciurns a sci oí daia.

Vhat arc Storc Proccdurcs?

A prccompilcd collcciion oí SOL siaicmcnis and opiional conirol-oí-ílow
siaicmcnis siorcd undcr a namc and proccsscd as a unii. Siorcd proccdurcs
arc siorcd in an SOL daiabasc such as Microsoíi SOL Scr·cr. usually on a
scr·cr. can bc run wiih onc call írom an applicaiion. and allow uscr-
dcclarcd ·ariablcs. condiiional cxccuiion. and oihcr powcríul programming
(FAQ) For Core & Advance Java

Vhat arc Functions?

A SOL Iunciion is a rouiinc ihai pcríorms a spcciíic opcraiion and
rciurns a rcsuli. Ii is similar io proccdurc. cxccpi ihai proccdurc docs
noi rciurn any ·alucs. A Iunciion can rccci·c argumcnis ihai will bc
uscd in proccssing.
1hc Syniax oí íunciion is.

Iuncnamc(arg:. arg:. ....argN)
Vhcrc Iuncnamc is ihc namc oí ihc íunciion and arg: is ihc opiional
íicld io ihc íunciion.

Vhat arc Triggcrs?

A Triggcr is a siorcd proccdurc ihai is auiomaiically cxccuicd whcn daia
in ihc iablc changcs duc io ihc cxccuiion oí a SOL INSER1. UIDA1E or
DELE1E command.
Uscs: Onc oí ihc mosi common uscs is io íorcc limiiaiions ihai arc
morc complcx ihan ihc oncc allowcd ihrough ihc CHECK consirainis.
which limiis ihc iníormaiion inscricd in ihc column.

Vhat arc thc Iimitations of Triggcrs?

1hc limiiaiions oí iriggcrs arc.
• A iriggcr can cxccuic ihc commands coniaincd in iis body or acii·aic
siorcd proccdurcs and oihcr iriggcrs in ordcr io cxccuic ccriain
• Any SE1 command can bc spcciíicd insidc a iriggcr. Ii rcmains
acii·c during ihc cxccuiion oí ihc iriggcr.
• You cannoi crcaic a iriggcr íor a ·icw. Howc·cr. whcn ihc ·icw is
uscd. ihc iriggcrs oí ihc basc iablc normally arc acii·aicd.
• Vhcn a iriggcr is cxccuicd. ihc rcsulis arc rciurncd io ihc
applicaiion ihai callcd ii. 1o a·oid rciurning ihc rcsulis or cxccuic
ihc aiiribuiion oí conicnis io ihc ·ariablc.
(FAQ) For Core & Advance Java
• A iriggcr oí ihc dclcic iypc cannoi inicrccpi ihc 1RUNCA1E
1ABLE command. 1hc VRI1E1EX1 command docs noi acii·aic a

Vhat is Indc×ing and how it work?

Indc×: An Indc× is íormcd by kcys bascd on ihc conicnis oí onc or morc
columns oí ihc iablc.
1hc SOL Scr·cr uscs : iypcs oí Indcxcs.

• CIustcrcd Indc×: A C}u.í«· 1n(«r organizcs ihc rows oí ihc iablcs in
a pariicular ordcr. such an alphabciical or numcrical.
E.g. In an Indcx ihai is ordcrcd alphabciically. ihc rccord oí auihor
Villiams comcs bcíorc Yokomoio. 1his iypc oí Indcx is pcrícci whcn
you don`i ha·c much iablc updaiing io do. sincc whcn ihc conicnis oí
ihc kcy íicld arc changcd. ihc iablc rcorganizcd.
NOTE: Thcrc can onIv bc onc CIustcrcd Indc× pcr tabIc.

• Non-CIustcrcd Indc×: A Aon-C}u.í«·«( 1n(«r has a siruciurc
scparaic írom iablc. 1hc physical ordcr oí ihc iablc`s row docs noi
íollow ihc ordcr oí Indcx íilc. 1hc Non-Clusicrcd Indcx is similar io
ihc Indcx íound ai ihc cnd oí ihc book. 1hc daia (iablc`s rows) arc
pui in onc placc and ihc Indcxcd is locaicd on anoihcr pagc. 1hc
Indcx`s íilc`s daia arc siorcd in ordcr oí ihcir ·alucs. 1hc iníormaiion
oí ihc iablcs is siorcd in anoihcr ordcr ihai docs noi íollow ihc samc
scqucncc oí ihc Non-Clusicrcd Indcx íilc.
Notc: Thcrc can bc scvcraI Indc×cs of this tvpc pcr tabIc.

Vhat is FuII-Tc×t Indc×cs?

1his spccial iypc oí Indcx allows ihc cxccuiion oí qucrics bascd on columns
wiih conicnis oí ihc Varchar and 1cxi iypcs. 1hcsc arc ihc main
diíícrcnccs bciwccn a normal indcx and a íull-icxi indcx.

Normal Iull-1cxi Indcx
Crcaicd and dclcicd ihrough SOL
Crcaicd and dclcicd ihrough ihc usc
oí Siorc proccdurc
Can bc sc·cral indcxcs in cach iablc. Can bc only onc íull-icxi indcx in
(FAQ) For Core & Advance Java
cach iablc.
Auiomaiically updaicd whcn ihc
íiclds oí ihc iablc changc
Updaicd only whcn rcqucsicd.

Vhat is FiII Factor?

1hc abiliiy io crcaic íilcs oí Indcxcs wiih room íor ihc inscriion oí ncw
kcys. wiihoui ha·ing io physically rcorganizc all ihc Indcx íilcs is known
as FiII Factor.

Vhat arc Constraints? And how manv tvpcs of Constraints arc thcrc?

Constraints arc a way io cnsurc ihc inicgriiy oí a daiabasc ihrough ihc
íilicring oí iníormaiion inscricd in a iablc`s column. Consirainis arc basic
mcchanism io cnsurc ihc inicgriiy oí a íicld.
1hcrc arc íi·c iypcs oí Consirainis.
• NOT NULL: Ii spcciíics ihai a column cannoi acccpi ihc ·aluc Null.
• CHECK: Ii limiis ihc ·aluc ihai can bc pui in ihc column by icsiing
íor condiiion.
• UNIOUE: Ii íorccs ihc columns io ha·c cxclusi·c ·alucs. in oihcr
words. in a column ihai is dcsignaicd UNIOUE. ihcrc cannoi bc iwo
rccords wiih ihc samc conicnis. 1hc Uniquc column can ha·c null
·aluc. bui only onc null ·aluc.
• PRIMARY KEY: Ii crcaics ihc Primarv kcv oí ihc iablc. a column.
or combinaiion oí columns wiih ·alucs ihai musi bc cxclusi·c insidc
ihc iablc in ordcr io idcniiíy ihc row. A column wiih primary kcy
cannoi ha·c Null ·aluc aiiribuicd io ii.
• CANDIDATE KEY: Vhcn ihc primary kcy is íormcd by morc ihan
onc column. jusi onc oí ihcm will bc considcrcd ihc primary kcy. 1hc
oihcr columns ihai íorm ihc kcy arc callcd Candidatc kcvs.
• FOREIGN KEY: Ii idcniiíics ihc rclaiionship bciwccn ihc iablcs.
1his is ihc íicld in ihc currcni iablc ihai poinis io a kcy íicld in
anoihcr iablc. 1hc usc oí ihc Forcign kcv prc·cnis ihc dclciion oí
rows in ihc currcni iablc whcn ihcrc arc rcícrcnccs io kcy íiclds oí
cxicrnal iablcs.
NOTE: Consirainis ha·c a prioriiy o·cr iriggcrs. rulcs. and dcíauli ·alucs.

(FAQ) For Core & Advance Java
Givc thc dcfinition of VHERE, GROUP BY, HAVING, ORDER BY

• VHERE: Ii íilicrs ihc linc ihai will bc displaycd by ihc qucry.
Viihoui ihis clausc all ihc linc will bc displaycd.
• GROUP BY: Ii groups ihc sclccicd linc bascd on ihc ·aluc oí
<cxprcssion> íor cach linc. and rciurns only onc linc wiih
iníormaiion íor cach group.
• HAVING: Ii íilicrs ihc lincs rciurncd. displaying jusi ihosc ihai
ha·c an <cxprcssion> ihai c·aluaics io 1RUE. Normally ii is uscd
along wiih GRO1P B1 io íilicr ihc group`s lincs.
• ORDER BY: column namc. Ii ordcrs ihc lincs rciurncd by sclcci
command by ihc column spcciíicd.
• COMPUTE: Ii gcncraics a ·aluc rcsuliing íorm ihc usc oí a íunciion
applicd io ihc proccssing oí a subgroup. 1hc COMP1T1 clausc
columns musi appcar in ihc lisi oí sclccicd íiclds.
• UNION: Ii rciurns all ihc cxclusi·c lincs sclccicd by any oí ihc
• UNION ALL: Ii rciurns all ihc lincs sclccicd by ihc qucrics.
including ihc duplicaics.
• MINUS: Ii sclccis all ihc lincs sclccicd by ihc íirsi qucry bui noi by
ihc sccond qucry.
• CoIumn_namc: Ii is an cxprcssion. gcncrally a íicld by which ihc
lincs will bc ordcrcd. ASC is ihc dcíauli and uscs ihc asccnding
• DISTINCT: Ii spcciíics ihai íunciion aci only on ihc rows wiih ihc
diíícrcni ·alucs.
• IDENTITY: Ii indicaics ihai ihc íicld will ha·c only onc ·aluc ihai
will bc auiomaiically gcncraicd by ihc sysicm c·cry iimc a ncw
rccord is includcd.

Vhat is !DBC?

!DBC (Ja·a Daiabasc Connccii·iiy) API (Applicaiion Irogram Inicríacc)
is a Ja·a AII íor acccssing ·iriually any kind oí daia sourcc.
(FAQ) For Core & Advance Java
1hc JDBC AII makcs ii casy io scnd SOL siaicmcnis io rclaiional daiabasc
sysicms and supporis all dialcciics oí SOL.

Vhat arc thc diffcrcnt kinds of databasc managcmcnt svstcms?

1hc diíícrcni iypcs oí DMS arc.
• Hicrarchical
• Nciwork
• Rclaiional
• Objcci
• Objcci-Rclaiional
Vhilc hicrarchical and nciwork daiabascs arc oldcr ihan rclaiional
daiabascs. objcci and objcci - rclaiional daiabascs arc oí rclaii·cly rcccni

Vhat is an Entitv-RcIationship (E-R) diagram?

A uscíul icchniquc in dcsigning your daiabasc is io draw a piciurc oí your
iablcs showing ihc rclaiionship bciwccn iablcs. 1his graphical display oí a
daiabasc is callcd an Entitv-RcIationship or an E-R diagram.

Vhat is rcfcrcntiaI intcgritv?

A ícaiurc pro·idcd by RDBMS ihai prc·cnis uscrs or applicaiions írom
cnicring inconsisicni daia. Mosi RDBMS ha·c ·arious rcícrcniial inicgriiy
rulcs ihai you can apply whcn you crcaic a rclaiionship bciwccn iwo iablcs.
1hcsc rulcs hclp in prcscr·ing ihc inicgriiy oí ihc daia. In simplc icrms
rcícrcniial inicgriiy consirainis makc surc ihai ihcrc arc no orphan
Lci us say you ha·c an 'cmploycc' iablc ihai has a íorcign kcy ihai poinis io
'dcparimcni_id'. which is a kcy in ihc 'dcparimcni' iablc. Rcícrcniial
inicgriiy will prc·cni you írom dclciing a dcparimcni ihai has cmployccs
assigncd io ii. 1his is known as a rcsiriciion. Ií you arc dcaling wiih 'ordcr'
iablc and muliiplc iicms coniaincd in an ordcr. ii is possiblc íor you sci ii
up such a way ihai all iicms in an ordcr gci dclcicd whcn you dclcic an
ordcr. 1his is known as a cascading dclcic.
Vhat arc thc thrcc normaI forms?

First normaI form: Rcmo·c rcpcaiing groups.
(FAQ) For Core & Advance Java
Ií. in ihc cmploycc iablc. you ha·c a column namc_oí_childrcn. ihis will
ha·c morc ihan onc ·aluc íor somc oí ihc cmployccs.

Sccond normaI form: Rcmo·c columns ihai do noi dcpcnd on ihc wholc
Ií. in ihc iicm iablc. you ha·c somc aiiribuics oí ihc ordcr. ihcn ii will
dcpcnd on only pari oí ihc kcy (namcly. ihc ordcr_numbcr).

Third normaI form: Rcmo·c columns ihai do noi dcpcnd only on ihc kcy.
Ií. in ihc ordcr iablc. you ha·c somc aiiribuics oí ihc iicm. ihcn ii will noi
dcpcnd only on ihc kcy (ii will dcpcnd on ordcr_numbcr and
Hcrc is an casy way io rcmcmbcr ihis.
CoIumns must dcpcnd on thc kcv, thc whoIc kcv and nothing but thc
Vhai do you do wiih all ihosc columns ihai you rcmo·cd' Makc ihcm inio
scparaic iablcs.
How docs SOL statcmcnts arc c×ccutcd in !ava?
SOL siaicmcnis arc cxccuicd in Ja·a by crcaiing a
java.sqI.Stamcmcnt o|]«cí using conncciion`s CrcaicSiaicmcni( )
E.g. public Siaicmcni crcaicSiaicmcni( ) ihrows SOLExccpiion
ihcrc arc : mcihods associaicd wiih crcaicSiaicmcni()
c×ccutcOucrv( )
1hosc ihai qucry a daiabasc and rciurns R«.u}íS«í objcci
corrcsponding io ihc rcsuli oí daiabasc qucry. 1hcsc qucry arc
cxccuicd using ihc cxccuicOucry() mcihod.
c×ccutcUpdatc( )
1hosc ihai crcaic. modiíy. or drop iablcs and inscri or dclcic daia.
1his iypcs uscs ihc cxccuicUpdaic( ) mcihod
1his mcihod rciurns a Siaicmcni objcci. which is commonly uscd íor
cxccuiing basic SOL siaicmcnis againsi ihc daiabasc.
(FAQ) For Core & Advance Java
How manv tvpcs of statcmcnts arc thcrc in SOL?
1hcrc arc · iypcs oí SOL siaicmcnis in SOL ihai arc prcdominanily usc in
i. Statcmcnt Objcct:
Statcmcnt Objccts arc runiimc objccis. which cxccuic and
Inscri/Updaic/Dclcic ihc ·alucs in ihc daiabasc ai runiimc.
1hcy do noi kccp ihc cxccuiion paih in ihc mcmory.
1hcy arc slow in daiabasc updaiing.
e. PrcparcdStatcmcnt:

1hc PrcparcdStatcmcnt arc prccompilcd oncc inio mcmory and
subscqucnily rccyclcs ihc Siring io bc placcd in daiabasc.
1hc IrcparcdSiaicmcni can bc paramcicrizcd and quickly iicraicd
o·cr. 1his is idcal ií similar SOL siaicmcni nccds io bc cxccuicd
muliiplc iimcs.
Ircparcd Siaicmcni is íasi in daiabasc updaiing.

s. CaIIabIcStatcmcnt:
CaIIabIcStatcmcnt inicríacc is usc io call ihc daiabasc Siorc Iroccdurc
Docs thc !DBC-ODBC Bridgc support muItipIc concurrcnt opcn
statcmcnts pcr conncction?
No. You can opcn only onc Siaicmcni objcci pcr conncciion whcn you arc
using ihc JDBC-ODBC Bridgc.

Is thc !DBC-ODBC Bridgc muIti-thrcadcd?
No. 1hc JDBC-ODBC Bridgc docs noi suppori concurrcni acccss írom
diíícrcni ihrcads. 1hc JDBC-ODBC Bridgc uscs synchronizcd mcihods io
scrializc all oí ihc calls ihai ii makcs io ODBC.
(FAQ) For Core & Advance Java

Vhat is Conncction pooIing? Is it advantagcous?

A Conncction pooIing is a mcihodology oí rcusing opcn conncciions.
Aíicr íinishing oí a iransaciion ihc conncciion is noi closcd. bui is pui io
ihc pool. and whcn a uscr rcqucsis a daiabasc conncciion ihc ncxi iimc. hc
gcis a poolcd conncciion insicad oí a ncwly opcncd onc. 1his grcaily
rcduccs ihc iimc io pcríorm singlc daiabasc opcraiions. bccausc oí a·oiding
hca·ywcighi opcn/closc opcraiions. Ii's rccommcndcd io usc daiabasc
conncciion pooling as widcly as possiblc.

Mcthod and cIass uscd for Conncction pooIing?

!DBC pro·idcs an inicríacc. which musi bc implcmcnicd by a JDBC dri·cr
supporiing conncciion pooling. 1his inicríacc is callcd
ConncciionIoolDaiaSourcc. Mosi cxisiing JDBC dri·crs (DaiaSourcc
insianccs) implcmcni ihis inicríacc. Ii pro·idcs ihc íollowing mcihods íor
obiaining IoolcdConncciions.
IoolcdConncciion gciIoolcdConncciion () ihrows SOLExccpiion.
IoolcdConncciion gciIoolcdConncciion (Siring uscr. Siring password)
ihrows SOLExccpiion.
Such conncciions can bc closcd by issuing conncciion.closc(). bui ii's only a
logical closing. noi physical. Ihysically. no conncciion is closcd. ii's jusi
rciurncd io ihc pool.

Vhat is RcpIication?

RcpIication is ·cry imporiani icchnology in ihc corporaic daiabasc
cn·ironmcni. Rcplicaiion cnablcs you io disiribuic daia and siorcd
proccdurc ihroughoui ihc organizaiion. 1hc daia in ihc daiabasc can bc
duplicaicd and copicd io sc·cral placcs insidc ihc company. 1hcsc
disiribuicd daiabascs can bc synchronizcd io always ha·c ihc samc ·aluc.
such as a pricc lisi synchronizcd across all branchcs.

Vhat is Data Varchousing?

(FAQ) For Core & Advance Java
A daia warchousing can hclp ihc dccision suppori applicaiions and ihc
onlinc analyiical proccssing (OLAI). bccausc ii pro·idcs daia ihai arc.
• ConsoIidatcd and consistcnt: A Daia warchousc consolidaics ihc
opcraiional daia írom a ·ariciy oí sourccs wiih consisicni
con·cniions oí naming. mcasurcs. physical aiiribuics. and scmaniics.
• Oricntcd to thc subjcct: 1hc opcraiional daia sourccs oí an
organizaiion icnds io coniain a grcai amouni oí daia aboui sc·cral
busincss-rclaicd íunciions. such as clicni rccords. iníormaiion aboui
producis. and so on.
• HistoricaI Data: 1hc OL1I sysicm daia rcprcscnis ihc currcni
·aluc ai any momcni in iimc. 1hc OL1I sysicm oíicn coniains only
currcni daia. Ior c.g. ihc ordcr cniry applicaiion always shows ihc
currcni ·aluc oí ihc in·cniory. ii docs noi show ihc in·cniory írom
ycsicrday or iwo hours ago. Any íurihcr qucry io ihc io ihc
in·cniory can rciurn a diíícrcni answcrs
• Rcad OnIv: As ihc daia siorcd in daia warchousc rcprcscnis a poini
in iimc. dclciions. inscriions. and updaics (cxccpi íor ihosc in·ol·cd
in ihc daia loading proccss) do noi apply io a daia warchousc. Aíicr
ihc daia is mo·cd io ihc daia warchousc. ii gcncrally docs noi
changc. unlcss ii is incorrcci.

(FAQ) For Core & Advance Java
£wj£PPP1´£ !/\/P£/w £wj£PPP1´£ !/\/P£/w £wj£PPP1´£ !/\/P£/w £wj£PPP1´£ !/\/P£/w

Vhat arc Entitv Bcan and Scssion Bcan?
An scssion bcan is a non-pcrsisicni objcci ihai implcmcnis somc busincss
logic running on ihc scr·cr. Onc way io ihink oí a scssion objcci is as a
logical cxicnsion oí ihc clicni program ihai runs on ihc scr·cr.
An cntitv bcan is a componcni ihai rcprcscnis an objcci-oricnicd ·icw oí
somc cniiiics siorcd in a pcrsisicni sioragc. such as a daiabasc. or cniiiics
ihai arc implcmcnicd by an cxisiing cnicrprisc applicaiion.

Can a Scssion Bcan bc dcfincd without cjbCrcatc() mcthod?

1hc cjbCrcaic() mcihods is pari oí ihc bcan's liíccyclc. so. ihc compilcr will
noi rciurn an crror bccausc ihcrc is no cjbCrcaic() mcihod.

Howc·cr. ihc J:EE spcc is cxplicii.
• 1hc homc inicríacc oí a Siaiclcss Scssion Bcan must ha·c a singlc
crcaic() mcihod wiih no argumcnis.
whilc ihc scssion bcan class must coniain cxacily onc cjbCrcaic()
mcihod. also wiihoui argumcnis.
• Siaicíul Scssion Bcans can ha·c argumcnis (morc ihan onc crcaic

Vhat arc thc mcthods of Entitv Bcan?

i. crcatc mcthods. 1hcy look likc ihis. EniiiyBcanClass
cjbCrcaicXXX(paramcicrs). whcrc EniiiyBcanClass is an Eniiiy Bcan you
arc irying io insianiiaic. cjbCrcaicXXX (paramcicrs) mcihods arc uscd íor
crcaiing Eniiiy Bcan insianccs according io ihc paramcicrs spcciíicd and io
somc programmcr-dcíincd condiiions. Implcmcniaiion oí crcaic () mcihods
oí homc inicríacc is ncccssary only in Eniiiy Bcans wiih Bcan-Managcd
e. findcr mcthods. Each íindcr mcihod is uscd íor íinding Eniiiy Bcans
(singlc objccis or a collcciion oí objccis) wiihin ihc homc. 1hcrc is a
mcihod uscd íor íinding objccis by ihc uniquc idcniiíicr (a P·:mu·y K«y) ÷
íindByIrimaryKcy (IrimaryKcyClass). 1his mcihod rciurns a singlc
Eniiiy Bcan. whcrcas mosi oihcr íindcr mcihods rciurn a Collcciion oí
rcmoic inicríaccs.
(FAQ) For Core & Advance Java
s. rcmovc mcthods. 1hcsc mcihods (you may ha·c up io : rcmo·c
mcihods. or don'i ha·c ihcm ai all) allow ihc clicni io physically rcmo·c
Eniiiy Bcans. You can rcmo·c ihcsc bcans by spcciíying ciihcr Handlc or a
Irimary Kcy íor ihc Eniiiy Bcan.
4. homc mcthods. 1hcsc mcihods arc dcsigncd and implcmcnicd by a
dc·clopcr. and EJB spcciíicaiion docsn'i ha·c any rcquircmcnis íor ihcm
cxccpi ihc nccd io ihrow a RcmoicExccpiion is cach homc mcihod.

Vhat arc thc diffcrcnccs bctwccn !avaBcans and Entcrprisc

!avaBcans wcrc originally dcsigncd íor uscr-inicríacc and clicni-sidc iasks.
1o allow íor scr·cr-sidc. componcni-bascd dc·clopmcni in Ja·a. Sun has
1hc Entcrprisc !avaBcans (EJB) spcciíicaiion ihai adds scr·cr-sidc
ícaiurcs such as 1ransaciions. Icrsisicncc. and Scalabiliiy.

Vhat is bcan-managcd transaction?

In a bcan managcd iransaciion ihc codc in ihc scssion or mcssagc-dri·cn
bcan cxpliciily marks ihc boundarics oí ihc iransaciion. An cniiiy bcan
cannoi ha·c bcan-managcd iransaciions. ii musi usc coniaincr-managcd
iransaciions insicad. Alihough bcans wiih coniaincr-managcd iransaciions
rcquirc lcss coding. ihcy ha·c onc limiiaiion. Vhcn a mcihod is cxccuiing.
ii can bc associaicd wiih ciihcr a singlc iransaciion. whcn a mcihod is
cxccuiing. ii can bc associaicd wiih ciihcr a singlc iransaciion or no
iransaciion ai all. Ií ihis limiiaiion will makc coding your bcan diííiculi. you
should considcr using bcan-managcd iransaciions.

1hc íollowing pscudocodc illusiraics ihc kind oí íinc-graincd conirol you
can obiain wiih bcan-managcd iransaciions. By chccking ·arious
condiiions. ihc pscudocodc dccidcs whcihcr io siari or siop diíícrcni
iransaciions wiihin ihc busincss mcihod.

ií (condiiion-x)
commii iransaciion
clsc ií (condiiion-y)
updaic iablc-b
commii iransaciion
(FAQ) For Core & Advance Java
rollback iransaciion
bcgin iransaciion
updaic iablc-c
commii iransaciion

Vhcn coding a bcan-managcd iransaciion íor scssion or mcssagc-dri·cn
bcans. you musi dccidc whcihcr io usc !DBC or !TA iransaciions. 1hc
scciions ihai íollow discuss boih iypcs oí iransaciions.

Vhat arc thc scrviccs providcd bv thc containcr?

1hcrc arc o Scr·iccs pro·idc by EJB Coniaincr ihcy arc.
• Concurrcncv
• Transactions
• Pcrsistcncc
• Distributcd Objccts
• Naming and
• Sccuritv

How do Entcrprisc !avaBcans comparc to Microsoft's Transaction

In many ways ihc Entcrprisc !avaBcans archiicciurc compcics dirccily
wiih Microsoft's Transaction Scrvcr (M1S). Boih M1S and scr·cr
applicaiions ihai coníorm io ihc EJB spcciíicaiion pro·idc similar
íunciionaliiy íor COM-bascd Acii·cX conirols and Ja·a-bascd
componcnis. rcspccii·cly.

Vhat is "swizzIc", as in "SwizzIc this objcct?"

Ii mcans scrializc. 1o swizzIc an objcci is io rccursi·cly scrializc or ílaiicn
composcd objccis.

(FAQ) For Core & Advance Java

How docs StatcfuI Scssion bcan storc its statc?

Iirsi oí all. a StatcfuI Scssion bcan is a Scssion Bcan insiancc coniaining
somc con·crsaiional siaic (u ·«.u}í oJ (uíu :ní«·cnunv« o· p·oc«..:nv). which
nccds io bc rciaincd across mcihods and iransaciions.
A Siaicíul Scssion bcan siorcs iis siaic in ihc insiancc's íiclds and objccis
rcícrrcd io írom ihcsc íiclds. 1hcsc íiclds may coniain somc up-io-iimc
iníormaiion as opcn sockcis or daiabasc conncciions.

Docs StatcfuI Scssion bcan support instancc pooIing?

Siaicíul Scssion Bcan conccpiually docsn'i ha·c insiancc pooling.

Vhv docs StatcIcss Scssion bcan not storc its statc cvcn though it has
cjbActivatc () and cjbPassivatc ()?

1o answcr ihis qucsiion you nccd io rcmcmbcr whai a StatcIcss Scssion
bcan is. Ii's a Scssion Bcan insiancc NO1 coniaining any con·crsaiional
siaic. Such bcans arc uscd mosily íor pcríorming somc aiomic opcraiions.
bccausc NO daia is sa·cd in ihc insiancc's ·ariablcs. 1hc oihcr poini is ihai
you nc·cr know which insiancc scr·cs your applicaiion rcqucsi ai a gi·cn
iimc. Ií your applicaiion rcqucsis a Siaiclcss Scssion bcan oncc and anoihcr
Siaiclcss Scssion bcan in somc iimc. you can nc·cr bc surc ihai a Coniaincr
gi·cs you ihc samc rcícrcncc. As a conclusion. ihc Siaiclcss Scssion bcans
ha·c ihc cjbActivatc() and cjbPassivatc() mcihods. bui ihcrc is no scnsc
io rcly on ihcir pcrsisicncc. bccausc oí ihc implcmcniaiion oí such bcans.

Vhat arc thc scrviccs providcd bv thc containcr?

A coniaincr pro·idcs a sci oí scr·iccs lisicd bclow.
i. Entcrprisc !ava Bcans suppori. 1hc suppori íor EJBs is mandaiory íor
cach coniaincr. Ii musi suppori all iypcs oí bcans spcciíicd by SUN. (1hc
laicsi spcciíicaiion is :.o. ihough ii's íinal ·crsion is noi yci rclcascd. bui
mosi EJB coniaincrs alrcady suppori ii.)
e. !TS (!ava Transaction Scrvicc). 1his scr·icc is inicndcd io conirol
iransaciions wiihin an cnicrprisc sysicm.
(FAQ) For Core & Advance Java
s. !NDI (!ava Naming and Dircctorv Intcrfacc). E·cry EJB Coniaincr
MUS1 pro·idc a namc spacc íor acccssing rcsourccs (including EJB
insianccs). as wcll as a way io acccss ihcm using JNDI.
4. !DBC (!ava Databasc Conncction). A Coniaincr musi bc ablc io
pro·idc EJB wiih abiliiy io acccss cxisiing daiabasc(s). In mosi cascs ihc
XA and conncciion pooling arc also supporicd. bui ii's also a hcadachc oí a
JDBC dri·cr ·cndor.
a. !MS (!ava Mcssaging Scrvicc). 1his scr·icc is ncw io EJB. and ii was
iniroduccd in EJB :.o. 1his scr·icc allows inicrchangc oí asynchronous
mcssagcs. EJB :.o also iniroduccd ihc ncw iypc oí bcans - Mcssagc-dri·cn
Bcans in·okcd by a Coniaincr only.

Vhat arc diffcrcnt Tvpcs of transaction?

i. SimpIc transactions. 1ransaciions pcríorming simplc opcraiions arc
callcd SimpIc Transactions. 1his iypc oí iransaciions is ihc mosi oíicn
e. Ncstcd transactions. 1ransaciions can bc ncsicd. i.c. somc mcihod.
which opcns a iransaciion. may call anoihcr mcihod. also opcning a
iransaciion. In such casc a Ncstcd Transaction appcars. 1hcrc is no nccd
io makc such iransaciions cxpliciily. bccausc mosi sysicms don'i rcquirc
such complicaicd logic. Nc·crihclcss. modcrn Daiabasc Scr·crs suppori
ncsicd iransaciions. Noic ihai ií an inncr iransaciion rolls back. ihc ouicr
iransaciion auiomaiically rolls back. and ií an inncr iransaciion íails. ihc
ouicr iransaciion also íails.
s. Distributcd transactions. 1his kind oí iransaciion is uscd íor
pcríorming opcraiions on daia locaicd in diíícrcni daiabascs. Ií morc ihan
onc daiabasc is in·ol·cd in a iransaciion. such iransaciion is callcd a
Distributcd Transaction.

Vhat is bcan-managcd transaction?

Ií a dc·clopcr docsn'i wani a Coniaincr io managc iransaciions. ii's possiblc
io implcmcni all daiabasc opcraiions manually by wriiing ihc appropriaic
JDBC codc. 1his oíicn lcads io producii·iiy incrcasc. bui ii makcs an
Eniiiy Bcan incompaiiblc wiih somc daiabascs and ii cnlargcs ihc amouni
oí codc io bc wriiicn. A dc·clopcr cxpliciily pcríorms all iransaciion

(FAQ) For Core & Advance Java
Vhv docs E!B nccd two intcrfaccs (Homc and Rcmotc Intcrfacc)?

A clicni scarchcs íor a homc inicríacc io insianiiaic a rcmotc inicríacc. A
rcmotc inicríacc is a clicni-sidc mapping io a scr·cr objcci pcríorming
busincss mcihods. Such schcma was iniroduccd purposcly io dcmarcaic ihc
diíícrcncc bciwccn scr·cr and clicni inicríaccs.

Can I dcvcIop an Entitv Bcan without impIcmcnting thc crcatc()
mcthod in thc homc intcrfacc?

As pcr ihc spcciíicaiions. ihcrc can bc 'ZERO' or 'MORE' crcaic() mcihods
dcíincd in an Eniiiy Bcan. In cascs whcrc crcaic() mcihod is noi pro·idcd.
ihc only way io acccss ihc bcan is by knowing iis primary kcy. and by
acquiring a handlc io ii by using iis corrcsponding íindcr mcihod. In ihosc
cascs. you can crcaic an insiancc oí a bcan bascd on ihc daia prcscni in ihc
iablc. All onc nccds io know is ihc primary kcy oí ihai iablc. i.c. a sci a
columns ihai uniqucly idcniiíy a singlc row in ihai iablc. Oncc ihis is
known. onc can usc ihc 'gciIrimaryKcy()' io gci a rcmoic rcícrcncc io ihai
bcan. which can íurihcr bc uscd io in·okc busincss mcihods.

Vhat is thc diffcrcncc bctwccn Contc×t, InitiaIContc×t and Scssion
Contc×t? How thcv arc uscd?

java×.naming.Contc×t is an inicríacc ihai pro·idcs mcihods íor binding a
namc io an objcci. Ii's much likc ihc RMI Naming.bind() mcihod.

java×.naming.InitiaIContc×t is a Conicxi and pro·idcs implcmcniaiion
íor mcihods a·ailablc in ihc Conicxi inicríacc.

Vhcrc as ScssionContc×t is an EJBConicxi objcci ihai is pro·idcd by ihc
EJB coniaincr io a ScssionBcan in ordcr íor ihc ScssionBcan io acccss ihc
iníormaiion and/or scr·iccs or ihc coniaincr.

1hcrc is EniiiyConicxi ioo which is also and EJBConicxi objcci ihai'll bc
pro·idcd io an EniiiyBcan íor ihc purposc oí ihc EniiiyBcan acccssing ihc
coniaincr dciails. In gcncral. ihc EJBConicxi (ScssionConicxi and
EniiiyConicxi). ApplciConicxi and Scr·lciConicxi hclp ihc corrcsponding
Ja·a objccis in knowing aboui iis 'conicxi' ¯cn·ironmcni in which ihcy
run¯. and io acccss pariicular iníormaiion and/or scr·icc. Vhcrc as. ihc
(FAQ) For Core & Advance Java
ja·ax.naming.Conicxi is íor ihc purposc oí 'NAMING' ¯by ihc way oí
rcícrring io¯ an objcci.

Vhv an onMcssagc caII in Mcssagc-drivcn bcan is aIwavs a scparatc

EJB :.o spcciíicaiion. "An onMcssagc call is always a scparaic iransaciion.
bccausc ihcrc is nc·cr a iransaciion in progrcss whcn ihc mcihod is callcd."

Vhcn a mcssagc arri·cs. ii is passcd io ihc Mcssagc Dri·cn Bcan ihrough
ihc onMcssagc() mcihod. ihai is whcrc ihc busincss logic gocs.

Sincc ihcrc is no guaranicc whcn ihc mcihod is callcd and whcn ihc
mcssagc will bc proccsscd. is ihc coniaincr ihai is rcsponsiblc oí managing
ihc cn·ironmcni. including iransaciions.

Static variabIcs in E!B shouId not bc rcIicd upon as thcv mav brcak in
cIustcrs. Vhv?

Siaiic ·ariablcs arc only ok ií ihcy arc íinal. Ií ihcy arc noi íinal. ihcy will
brcak ihc clusicr. Vhai ihai mcans is ihai ií you clusicr your applicaiion
scr·cr (sprcad ii across sc·cral machincs) cach pari oí ihc clusicr will run
in iis own JVM.

Say a mcihod on ihc EJB is in·okcd on clusicr : (wc will ha·c iwo clusicrs
- : and :) ihai causcs ·aluc oí ihc siaiic ·ariablc io bc incrcascd io :o:. On
ihc subscqucni call io ihc samc EJB írom ihc samc clicni. a clusicr : may
bc in·okcd io handlc ihc rcqucsi. A ·aluc oí ihc siaiic ·ariablc in clusicr :
is siill :oo bccausc ii was noi incrcascd yci and ihcrcíorc your applicaiion
ccascs io bc consisicni. 1hcrcíorc. siaiic non-íinal ·ariablcs arc sirongly
discouragcd in EJBs.

If I throw a custom AppIicationE×ccption from a busincss mcthod in
Entitv bcan, which is participating in a transaction, wouId containcr
roII thc transaction back?

EJB 1ransaciion is auiomaiically rollcd back only whcn a SysicmExccpiion
(or a subiypc oí ii) is ihrown.
(FAQ) For Core & Advance Java

Your ApplicaiionExccpiions can cxicnd írom ja·ax.cjb.EJBExccpiion.
which is a sub class oí RuniimcExccpiion. Vhcn a EJBExccpiion is
cncounicrcd ihc coniaincr rolls back ihc iransaciion. EJB Spcciíicaiion docs
noi mcniion anyihing aboui Applicaiion cxccpiions bcing sub-classcs oí

You can icll coniaincr io rollback ihc iransaciion. by using
sciRollBackOnly on ScssionConicxi/EJBConicxi objcci as pcr iypc oí bcan
you arc using.

How to impIcmcnt an cntitv bcan which thc PrimarvKcv is an
autonumcric ficId

1hc EJB : Spcc (:o.s.· - Spccial casc. Unknown primary kcy class) says
ihai in cascs whcrc ihc IrimaryKcys arc gcncraicd auiomaiically by ihc
undcrlying daiabasc. ihc bcan pro·idcr musi dcclarc ihc íindByIrimaryKcy
mcihod io rciurn ja·a.lang.Objcci and spcciíy ihc Irimary Kcy Class as
ja·a.lang.Objcci in ihc Dcploymcni Dcscripior.

Vhcn dcíining ihc Irimary Kcy íor ihc Enicrprisc Bcan. ihc Dcploycr
using ihc Coniaincr Iro·idcr's iools will iypically add addiiional coniaincr-
managcd íiclds io ihc concrcic subclass oí ihc cniiiy bcan class.

In ihis casc. ihc Coniaincr musi gcncraic ihc Irimary Kcy ·aluc whcn ihc
cniiiy bcan insiancc is crcaicd (and bcíorc cjbIosiCrcaic is in·okcd on ihc

Vhat is cIustcring?

Clusicring is grouping machincs iogcihcr io iransparcnily pro·idc
cnicrprisc scr·iccs. Clusicring is an csscniial piccc io sol·ing ihc nccds íor
ioday's largc wcbsiics.

1hc clicni docs noi know ihc diíícrcncc bciwccn approaching onc scr·cr or
approaching a cIustcr of scrvcrs.

(FAQ) For Core & Advance Java

Is it possibIc to sharc an HttpScssion bctwccn a !SP and E!B? Vhat
happcns whcn I changc a vaIuc in thc HttpScssion from insidc an E!B?

You can pass ihc HiipScssion as paramcicr io an EJB mcihod.onIv if aII
objccts in scssion arc scriaIizabIc. 1his has io bc considcr as "passcd-by-
·aluc". ihai mcans ihai ii's rcad-only in ihc EJB. Ií anyihing is alicrcd írom
insidc ihc EJB. ii won'i bc rcílccicd back io ihc HiipScssion oí ihc Scr·lci

Vhat arc transaction attributcs?

1hc iransaciion aiiribuic spcciíics how ihc Coniaincr musi managc
iransaciions íor a mcihod whcn a clicni in·okcs ihc mcihod ·ia ihc
cnicrprisc bcan`s homc or componcni inicríacc or whcn ihc mcihod is
in·okcd as ihc rcsuli oí ihc arri·al oí a JMS mcssagc. (Sun'. 1JB
Sp«c:J:cuí:on) Bclow is a lisi oí iransaciional aiiribuics.
• NotSupportcd - iransaciion conicxi is unspcciíicd.
• Rcquircd - bcan's mcihod in·ocaiion is madc wiihin a iransaciional
conicxi. Ií a clicni is noi associaicd wiih a iransaciion. a ncw
iransaciion is in·okcd auiomaiically.
• Supports - ií a iransaciional conicxi cxisis. a Coniaincr acis likc ihc
iransaciion aiiribuic is Rcquircd. clsc - likc NotSupportcd.
• RcquircsNcw - a mcihod is in·okcd in a ncw iransaciion conicxi.
• Mandatorv - ií a iransaciional conicxi cxisis. a Coniaincr acis likc
ihc iransaciion aiiribuic is Rcquircd. clsc ii ihrows a
• Ncvcr - a mcihod cxccuics only ií no iransaciion conicxi is spcciíicd.

Vhat is thc diffcrcncc bctwccn Containcr-Managcd Pcrsistcnt (CMP)
bcan and Bcan-Managcd (BMP) pcrsistcnt cntitv bcan?

A CMP bcan dc·clopcr docsn'i nccd io worry aboui JDBC codc and
iransaciions. bccausc ihc Coniaincr pcríorms daiabasc calls and iransaciion
managcmcni insicad oí ihc programmcr. All iablc mapping is spcciíicd in
ihc dcploymcni dcscripior. Oihcrwisc.
(FAQ) For Core & Advance Java
A BMI bcan dc·clopcr iakcs ihc load oí linking an applicaiion and a
daiabasc on his shouldcrs. BMI bcans arc noi :oo° daiabasc-indcpcndcni.
bccausc ihcy may coniain daiabasc-spcciíic codc.
Bui CMI bcans arc unablc io pcríorm complicaicd DML (Daia
Manipulaiion Languagc) siaicmcnis.
EJB :.o spcciíicaiion iniroduccd somc ncw ways oí qucrying daiabasc (by
using ihc EJB OL - qucry languagc).

Vhcn shouId I adopt BMP and whcn I shouId usc CMP?

You can usc CMI and BMI bcans in ihc samc applicaiion... ob·iously. a
bcan can bc BMI or CMI. noi boih ai ihc samc iimc (ihcy arc muiually

1hcrc is a common approach ihai is normally uscd and considcrcd a good
onc. You should siari dc·cloping CMI bcans. unlcss you rcquirc somc kind
oí spccial bcan. likc mulii-iablcs. ihai cannoi bc complcicly rcalizcd wiih a
singlc bcan. 1hcn. whcn you rcalizc ihai you nccd somcihing morc or ihai
you would prcícr handling ihc pcrsisicncc (pcríormancc issuc arc ihc mosi
common rcason). you can changc ihc bcan írom a CMI io a BMI.

How is cntitv bcan crcatcd using CMP cntitv bcan?

1hc coniaincr crcaics an cniiiy bcan auiomaiically. Hcrc. "auiomaiically"
mcans ihai a dc·clopcr docsn'i nccd io wriic any SOL codc íor ii. Ccriainly.
ihis docsn'i cxcmpi a programmcr írom implcmcniing busincss mcihods.

Can I map morc than onc tabIc in a CMP?

No. you cannoi map morc ihan onc iablc io a singlc CMI Eniiiy Bcan.
CMI has bccn. in íaci. dcsigncd io map a singlc iablc.

Can I invokc Runtimc.gc() in an E!B?

You shouldn'i. Vhai will happcn dcpcnds on ihc implcmcniaiion. bui ihc
call will mosi likcly bc ignorcd.

(FAQ) For Core & Advance Java
Vhat is softwarc architccturc of E!B?

Scssion and Eniiiy EJB consisi oí + or : paris.
• Rcmotc intcrfacc (a clicni inicracis wiih ii).
• Homc intcrfacc (uscd íor crcaiing objccis and íor dcclaring
busincss mcihods).
• Bcan objcct (an objcci. which aciually pcríorms busincss logic and
EJB-spcciíic opcraiions).
• DcpIovmcnt dcscriptor (an XML íilc coniaining all iníormaiion
rcquircd íor mainiaining ihc EJB) or a sci oí dcploymcni dcscripiors
(ií you arc using somc coniaincr-spcciíic ícaiurcs).
• Primarv Kcv class - is only Eniiiy bcan spcciíic.

Can Entitv Bcans havc no crcatc () mcthods?

Ccriainly. In somc cascs ihc daia is inscricd NO1 using Ja·a applicaiion. so
you may only nccd io rciric·c ihc iníormaiion. pcríorm iis proccssing. bui
noi crcaic your own iníormaiion oí ihis kind.

In which casc cjbLoad () docsn't rcad data from thc databasc?

cjbLoad () rcads daia írom ihc daiabasc in all cascs cxccpi onc. whcn ihc
iníormaiion siorcd in an Eniiiy Bcan has noi bccn changcd aíicr crcaiion or
rciric·ing. In ihis casc cjbLoad () bccomcs a rcdundani opcraiion.

Vhat is an "cagcr Ioading of statc"?

An EJB coniaincr usually rcads all daia io consiruci ihc Eniiiy Bcan. 1his
is opposiic io "Iazv Ioading" whcn daia is rcad as picccs. noi as a wholc
block. 1his incrcascs spccd in mosi cascs bui makcs ihc applicaiion morc

How wouId vou impIcmcnt a casc whcn Entitv Bcan i caIIs Entitv
Bcan e, which caIIs Entitv Bcan i?

You nccd io mark Eniiiy Bcan : as rc-cnirani.

(FAQ) For Core & Advance Java

How wouId vou impIcmcnt E!B SubcIassing?

1hc íollowing guidclincs assumc ihai wc ha·c EJB:. which Subclasscs
EJB:. (Boín 1JB. u·« .«..:on on«..)
1hc Rcmoic inicríacc oí EJB: implcmcnis ihc Rcmoic inicríacc oí EJB:
1hc Bcan class oí EJB: cxicnds ihc Bcan class oí EJB:

Sign up to vote on this title
UsefulNot useful