You are on page 1of 49

INTERNAL

Lab Guide for Introduction to JEE


Author(s) Vaishali S Mudkhedkar Raoke,
Sandeep Kulkarni, Neelam Santani
and Shinu Thomas
Authorized by Dr. M. P. Raindra
Creation/Revision Date !ul"#$$%
Version &Ver No'
C!"RIG#$ %$ICE
All ideas and information contained in this document are the intellectual property of
Education and Research Department, Infosys Technologies Limited. This document is
not for general distribution and is meant for use only for the person they are
specifically issued to. This document shall not be loaned to anyone, within or outside
Infosys, including its customers. Copying or unauthoried distribution of this
document, in any form or means including electronic, mechanical, photocopying or
otherwise is illegal.
Edu(ation and Resear(h Department
In)os*s Te(hnolo+ies Limited
Ele(troni( ,it*
-osur Road
.an+alore " /01 ##2, India.
Tel3 21 4$ 4/# $#01"#%$
5a63 21 4$ 4/# $70#
888.in)*.(om
mailto3E9R:in)*.(om
In)os*s Te(hnolo+ies Limited Do(ument Reision -istor*
Docu&ent Revision #istory
Version Date Author(s) Revie'er(s) Co&&ents
1.$ !ul"#$$% Vaishali S
Mudkhedkar Raoke,
Sandeep Kulkarni,
Neelam Santani and
Shinu Thomas
Ra;a+opalan P
Sureesh !oseph
Initial Dra)t
ER<,=RP<,RS<ED>7A<$$% Version No 31.$ i
In)os*s Te(hnolo+ies Limited Ta?le o) ,ontents
Contents
C!"RIG#$ %$ICE((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((II
DC)*E%$ REVI+I% #I+$R"((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((I
C%$E%$+((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( II
C%$E,$((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( -
DA" - A++IG%*E%$+(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
ASSI@NMENT 13 DEVEL=PIN@ !AVA PR=!E,T AIT- E,LIPSE !.=SS IDE..................................................1
ASSI@NMENT #3 DEVEL=PIN@ AE. APPLI,ATI=N AIT- E,LIPSE !.=SS IDE.............................................11
DA" . A++IG%*E%$+((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((.-
ASSI@NMENT 13 BNDERSTANDIN@ DE,LARATI=N AND S,RIPTIN@ ELEMENTS..............................................#1
ASSI@NMENT #3 !SP ,=MPILE TIME ERR=R AND RBNTIME ERR=R........................................................#>
ASSI@NMENT 73 BNDERSTANDIN@ =5 IMPLI,IT =.!E,TS C=BT AND REDBESTE...........................................#0
ASSI@NMENT >3 A=RKIN@ AIT- -TML 5=RM AND REDBEST PARAMETERS..............................................#2
ASSI@NMENT /3 BNDERSTANDIN@ SESSI=N =.!E,T AND SESSI=N VARIA.LE..............................................7$
DA" / A++IG%*E%$+((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((/0
ASSI@NMENT 1 3 !SP STANDARD A,TI=NS &!SP35=RAARD' ...........................................................70
ASSI@NMENT #3 PASSIN@ PARAMETERS T-R=B@- BRL .................................................................>$
ASSI@NMENT 73 !SP AND !D., AIT- SIMPLE AE. APPLI,ATI=N ......................................................>1
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
ii
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
Conte1t
This do(ument (ontains assi+nments to ?e (ompleted as part o) the hands on )or the su?;e(t
Introduction to JEE C,ourse (ode3 ED>7AE.
%ote2 In order to (omplete the (ourse, assi+nments in this do(ument must ?e (ompleted in
the seFuen(e mentioned.
Day - Assi3n&ents
Assi3n&ent -2 Deve4o5in3 Java !ro6ect 'ith Ec4i5se J7oss IDE
b6ective2 To understand ho8 to use !.ossIDE )or E(lipse to (reate the !aa Pro;e(t.
!rob4e& Descri5tion2 ,reate a pa(ka+ed !aa )ile C!D.,Demo.;aaE 8hi(h 8ill (onne(t to
the data?ase serer and Fuer* )or its s*stem date.
Esti&ated ti&e2 .8 *in(
+te5 -2 Start the !.ossIDE )or E(lipse. Gou should +et the )ollo8in+ s(reen. C?* runnin+ the
e(lipse.e6e aaila?le in the (3H;eeIso)t )older or similar )oldersE.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 .2 Gou (an (reate arious t*pes o) pro;e(ts usin+ this enironment. In this assi+nment 8e
8ill (reate !aa Pro;e(t CStandalone appli(ationE. Sele(t 5ile Ne8 Pro;e(t.
+te5 .2 Gou (an (reate arious t*pes o) pro;e(ts usin+ this enironment. In this assi+nment 8e
8ill (reate !aa Pro;e(t CStandalone appli(ationE. Sele(t 5ile Ne8 Pro;e(t !aa
pro;e(t.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 /2 Enter the pro;e(t name Chere, it is -elloAorldE, and (li(k on 5inish ?utton.
Gou 8ill +et s(reen as ?elo8.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 92 In this pro;e(t, i) *ou 8ant to add a pa(ka+e, Ri+ht ,li(k on the Pro;e(t Name C-ere
-elloAorldE Ne8 Pa(ka+e as sho8n in the )ollo8in+ s(reen shot.
+te5 :2 5ill the name o) the pa(ka+e in the 8iJard as ?elo8, and (li(k on 5inish.
Gou hae su((ess)ull* (reated the pa(ka+e.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 02 Let us (reate the !aa (lass under this pa(ka+e. In order to (reate a (lass, ri+ht (li(k
on the pa(ka+e name sele(t Ne8 ,lass as sho8n ?elo8.
+te5 ;2 5ill the alid name )or the (lass. I) *ou 8ant to make this (lass as starter (lass, ti(k
the (he(k?o6 <5ub4ic static void &ain(+trin3=> ar3s)?@ other8ise, leae it un(he(ked CAe
hae kept it un(he(ked )or JD7CDe&o. .ut )or other *ainC4ass (lass, 8e hae sele(ted it.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
/
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 A2 =n(e *ou )inish the a?oe 8iJard, *ou should +et the s(reen as sho8n ?elo8.
+te5 B2 T*pe the )ollo8in+ (ode in the JD7CDe&o(6ava )ile.
/**
* This file demonstrates the usage of JDBC...
*/
package com.infy.enr;
import java.sl.Connection;
import java.sl.!tatement;
import java.sl."esult!et;
import java.sl.Driver#anager;
pu$lic class JDBCDemo %
pu$lic void connectToData$ase&'%
try%
Class.for(ame&)oracle.jd$c.driver.*racleDriver)';
Connection con +
Driver#anager.getConnection&)jd$c,oracle,thin,-./..00..1/.10,.20.,trai
ning)3 )vaishali)3 )infy)';
!tatement o$j!tatement + con.create!tatement&';
"esult!et rs +
o$j!tatement.e4ecute5uery&)!elect sysdate from dual)';
!ystem.out.println&)Data$ase server date is , ) 6 rs.getDate&.'';
7 catch&84ception e'%
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
0
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
!ystem.out.println&)84ception , ) 6 e 6) has occured999)';
7
7
7
,han+e the )ollo8in+ line in *our (ode to a((ommodate *our Data?ase userid and pass8ord
and D. seri(e name.
Driver#anager.getConnection&)jd$c,oracle,thin,-ip address of your DB
server,.20.,DB service name)3 )userid)3 )pass:ord)';
+te5 -82 Repeat steps 0 and % to (reate *ainC4ass(6ava. T*pe the )ollo8in+ (ode in it.
/** This is the Java file3 :hich :ill have main function in it.
* ;e :ill create the instance of the JDBCDemo class in this file3 and
* invoke its method.
*/
import com.infy.enr.JDBCDemo;
pu$lic class #ainClass %
/**
* -param args
*/
pu$lic static void main&!tring<= args' %
JDBCDemo o$j*ne + ne: JDBCDemo&';
o$j*ne.connectToData$ase&';
7
7
+te5 --2 To run the (lass *ainC4ass@ Ri+ht (li(k on the )ile Run As !aa Appli(ation
sho8n as ?elo83
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
%
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
Still 8e are +ettin+ a ,lassNot5oundE6(eption )or ora(le.;d?(.drier.=ra(leDrier.(lass )ile. In
*our Ad. !aa (ourse, *ou hae learnt that, this (lass is present in the (lasses1#.Jip <
(lasses1#.;ar )ile. Ae need to hae this (lass in our ,LASSPAT- in order to 8rite !D.,
appli(ation usin+ =ra(le Drier.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
4
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 -.2 To put it in the .uild Path, 8e need to Add this E6ternal )ile )or our pro;e(t. Ri+ht
(li(k on the pro;e(t C-ere in this (ase3 -elloAorldE, Properties !aa .uild Path. ,li(k
on Add E1terna4 JARsC as sho8n ?elo8.
+te5 -/2 AiJard 8ill ?e open ?ro8se )or the c4asses-.(zi5 or c4asses-.(6ar and (li(k on
open. CIt ma* ?e present in )older (3Hora(leHora41H;d?(Hli?E It 8ill ?e added to the Li?raries
ta? as sho8n ?elo8.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
2
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 -92 Repeat Step 11 to run the (lassK Gou should +et the output as sho8n ?elo83
+u&&ary of this assi3n&ent2
Gou hae ;ust learnt3
to (reate a !aa pro;e(t usin+ !.ossIDE )or E(lipse.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1$
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
The deliera?les o) this assi+nment are -elloAorld Pro;e(t (ontainin+ Main,lass.;aa,
!D.,Demo.;aa and their respe(tie .(lass )iles.
Assi3n&ent .2 Deve4o5in3 Deb A554ication 'ith Ec4i5se J7oss IDE
b6ective2 To understand ho8 to deelop a simple 8e? appli(ation usin+ !.ossIDE )or
E(lipse.
!rob4e& Descri5tion2 Deelop a simple 8e? appli(ation usin+ !.ossIDE )or E(lipse.
Esti&ated ti&e2 .8 *ins
+te5 -2 A)ter *ou laun(h e(lipse )or the )irst time, and ?e)ore 8e (an ?uild our )irst
8e? appli(ation, 8e need to do a little e(lipse housekeepin+. I) latest !DK is not
alread* de)ined, *ou 8ill need to set the !aa pre)eren(es to point to *our !DK
installation Csee )i+ureE. Aindo8 Pre)eren(e !aa Installed !REs
+te5 .2 ,hoose a Serer Runtime Enironment. This step inoles (han+in+ or (reatin+
the settin+s )or Installed Serer Runtimes. Durin+ 8e? appli(ation deelopment, 8e
8ill need a serer runtime enironment to test, de?u+ and run our pro;e(t. Ae ?e+in
?* tellin+ e(lipse 8hat our serer runtime is, and 8here it is lo(ated. A Lserer
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
11
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
runtime enironmentM is not a serer. It proides the enironment, li?raries and
in)rastru(ture that a LsererM needs. A serer is an instan(e o) the serer runtime
that (an host our 8e? appli(ations and other serer"side (omponents. To de)ine a
serer runtime, 8e need to sele(t3
Aindo8 Pre)eren(e Serer InstalledRuntime
-ere *ou 8ill )ind a list o) serer runtimes that hae ?een de)ined preiousl*. Gou (an
(hoose a serer runtime and (han+e its properties. To add a ne8 one (li(k LAddKM. A
8iJard 8ill popup and displa* a list o) serer runtimes that are supported ?* ATP
CAe? Tools Plat)ormE. ,hoose !.oss >.$ serer runtime )rom the list. A)ter *ou (li(k
LNe6tM, *ou 8ill hae to (hoose the !DK and the )older 8here *ou hae installed *our
serer runtime. ,li(k )inish. No8 8e (an deelop the 8e? appli(ation. CThe ;?oss
)older ma* ?e present in (3H;eeIso)t )older or in (3H drieE
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1#
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 /2 ,reate a !EE Ae? Pro;e(t 8ith a 8e? module. @o to Ne8 Pro;e(t. ,hoose
Deb cate3ory )rom the list o) aaila?le 8iJards to (reate a ne8 e(lipse resour(e. Gou
8ill )ind t8o pro;e(t t*pes aaila?le under this (ate+or*. Simple Ae? Pro;e(t is a
?asi( E(lipse resour(e pro;e(t that (an ?e asso(iated 8ith a serer. Ae are interested
in (reatin+ a D*nami( Ae? Appli(ation, so 8e 8ill (hoose the D*nami( Ae? Pro;e(t:
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
17
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
A Ae? Pro;e(t is a t*pe o) !aa pro;e(t that has ?een initialiJed to host a serer"side
appli(ation. It needs to ?e asso(iated 8ith a Tar+et Serer. A tar+et serer proides
the (ontainer 8hi(h 8ill ?e used to e6e(ute our 8e? appli(ation. Spe(i)i(all*, it 8ill
proide a set o) li?raries C;arsE, 8hi(h are added to the pro;e(t CLA!!"AT#, 8hi(h are
needed to (ompile our (ustom (lasses.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1>
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
@ie appropriate pro;e(t name, set the Tar+et Runtime as L!.oss >.$M and then (li(k
on L5inishM.
+te5 92 Ahen the 8iJard (ompletes its tasks, *ou 8ill )ind that it has (reated a ne8
!aa Pro;e(t and has initialiJed it so that *ou (an start 8ith *our deelopment ri+ht
a8a*3 It has (reated a ;aa sour(e )older. Gou 8ill add *our (ustom ;aa pa(ka+es and
(lasses here. It has de)ined the CLA!!"AT# C!RE and !.ossE so that *ou 8ill hae !#EE
li?raries in *our pro;e(t CLA!!"AT#. It has (reated a resour(e )older named LAe?
,ontentM, 8hi(h mimi(s the standard AAR stru(ture )rom the !EE spe(i)i(ation. It 8ill
(ontain the 8e? resour(es that 8ill ?e pa(ka+ed 8ith *our !EE 8e? module. All
arti)a(ts inside this )older are a((essi?le )rom the Ae? Appli(ation (onte6t.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1/
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 :2 Add !SP to our 8e? pro;e(t. Ri+ht (li(k on the pro;e(t and sele(t Ne8 !SP.
@ie appropriate name to the !SP )ile then (li(k on L5inishM
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
10
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
Listin+3 )irst.;sp
>?- page language+)java) contentType+)te4t/html; charset+@!*ABB2CA.)
page8ncoding+)@!*ABB2CA.)?D
>9D*CTEF8 html FGBH@C )A//;1C//DTD IT#H J./. Transitional//8()
)http,//:::.:1.org/T"/htmlJ/loose.dtd)D
>htmlD
>headD
>titleD!ho: Bro:ser>/titleD
>/headD
>$odyD
>h.D;elcome to 8clipse A JBoss @D8>/h.D
>ta$le $order+).)D
>trD
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
1%
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
>thDIeader>/thD
>thDKalue>/thD
>/trD
>trD
>tdDGserALgent>/tdD
>tdD>?+reuest.getIeader&)GserALgent)'?D>/tdD
>/trD
>/ta$leD
>/$odyD
>/htmlD
+te5 02 In"order to run the appli(ation 8e need to (reate serer. Serers (an ?e
(reated usin+ the Serer Vie8. To open a serer ie8 +o to Aindo8 Sho8 Vie8"
'=therK, and (hoose the Serers under the Serer (ate+or*. The serer ie8 allo8s
*ou to (reate serers )or the (urrentl* installed serer runtime enironments. Bse the
ri+ht (li(k menu to (reate a ne8 serer. In the )ollo8in+ 8iJard, (hoose the serer
address 8here *ou 8ill deplo* Cpu?lishE *our 8e? module. Gou 8ill also hae to
(hoose the serer t*pe and the serer runtime. Ahen *ou (li(k )inish, *ou hae a
SererN
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
14
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 ;2 Start the !.oss serer. In the serer ie8, do a menu"(li(k on *our serer and
(hoose start, or *ou (an simpl* (li(k the start ?utton. Gou 8ill see the LStatusM o) the
serer )rom Stopped to LStartin+M, and 8hen the serer is read* it 8ill (han+e its
status to OStartedO. CDou?le (li(kin+ on the Serers I(on 8ill minimiJe the (onsoleE
+te5 A2 To run 8e? pro;e(t, ri+ht (li(k on the pro;e(t and sele(t Run As Run on
Serer.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
12
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
,omplete the 8iJard and (li(k on L5inishM. Gou (an see the )ollo8in+ output.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#$
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+u&&ary of this assi3n&ent2
Gou hae ;ust learnt3
To (reate a d*nami( 8e? pro;e(t in !.ossIDE )or E(lipse.
Day . Assi3n&ents
Assi3n&ent -2 )nderstandin3 dec4aration and scri5tin3 e4e&ents
b6ective2 To understand the de(laration, s(riptin+ element, e6pression and !SP li)e (*(le
methods.
!rob4e& Descri5tion2 ,reate a !SP )ile (alled dec4aration(6s5 to understand ho8 to de(lare
instan(e aria?les, methods, lo(al aria?le and li)etime o) instan(e aria?le.
Esti&ated ti&e2 -8 *in(
+te5 -2 ,reate the )ile dec4aration(6s5 and t*pe the )ollo8in+ (ode3
>htmlD
>$odyD
>?9
// declaring a instance varia$le
int count+/;
private int getCount&'
%
count66;
return count;
7
// init method :ill $e called only once in J!F life cycle.
pu$lic void jsp@nit&'
%
count+/;
!ystem.out.println&)@nside init method)6count';
7
//Destroy method :ill $e called only once in J!F life cycle
pu$lic void jspDestroy&'
%
!ystem.out.println&)@nside Destroy&' method)6 count';
7
?D
>?
// declaring local varia$le
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#1
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
int locCount+/;
locCount66;
!ystem.out.println&)@nside !ervice method)';
?D
>h.D
>pD Kalue of Count is, >?+getCount&'?D>$rD
>pD Kalue of locCount is, >?+locCount?D>$rD
>/pD
>/h.D
>/$odyD
>/htmlD
%ote2 Gou (an use the s*stem ?ro8ser )or runnin+ 8e? pa+es instead o) the
de)ault ?ro8ser o) the E(lipse usin+ the )ollo8in+ option in E(lipse IDE.
Aindo8 "' Ae? .ro8ser "' Internet E6plorer
+te5 .2
1. Run the !SP usin+ !.oss
#. Gou 8ill +et )ollo8in+ output3
7. Anal*Je the output.
>. ,he(k the (onsole )or eri)*in+ the output. CS*stem.out.printlnCE is +oin+ to print on
the (onsole not on the ?ro8serE.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
##
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 /2 Re)resh the pa+e and (he(k the output. Anal*Je the output.
+te5 /2 No8 (lose this ?ro8ser 8indo8 and open a ne8 ?ro8ser 8indo8. Ahat is the output
no8P ,an *ou (ompare and reason the outputP
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#7
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
%ote2 In the !SP li)e (*(le the (ontainer (reates onl* one instan(e o) the
+enerated Serlet. So there 8ill ?e onl* one instan(e o) the serlet and one
(op* o) instan(e aria?le. The (ode inside &Q Q' is pla(ed in the seri(e
method o) the +enerated serlet and this 8ill ?e inoked )or ea(h reFuest
)rom the (lient.
I) 1$ users are a((essin+ the !SP, all the 1$ users are a((essin+
Csharin+E the same instan(e aria?le.
+u&&ary of this assi3n&ent2
Gou hae learnt,
-o8 to de(lare instan(e aria?leP
-o8 to de(lare the methodsP
-o8 to de(lare lo(al aria?le and usin+ e6pressionP
!SP li)e (*(le methods.
Assi3n&ent .2 J+! co&5i4e ti&e error and runti&e error(
b6ective2 To understand the !SP (ompile time error and runtime error.
!rob4e& Descri5tion2 8rite a !SP pa+e to understand (ompile time error and runtime
error.
Esti&ated ti&e2 : *in(
+te5-2 T*pe the )ollo8in+ (ode and sae it as debu3(6s5
>htmlD
>$odyD
>?
!tring name;
?D
>h.D name is >?+name.to!tring&'?D
>/$odyD
>/htmlD
+te5.2 Run the a?oe !SP.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#>
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5/2 Veri)* the error modi)* the (ode as mentioned ?elo8. C this is a (ompile time errorE
>htmlD
>$odyD
>?
!tring name+null;
?D
>h.D name is >?+name.to!tring&'?D
>/$odyD
>/htmlD
+te592 Run the !SP and eri)* the output.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#/
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te592 This is a runtime error. ,orre(t the mistake and e6e(ute the !SP.
Assi3n&ent /2 )nderstandin3 of i&54icit ob6ects (out and reEuest)
b6ective2 To understand arious !SP impli(it o?;e(ts.
!rob4e& Descri5tion2 ,reate !SP appli(ation to use impli(it o?;e(ts out, reEuest.
Esti&ated ti&e2 -: *in(
+te5 -2 ,reate a )ile reEuest$estFor&(ht&4 and t*pe ?elo8 mentioned (ode3
>IT#HD
>I8LDD
>T@TH8D"euest Test J!F Morm>/T@TH8D
>/I8LDD
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#0
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
>B*DED
>M*"# LCT@*(+)reuestTest.jsp) #8TI*D+)Net)D
Gser (ame
>@(FGT TEF8+)T8OT) (L#8+)Gser(ame)D
>B"D
>@(FGT TEF8+)!GB#@T) KLHG8+)*k)D
>/M*"#D
>/B*DED
>/IT#HD
+te5 .2 ,reate a )ile reEuest$est(6s5 and t*pe ?elo8 mentioned (ode. C in this (ase ?oth !SP
and -TML )iles should ?e pla(ed in the same )olderE
>?AA This is a J!F to use implicit o$ject AA?D
>IT#HD
>I8LDD
>T@TH8D
"euest Test J!F
>/T@TH8D
>/I8LDD
>B*DED
>I.D
Iello
>?
// the parameter Gser(ame is case sensitive
!tring name + reuest.getFarameter&)Gser(ame)';
out.println&name';
?D
>/I.D
>?AAThe follo:ing e4pression also does the same :orkAA?D
>I.D
Iello >?+reuest.getFarameter&)Gser(ame)'?D
>/I.D
>/B*DED
>/IT#HD
%ote2 serlet API do(umentation is not aaila?le 8ith ;dk do(. Gou (an )ind
the Serlet do(s )rom the )ollo8in+ )older. 5ind di))erent methods o) reFuest
o?;e(t.
@o to !EEIS=5T dire(tor* and then !EEIS=5THTom(at /./H8e?appsHtom(at"
do(sHserletapi. =pen the inde6.html
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#%
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 /2 No8 t*pe the BRL )or the reEuest$estFor&(ht&4 and *ou 8ill see the )ollo8in+
s(reen3
+te5 92 T*pe In)os*s in the Bsername te6t?o6 and (li(k on =K and *ou 8ill see the )ollo8in+
s(reen3
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#4
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
%ote2 In the a?oe e6ample the parameter is passin+ to the serer throu+h
the BRL. C G)ser%a&eHInfosysE. This is happenin+ ?e(ause 8e are usin+ @ET
method )or su?mittin+ the -TML )orm.
+te5 :2 modi)* the reEuest$estFor&(ht&4 ?* (han+in+ the *E$#DH?!+$? and run
reEuest$estFor&(ht&4(
+te5 02 E6e(ute the !SP and (he(k the BRL.
%ote2 The (han+es *ou made )or html<;sp ma* not ?e re)le(ted immediatel*
in the ?ro8ser ?e(ause o) ?ro8ser (a(hin+. So re)resh the ?ro8ser or (lose
the ?ro8ser and open the pa+e a+ain.
+u&&ary of this assi3n&ent2
Gou hae learnt,
To use impli(it o?;e(ts in !SP
Readin+ data sent ?* the user
Di))eren(e ?et8een @ET and P=ST method
Assi3n&ent 92 DorIin3 'ith #$*L for& and reEuest 5ara&eters(
b6ective2 to (reate an -TML )orm and sent the data to the serer.
!rob4e& Descri5tion2
1E ,reate an -TML @BI to a((ept the )ollo8in+ )ields Cfor&(ht&4E
Name 3 CTe6t ?o6E
@ender 3 CRadio .uttons )or Male and 5emaleE
Address 3 CTe6tareaE
#E Ahen the user (li(ks on the for&(ht&4 it should inoke a ;sp (alled for&dis54ay(6s5. the
for&dis54ay(6s5 should displa* the input in a -TML ta?le )ormat.
7E E6pe(ted output is displa*ed ?elo8.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
#2
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
Esti&ated ti&e2 -: *in(
Assi3n&ent :2 )nderstandin3 session ob6ect and session variab4e
b6ective2 To understand the session aria?le and its s(ope.
!rob4e& Descri5tion2 ,reate !SP appli(ation to use impli(it o?;e(ts session.
Esti&ated ti&e2 .8 *in(
+te5 -2 ,reate a )ile session(6s5 and t*pe ?elo8 mentioned (ode3
>IT#HD
>I8LDD
>T@TH8DGsing session *$ject>/T@TH8D
>/I8LDD
>B*DED
>I1DGsing session *$ject>/I1D
>?
// getting the session vari$ale from session o$ject
@nteger counter + &@nteger'session.getLttri$ute&)countval)';
// for the first time the couter o$ject is null other:ise it is
// not null
if &null ++counter'
%
counter + ne: @nteger&.';
7
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7$
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
else
%
counter + ne: @nteger&counter.intKalue&' 6 .';
7
// setting the session vari$ale
session.setLttri$ute&)countval)3 counter';
?D
>h1D!ession vari$ale valus is >?+counter.intKalue&'?D>/h1D
>h1D!ession id is >?+session.get@d&'?D>/h1D
>/B*DED
>/IT#HD
%ote2 Gou (an use the s*stem ?ro8ser )or displa*in+ 8e? pa+es instead o)
the de)ault ?ro8ser o) the E(lipse usin+ the )ollo8in+ option in E(lipse IDE.
Aindo8 "' Ae? .ro8ser "' Internet E6plorer
+te5 .2 T*pe the BRL to see the output o) session(6s5.
+te5 /2 =pen a ne8 ?ro8ser 8indo8 )rom the e6istin+ 8indo8. C5rom the ?ro8ser sele(t
fi4e &enu JK ne' JK 'indo')( Veri)* the output and note that the session id is same )or ?oth
the ?ro8sers.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
71
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 92 ,lose the ne8 8indo8 Copened usin+ step 7E and re)resh C# timesE the )irst 8indo8
Copened usin+ step#E. ,he(k the output.
+te5 :2 =pen a ne8 ?ro8ser not )rom the e6istin+ ?ro8ser. C5rom start"' IE or )rom the IE
short (utE. Veri)* the output and note that the session id is di))erent )or the ne8 ?ro8ser.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7#
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
%ote2 Session o?;e(t is (reated )or ea(h (lient and stored in the serer.
Session aria?les are a((essi?le )rom an* !SP pa+e )or the parti(ular session
C(lient or ?ro8serE. Ea(h user is hain+ there o8n session o?;e(t and session
aria?les. Ahen *ou are openin+ a ?ro8ser )rom e6istin+ ?ro8ser Cusin+ )ile"
'ne8"'8indo8 or 8indo8.openCE methodE the session o?;e(t is shared
?et8een multiple ?ro8sers. I) *ou are openin+ a ne8 ?ro8ser a ne8 session
o?;e(t is (reated. CDependin+ on the .ro8sers and ersionsE.
+te5 02 ,reate a )ile readsession(6s5 and t*pe ?elo8 mentioned (ode3
>IT#HD
>I8LDD
>T@TH8D"ead !ession>/T@TH8D
>/I8LDD
>B*DED
>?
@nteger counter + &@nteger'session.getLttri$ute&)countval)';
?D
>font color+)red) siPe+)2)D!ession varia$le
>?+counter.intKalue&'?D>/fontD
+te5 ;2 ,lose all the ?ro8sers and open readsession(6s5 in a ne8 8indo8 and eri)* the
output.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
77
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5A2 Read the error details. The error is +enerated ?e(ause o) NullPointerE6(eption.
+te5B2 Veri)* 8h* the session aria?le is null.
%ote2 8e are openin+ readsession.;sp dire(tl* 8ithout settin+ the session
aria?le. .e(ause o) this the session aria?le is null. It is al8a*s ?etter to
(he(k the o?;e(t is null or not ?e)ore usin+ it. This is appli(a?le to reFuest
parameters, session aria?les and appli(ation aria?les.
+te5B2 Modi)* the (ode as mentioned ?elo8.
>IT#HD
>I8LDD
>T@TH8D"ead !ession>/T@TH8D
>/I8LDD
>B*DED
>?
@nteger counter + &@nteger'session.getLttri$ute&)countval)';
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7>
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
if &null ++counter'
%
?D
>font color+)red) siPe+)2)D!ession varia$le is (*T set>/fontD
>?
7
else
%
?D
>font color+)red) siPe+)2)D!ession varia$le
>?+counter.intKalue&'?D>/fontD
>?
7
?D
+te5-82 ,lose all the ?ro8sers and open the readsession(6s5 in a ne8 ?ro8ser. ,he(k the
output.
+te5 --2 =pen session(6s5 and re)resh the pa+e )or 5ie times. Aithout (losin+ the ?ro8ser
open readsession(6s5(
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7/
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+u&&ary of this assi3n&ent2
Gou hae learnt,
-o8 to use session o?;e(t
,reatin+ session aria?les
Readin+ session aria?les
S(ope o) session o?;e(t
Day / Assi3n&ents
Assi3n&ent - 2 J+! +tandard Actions L6s52for'ardK
b6ective2 To understand the &;sp3)or8ard' a(tion element.
!rob4e& Descri5tion2 ,reate a simple 8e? appli(ation to understand the ;sp3)or8ard
a(tion element.
Esti&ated ti&e2 .8 *ins(
+te5 -2 =pen the notepad editor and t*pe the )ollo8in+ (ode in it and sae it as
L4o3in(ht&4M.
>htmlD
>headD
>titleD Hogin Fage >/titleD
>/headD
>$odyD
>form name+)loginMorm) action+)loginCheck.jsp)D
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
70
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
>ta$le :idth+)2/?) align+)center) $order+).)D
>tr $gcolor+)lightgrey)D
>td align+)center) colspan+)0)D >$DHogin Morm>/$D>/tdD
>/trD
>trD
>tdD Gser (ame >/tdD
>tdD >input type+)te4t) name+)user(ame)D >/tdD
>/trD
>trD
>tdDFass:ord >/tdD
>tdD >input type+)pass:ord) name+)pass:ord)D >/tdD
>/trD
>trD
>td colspan+)0)D
>input type+)!u$mit) value+)Hog @n)D >/tdD
>/trD
>/ta$leD
>/formD
>/$odyD
>/htmlD
+te5 .2 =pen the notepad editor and t*pe the )ollo8in+ (ode in it and sae it as
L4o3inChecI(6s5M.
>htmlD
>headD
>titleD ;elcome Fage >/titleD
>/headD
>$odyD
>h. align+)center)D ;elcome to our page999 >/h.D
>?AA Check the input parameter AA?D
>?
!tring user+&reuest.getFarameter&)user(ame)'';
// null validation should $e checked first
// if the o$ject is null :e can not invoke any method
if&&null ++ user' QQ &user.length&'++/''
%
?D
>?AA Mor:ard the reuest if user(ame is not specified AA?D
>jsp,for:ard page+)handle@t.jsp)/D
>?
7
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
7%
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
?D
>h0D Iello >?+ reuest.getFarameter&)user(ame)' ?D999 >/h0D
>/$odyD
>/htmlD
+te5 /2 =pen the notepad editor and t*pe the )ollo8in+ (ode in it and sae it as
Lhand4eIt(6s5M.
>htmlD
>$odyD
>h0 align+)center) style+)$ackgroundAcolor,red)D
Flease enter the Gser (ame999 >/h0D
>jsp,include page+)login.html)/D
>/$odyD
>/htmlD
+te5 92 Deplo* all the )iles on the 8e? serer.
+te5 :2 =pen the lo+in.html
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
74
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 02 Do not enter an*thin+ in Bser Name )ield and ,li(k on the LLo+ InM ?utton to see the
)ollo8in+ output.
+te5 ;2 No8 enter the Bser Name as L!ohnM and (li(k on LLo+ InM ?utton to see the )ollo8in+
output.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
72
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+u&&ary of this assi3n&ent2
In this assi+nment *ou hae learnt the use o) &;sp3)or8ard' ta+.
%ote2
1. Aith &;sp3)or8ard', the ?u))er is (leared ?e)ore the )or8ard.
Assi3n&ent .2 !assin3 5ara&eters throu3h )RL
b6ective2 !assin3 5ara&eters throu3h )RL((
!rob4e& Descri5tion2 Ahen the user (li(ks on di))erent links )or the same pa+e displa* is
(han+in+ ?ased on the parameters passed ?* the BRL.
Esti&ated ti&e2 -8 *in(
1E ,reate a html pa+e (alled 4inIde&o(ht&4 8ith the )ollo8in+ links.
aE &a hre)RMuser.;spPnameR!a(kM' Bser1&<a'
?E &a hre)RMuser.;spPnameRT=MM' Bser#&<a'
(E &a hre)RMuser.;spPnameR!err*M'Bser7&<a'
Ahen the user (li(ks on the links it should inoke user.;sp and displa* the na&e ?ased on the
links.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>$
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
#int2 The name is aaila?le 8ith the reFuest o?;e(t as a parameter. Bsin+ reFuest o?;e(t *ou
(an retriee the name alue.
%ote2 P=ST method (an ?e used onl* 8ith -TML &)orm' ta+. All reFuest
methods to a 8e? serer is @ET ?* de)ault. So 8hen 8e (li(k on the a?oe
BRL the data is passin+ to the serer throu+h the BRL. Modi)*in+ the BRL
8ith parameters is kno8n as BRL re8ritin+.
Assi3n&ent /2 J+! and JD7C 'ith si&54e 'eb a554ication
b6ective2 )sin3 JD7C in J+!
!rob4e& Descri5tion2 ,reate a simple 8e? appli(ation to maintain traineesS stream
allo(ation operation.
Esti&ated ti&e2 /8 *in(
+te5 -2 Crete a fo44o'in3 tab4es in the database
$rainee ta?le (ontains the )ollo8in+ in)ormation a?out the trainees.
Co4u&n %a&e Data ty5e Descri5tion
Emplo*eeId Num?erC%E Primar* Ke*
Name Var(har#C7$
,it* Var(har#C#$E
Sample Data3
E&54oyeeId %a&e City
1$$ Sa(hin Mum?ai
1$1 Mahesh ,hennai
1$# Rahul .an+alore
The +trea& ta?le (ontains the )ollo8in+ in)ormation a?out the arious streams o))ered ?* the
(ompan*.
Co4u&n %a&e Data ty5e Descri5tion
StreamId Num?erC>E Primar* Ke*
Des(ription Var(har#C#$
Sample Data3
+trea&Id Descri5tion
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>1
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
1$$$ !EE
1$$1 D=T NET
1$$# Main)rame
1$$7 =pen S*stem
The A44ocation ta?le (ontains the )ollo8in+ in)ormation a?out the arious allo(ations.
Co4u&n %a&e Data ty5e Descri5tion
Emplo*eeID Num?erC%E 5K
StreamId Num?erC>E 5K
!oinin+Date Date
Sample Data3
E&54oyeeId +trea&Id Joinin3 Date
1$$ 1$$$ 1#"!un"#$$%
1$1 1$$1 1/"!un"#$$%
%ote2 Assume that one trainee (an ?elon+s to onl* one stream.
+te5 .2 ,reate a ne8 d*nami( AE. pro;e(t LTraineeMana+ementM
+te5 /2 ,reate a ne8 html )ile as Lho&e(ht&4M 8ith three links as sho8 ?elo8.
1E Add Trainee Cadd$rainee(ht&4)(
#E Allo(ate Stream Ca44ocate+trea&For&(6s5)
7E Report Cre5ort(6s5)
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>#
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 92 ,reate a ne8 html )ile as <add$rainee(ht&4M.
+te5 :2 Ahen the user (li(ks on the Su?mit ?utton, it should inoke add$rainee(6s5 and
inserts the re(ord to the $rainee ta?le. Bse a seFuen(e )or +eneratin+ emplo*ee id startin+
)rom 1$7. Displa* ne8 emplo*ee num?er on su((ess and error messa+es in (ase o) )ailure.
%ote2 Sin(e !SP is @eneratin+ a Serlet all the !aa API (an ?e used inside
the !SP. In this e6ample *ou (an dire(tl* use the !D., API inside the !SP. C it
is not a +ood pra(ti(e and in !EE appli(ation use !SP onl* )or the
presentation purposeE. Please )ind the (ode ?elo8 )or usin+ !D.,.
>?
Connection connection+null;
try
%
Driver#anager.registerDriver&ne:
oracle.jd$c.driver.*racleDriver&'';
connection+Driver#anager.getConnection
&)jd$c,oracle,thin,-@F address,.20.,
Iost string)3)username)3)pass:ord)';
//code
7
catch&84ception e4ception'
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>7
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
%
out.println&)8rror) 6e4ception';
!ystem.out.println&)8rror) 6e4ception';
//it is a good practice to use !ystem.out.println&'
//for printing the error messages in the server console
7
finally
%
if&null 9+ connection'
%
connection.close&';
7
7
?D
%ote2 In the a?oe (ode 8e are usin+ ,onne(tion as lo(al aria?le to the
seri(e method. This means ea(h reFuest is openin+ a ne8 (onne(tion and
(losin+ the (onne(tion. This is not a +ood pra(ti(e and in !EE pro;e(ts 8e 8ill
use ,onne(tion poolin+ and a((ess the D. )rom D. (lasses not )rom the !SP.
+te5 02 ,reate a ne8 html )ile as La44ocate+trea&For&(6s5M )or allo(atin+ stream )or the
Trainees.
1E Displa* a te6t?o6 )or a((eptin+ the Emplo*ee ID
#E D*nami(all* +enerate the sele(t ?o6 ?* )et(hin+ stream id )rom the data?ase. This
should (ontain all stream ids aaila?le in the strea& ta?le.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>>
In)os*s Te(hnolo+ies Limited La? @uide )or Introdu(tion to !EE Part " 1
+te5 ;2 Ahen the user (li(ks on the su?mit ?utton it should inoke La44ocate+trea&(6s5M.
The !SP should (he(k i) the emplo*ee id is present in the $rainee ta?le. I) present it should
(he(k i) the trainee is alread* allo(ated to a stream or not Cin the A44ocation ta?leE. I) the
emplo*ee is a alid emplo*ee and not allo(ated to a stream insert the re(ord in to the
a44ocation ta?le. Displa* proper messa+es.
+te5 A2 Ahen the user (li(ks on the Report link it should displa* the )ollo8in+ report ?*
)et(hin+ data )rom the data?ase. Displa* the report in a -TML ta?le )ormat. The Name should
?e displa*ed as a link and 8hen the user (li(ks on the Name it should open a !SP that displa*s
the trainee details in a ta?le )ormat. CIn the link, (on(atenate the emplo*eeid 8ith the BRL.E
-int3 Re)er the preious assi+nment.
Name Stream Des(ription !oinin+ Date
+u&&ary of this assi3n&ent2
At the end o) this assi+nment, *ou hae learnt,
1. -o8 to insert the data in the data?ase ia !SP pa+e.
#. -o8 to (reate d*nami( pa+e usin+ data?ase
7. BRL 8ritin+ to pass parameters throu+h BRL.
>. @eneratin+ report )rom the data?ase.
ER<,=RP<,RS<ED>7A<$$% Version No. 1.$
>/

You might also like