Professional Documents
Culture Documents
Advanced Java Lab Mannual
Advanced Java Lab Mannual
LAB MANNUAL
Prepared by:
April, "#$#
Advanced Institute of Technology & Management, Palwal Lab Planning Course: C ! emester: "th Su%&ect C'(e ) CSE-406-E Su%&ect Na*e) Advanced JAVA Lab Su%&ect +aculty ,'r la%) Ms. Shilpy Gupta
Course #b$ective: To enhance the knowledge about various implementations in the working of a compiler.
Program %o&
Assign&
) * + , . / " 0 )1
WAP for Client/Server (SOCKET) PROGRAMMING WAP to implement MULTITHREADING in JAVA. WAP to implement the concept of SWINGS. Write steps to create DATA SOURCE NAME (DSN). WAP to execute DATABASE QUERIES using JDBC connecti it!. WAP to access Database using D"t"B"#eMet"$"t". WAP to access Database using Re#%&tSetMet"$"t". WAP to develop an RMI #!#te'. WAP to implement SER(LETS. WAP to implement JSP.
PROGRAM ) *+
2
i p!"t #ava.i!.$% i p!"t #ava.net.$% public class se"ve & public static v!id ain'St"in( s)*+ & Se"ve"S!c,et sc% S!c,et client% St"in( na e-ne/ St"in('+% 0ata1nputSt"ea d% 2"intSt"ea t!Client% 3u44e"ed5eade" 4"! Client% St"in( st"-..% t"y & d-ne/ 0ata1nputSt"ea 'Syste .in+% sc-ne/ Se"ve"S!c,et'76+% Syste .!ut.p"intln'.Se"ve"Sta"ted.+% Syste .!ut.p"intln'.2"ess ct"l;C t! <uit.+% Syste .!ut.p"int'.Ente" y!u" na e>.+% na e-d."eadLine'+% Syste .!ut.p"intln'.?elc! e6 .;na e;.89n?aitin( 4!" Client88.+% client-sc.accept'+% 1nputSt"ea 5eade" is"-ne/ 1nputSt"ea 5eade"'client.(et1nputSt"ea '++% 4"! Client-ne/ 3u44e"ed5eade"'is"+% t!Client-ne/ 2"intSt"ea 'client.(et=utputSt"ea '++% st"-4"! Client."eadLine'+% Syste .!ut.p"intln'st";. C!nnected88.+% /hile't"ue+ & st"-4"! Client."eadLine'+% Syste .!ut.p"intln'st"+% Syste .!ut.p"int'na e;.>.+% st"-na e;.>.;d."eadLine'+% t!Client.p"intln'st"+% @ @ catch'EAcepti!n e+ & Syste .!ut.p"intln'e+% @ @ @
PROGRAM ) .+
4
@ @
PROGRAM ) /+
6
(l.setC!nst"aints'lG6(bc+% p.add'lG+% (bc.("idA-G% (bc.("idy-0% (bc.("id/idth-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints't46(bc+% p.add't4+% (bc.("idA-0% (bc.("idy-2% (bc.("id/idth-G% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'l26(bc+% p.add'l2+% b(-ne/ 3utt!nG"!up'+% (bc.("idA-G% (bc.("idy-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% b(.add' +% (l.setC!nst"aints' 6(bc+% p.add' +% (bc.("idA-2% (bc.("idy-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% b(.add'4+% (l.setC!nst"aints'46(bc+% p.add'4+% (bc.("idA-0% (bc.("idy-4% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'lB6(bc+% p.add'lB+% (bc.("idA-G% (bc.("idy-4% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'cb6(bc+% p.add'cb+% (bc.("idA-0% (bc.("idy-6% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% K
(l.setC!nst"aints'l46(bc+% p.add'l4+% (bc.("idA-G% (bc.("idy-6% (bc.anch!"-G"id3a(C!nst"aints.C=5DO% (l.setC!nst"aints'#l6(bc+% p.add'#l+% (bc.("idA-G% (bc.("idy-K% (bc.("id/idth-G% (bc.4ill-G"id3a(C!nst"aints.O=51Q=CDAL% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'b6(bc+% p.add'b+% (etC!ntent2ane'+.add'p+% b.addActi!nListene"'this+% @ public v!id acti!n2e"4!" ed'Acti!nEvent a+ & St"in( s% s-t4.(etDeAt'+% i4' .isSelected'++ & s-s;.6 .; .Male.% @ else & s-s;.6 .;.Je ale.% @ s-s;. pu"suin( .;St"in(.value=4'cb.(etSelected1te '++% s-s;. li,es .;St"in(.value=4'#l.(etSelectedValue'++% J=pti!n2ane.sh!/Messa(e0ial!('this6s+% @ @ HHEAecute the ab!ve p"!("a li,e a si ple applet p"!("a usin( appletvie/e"
G. =pen C!nt"!l 2anel and Clic, Ad inist"ative D!!ls6 then !pen this 0ial!( b!A 2. Clic, the Add butt!n. Dhen !pen this 0ial!( 3!A
B. Select MS Access 0"ive" and clic, !n Jinish butt!n. Dhen !pen this 0ial!( b!A
G0
GG
PROGRAM ) 1+
G2
int c!unt4-st t.eAecuteTpdate'st"6+% Syste .!ut.p"intln'.table is updated.+% int c!untE-st t.eAecuteTpdate'st"L+% Syste .!ut.p"intln'.table is deleted.+% c!n.cl!se'+% @ catch'EAcepti!n eA+ & Syste .!ut.p"intln'.e""!" !ccu""ed.; eA+% @ @ @ //C'*pile the pr'/ra* 3ith 1&a0ac %ase.&a0a2 //Create DSN as *e-ti'-e( i- pre0i'us pr'/ra*. //E4ecute the pr'/ra* 3ith 1&a0a %ase2
PROGRAM ) 4+
G4
PROGRAM ) 5+
GE
Syste .!ut.p"intln'eA+% @ @ @ //C'*pile the pr'/ra* usi-/ 1&a0ac 6#Met"D"t".&a0a2 //Create DSN as i- pre0i'us pr'/ra* //e4ecute the pr'/ra* usi-/ 1&a0a 6#Met"D"t" 2
@
Step ;) 7e%i-( the Ser0er 1:ell'Ser0er.&a0a2
import java.rmi.*; import java.rmi.server.*; public class HelloServer { public static void main(String args[]) { try { System.setSecurityManager( ne !M"SecurityManager()); Hello #$ne Hello"mpl(); %aming.rebind(&server&'#); System.out.println(&object is registered&); System.out.println(&no servaer is aiting&); ( catc#()*ception e) { System.out.println(&error+&,e); GK
( ( (
Step <)
import java.rmi.*; public class Hello-lient { public static void main(String args[]) { try { Hello #$(Hello)%aming.loo.up(&rmi+//012.034.5.33/server&); System.out.println(&client+ Hello6&); System.out.println(&server+& ,#.sayHello()); ( catc#()*ception e) { System.out.println(&)rror+&,e); ( ( (
Step 6: Generate the Stub & Skelton using: rmic HelloImpl Step 7: Start the RMI registr start rmiregistr Step !: start the ser"er #a"a HelloSer"er Step $: Start the client #a"a HelloClient
Step "! Create ,'l(ers as e4a*ples >? 5EB>IN+ >? classes Step ;! 5rite the ,'ll'3i-/ pr'/ra* i- a ,ile 1:ell'5'rl(.&a0a2 a-( sa0e i- classes ,'l(er i i i i p!"t #ava.i!.$% p!"t #ava.util.$% p!"t #avaA.se"vlet.$% p!"t #avaA.se"vlet.http.$%
public class Oell!?!"ld eAtends OttpSe"vlet & public Oell!?!"ld'+ & supe"'+% @ p"!tected v!id d!Get'OttpSe"vlet5e<uest "e<uest6 OttpSe"vlet5esp!nse "esp!nse+ th"!/s Se"vletEAcepti!n6 1=EAcepti!n & HH D=0= Aut!-(ene"ated eth!d stub "esp!nse.setC!ntentDype'.teAtHht l.+% 2"int?"ite" p/ - "esp!nse.(et?"ite"'+% p/.p"intln'.Iht lF.+% p/.p"intln'.IheadFItitleFOell! ?!"ldIHtitleFIHtitleF.+% p/.p"intln'.Ib!dyF.+% p/.p"intln'.IhGFOell! ?!"ldIHhGF.+% p/.p"intln'.IHb!dyFIHht lF.+% "esp!nse.addOeade"'.5e4"esh.6 .G.+% p/.p"intln'ne/ 0ate'+.t!St"in('++% @ p"!tected v!id d!2!st'OttpSe"vlet5e<uest "e<uest6 OttpSe"vlet5esp!nse "esp!nse+ th"!/s Se"vletEAcepti!n6 1=EAcepti!n & HH D=0= Aut!-(ene"ated eth!d stub @ @ Step <! 'pe- the c'**a-( pr'*pt usi-/ c*( a-( set the (ri0e l'cati'- as) C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@3e%apps@e4a*ples@5EB> IN+@classes Step <! C'*pile the a%'0e pr'/ra* usi-/ 1&a0ac :ell'5'rl(.&a0a2. Step B! C'py paste the 3e%.4*l ,ile ,r'* 3e%apps >? 766 >? 5EB>IN+ ' 3e%apps >? e4a*ples >? 5EB>IN+ 20
Step A! E(it the 3e%.4*l ,ile t' set the ser0let -a*e a-( ser0let>*appi-/s acc'r(i-/ t' y'ur ser0let. Step C! Start the '*cat Ser0ice 7u--er i- 1C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@%i-2 Step D! 6pe- the 3e% %r'3ser a-( type the ,'ll'3i-/ U7L t' ru- the ser0let ,r'* a-y clie-t syste*) http)//l'calh'st)D#D#/e4a*ples/:ell'5'rl( 'r http)// EI. a((ress ', ser0er? /e4a*ples/:ell'5'rl( a-( press e-ter.
2G
Step ;! 5rite the ,'ll'3i-/ pr'/ra* a-( sa0e li9e a- ht*l ,ile, 3ith e4te-si'- &sp, say 1ti*e.&sp2 i- the a%'0e create( ,'l(er Iht lF IheadF ItitleF Cu""ent D1 e IHtitleF IHheadF Ib!dyF Itable ali(n-:cente": F It"F ItdF IbFCu""ent 0ate and Di eIHbF IHtdF ItdF IU-ne/ #ava.util.0ate'+UF IHtdF IHt"F IHtableF IHb!dyF IHht lF Step ;! Start the '*cat Ser0ice 7u--er i- 1C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@%i-2 Step <! 6pe- the 3e% %r'3ser a-( type the ,'ll'3i-/ U7L t' ru- the ser0let ,r'* a-y clie-t syste*) http)//l'calh'st)D#D#/e4a*ples/ti*e.&sp 'r http)// EI. a((ress ', ser0er? /e4a*ples/ti*e.&sp a-( press e-ter.
22