7/25/2019 java file.doc
1/35
INDEX
S.No NAME OF THE PROGRAMS SIGNATURE
1 Program for learning basics of java language and its
library.
2 Program to create an applet with a textfield and
three buttonswhen you press each button ma!e
some different text appear in the textfield.
3 Program to create an applet with a button and a
textfieldwrite a handle event so that if the button
have been focusedcharacter types into it will appear
in textfield
4 Program to show connectivity of java with database
5 Program to create an editor screen containing
menusdialog boxes etc using java
6 Program to show "ava Networ!ing#"ava $oc!ets
and %&I7 Program to develop a servlet that get invo!ed when
a form on a web page in '(&) is submitted.
7/25/2019 java file.doc
2/35
)earn basics of java language and its development
tools*libraries.
Introduction+Java is an object-oriented programming language with a built-in application
programming interface (API) that can handle graphics and user interfaces and that can be
used to create applications or applets. Because of its rich set of APIs! similar to"acintosh and #indows! and its platform independence! Java can also be thought of as a
platform in itself. Java also has standard libraries for doing mathematics
Need for "ava+$he java language contains built in support for the #orld #ide #eb(###)! which is a
service of the internet to retrieve information in the form of the #eb pages. $he primar%motive behind developing Java language was the need for portable and platform
independent language that could be used to produce code that run on a variet% of &P'
under different environments. ne can use Java to develop etwor* oriented programs
because networ*ing features are built in features in Java.
,eatures of "ava + +imple
bject-riented
&ompiled and interpreted
robust
"ultithreaded
Platform Independent
,igh Performance
istributed
+ecure
Portable
$I&P)E
A java programmer does not need to *now the interior details of java as to how memor%
is allocated to data because in java! $he programmer does not need to handle memor%
manipulation. Java is a simple language that can be learn easil% even if u have just started
programming. It was designed to be eas% for the programmer to learn and use effectivel%.$he s%nta for various java statements is eas% to understand.
-"E/( -%IEN(ED
Java supports the object oriented approach to develop programs.It supports various
features of an object oriented language. $o implement the object oriented language theentire code of the program must be written within the class. $he java language does not
7/25/2019 java file.doc
3/35
support stand-alone statements. /ven the most basic program in java must be within a
class .
/-&PI)ED 0ND IN(E%P%E(ED
$he java programs are first compiled and then interpreted while compiling!the compilerchec*s for the error in the programs. After %ou have made the program error free andhave recompiled it! the compiler converts the program into computer language. $he java
compiler compiles the code to a b%te code that is understood b% the java. $he java virtual
machine then interprets this b%tecode into the computer code and runs it.
P-%(0)E
Portabilit% refers to the abilit% of the program to run on an% platform without changing
the source code of the program.$he java enables the creation of cross platform programsb% compiling the programs into an intermediate representation called java b%tecode. 0ou
can eecute this code on an% platform.
P)0(,-%& INDEPENDEN(
Java is a platform for application development. Java b%te code is eactl% the same on
ever% platform. Java programs that have been compiled into b%te code still need an
interpreter to eecute them on an% given platform. $he interpreter reads the b%te code andtranslates it into the native language of the host machine on the fl%. +ince the b%te code is
completel% platform independent! onl% the interpreter and a few native libraries need to
be ported to get Java to run on a new computer or operating s%stem.
%-1$(
Java implements a robust eception handling mechanism to deal with both epected and
unepected errors. $he worst that an applet can do to a host s%stem is bringing down theruntime environment. It cannot bring down the entire s%stem.
"ost importantl% Java applets can be eecuted in an environment that prohibits themfrom introducing viruses! deleting or modif%ing files! or otherwise destro%ing data and
crashing the host computer. A Java enabled web browser chec*s the b%te codes of an
applet to verif% that it doesnt do an%thing nast% before it will run the applet.
&1)(I('%E0D
Java is inherentl% multi-threaded. A single Java program can have man% different threads
eecuting independentl% and continuousl%. $hree Java applets on the same page can runtogether with each getting e1ual time from the &P' with ver% little etra effort on the
part of the programmer.
'I2' PE%,-%&0N/E
Java b%te codes can be compiled on the fl% to code that rivals &22 in speed using a 3just-
in-time compiler.3 +everal companies are also wor*ing on native-machine-architecturecompilers for Java. $hese will produce eecutable code that does not re1uire a separate
interpreter! and that is indistinguishable in speed from &22.
7/25/2019 java file.doc
4/35
$ome important concept of --P$
4 /ncapsulation
5 Abstraction
6 Inheritance
7 Pol%morphism
EN/0P$1)0(I-N
/ncapsulation is the process of hiding all of the details of an object that do not contributeto its essential characteristics./ncapsulation implies that the non-essential details of an
object are hidden from the user and an access is provided to its essential details. $he
compleit% is hidden from the user as a result of encapsulation.In object orientedmethodolog%! need of encapsulation arises because the emphasis is on designing classes
in such a manner that the classes share data and methods among themselves.
/ncapsulation is the feature that provides securit% to the data and the methods of a class.
0$(%0/(I-N
An abstraction denotes the essential characteristics of an object that uni1uel%
distinguishes it from all other *inds of objects and thus crispl% defined conceptualboundaries !relative to the perspective of the viewer. Abstraction refers to the attributes of
an object that clearl% demarcates it from other objects .$o implement abstraction! we also
use the encapsulation feature .encapsulation hides the irrelevant details of an object andabstraction ma*es onl% the relevant details of an object visible .#e use access specifier to
implement abstraction and encapsulation.
IN'E%I(0N/E
In object-oriented methodolog%! inheritance enables %ou to etend the functionalit% of an
eisting class. #e create a class that inherits the attributes and the behavior of anotherclass .In addition! the new class can consist of a new attributes and the behaviors that are
specific to the class .In terms of classes and object! attributes refer to the methods.
A program can have following t%pes of inheritance8
(a.) +ingle inheritance(b.)"ultiple inheritance
P-)3&-%P'I$&
Pol%morphism is derived from two 9atin words : pol% !which means man% ! and morph!
which means forms .An% thing that eist in more than one form is *nown as pol%morph .
In object-oriented methodolog%! pol%morphism is the feature that enables us to assign adifferent meaning or usage to an entit% in different contets. $he entit% can be a variable !
method !or an object .Pol%morphism enables an entit% to have more than one form
7/25/2019 java file.doc
5/35
depending upon the contet in which it is used . Pol%morphism enables one entit% to be
used as a general categor% for different t%pes of actions.
"ava 0rchitecture
;arious components of Java architecture are84.Java programming language and Java class file
5. Java platform
"ava Programming )anguage and class file+
Java programs are saved with an etension !.java. A .java file is compiled to generate
the .class file! which contains the b%tecode. $he J;" converts the B%tecode contained inthe .class file to machine object code. $he J;" needs to be implemented for each
platform running on different operating s%stems.
"ava platform
A java platform is the hardware or software environment in which a program runs. $he
java platform has two components8
Java ;irtual "achine (J;")Java Application Programming Interface (Java API)
J;" is a standardi
7/25/2019 java file.doc
6/35
$he J;" forms the base for the java platform and is convenient to use on various
hardware based platforms.
/omponents of "4&+
1.Class loader
$he class loader loads the class files! which are re1uired b% the program running in the
memor%. $he classes are loaded d%namicall% when re1uired b% the running program.
2. Execution engine
$he Java eecution engine is the component of the J;" that runs the b%tecode one line
after another. It converts the b%tecode into the machine object code and runs it.
3. Just In Time(JIT) compiler
$he JI$ is used for compiling the b%tecode into the eecutable code. $he J;" runs theJI$ compiled code without interpreting because the JI$ compiled code is in the machine
code format.
"ava 0pplication Programming Interface+
$he Java API is a collection of software components that provide capabilities! such as
='I. $he related classes and interfaces of the Java API are grouped into pac*ages.
0dvantages of -bject -riented Programming+
4. >eal #orld Programming5. >eusabilit% of the code
6. "odularit% of the code
7/25/2019 java file.doc
7/35
7. >esillence to change
?. Information hiding
"ava applications
1.Applications that use Character User Interace(CUI)
Applications are eecutable programs that are controlled b% operating s%stem.
2. Applications that use !raphical User Interace(!UI)
$hese applications are used in #indows /nvironment.In ='I! %ou can interact with the
application in graphical mode.
3. Applets
Applets are small eecutable programs that run a web page.
". #er$lets
+ervlets are the programs that are used to etend the functionalit% of the web servers.
%.&ac'ages
Pac*ages are collection of classes that are reused b% applications and applets
7/25/2019 java file.doc
8/35
Program-4
@@ Program for learning basics of java language and its librar%.
@@ 4st class
class Bo
double width
double depth
double height
Bo(Bo obj4)
widthCobj4.width
depthCobj4.depth
heightCobj4.height
D
Bo(double w!double d!double h)
widthCwdepthCd
heightCh
D
Bo()
widthC-4
depthC-4
heightC-4
D
double volume( )
return(widthEdepthEheight)
D
D
@@5nd class inheriting features from Bo class
class Bo#eight etends Bo
double mass
Bo#eight(Bo#eight obj5)
7/25/2019 java file.doc
9/35
super(obj5)
massCobj5.mass
D
Bo#eight(double w!double d!double h!double m)
super(w!d!h)
massCmD
Bo#eight()
super()
massC-4
D
D
@@6rd class inheriting features from Bo#eight class
class +hipment etends Bo#eight
double cost
+hipment(+hipment obj6)
super(obj6)
costCobj6.cost
D
+hipment(double w!double d!double h!double m!double c)
super(w!d!h!m)
costCc
D
+hipment()
super()
costC-4
D
D
@@main classclass emo+hipment
public static void main(+tring argsFG)
+hipment shipment4Cnew +hipment(5H!6H!7H!?H!4H)
double vol4Cshipment4.volume()
+hipment shipment5Cnew +hipment(shipment4)
double vol5Cshipment5.volume()
7/25/2019 java file.doc
10/35
+hipment shipment6Cnew +hipment()
double vol6Cshipment6.volume()
+%stem.out.println(34st ;olume is32vol4)
+%stem.out.println(35nd ;olume is32vol5)
+%stem.out.println(36rd ;olume is32vol6)
+%stem.out.println(34st #eight is32shipment4.mass)
+%stem.out.println(35nd #eight is32shipment5.mass)
+%stem.out.println(36rd #eight is32shipment6.mass)
D
D
7/25/2019 java file.doc
11/35
-1(P1(+
7/25/2019 java file.doc
12/35
Program-5
@E Program to create an applet with a tetfield and three buttons!when %ou press each button ma*e
some different tet appear in the tetfield.Add a chec*bo to the applet created!capture the
event!and insert different tet into the tetfield.E@
import java.awt.E
import java.awt.event.E
import java.applet.E
@Eapplet codeC3button.java3 heightC35HH3 widthC35HH3 @appletE@
public class button etends Applet implements Action9istener!Item9istener
$etKield t4Button b4!b5!b6
&hec*bo c4!c5!c6
Border9a%out b
Panel p4!p5
@@applet codeCbutton.java widthCLHH heightC6HH @applet
public void init()
bCnew Border9a%out()
t4Cnew $etKield(4H)t4.set/ditable(false)
b4Cnew Button(3&lic* ,ere3)
b5Cnew Button(3ow &lic* ,ere3)
b6Cnew Button(3And &lic* ,ere3)
c4Cnew &hec*bo(3&lic* ,ere3)
c5Cnew &hec*bo(3ow &lic* ,ere3)
c6Cnew &hec*bo(3And &lic* ,ere3)
set9a%out(b)
D
public void start()
p4Cnew Panel()p4.add(t4)
p4.add(b4)
p4.add(b5)
p4.add(b6)
7/25/2019 java file.doc
13/35
add(p4!Border9a%out.>$,)
p5Cnew Panel()
p5.add(c4)
p5.add(c5)
p5.add(c6)
add(p5!Border9a%out.+'$,)
b4.addAction9istener(this)
b5.addAction9istener(this)
b6.addAction9istener(this)
c4.addItem9istener(this)
c5.addItem9istener(this)
c6.addItem9istener(this)
D
public void actionPerformed(Action/vent e)
if(e.get+ource()CCb4)
t4.set$et(3M3)
if(e.get+ource()CCb5)
t4.set$et(3&ancel3)
if(e.get+ource()CCb6)
t4.set$et(3,elp3)D
public void item+tate&hanged(Item/vent ie)
if(ie.get+ource()CCc4)
t4.set$et(3Kile3)
if(ie.get+ource()CCc5)
t4.set$et(3edit3)
if(ie.get+ource()CCc6)
t4.set$et(3view3)
D
D
7/25/2019 java file.doc
14/35
-1(P1(+
7/25/2019 java file.doc
15/35
Program-6
@EProgram to create an applet with a button and a tetfield!write a handle event so that if
the button have been focused!character t%pes into it will appear in tetfieldE@
import java.awt.E
import java.awt.event.E
import java.applet.E@Eapplet codeC3focus3 widthC6HH heightC?HH @appletE@
public class focus etends Applet implements Kocus9istener
$etKield t4Button b4!b5
public void init()
t4Cnew $etKield(4H)
b4Cnew Button(3cancel3)
b5Cnew Button(3help3)
t4.set/ditable(false)
add(t4)add(b4)
add(b5)b4.addKocus9istener(this)b5.addKocus9istener(this)
D
public void focus=ained(Kocus/vent e)
if(e.get+ource()CCb4) t4.set$et(3cancel3)
if(e.get+ource()CCb5) t4.set$et(3help3)
Dpublic void focus9ost(Kocus/vent ae)
if(ae.get+ource()CCb4) t4.set$et(33)if(ae.get+ource()CCb5) t4.set$et(33)
D
D
7/25/2019 java file.doc
16/35
-1(P1(+
7/25/2019 java file.doc
17/35
Program-7
@@Program to show connectivit% of java with database
import java.io.E
import java.s1l.Eclass data
public static void main(+tring arcFG)
tr%
&lass.forame(3sun.jdbc.odbc.Jdbcdbcriver3)
&onnection conCriver"anager.get&onnection(3jdbc8odbc8man3)
Prepared+tatement ps>esult+et rs
while(true)
+%stem.out.println(3NnNn3)
+%stem.out.println(3 4 ;I/# &$A&$+3)
+%stem.out.println(3 5 A &$A&$+3)
+%stem.out.println(3 6 /9/$/ &$A&$+3)+%stem.out.println(3 7 /OI$3)
+%stem.out.print(3 /$/> 0'> &,I&/ 3)ataInput+tream brCnew ataInput+tream(+%stem.in)
+tring aCbr.read9ine()
int a4CInteger.parseInt(a)
switch(a4)
case 48
+tatement stCcon.create+tatement()rsCst.eecuteuer%(3select sno!name!address!tno!emailid from mang3)
+%stem.out.println(3NnNn3)+%stem.out.println(3--------------------------------------3)+%stem.out.println(3+.323Nt323A"/323Nt323A>/++323Nt323$/9.323Nt323/
"AI9 I3)
while(rs.net())
int s4Crs.getInt(4)
+tring s5Crs.get+tring(5)
7/25/2019 java file.doc
18/35
+tring s6Crs.get+tring(6)
int s7Crs.getInt(7)
+tring s?Crs.get+tring(?)+%stem.out.println(s423Nt32s523Nt32s623Nt32s723Nt32s?)
Dbrea*case 58
int b4CH
psCcon.prepare+tatement(3select sno from mang3)
rsCps.eecuteuer%()while(rs.net())
b422D
+%stem.out.print(3/$/> A"/ 3)
+tring b5Cbr.read9ine()+%stem.out.print(3/$/> A>/++ 3)
+tring b6Cbr.read9ine()
+%stem.out.print(3/$/> $/9/P,/ '"B/> 3)
+tring b7Cbr.read9ine()int b?CInteger.parseInt(b7)
+%stem.out.print(3/$/> /"AI9 I 3)
+tring bLCbr.read9ine()
psCcon.prepare+tatement(3insert into mang values(Q!Q!Q!Q!Q)3)
ps.setInt(4!b4)
ps.set+tring(5!b5)ps.set+tring(6!b6)
ps.setInt(7!b?)ps.set+tring(?!bL)
ps.eecute'pdate()
con.commit()
+%stem.out.println(3;A9'/+ /$/>/ +'&&/++K'9903)brea*
case 68
psCcon.prepare+tatement(3delete from mang where snoCQ3)+%stem.out.print(3/$/> + $ /9/$/ 3)
int mCInteger.parseInt(br.read9ine())
ps.setInt(4!m)ps.eecute'pdate()
con.commit()
+%stem.out.println(3/9/$/ +'&&/++K'9903)
brea*default8
+%stem.out.println(3/OI$/ +'&&/++K'9903)
7/25/2019 java file.doc
19/35
con.close()
+%stem.eit(H)
DD
Dcatch(/ception e)
+%stem.out.println(3/ception &aught3)
D
DD
7/25/2019 java file.doc
20/35
-1(P1(+
7/25/2019 java file.doc
21/35
Program-?
@EProgram to create an editor screen containing menus!dialog boes etc using javaE@
import java.awt.E
import java.awt.event.E
import java.applet.E@Eapplet codeC3ialogemo.java3 heightC35HH3 widthC35HH3 @appletE@
class +ampleialog etends ialog implements Action9istener
+ampleialog (Krame parent!+tring title)
super(parent!title!false)
set9a%out(new Klow9a%out())set+i
7/25/2019 java file.doc
22/35
file.add(new "enuItem(3-3))
file.add(item7Cnew "enuItem(3uit3))
mbar.add(file)
"enu editCnew "enu(3/dit3)"enuItem item?!itemL!itemRedit.add(item?Cnew "enuItem(3&ut3))
edit.add(itemLCnew "enuItem(3&op%3))
edit.add(itemRCnew "enuItem(3Paste3))
edit.add(new "enuItem(3-3))
"enu subCnew "enu(3+pecial3!true)
"enuItem itemS!itemT!item4Hsub.add(itemSCnew "enuItem(3Kirst3))
sub.add(itemTCnew "enuItem(3+econd3))
sub.add(item4HCnew "enuItem(3$hird3))edit.add(sub)
debugCnew &hec*bo"enuItem(3ebug3)
edit.add(debug)
testCnew &hec*bo"enuItem(3$esting3)edit.add(test)
mbar.add(edit)
"%"enu,andler handlerCnew "%"enu,andler(this)item4.addAction9istener(handler)
item5.addAction9istener(handler)
item6.addAction9istener(handler)
item7.addAction9istener(handler)item?.addAction9istener(handler)
itemL.addAction9istener(handler)itemR.addAction9istener(handler)
itemS.addAction9istener(handler)
itemT.addAction9istener(handler)
item4H.addAction9istener(handler)debug.addItem9istener(handler)
test.addItem9istener(handler)
"%#indowAdapter adapterCnew "%#indowAdapter(this)add#indow9istener(adapter)
D
public void paint(=raphics g)
g.draw+tring(msg!4H!5HH)
if(debug.get+tate())g.draw+tring(3ebug Is n3!4H!55H)
else
7/25/2019 java file.doc
23/35
g.draw+tring(3ebug Is ff3!4H!55H)
if(test.get+tate())
g.draw+tring(3$esting Is n3!4H!57H)else
g.draw+tring(3$esting Is ff3!4H!57H)DD
class "%#indowAdapter etends #indowAdapter
"enuKrame menuKrame
public "%#indowAdapter("enuKrame menuKrame)
this.menuKrameCmenuKrame
D
public void window&losing(#indow/vent we)
menuKrame.dispose()D
D
class "%"enu,andler implements Action9istener!Item9istener
"enuKrame menuKrame
public "%"enu,andler("enuKrame menuKrame)
this.menuKrameCmenuKrame
Dpublic void actionPerformed(Action/vent ae)
+tring msg C 30' +/9/&$/3
+tring arg C (+tring)ae.getAction&ommand()if(arg.e1uals(3ew3))
msg2C3ew3+ampleialog dCnew +ampleialog(menuKrame!3ew
ialogBo3)
d.set;isible(true)D
else if(arg.e1uals(3pen3))
msg2C3pen3
else if(arg.e1uals(3&lose3))msg2C3&lose3
else if(arg.e1uals(3uit3))
7/25/2019 java file.doc
24/35
msg2C3uit3
else if(arg.e1uals(3/dit3))
msg2C3/dit3else if(arg.e1uals(3&ut3))
msg2C3&ut3else if(arg.e1uals(3&op%3))msg2C3&op%3
else if(arg.e1uals(3Paste3))
msg2C3Paste3
else if(arg.e1uals(3Kirst3))msg2C3Kirst3
else if(arg.e1uals(3+econd3))
msg2C3+econd3else if(arg.e1uals(3$hird3))
msg2C3$hird3
else if(arg.e1uals(3ebug3))msg2C3ebug3
else if(arg.e1uals(3$esting3))
msg2C3$esting3
menuKrame.msgCmsgmenuKrame.repaint()
D
public void item+tate&hanged(Item/vent ie)
menuKrame.repaint()
D
Dpublic class ialogemo etends Applet
Krame f
public void init()
fCnew "enuKrame(3"enu emo3)int widthCInteger.parseInt(getParameter(3width3))
int heightCInteger.parseInt(getParameter(3height3))
set+i
7/25/2019 java file.doc
25/35
f.set;isible(false)
D
D
-1(P1(+
7/25/2019 java file.doc
26/35
import java.swing.E
import java.awt.Eimport java.net.Eimport java.awt.event.E
import java.io.E
@Eapplet codeC3&ustomerApplet.java3 heightC35HH3 widthC35HH3 @appletE@
class &ustomer etends bject implements java.io.+eriali$,#/+$
gbc.gridC4
gbc.grid%C?
g4.set&onstraints(label&ustame!gbc)
7/25/2019 java file.doc
27/35
panelbject.add(label&ustame)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC7
gbc.grid%C?
g4.set&onstraints(tet&ustame!gbc)
panelbject.add(tet&ustame)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC4
gbc.grid%CT
g4.set&onstraints(label&ustPassword!gbc)
panelbject.add(label&ustPassword)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC7
gbc.grid%CT
g4.set&onstraints(tet&ustPassword!gbc)
panelbject.add(tet&ustPassword)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC5
gbc.grid%C46
g4.set&onstraints(button9ogin!gbc)
panelbject.add(button9ogin)
9oginAction loginre1uestCnew 9oginAction()
button9ogin.addAction9istener(loginre1uest)
D
class 9oginAction implements Action9istener
public void actionPerformed(Action/vent evt)
bject objCevt.get+ource()
if(objCCbutton9ogin)
&ustomer dataCnew &ustomer()data.custameCtet&ustame.get$et()
data.custPasswordCnew +tring(tet&ustPassword.getPassword())
7/25/2019 java file.doc
28/35
tr%
+oc*et to+erver
to+erverCnew +oc*et(34T5.4LS.H.43!4HH4)bjectutput+tream stream$o+erverCnewbjectutput+tream(to+erver.getutput+tream())
stream$o+erver.writebject((&ustomer )data)
Buffered>eader from+erverCnew Buffered>eader(new
Input+tream>eader(to+erver.getInput+tream()))+tring statusCfrom+erver.read9ine()
getApplet&ontet().show+tatus(status)
stream$o+erver.close()from+erver.close()
D
catch(Invalid&lass/ception e)
getApplet&ontet().show+tatus(3$he &ustomer ame is ;alid32e)
D
catch(ot+eriali
7/25/2019 java file.doc
29/35
Program-L
@@Program to show Java etwor*ing-Java +oc*ets and >"I
import java.awt.event.E
import java.io.E
import java.net.Eclass &ustomer implements +eriali
7/25/2019 java file.doc
30/35
protected +oc*et net&lient
protected bjectInput+tream from&lient
protected Print+tream to&lientpublic &onnection(+oc*et client)
net&lientCclienttr% from&lientCnew bjectInput+tream(net&lient.getInput+tream())
to&lientCnew Print+tream(net&lient.getutput+tream())
D
catch(I/ception e) tr%
net&lient.close()
Dcatch(I/ception e4)
+%stem.err.println(3unable to set up streams32e4)
returnD
D
this.start()
Dpublic void run()
&ustomer client"essage
tr% for()
client"essageC(&ustomer)from&lient.readbject()
if(client"essageCCnull)
brea*to&lient.println(3>eceived
from832client"essage.custame)D
D
catch(I/ception e)
Dcatch(&lassotKound/ception e4)
+%stem.out.println(3/rror in reading object32e4)
Dfinall%
tr%
net&lient.close()D
catch(I/ception e)
D
DD
D
7/25/2019 java file.doc
31/35
import java.swing.E
import java.awt.Eimport java.net.Eimport java.awt.event.E
import java.io.E
@Eapplet codeC3&ustomerApplet.java3 heightC35HH3 widthC35HH3 @appletE@
class &ustomer etends bject implements java.io.+eriali$,#/+$
gbc.gridC4
gbc.grid%C?
g4.set&onstraints(label&ustame!gbc)
7/25/2019 java file.doc
32/35
panelbject.add(label&ustame)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC7
gbc.grid%C?
g4.set&onstraints(tet&ustame!gbc)
panelbject.add(tet&ustame)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC4
gbc.grid%CT
g4.set&onstraints(label&ustPassword!gbc)
panelbject.add(label&ustPassword)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC7
gbc.grid%CT
g4.set&onstraints(tet&ustPassword!gbc)
panelbject.add(tet&ustPassword)
gbc.anchorC=ridBag&onstraints.>$,#/+$
gbc.gridC5
gbc.grid%C46
g4.set&onstraints(button9ogin!gbc)
panelbject.add(button9ogin)
9oginAction loginre1uestCnew 9oginAction()
button9ogin.addAction9istener(loginre1uest)
D
class 9oginAction implements Action9istener
public void actionPerformed(Action/vent evt)
bject objCevt.get+ource()
if(objCCbutton9ogin)
&ustomer dataCnew &ustomer()data.custameCtet&ustame.get$et()
data.custPasswordCnew +tring(tet&ustPassword.getPassword())
7/25/2019 java file.doc
33/35
tr%
+oc*et to+erver
to+erverCnew +oc*et(34T5.4LS.H.43!4HH4)bjectutput+tream stream$o+erverCnewbjectutput+tream(to+erver.getutput+tream())
stream$o+erver.writebject((&ustomer )data)
Buffered>eader from+erverCnew Buffered>eader(new
Input+tream>eader(to+erver.getInput+tream()))+tring statusCfrom+erver.read9ine()
getApplet&ontet().show+tatus(status)
stream$o+erver.close()from+erver.close()
D
catch(Invalid&lass/ception e)
getApplet&ontet().show+tatus(3$he &ustomer ame is ;alid32e)
D
catch(ot+eriali
7/25/2019 java file.doc
34/35
-1(P1(+
7/25/2019 java file.doc
35/35
Program-R
@EProgram to develop a servlet that get invo*ed when a form on a web page in ,$"9 is
submitted.&reate a coo*ie object and enter@ displa% value for that coo*ie.E@
import java.io.E
import java.servlet.E
import java.servlet.http.Epublic class ,ello etends ,ttp+ervlet
public void do=et(,ttp+ervlet>/1uest re1!,ttp+ervlet>esponse res)throws
+ervlet/ception!I/ceptionres.set&ontent$%pe(3$et@,tml3)
Printwriter outCres.get#riter()
3html Nn323headtitle,ello@title@head Nn32
3bod% bgcolorC33
out.println(3,ello3)out.println(3@bod%@html3)
D