WKLVLVDQHZFKDSWHU
:HEDSSOLFDWLRQVDUHKRW6XUH*8,DSSOLFDWLRQVPLJKWXVHH[RWLF
6ZLQJZLGJHWVEXWKRZPDQ\*8,DSSVGR\RXNQRZWKDWDUHXVHGE\PLOOLRQV
RIXVHUVZRUOGZLGH"$VDZHEDSSGHYHORSHU\RXFDQIUHH\RXUVHOIIURPWKH
JULSRIGHSOR\PHQWSUREOHPVDOOVWDQGDORQHDSSVKDYHDQGGHOLYHU\RXUDSSWR
DQ\RQHZLWKDEURZVHU%XWWREXLOGDWUXO\SRZHUIXOZHEDSS\RXQHHG-DYD
<RXQHHGVHUYOHWV<RXQHHG-63V%HFDXVHSODLQROGVWDWLF+70/SDJHVDUHVR
ZHOO7RGD\¶VXVHUVH[SHFWVLWHVWKDWDUHG\QDPLFLQWHUDFWLYHDQGFXVWRP
WDLORUHG:LWKLQWKHVHSDJHV\RX¶OOOHDUQWRPRYHIURPZHEVLWHWRZHEDSS
:K\XVH6HUYOHWV-63V"
LQWURDQGRYHUYLHZ
<RXIRRO<RXPXVWXVH
6HUYOHWVDQG-63V,I\RX
FRQWLQXHWRZULWH3HUO
VFULSWV,ZLOOGHVWUR\
\RX
+DK,NQRZ
&*,0\ZHEVLWHZLOO
UXOHWKHZRUOG
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
)RUHDFKRIWKH+7730HWKRGVVXFKDV*(7
3267+($'DQGVRRQ
'HVFULEHEHQH¿WVRIWKH+7730HWKRG
'HVFULEHIXQFWLRQDOLW\RIWKH+7730HWKRG
/LVWWULJJHUVWKDWPLJKWFDXVHD&OLHQW
XVXDOO\D:HEEURZVHUWRXVHWKHPHWKRG
$OVRSDUWRI2EMHFWLYHEXWQRWFRYHUHG
LQWKLVFKDSWHU
,GHQWLI\WKH+WWS6HUYOHWPHWKRGWKDW
FRUUHVSRQGVWRWKH+7730HWKRG
6HUYOHWV-63RYHUYLHZ
RI¿FLDO6XQH[DPREMHFWLYHV
4HEOBJECTIVESINTHISSECTIONARECOVERED
COMPLETELYINANOTHERCHAPTERSOTHINKOF
THISCHAPTERASAlRSTLOOKFOUNDATIONFOR
WHATCOMESLATER)NOTHERWORDSDONTWORRY
ABOUTlNISHINGTHISCHAPTERKNOWINGAND
REMEMBERINGANYTHINGSPECIlCFROMTHESE
OBJECTIVESJUSTUSEITFORBACKGROUND)FYOU
ALREADYKNOWTHESETOPICSYOUCANJUSTSKIM
THISCHAPTERANDJUMPTOCHAPTER
9OUWONTHAVEANYMOCKEXAMQUESTIONSON
THESETOPICSUNTILYOUGETTOTHEMORESPECIlC
CHAPTERWHERETHOSETOPICSARECOVERED
&RYHUDJH1RWHV
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
Copyright Safari Books Online #747221
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
-^MZaJWLa_IV\[I_MJ[Q\M
9OUHAVEAKILLERIDEAFORAWEBSITE4ODESTROYTHECOMPETITIONYOU
NEEDAmEXIBLESCALABLEARCHITECTURE9OUNEEDSERVLETSAND*30S
"EFOREWESTARTBUILDINGLETSTAKEALOOKATTHE7ORLD7IDE7EB
FROMABOUTKFEET7HATWECAREMOSTABOUTINTHISCHAPTERARE
HOWWEBCLIENTSANDWEBSERVERSTALKTOONEANOTHER
4HESENEXTSEVERALPAGESAREPROBABLYALLREVIEWFORYOUESPECIALLYIF
YOUREALREADYAWEBAPPLICATIONDEVELOPERBUTITLLGIVEUSACHANCE
TOEXPOSESOMEOFTHETERMINOLOGYWEUSETHROUGHOUTTHEBOOK
The web consists of gazillions of
clients (using browsers like Mozilla or
Safari) and servers (using web server
apps like Apache) connected through
wires and wireless networks. Our goal is
to build a web application that clients
around the globe can access. And to
become obscenely rich.
:HE
EURZVHU
:HE
EURZVHU
:HE
EURZVHU
:HE
EURZVHU
:HE
EURZVHU
the earth
&OLHQW
&OLHQW
&OLHQW
&OLHQW
&OLHQW
6HUYHU
6HUYHU
6HUYHU
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
!WEBBROWSERLETSAUSERREQUESTARESOURCE4HEWEBSERVERGETSTHE
REQUESTlNDSTHERESOURCEANDRETURNSSOMETHINGTOTHEUSER
3OMETIMESTHATRESOURCEISAN(4-,PAGE3OMETIMESITSAPICTURE/R
ASOUNDlLE/REVENA0$&DOCUMENT$OESNTMATTERTHECLIENTASKS
FORTHETHINGRESOURCEANDTHESERVERSENDSITBACK
5NLESSTHETHINGISNTTHERE/RATLEASTITSNOTWHERETHESERVERIS
EXPECTINGITTOBE9OUREOFCOURSEQUITEFAMILIARWITHTHEh.OT
&OUNDvERRORTHERESPONSEYOUGETWHENTHESERVERCANTlNDWHATIT
THINKSYOUASKEDFOR
7HENWESAYhSERVERvWEMEANEITHERTHEPHYSICALMACHINEHARDWARE
ORTHEWEBSERVERAPPLICATIONSOFTWARE4HROUGHOUTTHEBOOKIF
THEDIFFERENCEBETWEENSERVERHARDWAREANDSOFTWAREMATTERSWELL
EXPLICITLYSAYWHICHONEHARDWAREORSOFTWAREWERETALKINGABOUT
?PI\LWM[aW]Z_MJ[MZ^MZLW'
$ZHEVHUYHUWDNHVDFOLHQWUHTXHVWDQGJLYHV
VRPHWKLQJEDFNWRWKHFOLHQW
UHTXHVW
UHVSRQVH
&OLHQW
:HE
EURZVHU
6HUYHU
The clientÕs request contains the
name and address (the URL), of
the thing the client is looking for.
The serverÕs response contains the
actual document that the client
requested (or an error code if the
request could not be processed).
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
P\UHTXHVW
LV[\]
The server usually has
lots of ÒcontentÓ that
it can send to clients.
That content can be
web pages, JPEGs, and
other resources.
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
ZHEVHUYHU
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
$ZHEFOLHQWOHWVWKHXVHUUHTXHVWVRPHWKLQJRQWKH
VHUYHUDQGVKRZVWKHXVHUWKHUHVXOWRIWKHUHTXHVW
7HENWETALKABOUTCLIENTSTHOUGHWEUSUALLYMEANBOTHOREITHER
THEHUMANUSERANDTHEBROWSERAPPLICATION
4HEBROWSERISTHEPIECEOFSOFTWARELIKE.ETSCAPEOR-OZILLATHAT
KNOWSHOWTOCOMMUNICATEWITHTHESERVER4HEBROWSERSOTHERBIGJOB
ISINTERPRETINGTHE(4-,CODEANDRENDERINGTHEWEBPAGEFORTHEUSER
3OFROMNOWONWHENWEUSETHETERMCLIENTWEUSUALLYWONTCARE
WHETHERWERETALKINGABOUTTHEHUMANUSERORTHEBROWSERAPP)N
OTHERWORDSTHECLIENTISTHEBROWSERAPPDOINGWHATTHEUSERASKEDITTODO
?PI\LWM[I_MJKTQMV\LW'
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
P\UHTXHVW
LV[\]
8VHUFOLFNVDOLQN
LQWKHEURZVHU
8VHU
%URZVHUIRUPDWVWKH
UHTXHVWDQGVHQGVLW
WRWKHVHUYHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
6HUYHUÀQGVWKH
UHTXHVWHGSDJH
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
%URZVHUJHWVWKH+70/
DQGUHQGHUVLWLQWRD
GLVSOD\IRUWKHXVHU
8VHU
6HUYHUIRUPDWVWKH
UHVSRQVHDQGVHQGVLW
WRWKHFOLHQWEURZVHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
FOLFN
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
+TQMV\[IVL[MZ^MZ[
SVW_0<54IVL0<<8
%XWKRZGRWKH
FOLHQWVDQGVHUYHUV
WDONWRHDFKRWKHU"
$ZLVHTXHVWLRQ,QRUGHU
WRFRPPXQLFDWHWKH\PXVW
VKDUHDFRPPRQODQJXDJH2Q
WKHZHEFOLHQWVDQGVHUYHUV
PXVWVSHDN+773DQGEURZVHUV
PXVWNQRZ+70/
-OSTOFTHECONVERSATIONSHELDONTHE
WEBBETWEENCLIENTSANDSERVERSARE
HELDUSINGTHE(440PROTOCOLWHICH
ALLOWSFORSIMPLEREQUESTANDRESPONSE
CONVERSATIONS4HECLIENTSENDSAN
(440REQUESTANDTHESERVERANSWERS
WITHAN(440RESPONSE"OTTOMLINE
IFYOUREAWEBSERVERYOUSPEAK(440
7HENAWEBSERVERSENDSAN(4-,
PAGETOTHECLIENTITSENDSITUSING
(4409OULLSEETHEDETAILSONHOW
ALLTHISWORKSINTHENEXTFEWPAGES
&9)(440STANDSFOR(YPER4EXT
4RANSPORT0ROTOCOL
+773
7HENASERVERANSWERSAREQUEST
THESERVERUSUALLYSENDSSOMETYPE
OFCONTENTTOTHEBROWSERSOTHATTHE
BROWSERCANDISPLAYIT3ERVERSOFTEN
SENDTHEBROWSERASETOFINSTRUCTIONS
WRITTENIN(4-,THE(YPER4EXT
-ARKUP,ANGUAGE4HE(4-,
TELLSTHEBROWSERHOWTOPRESENTTHE
CONTENTTOTHEUSER
!LLWEBBROWSERSKNOWWHATTODO
WITH(4-,ALTHOUGHSOMETIMESAN
OLDERBROWSERMIGHTNOTUNDERSTAND
PARTSOFAPAGETHATWASWRITTENUSING
NEWERVERSIONSOF(4-,
+70/
+70/WHOOVWKHEURZVHU
KRZWRGLVSOD\WKHFRQWHQW
WRWKHXVHU
+773LVWKHSURWRFRO
FOLHQWVDQGVHUYHUVXVHRQ
WKHZHEWRFRPPXQLFDWH
7KHVHUYHUXVHV+773WR
VHQG+70/WRWKHFOLHQW
+70/DQG+773
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
<_WUQV]\M0<54O]QLM
7HENYOUDEVELOPAWEBPAGEYOUUSE(4-,TODESCRIBEWHATTHE
PAGESHOULDLOOKLIKEANDHOWITSHOULDBEHAVE
(4-,HASDOZENSOFTAGSANDHUNDREDSOFTAGATTRIBUTES4HEGOALOF
(4-,ISTOTAKEATEXTDOCUMENTANDADDTAGSTHATTELLTHEBROWSER
HOWTOFORMATTHETEXT"ELOWARETHETAGSWEUSEINTHENEXTSEVERAL
CHAPTERS)FYOUNEEDAMORECOMPLETEUNDERSTANDINGOF(4-,WE
RECOMMENDTHEBOOK(4-,8(4-,4HE$ElNITIVE'UIDE
!
D!
DOLJQ!
ERG\!
EU!
FHQWHU!
IRUP!
K!
KHDG!
KWPO!
LQSXWW\SH!
S!
WLWOH!
WHEREYOUPUTYOURCOMMENTS
ANCHORUSUALLYFORPUTTINGINAHYPERLINK
ALIGNTHECONTENTSLEFTRIGHTCENTEREDORJUSTIlED
DElNETHEBOUNDARIESOFTHEDOCUMENTSBODY
ALINEBREAK
CENTERTHECONTENTS
DElNEAFORMWHICHUSUALLYPROVIDESINPUTlELDS
THElRSTLEVELHEADING
DElNETHEBOUNDARIESOFTHEDOCUMENTSHEADER
DElNETHEBOUNDARIESOFTHE(4-,DOCUMENT
DElNESANINPUTWIDGETTOAFORM
ANEWPARAGRAPH
THE(4-,DOCUMENTSTITLE
7DJ
'HVFULSWLRQ
(Technically, the <center> and <align> tags
have been deprecated in HTML 4.0, but weÕre
using them in some of our examples because itÕs
simpler to read than the alternative, and youÕre
not here to learn HTML anyway.)
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
KWPO!
6RPHVDPSOH+70/!
KHDG!
WLWOH!$/RJLQ3DJHWLWOH!
KHDG!
ERG\!
KDOLJQ ´FHQWHU´!6N\OHU¶V/RJLQ3DJHK!
SDOLJQ ´ULJKW´!
LPJVUF ´6.</(5MSJ´ZLGWK ´´KHLJKW ´´!
S!
IRUPDFWLRQ ´GDWH´!
1DPHLQSXWW\SH ´WH[W´QDPH ´SDUDP´!EU!
3DVVZRUGLQSXWW\SH ´WH[W´QDPH ´SDUDP´!EU!EU!EU!
FHQWHU!
LQSXWW\SH ´68%0,7´!
FHQWHU!
IRUP!
ERG\!
KWPO!
The <img> tag is nested
inside a paragraph <align>
tag in order to place
the image roughly where
we want it. (Remember,
<align> is deprecated, but
weÕre using it because itÕs
simple to read.)
The ÒsubmitÓ button
in the form.
?PI\aW]_ZQ\M
\PM0<54
The <br> tags
cause line breaks.
An HTML comment
The servlet to send
the request to.
)MAGINEYOURECREATINGALOGINPAGE4HESIMPLE(4-,
MIGHTLOOKSOMETHINGLIKETHIS
$
%
&
'
(
WeÕll talk more about
forms later, but brießy,
the browser can collect
the userÕs input and
return it to the server.
<RXQHHGRQO\WKHPRVWEDVLF+70/
NQRZOHGJH
(4-,POPSUPALLOVERTHEEXAM"UTYOURE
NOTBEINGTESTEDONYOUR(4-,KNOWLEDGE9OULLSEE
(4-,INTHECONTEXTOFALARGECHUNKOFQUESTIONS
THOUGHSOYOUNEEDATLEASTSOMEIDEAOFWHATS
HAPPENINGWHENYOUSEESIMPLE(4-,
ZULWLQJ
+70/
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
?PI\\PMJZW_[MZKZMI\M[
4HEBROWSERREADSTHROUGHTHE(4-,CODECREATESTHE
WEBPAGEANDRENDERSITTOTHEUSERSDISPLAY
$
%
&
'
(
KWWSZZZZLFNHGO\VPDUWFRPVN\ORJLQKWPO
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
(440RUNSONTOPOF4#0)0)FYOURENOTFAMILIARWITH
THOSENETWORKINGPROTOCOLSHERESTHECRASHCOURSE4#0
ISRESPONSIBLEFORMAKINGSURETHATAlLESENTFROMONE
NETWORKNODETOANOTHERENDSUPASACOMPLETElLEATTHE
DESTINATIONEVENTHOUGHTHElLEISSPLITINTOCHUNKSWHEN
ITSSENT)0ISTHEUNDERLYINGPROTOCOLTHATMOVESROUTES
THECHUNKSPACKETSFROMONEHOSTTOANOTHERONTHEIR
WAYTOTHEDESTINATION(440THENISANOTHERNETWORK
PROTOCOLTHATHAS7EBSPECIlCFEATURESBUTITDEPENDS
ON4#0)0TOGETTHECOMPLETEREQUESTANDRESPONSE
FROMONEPLACETOANOTHER4HESTRUCTUREOFAN(440
CONVERSATIONISASIMPLE2EQUEST2ESPONSESEQUENCEA
BROWSERREQUESTSANDASERVERRESPONDS
?PI\Q[\PM0<<8XZW\WKWT'
<RXGRQ¶WKDYHWRPHPRUL]HWKH+773VSHF
4HE(440PROTOCOLISAN)%4&STANDARD2&#)FYOUCARE
&ORTUNATELYTHEEXAMDOESNTEXPECTYOUTO!PACHEISANEXAMPLE
OFA7EBSERVERTHATPROCESSES(440REQUESTS-OZILLAISANEXAMPLE
OFA7EBBROWSERTHATPROVIDESTHEUSERWITHTHEMEANSTOMAKE(440
REQUESTSANDTOVIEWTHEDOCUMENTSRETURNEDBYTHESERVER
+773UHTXHVW
+773UHVSRQVH
&OLHQW
:HE
EURZVHU
6HUYHU
.H\HOHPHQWVRIWKHUHTXHVW
VWUHDP
+773PHWKRGWKHDFWLRQWR
EHSHUIRUPHG
7KHSDJHWRDFFHVVD85/
)RUPSDUDPHWHUVOLNH
DUJXPHQWVWRDPHWKRG
.H\HOHPHQWVRIWKH
UHVSRQVHVWUHDP
$VWDWXVFRGHIRUZKHWKHU
WKHUHTXHVWZDVVXFFHVVIXO
&RQWHQWW\SHWH[WSLFWXUH
+70/HWF
7KHFRQWHQWWKHDFWXDO
+70/LPDJHHWF
+773SURWRFRO
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
0<54Q[XIZ\WN\PM0<<8ZM[XWV[M
!N(440RESPONSECANCONTAIN(4-,(440ADDSHEADER
INFORMATIONTOTHETOPOFWHATEVERCONTENTISINTHERESPONSEIN
OTHERWORDSTHETHINGCOMINGBACKFROMTHESERVER!N(4-,
BROWSERUSESTHATHEADERINFOTOHELPPROCESSTHE(4-,PAGE4HINK
OFTHE(4-,CONTENTASDATAPASTEDINSIDEAN(440RESPONSE
+773UHTXHVW
+773UHVSRQVH
&OLHQW
:HE
EURZVHU
6HUYHU
When the browser gets to an
image tag, it generates another
HTTP request to go get the
resource described. In this case
the browser will make a second
HTTP request to get the picture
referenced in the <img> tag.
When the browser Þ nds the
opening <html> tag it goes into
HTML-rendering mode and
displays the page to the user.
+773KHDGHULQIR
KWPO!
KHDG!
KHDG!
ERG\!
LPJVUF !
ERG\!
KWPO!
HTTP header
HTTP body
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
1N\PI\¼[\PMZM[XWV[M_PI\¼[QV\PMZMY]M[\'
4HElRSTTHINGYOULLlNDISAN(440METHODNAME4HESEARENT*AVA
METHODSBUTTHEIDEAISSIMILAR4HEMETHODNAMETELLSTHESERVERTHE
KINDOFREQUESTTHATSBEINGMADEANDHOWTHERESTOFTHEMESSAGEWILLBE
FORMATTED4HE(440PROTOCOLHASSEVERALMETHODSBUTTHEONESYOULL
USEMOSTOFTENARE'%4AND0/34
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
%URZVHUVHQGVDQ+773*(7
WRWKHVHUYHUDVNLQJWKH
VHUYHUWR*(7WKHSDJH
8VHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
8VHUFOLFNV
DOLQNWRD
QHZSDJH
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
%URZVHUVHQGVDQ+7733267
WRWKHVHUYHUJLYLQJWKH
VHUYHUZKDWWKHXVHUW\SHG
LQWRWKHIRUP
8VHU
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
8VHUW\SHVLQD
IRUPDQGKLWVWKH
6XEPLWEXWWRQ
*(7
3267
*(7
3267
+773PHWKRGV
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
/-<Q[I[QUXTMZMY]M[\
87;<KIV[MVL][MZLI\I
'%4ISTHESIMPLEST(440METHODANDITS
MAINJOBINLIFEISTOASKTHESERVERTOGETA
RESOURCEANDSENDITBACK4HATRESOURCEMIGHT
BEAN(4-,PAGEA*0%'A0$&ETC$OESNT
MATTER4HEPOINTOF'%4ISTOGETSOMETHING
BACKFROMTHESERVER
0/34ISAMOREPOWERFULREQUEST)TSLIKEA
'%4PLUSPLUS7ITH0/34YOUCANREQUEST
SOMETHINGANDATTHESAMETIMESENDFORMDATA
TOTHESERVERLATERINTHISCHAPTERWELLSEEWHAT
THESERVERMIGHTDOWITHTHATDATA
:DLWDPLQXWH,
FRXOGVZHDU,·YHVHHQ
*(7UHTXHVWVWKDW
GLG
VHQGVRPHSDUDPHWHU
GDWDWRWKHVHUYHU
WKHUH DUH QR
'
XPE 4XHVWLRQV
4
3OWHATABOUTTHEOTHER(440METHODS
BESIDES'%4AND0/34
$
4HOSEARETHETWOBIGONESTHATEVERYBODY
USES"UTTHEREAREAFEWRARELYUSEDMETHODSAND
3ERVLETSCANHANDLETHEMINCLUDING(%!$42!#%
054$%,%4%/04)/.3AND#/ %#4
9OUREALLYDONTNEEDTOKNOWMUCHABOUTTHESE
OTHERSFORTHEEXAMALTHOUGHYOUMIGHTSEE
THEMAPPEARINAQUESTION4HE,IFEAND$EATH
OFA3ERVLETCHAPTERCOVERSTHERESTOFTHE(440
METHODDETAILSYOULLNEED
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
1\¼[\Z]MaW]KIV[MVLITQ\\TMLI\I_Q\P0<<8/-<
"UTYOUMIGHTNOTWANTTO2EASONSYOUMIGHTUSE0/34INSTEADOF'%4INCLUDE
The original URL before the
extra parameters.
The Ò?Ó separates the path and the parameters
(the extra data). The amount of data you
can send along with the GET is limited, and
itÕs exposed up here in the browser bar for
everyone to see. Together, the entire String is
the URL that is sent with the request.
4HETOTALAMOUNTOFCHARACTERSINA'%4ISREALLYLIMITEDDEPENDING
ONTHESERVER)FTHEUSERTYPESSAYALONGPASSAGEINTOAhSEARCHv
INPUTBOXTHE'%4MIGHTNOTWORK
4HEDATAYOUSENDWITHTHE'%4ISAPPENDEDTOTHE52,UPIN
THEBROWSERBARSOWHATEVERYOUSENDISEXPOSED"ETTERNOTPUTA
PASSWORDORSOMEOTHERSENSITIVEDATAASPARTOFA'%4
And if you need help with the exam, check out
javaranch which also includes 100% unbiased
recommendations to buy whatever books the
authors wrote.
+773*(7
"ECAUSEOFNUMBERTWOABOVETHEUSERCANT
BOOKMARKAFORMSUBMISSIONIFYOUUSE0/34
INSTEADOF'%4$EPENDINGONYOURAPPYOUMAY
ORMAYNOTWANTUSERSTOBEABLETOBOOKMARKTHE
RESULTINGREQUESTFROMAFORMSUBMISSION
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
)VI\WUaWNIV0<<8/-<ZMY]M[\
*(7VHOHFWVHOHFW%HHU7DVWHMVS"FRORU GDUNWDVWH PDOW\+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK833&0DF26;0DFK2HQ86UY*HFNR
1HWVFDSH
$FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W
SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T
$FFHSW/DQJXDJHHQXVHQT
$FFHSW(QFRGLQJJ]LSGHӿDWH
$FFHSW&KDUVHW,62XWIT T
.HHS$OLYH
&RQQHFWLRQNHHSDOLYH
In a GET request, parameters (if
there are any) are appended to
the Þ rst part of the request URL,
starting with a Ò?Ó. Parameters are
separated with an ampersand Ò
µ.
The HTTP
Method.
The path to the
resource on the
web server.
The protocol version
that the web browser
is requesting.
The Request line.
The Request
headers.
4HEPATHTOTHERESOURCEANDANYPARAMETERSADDEDTO
THE52,AREALLINCLUDEDONTHEhREQUESTLINEv
&OLHQW
:HE
EURZVHU
6HUYHU
+773UHTXHVW
+H\VHUYHU*(7PHWKHSDJH
RQWKLVKRVWWKDW·VDWVHOHFW
VHOHFW%HHU7DVWHKWPODQGRK\HDK
KHUHDUHWKHSDUDPHWHUVIRU\RX
FRORU GDUNWDVWH PDOW\$QG
KXUU\LWXS
6XUH,·OOJR*(7
WKDWSDJHDQGWKDQNVIRU
WKHSDUDPHWHUV$QGMXVW)<,
´KXUU\LWXSµLVQRWSDUWRI
WKH+773SURWRFRO
*(7
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
3267DGYLVRUVHOHFW%HHU7DVWHGR+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK833&0DF26;0DFK2HQ86UY*HFNR
1HWVFDSH
$FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W
SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T
$FFHSW/DQJXDJHHQXVHQT
$FFHSW(QFRGLQJJ]LSGHӿDWH
$FFHSW&KDUVHW,62XWIT T
.HHS$OLYH
&RQQHFWLRQNHHSDOLYH
FRORU GDUNWDVWH PDOW\
)VI\WUaWNIV0<<887;<ZMY]M[\
The Request line.
The Request
headers.
The HTTP
Method.
The path to the resource
on the web server.
The protocol version
that the web
browser is requesting.
The message body,
sometimes called
the ÒpayloadÓ.
(4400/34REQUESTSAREDESIGNEDTOBEUSEDBYTHEBROWSERTO
MAKECOMPLEXREQUESTSONTHESERVER&ORINSTANCEIFAUSERHASJUST
COMPLETEDALONGFORMTHEAPPLICATIONMIGHTWANTALLOFTHEFORMS
DATATOBEADDEDTOADATABASE4HEDATATOBESENTBACKTOTHESERVER
ISKNOWNASTHEhMESSAGEBODYvORhPAYLOADvANDCANBEQUITELARGE
This time, the parameters are down here
in the body, so they arenÕt limited the
way they are if you use a GET and have
to put them in the Request line.
&OLHQW
:HE
EURZVHU
6HUYHU
+773UHTXHVW
+H\VHUYHUSOHDVH3267WKLV
WRWKHUHVRXUFHDWDGYLVRU
VHOHFW%HHU7DVWHGR'RQ·WIRUJHW
WRORRNLQVLGHWKHERG\IRUWKH
LPSRUWDQWGDWD,·PVHQGLQJ
6XUH ,·OOÀQGWKDW
UHVRXUFHLW·VDFWXDOO\D
OLWWOHDSSOLFDWLRQDQGZKHQ,
GR,·OOJLYHLWWKHGDWDLQWKH
UHTXHVWERG\\RXVHQW
3267
+7733267
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
)VI\WUaWNIV0<<8ZM[XWV[MIVL
_PI\\PMPMKSQ[I¹515-\aXMº'
The HTTP status code
for the Response.
The protocol version that
the web server is using.
A text version of
the status code.
+7732.
6HW&RRNLH-6(66,21,' $$%&'((()&)%)&$&3DWK WHVW(/
&RQWHQW7\SHWH[WKWPO
&RQWHQW/HQJWK
'DWH:HG1RY*07
6HUYHU$SDFKH&R\RWH
&RQQHFWLRQFORVH
KWPO!
KWPO!
The body holds the
HTML, or other
content to be
rendered
The content-type response headerÕs
value is known as a MIME type. The
MIME type tells the browser what
kind of data the browser is about to
receive so that the browser will know
how to render it.
Notice that the MIME type value
relates to the values listed in the
HTTP requestÕs ÒAcceptÓ header.
(Go look at the Accept header from
the previous pageÕs POST request.)
.OWTHATWEVESEENTHEREQUESTSFROMTHEBROWSERTOTHESERVERLETS
LOOKATWHATTHESERVERSENDSBACKINRESPONSE!N(440RESPONSE
HASBOTHAHEADERANDABODY4HEHEADERINFOTELLSTHEBROWSER
ABOUTTHEPROTOCOLBEINGUSEDWHETHERTHEREQUESTWASSUCCESSFUL
ANDWHATKINDOFCONTENTISINCLUDEDINTHEBODY4HEBODYCONTAINS
THECONTENTSFOREXAMPLE(4-,FORTHEBROWSERTODISPLAY
&OLHQW
:HE
EURZVHU
6HUYHU
+773UHVSRQVH
HTTP
Response
headers.
+HUH·VP\UHVSRQVH
WR\RXUUHTXHVW,WV
W\SHLVWH[WKWPOEXWLQ\RXU
UHTXHVW\RXVDLGWKDWZDV
2.6RXQOHVV\RXZHUH
O\LQJWRPH
+7732.
KWPO!
KHDG!
KHDG!
ERG\!
LPJVUF !
ERG\!
KWPO!
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
KWPO!ERG\!
KDOLJQ FHQWHU!%HHU/RJLQ3DJHK!
IRUP!
6HOHFWDEHHUW\SHRUEX\EHHU
PDNLQJVXSSOLHV"S!
LQSXWW\SH UDGLRQDPH VHOHFW
YDOXH 6HOHFW!6HOHFWDEHHUEU!
LQSXWW\SH UDGLRQDPH VHOHFW
YDOXH %X\!%X\VXSSOLHVEU!EU!
FHQWHU!
LQSXWW\SH 68%0,7!
FHQWHU!
IRUP!
ERG\!KWPO!
)TT\PMXQMKM[
7VWVMXIOM
6HUYHU
&OLHQW
:HE
EURZVHU
The HTTP
response is sent
to the browser.
And generates an
HTTP response.
The server Þ nds
the page
The HTTP GET is
sent to the server.
The browser creates an
HTTP GET request.
The browser
renders the
HTML.
*MMZP\UT
The user
types a URL.
*(7WHVW%HHUKWPO+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK
+7732.
6HW&RRNLH
KWPO!ERG\!
KDOLJQ FHQWHU!%HHU/RJLQ3DJHK!
IRUP!
6HOHFWDEHHUW\SHRUEX\EHHU
Client looks
forward to a
successful beer
transaction.
UHTXHVWDQGUHVSRQVH
KWWSZZZZLFNHGO\VPDUWFRPWHVW%HHUKWPO
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
$XVHULVUHWXUQLQJDORJLQQDPHDQGSDVVZRUG
$XVHULVUHTXHVWLQJDQHZSDJHYLDDK\SHUOLQN
$FKDWURRPXVHULVVHQGLQJDZULWWHQUHVSRQVH
$XVHUKLWVWKHµQH[W¶EXWWRQWRVHHWKHQH[WSDJH
$XVHUKLWVWKHµORJRXW¶EXWWRQRQDVHFXUHEDQNLQJVLWH
$XVHUKLWVWKHµEDFN¶EXWWRQRQWKHEURZVHU
$XVHUVHQGVDQDPHDQGDGGUHVVIRUPWRWKHVHUYHU
$XVHUPDNHVDUDGLREXWWRQVHOHFWLRQ
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
*(7RU3267"
)RUHDFKGHVFULSWLRQFLUFOHHLWKHU3267RU*(7
GHSHQGLQJRQZKLFK+773PHWKRG\RX·GFKRRVH
IRULPSOHPHQWLQJWKDWIXQFWLRQDOLW\,I\RXWKLQNLW
FRXOGEHHLWKHUFLUFOHERWK%XWEHSUHSDUHGWR
GHIHQG\RXUDQVZHUV
6KDUSHQ \RXU SHQFLO
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
=:4?PI\M^MZaW]LWLWV¼\XZWVW]VKMQ\¹-IZTº
7HENYOUGETTOTHE5SINTHEACRONYMDICTIONARYTHERESATRAFlCJAM52)
52,52.WHEREDOESITEND&ORNOWWEREGOINGTOFOCUSONTHE52,SOR
5NIFORM2ESOURCE,OCATORSTHATYOUKNOWANDLOVE%VERYRESOURCEONTHEWEB
HASITSOWNUNIQUEADDRESSINTHE52,FORMAT
KWWSZZZZLFNHGO\VPDUWFRPEHHUDGYLFHVHOHFWEHHUKWPO
Port
:
This part of the URL is
optional. A single server supports
many ports. A server application is
identiÞed by a port. If you donÕt
specify a port in your URL, then
port 80 is the default, and as luck
would have it, thatÕs the default
port for web servers.
Server
: The unique name of
the physical server youÕre looking
for. This name maps to a unique
IP address. IP addresses are
numeric and take the form Òxxx.
yyy.zzz.aaaÓ. You can specify an
IP address here instead of a server
name, but a server name is a lot
easier to remember.
Path
: The path to the location,
on the server, of the resource
being requested. Because most of
the early servers on the web ran
Unix, Unix syntax is still used to
describe the directory hierarchies
on the web server.
Protocol:
Tells the
server which communications
protocol (in this case
HTTP) will be used.
Resource:
The name of the
content being requested. This
could be an HTML page, a
servlet, an image, PDF, music,
video, or anything else the
server feels like serving. If this
optional part of the URL is
left out, most web servers will
look for index.html by default.
DQDWRP\RID85/
Optional Query String
:
Remember, if this was a
GET request, the extra info
(parameters) would be appended
to the end of this URL, starting
with a question mark Ò?Ó, and with
each parameter (name/value pair)
separated by an ampersand Ò
Ó.
Not shown:
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
$7&3SRUWLVMXVWDQXPEHU
$ELWQXPEHUWKDWLGHQWLILHVD
VSHFLILFVRIWZDUHSURJUDPRQWKH
VHUYHUKDUGZDUH
9OURINTERNETWEB(440SERVERSOFTWARERUNSONPORT
4HATSASTANDARD)FYOUVEGOTA4ELNETSERVERITS
RUNNINGONPORT&400/0MAILSERVER
3-404HE4IMESERVERSITSAT4HINKOFPORTSAS
UNIQUEIDENTIlERS!PORTREPRESENTSALOGICALCONNECTION
TOAPARTICULARPIECEOFSOFTWARERUNNINGONTHESERVER
HARDWARE4HATSIT9OUCANTSPINYOURHARDWAREBOX
AROUNDANDlNDA4#0PORT&ORONETHINGYOUHAVE
OFTHEMONASERVERTO&ORANOTHER
THEYDONOTREPRESENTAPLACETOPLUGINPHYSICALDEVICES
4HEYREJUSTNUMBERSREPRESENTINGASERVERAPPLICATION
7ITHOUTPORTNUMBERSTHESERVERWOULDHAVENOWAYOF
KNOWINGWHICHAPPLICATIONACLIENTWANTEDTOCONNECTTO
!NDSINCEEACHAPPLICATIONMIGHTHAVEITSOWNUNIQUE
PROTOCOLTHINKOFTHETROUBLEYOUDHAVEWITHOUTTHESE
IDENTIlERS7HATIFYOURWEBBROWSERFOREXAMPLELANDED
ATTHE0/0MAILSERVERINSTEADOFTHE(440SERVER4HE
MAILSERVERWONTKNOWHOWTOPARSEAN(440REQUEST
!NDEVENIFITDIDTHE0/0SERVERDOESNTKNOWN
ANYTHINGABOUTSERVINGBACKAN(4-,PAGE
)FYOUREWRITINGSERVICESSERVERPROGRAMSTORUNONA
COMPANYNETWORKYOUSHOULDCHECKWITHTHESYSADMINS
TOlNDOUTWHICHPORTSAREALREADYTAKEN9OURSYSADMINS
MIGHTTELLYOUFOREXAMPLETHATYOUCANTUSEANYPORT
NUMBERBELOWSAY
23
21
25
37
110
FTP
Telnet
POP3
SMTP
Time
80
443
HTTP
HTTPS
6HUYHU
:HOONQRZQ7&3SRUWQXPEHUV
IRUFRPPRQVHUYHUDSSOLFDWLRQV
7KH7&3SRUWQXPEHUV
IURPWRDUH
UHVHUYHGIRUZHOONQRZQ
VHUYLFHVLQFOXGLQJWKH
%LJ2QHZHFDUHDERXW³
SRUW'RQ·WXVHWKHVH
SRUWVIRU\RXURZQ
FXVWRPVHUYHUSURJUDPV
Using one server app per port, a server
can have up to 65536 different server
apps running (although itÕs possible to run
more than one app on the same port if
the apps use different protocols).
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
,QZMK\WZa[\Z]K\]ZMNWZI[QUXTM)XIKPM_MJ[Q\M
7ELLTALKMOREABOUT!PACHEAND4OMCATLATERBUTFORNOWLETSASSUMETHATOURSIMPLE
WEBSITEISUSING!PACHETHEEXTREMELYPOPULAROPENSOURCEWEBSERVERYOUREPROBABLY
ALREADYUSING7HATWOULDTHEDIRECTORYSTRUCTURELOOKLIKEFORAWEBSITECALLEDWWW
WICKEDLYSMARTCOMHOSTINGTWOAPPLICATIONSONEGIVINGSKIINGADVICEANDTHEOTHER
BEERRELATEDADVICE)MAGINETHATTHE!PACHEAPPLICATIONISRUNNINGONPORT
4HEHTMLPAGESAREEACHMARKEDWITHALETTER!"#$FORTHEEXERCISEONTHE
OPPOSITEPAGE
,QGH[KWPO
KWPO!
KWPO!
VHOHFW%HHUKWPO
VHOHFW
VHOHFW
EHHU$GYLFH
VNLLQJ$GYLFH
KWGRFV
$SDFKHBKRPH
FKHFNRXW
,QGH[KWPO
,QGH[KWPO
Òindex.htmlÓ is the default
page that will be returned
to a user who keys
Òwww.wickedlysmart.com/Ó
into his browser.
The root folder
for the beerAdvice
application.
An Apache server will assume
that ÒhtdocsÓ is the directory
that is the root for all of the
serverÕs web applications.
The two
applications on
this server.
The folders for
the beerAdvice
appÕs two actions.
Òindex.htmlÓ is the
default page for the
beerAdvice application.
An HTML page
that gives the user
some advice.
KWPO!
KWPO!
KWPO!
KWPO!
KWPO!
KWPO!
)
*
+
,
ZHEVLWHGLUHFWRU\
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
)
0DSSLQJ85/VWRFRQWHQW
/RRNDWWKHGLUHFWRU\VWUXFWXUHRQWKHRSSRVLWHSDJHWKHQZULWHLQ
D85/WKDWZRXOGJHW\RXWRHDFKRIWKHIRXUKWPOSDJHVPDUNHG
ZLWKWKH$%&DQG':HGLGWKHILUVWRQH$IRU\RXEHFDXVH
WKDW·VWKHNLQGRISHRSOHZHDUH)RUWKHH[HUFLVHDVVXPH$SDFKHLV
UXQQLQJRQSRUW7KHDQVZHUVDUHDWWKHERWWRPRIWKHQH[WSDJH
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
*
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
+
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHVHOHFW%HHUKWPOSDJHDWORFDWLRQ
,
6KDUSHQ \RXU SHQFLO
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
FKDSWHU
?MJ[MZ^MZ[TW^M[MZ^QVO
[\I\QK_MJXIOM[
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!
ERG\!
KWPO!
7KLVLVZKDW,GR
$VNPHIRUDSDJH,ÀQGLW
DQG,KDQGLWEDFN:LWKDIHZ
KHDGHUV%XWWKDW·VLW'R127
DVNPHWROLNH
GR
DQ\WKLQJ
WRWKHSDJH
$VWDWLFSDJHMXVWVLWVWKHUHLQD
GLUHFWRU\7KHVHUYHUILQGVLWDQG
KDQGVLWEDFNWRWKHFOLHQWDVLV
(YHU\FOLHQWVHHVWKHVDPHWKLQJ
%XWZKDWLI,ZDQWVD\WKH
FXUUHQWWLPHWRVKRZXSRQ
P\SDJH":KDWLI,ZDQWDSDJH
WKDWKDVVRPHWKLQJ
G\QDPLF
"
&DQ·W,KDYHVRPHWKLQJOLNHD
YDULDEOH
LQVLGHP\+70/"
KWPO!
ERG\!
7KHFXUUHQWWLPHLV>LQVHUW7LPH2Q6HUYHU@
ERG\!
KWPO!
What if we want to stick
something variable inside
the HTML page?
These pages go straight
to the client just
exactly as they were
put on the server.
ZHEVHUYHU
DSSOLFDWLRQ
ZHEVHUYHU
PDFKLQH
%ZZZZLFNHGO\VPDUWFRPVNLLQJ$GYLFH
&ZZZZLFNHGO\VPDUWFRPEHHU$GYLFH
'ZZZZLFNHGO\VPDUWFRPEHHU$GYLFHVHOHFWVHOHFW%HHUKWPO
VWDWLFSDJHV
$QVZHUVIURPSUHYLRXVSDJH
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH
*]\[WUM\QUM[aW]VMMLUWZM
\PIVR][\\PM_MJ[MZ^MZ
,·PDZHEVHUYHU
DSSOLFDWLRQ,6(59(WKLQJV,
GRQ·WGRFRPSXWDWLRQRQWKHWKLQJV
,VHUYH%XW,NQRZDUHDOQLFH
SURJUDPRQWKHVDPHPDFKLQH
WKDW&$1KHOS\RXRXW
:HEVHUYHU
DSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ
RQWKHVHUYHU
,FDQKDQGOH
WKDWGDWHWKLQJ
IRU\RX
%XWKRZGRHVWKDWKHOS"
0\FOLHQWVDUHDOO
ZHE
FOLHQWV
7KHEURZVHUNQRZVRQO\DERXWWKH
ZHEVHUYHUVRLWZRQ·WEHDEOH
WRFDOOWKDWRWKHUDSSOLFDWLRQ
ZHEVHUYHU
DSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ
RQWKHVHUYHU
7KDW·VQRWDSUREOHP
,·OOWDNHFDUHRIJHWWLQJWKH
UHTXHVWWRWKHULJKWKHOSHUDSSWKHQ
,·OOWDNHWKDWDSS·VUHVSRQVHDQGVHQG
LWEDFNWRWKHFOLHQW,QIDFWWKH
FOLHQWQHYHUQHHGVWRNQRZWKDW
VRPHRQHHOVHGLGVRPH
RIWKHZRUN
ZHEVHUYHU
PDFKLQH
Chapter 1. Why use Servlets & JSPs?
Chapter 1. Why use Servlets & JSPs?
Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
Print Publication Date: 8/1/2004 User number: 747221 Copyright 2006, Safari Books Online, LLC.
This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior
written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that
otherwise violates the Safari Terms of Service is strictly prohibited.