Java Reference
In-Depth Information
The
doPost()
and
doPut()
methodsalsohavesimilararchitectures.Eachmeth-
od first transforms the argument passed to its
source
parameter (which identifies
the XML body of the
POST
or
PUT
request) to a
javax.xml.transform.dom.DOMResult
instance. This instance is then
searchedviaXPathexpressions,firstforasingle
book
element,thenforthe
<book>
tag's
isbn
and
pubyear
attributes,andfinallyforthe
book
element'snested
title
,
author
, and
publisher
elements —multiple
author
elements might be present.
The gathered information is used to construct
Author
and
Book
objects, where the
Author
object(s)is/arestoredinthe
Book
object.Theresulting
Book
objectisstored
in the
library
map, the map is serialized to
library.ser
, and a suitable XML
message is sent to the client.
As well as providing a slightly different response message,
doPost()
and
doPut()
differ in whether or not the topic is already recorded (as determined by
its ISBN) in the map. If
doPost()
is called and an entry for the topic is in the
map,
doPost()
throws
HTTPException
with response code 400 (bad request). If
doPut()
is called and an entry for the topic is not in the map,
doPut()
throws the
same exception.
The
doPut()
methodisfollowedby
deserialize()
and
serialize()
meth-
odsthatareresponsiblefordeserializingaserializedlibrarymapfrom
library.ser
and serializing this map to
library.ser
, respectively. These methods accomplish
their tasks with the help of the
java.beans.XMLDecoder
and
java.beans.XMLEncoder
classes. According to their documentation,
XMLEn-
coder
and
XMLDecoder
aredesignedtoserializeaJavaBeancomponenttoanXML-
basedtextualrepresentationanddeserializethisrepresentationtoaJavaBeancompon-
ent, respectively.
JAVABEANS
JavaBeansistheJavaarchitectureforcreatingself-containedandreusablecompon-
ents,whichareknownas
beans
.Abeanisinstantiatedfromaclassthatadherestoat
least the following three conventions:
• The class must include a
public
noargument constructor.
• Eachoftheclass'spropertiesmustincludeanaccessormethodprefixedby
get
or
is
(foraBooleanproperty)andamutatormethodprefixedby
set
.
Thenameofthepropertywiththefirstletteruppercasedmustfollowthepre-
fix.Forexample,a
String name;
propertydeclarationwouldincludea