Tải bản đầy đủ (.pdf) (75 trang)

Head First Servlets and JSP phần 1 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (19.97 MB, 75 trang )

WKLVLVDQHZFKDSWHU

:HEDSSOLFDWLRQVDUHKRW6XUH*8,DSSOLFDWLRQVPLJKWXVHH[RWLF
6ZLQJZLGJHWVEXWKRZPDQ\*8,DSSVGR\RXNQRZWKDWDUHXVHGE\PLOOLRQV
RIXVHUVZRUOGZLGH"$VDZHEDSSGHYHORSHU\RXFDQIUHH\RXUVHOIIURPWKH
JULSRIGHSOR\PHQWSUREOHPVDOOVWDQGDORQHDSSVKDYHDQGGHOLYHU\RXUDSSWR
DQ\RQHZLWKDEURZVHU%XWWREXLOGDWUXO\SRZHUIXOZHEDSS\RXQHHG-DYD
<RXQHHGVHUYOHWV<RXQHHG-63V%HFDXVHSODLQROGVWDWLF+70/SDJHVDUHVR
ZHOO7RGD\¶VXVHUVH[SHFWVLWHVWKDWDUHG\QDPLFLQWHUDFWLYHDQGFXVWRP
WDLORUHG:LWKLQWKHVHSDJHV\RX¶OOOHDUQWRPRYHIURPZHEVLWHWRZHEDSS
:K\XVH6HUYOHWV-63V"


LQWURDQGRYHUYLHZ
<RXIRRO<RXPXVWXVH
6HUYOHWVDQG-63V,I\RX
FRQWLQXHWRZULWH3HUO
VFULSWV,ZLOOGHVWUR\
\RX
+DK,NQRZ
&*,0\ZHEVLWHZLOO
UXOHWKHZRUOG
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


)RUHDFKRIWKH+7730HWKRGVVXFKDV*(7
3267+($'DQGVRRQ
'HVFULEHEHQH¿WVRIWKH+7730HWKRG
'HVFULEHIXQFWLRQDOLW\RIWKH+7730HWKRG
/LVWWULJJHUVWKDWPLJKWFDXVHD&OLHQW
XVXDOO\D:HEEURZVHUWRXVHWKHPHWKRG
$OVRSDUWRI2EMHFWLYHEXWQRWFRYHUHG
LQWKLVFKDSWHU
,GHQWLI\WKH+WWS6HUYOHWPHWKRGWKDW
FRUUHVSRQGVWRWKH+7730HWKRG

6HUYOHWV-63RYHUYLHZ
RI¿FLDO6XQH[DPREMHFWLYHV
4HEOBJECTIVESINTHISSECTIONARECOVERED
COMPLETELYINANOTHERCHAPTERSOTHINKOF
THISCHAPTERASAlRSTLOOKFOUNDATIONFOR
WHATCOMESLATER)NOTHERWORDSDONTWORRY
ABOUTlNISHINGTHISCHAPTERKNOWINGAND
REMEMBERINGANYTHINGSPECIlCFROMTHESE
OBJECTIVESJUSTUSEITFORBACKGROUND)FYOU
ALREADYKNOWTHESETOPICSYOUCANJUSTSKIM
THISCHAPTERANDJUMPTOCHAPTER
9OUWONTHAVEANYMOCKEXAMQUESTIONSON
THESETOPICSUNTILYOUGETTOTHEMORESPECIlC
CHAPTERWHERETHOSETOPICSARECOVERED
&RYHUDJH1RWHV
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
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

-^MZaJWLa_IV\[I_MJ[Q\M
9OUHAVEAKILLERIDEAFORAWEBSITE4ODESTROYTHECOMPETITIONYOU
NEEDAmEXIBLESCALABLEARCHITECTURE9OUNEEDSERVLETSAND*30S
"EFOREWESTARTBUILDINGLETSTAKEALOOKATTHE7ORLD7IDE7EB
FROMABOUTKFEET7HATWECAREMOSTABOUTINTHISCHAPTERARE
HOWWEBCLIENTSANDWEBSERVERSTALKTOONEANOTHER
4HESENEXTSEVERALPAGESAREPROBABLYALLREVIEWFORYOUESPECIALLYIF
YOUREALREADYAWEBAPPLICATIONDEVELOPERBUTITLLGIVEUSACHANCE
TOEXPOSESOMEOFTHETERMINOLOGYWEUSETHROUGHOUTTHEBOOK
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
!WEBBROWSERLETSAUSERREQUESTARESOURCE4HEWEBSERVERGETSTHE
REQUESTlNDSTHERESOURCEANDRETURNSSOMETHINGTOTHEUSER
3OMETIMESTHATRESOURCEISAN(4-,PAGE3OMETIMESITSAPICTURE/R
ASOUNDlLE/REVENA0$&DOCUMENT$OESNTMATTERTHECLIENTASKS
FORTHETHINGRESOURCEANDTHESERVERSENDSITBACK
5NLESSTHETHINGISNTTHERE/RATLEASTITSNOTWHERETHESERVERIS
EXPECTINGITTOBE9OUREOFCOURSEQUITEFAMILIARWITHTHEh.OT
&OUNDvERRORTHERESPONSEYOUGETWHENTHESERVERCANTlNDWHATIT

THINKSYOUASKEDFOR
7HENWESAYhSERVERvWEMEANEITHERTHEPHYSICALMACHINEHARDWARE
ORTHEWEBSERVERAPPLICATIONSOFTWARE4HROUGHOUTTHEBOOKIF
THEDIFFERENCEBETWEENSERVERHARDWAREANDSOFTWAREMATTERSWELL
EXPLICITLYSAYWHICHONEHARDWAREORSOFTWAREWERETALKINGABOUT
?PI\LWM[aW]Z_MJ[MZ^MZLW'
$ZHEVHUYHUWDNHVDFOLHQWUHTXHVWDQGJLYHV
VRPHWKLQJEDFNWRWKHFOLHQW
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!
ZHEVHUYHU
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

$ZHEFOLHQWOHWVWKHXVHUUHTXHVWVRPHWKLQJRQWKH
VHUYHUDQGVKRZVWKHXVHUWKHUHVXOWRIWKHUHTXHVW
7HENWETALKABOUTCLIENTSTHOUGHWEUSUALLYMEANBOTHOREITHER
THEHUMANUSERANDTHEBROWSERAPPLICATION
4HEBROWSERISTHEPIECEOFSOFTWARELIKE.ETSCAPEOR-OZILLATHAT
KNOWSHOWTOCOMMUNICATEWITHTHESERVER4HEBROWSERSOTHERBIGJOB
ISINTERPRETINGTHE(4-,CODEANDRENDERINGTHEWEBPAGEFORTHEUSER
3OFROMNOWONWHENWEUSETHETERMCLIENTWEUSUALLYWONTCARE
WHETHERWERETALKINGABOUTTHEHUMANUSERORTHEBROWSERAPP)N
OTHERWORDSTHECLIENTISTHEBROWSERAPPDOINGWHATTHEUSERASKEDITTODO
?PI\LWM[I_MJKTQMV\LW'
%URZVHU
6HUYHU
KWPO!
KHDG!

KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
P\UHTXHVW
LV[\]
8VHUFOLFNVDOLQN
LQWKHEURZVHU
8VHU
%URZVHUIRUPDWVWKH
UHTXHVWDQGVHQGVLW
WRWKHVHUYHU
KWPO!
KHDG!
KHDG!

ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
6HUYHUÀQGVWKH
UHTXHVWHGSDJH
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!

KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
%URZVHUJHWVWKH+70/
DQGUHQGHUVLWLQWRD
GLVSOD\IRUWKHXVHU
8VHU
6HUYHUIRUPDWVWKH
UHVSRQVHDQGVHQGVLW
WRWKHFOLHQWEURZVHU
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<54IVL0<<8
%XWKRZGRWKH
FOLHQWVDQGVHUYHUV
WDONWRHDFKRWKHU"
$ZLVHTXHVWLRQ,QRUGHU
WRFRPPXQLFDWHWKH\PXVW
VKDUHDFRPPRQODQJXDJH2Q
WKHZHEFOLHQWVDQGVHUYHUV
PXVWVSHDN+773DQGEURZVHUV
PXVWNQRZ+70/
-OSTOFTHECONVERSATIONSHELDONTHE
WEBBETWEENCLIENTSANDSERVERSARE
HELDUSINGTHE(440PROTOCOLWHICH
ALLOWSFORSIMPLEREQUESTANDRESPONSE
CONVERSATIONS4HECLIENTSENDSAN
(440REQUESTANDTHESERVERANSWERS
WITHAN(440RESPONSE"OTTOMLINE
IFYOUREAWEBSERVERYOUSPEAK(440
7HENAWEBSERVERSENDSAN(4-,
PAGETOTHECLIENTITSENDSITUSING
(4409OULLSEETHEDETAILSONHOW
ALLTHISWORKSINTHENEXTFEWPAGES
&9)(440STANDSFOR(YPER4EXT
4RANSPORT0ROTOCOL
+773
7HENASERVERANSWERSAREQUEST
THESERVERUSUALLYSENDSSOMETYPE
OFCONTENTTOTHEBROWSERSOTHATTHE

BROWSERCANDISPLAYIT3ERVERSOFTEN
SENDTHEBROWSERASETOFINSTRUCTIONS
WRITTENIN(4-,THE(YPER4EXT
-ARKUP,ANGUAGE4HE(4-,
TELLSTHEBROWSERHOWTOPRESENTTHE
CONTENTTOTHEUSER
!LLWEBBROWSERSKNOWWHATTODO
WITH(4-,ALTHOUGHSOMETIMESAN
OLDERBROWSERMIGHTNOTUNDERSTAND
PARTSOFAPAGETHATWASWRITTENUSING
NEWERVERSIONSOF(4-,
+70/
+70/WHOOVWKHEURZVHU
KRZWRGLVSOD\WKHFRQWHQW
WRWKHXVHU
+773LVWKHSURWRFRO
FOLHQWVDQGVHUYHUVXVHRQ
WKHZHEWRFRPPXQLFDWH
7KHVHUYHUXVHV+773WR
VHQG+70/WRWKHFOLHQW
+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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH


<_WUQV]\M0<54O]QLM
7HENYOUDEVELOPAWEBPAGEYOUUSE(4-,TODESCRIBEWHATTHE
PAGESHOULDLOOKLIKEANDHOWITSHOULDBEHAVE
(4-,HASDOZENSOFTAGSANDHUNDREDSOFTAGATTRIBUTES4HEGOALOF
(4-,ISTOTAKEATEXTDOCUMENTANDADDTAGSTHATTELLTHEBROWSER
HOWTOFORMATTHETEXT"ELOWARETHETAGSWEUSEINTHENEXTSEVERAL
CHAPTERS)FYOUNEEDAMORECOMPLETEUNDERSTANDINGOF(4-,WE
RECOMMENDTHEBOOK(4-,8(4-,4HE$ElNITIVE'UIDE
!
D!
DOLJQ!
ERG\!
EU!
FHQWHU!
IRUP!
K!
KHDG!
KWPO!
LQSXWW\SH!
S!
WLWOH!
WHEREYOUPUTYOURCOMMENTS
ANCHORUSUALLYFORPUTTINGINAHYPERLINK
ALIGNTHECONTENTSLEFTRIGHTCENTEREDORJUSTIlED
DElNETHEBOUNDARIESOFTHEDOCUMENTSBODY
ALINEBREAK
CENTERTHECONTENTS
DElNEAFORMWHICHUSUALLYPROVIDESINPUTlELDS
THElRSTLEVELHEADING

DElNETHEBOUNDARIESOFTHEDOCUMENTSHEADER
DElNETHEBOUNDARIESOFTHE(4-,DOCUMENT
DElNESANINPUTWIDGETTOAFORM
ANEWPARAGRAPH
THE(4-,DOCUMENTSTITLE
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!
6RPHVDPSOH+70/!
KHDG!
WLWOH!$/RJLQ3DJHWLWOH!
KHDG!
ERG\!
KDOLJQ ´FHQWHU´!6N\OHU¶V/RJLQ3DJHK!
SDOLJQ ´ULJKW´!
LPJVUF ´6.</(5MSJ´ZLGWK ´´KHLJKW ´´!

S!
IRUPDFWLRQ ´GDWH´!
1DPHLQSXWW\SH ´WH[W´QDPH ´SDUDP´!EU!
3DVVZRUGLQSXWW\SH ´WH[W´QDPH ´SDUDP´!EU!EU!EU!
FHQWHU!
LQSXWW\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
\PM0<54
The <br> tags
cause line breaks.
An HTML comment
The servlet to send
the request to.
)MAGINEYOURECREATINGALOGINPAGE4HESIMPLE(4-,
MIGHTLOOKSOMETHINGLIKETHIS
$

%
&
'
(
WeÕll talk more about
forms later, but brießy,
the browser can collect
the userÕs input and
return it to the server.
<RXQHHGRQO\WKHPRVWEDVLF+70/
NQRZOHGJH
(4-,POPSUPALLOVERTHEEXAM"UTYOURE
NOTBEINGTESTEDONYOUR(4-,KNOWLEDGE9OULLSEE
(4-,INTHECONTEXTOFALARGECHUNKOFQUESTIONS
THOUGHSOYOUNEEDATLEASTSOMEIDEAOFWHATS
HAPPENINGWHENYOUSEESIMPLE(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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

?PI\\PMJZW_[MZKZMI\M[
4HEBROWSERREADSTHROUGHTHE(4-,CODECREATESTHE

WEBPAGEANDRENDERSITTOTHEUSERSDISPLAY
$
%
&
'
(
KWWSZZZZLFNHGO\VPDUWFRPVN\ORJLQ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
(440RUNSONTOPOF4#0)0)FYOURENOTFAMILIARWITH
THOSENETWORKINGPROTOCOLSHERESTHECRASHCOURSE4#0
ISRESPONSIBLEFORMAKINGSURETHATAlLESENTFROMONE
NETWORKNODETOANOTHERENDSUPASACOMPLETElLEATTHE
DESTINATIONEVENTHOUGHTHElLEISSPLITINTOCHUNKSWHEN
ITSSENT)0ISTHEUNDERLYINGPROTOCOLTHATMOVESROUTES
THECHUNKSPACKETSFROMONEHOSTTOANOTHERONTHEIR
WAYTOTHEDESTINATION(440THENISANOTHERNETWORK
PROTOCOLTHATHAS7EBSPECIlCFEATURESBUTITDEPENDS
ON4#0)0TOGETTHECOMPLETEREQUESTANDRESPONSE
FROMONEPLACETOANOTHER4HESTRUCTUREOFAN(440
CONVERSATIONISASIMPLE2EQUEST2ESPONSESEQUENCEA
BROWSERREQUESTSANDASERVERRESPONDS
?PI\Q[\PM0<<8XZW\WKWT'

<RXGRQ¶WKDYHWRPHPRUL]HWKH+773VSHF
4HE(440PROTOCOLISAN)%4&STANDARD2&#)FYOUCARE
&ORTUNATELYTHEEXAMDOESNTEXPECTYOUTO!PACHEISANEXAMPLE
OFA7EBSERVERTHATPROCESSES(440REQUESTS-OZILLAISANEXAMPLE
OFA7EBBROWSERTHATPROVIDESTHEUSERWITHTHEMEANSTOMAKE(440
REQUESTSANDTOVIEWTHEDOCUMENTSRETURNEDBYTHESERVER
+773UHTXHVW
+773UHVSRQVH
&OLHQW
:HE
EURZVHU
6HUYHU
.H\HOHPHQWVRIWKHUHTXHVW
VWUHDP

+773PHWKRGWKHDFWLRQWR
EHSHUIRUPHG

7KHSDJHWRDFFHVVD85/

)RUPSDUDPHWHUVOLNH
DUJXPHQWVWRDPHWKRG
.H\HOHPHQWVRIWKH
UHVSRQVHVWUHDP

$VWDWXVFRGHIRUZKHWKHU
WKHUHTXHVWZDVVXFFHVVIXO

&RQWHQWW\SHWH[WSLFWXUH
+70/HWF


7KHFRQWHQWWKHDFWXDO
+70/LPDJHHWF
+773SURWRFRO
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

0<54Q[XIZ\WN\PM0<<8ZM[XWV[M
!N(440RESPONSECANCONTAIN(4-,(440ADDSHEADER
INFORMATIONTOTHETOPOFWHATEVERCONTENTISINTHERESPONSEIN
OTHERWORDSTHETHINGCOMINGBACKFROMTHESERVER!N(4-,
BROWSERUSESTHATHEADERINFOTOHELPPROCESSTHE(4-,PAGE4HINK
OFTHE(4-,CONTENTASDATAPASTEDINSIDEAN(440RESPONSE
+773UHTXHVW
+773UHVSRQVH
&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.

+773KHDGHULQIR
KWPO!
KHDG!

KHDG!
ERG\!
LPJVUF !
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\¼[\PMZM[XWV[M_PI\¼[QV\PMZMY]M[\'
4HElRSTTHINGYOULLlNDISAN(440METHODNAME4HESEARENT*AVA

METHODSBUTTHEIDEAISSIMILAR4HEMETHODNAMETELLSTHESERVERTHE
KINDOFREQUESTTHATSBEINGMADEANDHOWTHERESTOFTHEMESSAGEWILLBE
FORMATTED4HE(440PROTOCOLHASSEVERALMETHODSBUTTHEONESYOULL
USEMOSTOFTENARE'%4AND0/34
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
%URZVHUVHQGVDQ+773*(7
WRWKHVHUYHUDVNLQJWKH
VHUYHUWR*(7WKHSDJH

8VHU
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
8VHUFOLFNV
DOLQNWRD
QHZSDJH
%URZVHU
6HUYHU
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!

ERG\!

ERG\!
KWPO!
%URZVHUVHQGVDQ+7733267
WRWKHVHUYHUJLYLQJWKH
VHUYHUZKDWWKHXVHUW\SHG
LQWRWKHIRUP
8VHU
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
8VHUW\SHVLQD
IRUPDQGKLWVWKH
6XEPLWEXWWRQ
*(7
3267
*(7


3267


+773PHWKRGV
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

/-<Q[I[QUXTMZMY]M[\
87;<KIV[MVL][MZLI\I
'%4ISTHESIMPLEST(440METHODANDITS
MAINJOBINLIFEISTOASKTHESERVERTOGETA
RESOURCEANDSENDITBACK4HATRESOURCEMIGHT
BEAN(4-,PAGEA*0%'A0$&ETC$OESNT
MATTER4HEPOINTOF'%4ISTOGETSOMETHING
BACKFROMTHESERVER
0/34ISAMOREPOWERFULREQUEST)TSLIKEA
'%4PLUSPLUS7ITH0/34YOUCANREQUEST
SOMETHINGANDATTHESAMETIMESENDFORMDATA
TOTHESERVERLATERINTHISCHAPTERWELLSEEWHAT
THESERVERMIGHTDOWITHTHATDATA
:DLWDPLQXWH,
FRXOGVZHDU,·YHVHHQ
*(7UHTXHVWVWKDW
GLG
VHQGVRPHSDUDPHWHU
GDWDWRWKHVHUYHU
WKHUH DUH QR
'
XPE 4XHVWLRQV

4

3OWHATABOUTTHEOTHER(440METHODS
BESIDES'%4AND0/34
$
4HOSEARETHETWOBIGONESTHATEVERYBODY
USES"UTTHEREAREAFEWRARELYUSEDMETHODSAND
3ERVLETSCANHANDLETHEMINCLUDING(%!$42!#%
054$%,%4%/04)/.3AND#/ %#4
9OUREALLYDONTNEEDTOKNOWMUCHABOUTTHESE
OTHERSFORTHEEXAMALTHOUGHYOUMIGHTSEE
THEMAPPEARINAQUESTION4HE,IFEAND$EATH
OFA3ERVLETCHAPTERCOVERSTHERESTOFTHE(440
METHODDETAILSYOULLNEED
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]MaW]KIV[MVLITQ\\TMLI\I_Q\P0<<8/-<
"UTYOUMIGHTNOTWANTTO2EASONSYOUMIGHTUSE0/34INSTEADOF'%4INCLUDE
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.
4HETOTALAMOUNTOFCHARACTERSINA'%4ISREALLYLIMITEDDEPENDING
ONTHESERVER)FTHEUSERTYPESSAYALONGPASSAGEINTOAhSEARCHv
INPUTBOXTHE'%4MIGHTNOTWORK
4HEDATAYOUSENDWITHTHE'%4ISAPPENDEDTOTHE52,UPIN
THEBROWSERBARSOWHATEVERYOUSENDISEXPOSED"ETTERNOTPUTA
PASSWORDORSOMEOTHERSENSITIVEDATAASPARTOFA'%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
"ECAUSEOFNUMBERTWOABOVETHEUSERCANT
BOOKMARKAFORMSUBMISSIONIFYOUUSE0/34
INSTEADOF'%4$EPENDINGONYOURAPPYOUMAY
ORMAYNOTWANTUSERSTOBEABLETOBOOKMARKTHE
RESULTINGREQUESTFROMAFORMSUBMISSION



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.
LQWURDQGDUFKLWHFWXUH

\RXDUHKHUH

)VI\WUaWNIV0<<8/-<ZMY]M[\
*(7VHOHFWVHOHFW%HHU7DVWHMVS"FRORU GDUNWDVWH PDOW\+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK833&0DF26;0DFK2HQ86UY*HFNR
1HWVFDSH
$FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W
SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T 
$FFHSW/DQJXDJHHQXVHQT 
$FFHSW(QFRGLQJJ]LSGHӿDWH
$FFHSW&KDUVHW,62XWIT T 
.HHS$OLYH
&RQQHFWLRQNHHSDOLYH
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.
4HEPATHTOTHERESOURCEANDANYPARAMETERSADDEDTO
THE52,AREALLINCLUDEDONTHEhREQUESTLINEv
&OLHQW
:HE
EURZVHU
6HUYHU
+773UHTXHVW
+H\VHUYHU*(7PHWKHSDJH
RQWKLVKRVWWKDW·VDWVHOHFW
VHOHFW%HHU7DVWHKWPODQGRK\HDK
KHUHDUHWKHSDUDPHWHUVIRU\RX
FRORU GDUNWDVWH PDOW\$QG
KXUU\LWXS
6XUH,·OOJR*(7
WKDWSDJHDQGWKDQNVIRU
WKHSDUDPHWHUV$QGMXVW)<,
´KXUU\LWXSµLVQRWSDUWRI
WKH+773SURWRFRO
*(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
3267DGYLVRUVHOHFW%HHU7DVWHGR+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK833&0DF26;0DFK2HQ86UY*HFNR
1HWVFDSH
$FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W
SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T 
$FFHSW/DQJXDJHHQXVHQT 
$FFHSW(QFRGLQJJ]LSGHӿDWH
$FFHSW&KDUVHW,62XWIT T 
.HHS$OLYH
&RQQHFWLRQNHHSDOLYH
FRORU GDUNWDVWH PDOW\
)VI\WUaWNIV0<<887;<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Ó.
(4400/34REQUESTSAREDESIGNEDTOBEUSEDBYTHEBROWSERTO
MAKECOMPLEXREQUESTSONTHESERVER&ORINSTANCEIFAUSERHASJUST
COMPLETEDALONGFORMTHEAPPLICATIONMIGHTWANTALLOFTHEFORMS

DATATOBEADDEDTOADATABASE4HEDATATOBESENTBACKTOTHESERVER
ISKNOWNASTHEhMESSAGEBODYvORhPAYLOADvANDCANBEQUITELARGE
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
+773UHTXHVW
+H\VHUYHUSOHDVH3267WKLV
WRWKHUHVRXUFHDWDGYLVRU
VHOHFW%HHU7DVWHGR'RQ·WIRUJHW
WRORRNLQVLGHWKHERG\IRUWKH
LPSRUWDQWGDWD,·PVHQGLQJ
6XUH ,·OOÀQGWKDW
UHVRXUFHLW·VDFWXDOO\D
OLWWOHDSSOLFDWLRQDQGZKHQ,
GR,·OOJLYHLWWKHGDWDLQWKH
UHTXHVWERG\\RXVHQW
3267


+7733267
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

)VI\WUaWNIV0<<8ZM[XWV[MIVL
_PI\\PMPMKSQ[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.
+7732.
6HW&RRNLH-6(66,21,' $$%&'((()&)%)&$&3DWK WHVW(/
&RQWHQW7\SHWH[WKWPO
&RQWHQW/HQJWK
'DWH:HG1RY*07
6HUYHU$SDFKH&R\RWH
&RQQHFWLRQFORVH
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.)
.OWTHATWEVESEENTHEREQUESTSFROMTHEBROWSERTOTHESERVERLETS
LOOKATWHATTHESERVERSENDSBACKINRESPONSE!N(440RESPONSE
HASBOTHAHEADERANDABODY4HEHEADERINFOTELLSTHEBROWSER
ABOUTTHEPROTOCOLBEINGUSEDWHETHERTHEREQUESTWASSUCCESSFUL
ANDWHATKINDOFCONTENTISINCLUDEDINTHEBODY4HEBODYCONTAINS
THECONTENTSFOREXAMPLE(4-,FORTHEBROWSERTODISPLAY
&OLHQW
:HE
EURZVHU
6HUYHU
+773UHVSRQVH
HTTP
Response
headers.
+HUH·VP\UHVSRQVH
WR\RXUUHTXHVW,WV
W\SHLVWH[WKWPOEXWLQ\RXU
UHTXHVW\RXVDLGWKDWZDV
2.6RXQOHVV\RXZHUH
O\LQJWRPH

+7732.


KWPO!
KHDG!

KHDG!
ERG\!
LPJVUF !
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\!
KDOLJQ FHQWHU!%HHU/RJLQ3DJHK!
IRUP!
6HOHFWDEHHUW\SHRUEX\EHHU
PDNLQJVXSSOLHV"S!
LQSXWW\SH UDGLRQDPH VHOHFW
YDOXH 6HOHFW!6HOHFWDEHHUEU!
LQSXWW\SH UDGLRQDPH VHOHFW
YDOXH %X\!%X\VXSSOLHVEU!EU!
FHQWHU!
LQSXWW\SH 68%0,7!
FHQWHU!

IRUP!
ERG\!KWPO!
)TT\PMXQMKM[
7VWVMXIOM
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.
*MMZP\UT
The user
types a URL.
*(7WHVW%HHUKWPO+773
+RVWZZZZLFNHGO\VPDUWFRP
8VHU$JHQW0R]LOOD0DFLQWRVK

+7732.

6HW&RRNLH

KWPO!ERG\!
KDOLJQ FHQWHU!%HHU/RJLQ3DJHK!
IRUP!
6HOHFWDEHHUW\SHRUEX\EHHU
Client looks
forward to a
successful beer
transaction.
UHTXHVWDQGUHVSRQVH
KWWSZZZZLFNHGO\VPDUWFRPWHVW%HHU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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

$XVHULVUHWXUQLQJDORJLQQDPHDQGSDVVZRUG
$XVHULVUHTXHVWLQJDQHZSDJHYLDDK\SHUOLQN
$FKDWURRPXVHULVVHQGLQJDZULWWHQUHVSRQVH
$XVHUKLWVWKHµQH[W¶EXWWRQWRVHHWKHQH[WSDJH
$XVHUKLWVWKHµORJRXW¶EXWWRQRQDVHFXUHEDQNLQJVLWH
$XVHUKLWVWKHµEDFN¶EXWWRQRQWKHEURZVHU
$XVHUVHQGVDQDPHDQGDGGUHVVIRUPWRWKHVHUYHU
$XVHUPDNHVDUDGLREXWWRQVHOHFWLRQ

3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
3267*(7
*(7RU3267"
)RUHDFKGHVFULSWLRQFLUFOHHLWKHU3267RU*(7
GHSHQGLQJRQZKLFK+773PHWKRG\RX·GFKRRVH
IRULPSOHPHQWLQJWKDWIXQFWLRQDOLW\,I\RXWKLQNLW
FRXOGEHHLWKHUFLUFOHERWK%XWEHSUHSDUHGWR
GHIHQG\RXUDQVZHUV
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^MZaW]LWLWV¼\XZWVW]VKMQ\¹-IZTº
7HENYOUGETTOTHE5SINTHEACRONYMDICTIONARYTHERESATRAFlCJAM52)
52,52.WHEREDOESITEND&ORNOWWEREGOINGTOFOCUSONTHE52,SOR
5NIFORM2ESOURCE,OCATORSTHATYOUKNOWANDLOVE%VERYRESOURCEONTHEWEB
HASITSOWNUNIQUEADDRESSINTHE52,FORMAT
KWWSZZZZLFNHGO\VPDUWFRPEHHUDGYLFHVHOHFWEHHUKWPO

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\RID85/
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

$7&3SRUWLVMXVWDQXPEHU
$ELWQXPEHUWKDWLGHQWLILHVD
VSHFLILFVRIWZDUHSURJUDPRQWKH
VHUYHUKDUGZDUH
9OURINTERNETWEB(440SERVERSOFTWARERUNSONPORT
4HATSASTANDARD)FYOUVEGOTA4ELNETSERVERITS
RUNNINGONPORT&400/0MAILSERVER
3-404HE4IMESERVERSITSAT4HINKOFPORTSAS
UNIQUEIDENTIlERS!PORTREPRESENTSALOGICALCONNECTION
TOAPARTICULARPIECEOFSOFTWARERUNNINGONTHESERVER
HARDWARE4HATSIT9OUCANTSPINYOURHARDWAREBOX
AROUNDANDlNDA4#0PORT&ORONETHINGYOUHAVE
OFTHEMONASERVERTO&ORANOTHER
THEYDONOTREPRESENTAPLACETOPLUGINPHYSICALDEVICES
4HEYREJUSTNUMBERSREPRESENTINGASERVERAPPLICATION
7ITHOUTPORTNUMBERSTHESERVERWOULDHAVENOWAYOF
KNOWINGWHICHAPPLICATIONACLIENTWANTEDTOCONNECTTO
!NDSINCEEACHAPPLICATIONMIGHTHAVEITSOWNUNIQUE
PROTOCOLTHINKOFTHETROUBLEYOUDHAVEWITHOUTTHESE
IDENTIlERS7HATIFYOURWEBBROWSERFOREXAMPLELANDED
ATTHE0/0MAILSERVERINSTEADOFTHE(440SERVER4HE
MAILSERVERWONTKNOWHOWTOPARSEAN(440REQUEST
!NDEVENIFITDIDTHE0/0SERVERDOESNTKNOWN
ANYTHINGABOUTSERVINGBACKAN(4-,PAGE

)FYOUREWRITINGSERVICESSERVERPROGRAMSTORUNONA
COMPANYNETWORKYOUSHOULDCHECKWITHTHESYSADMINS
TOlNDOUTWHICHPORTSAREALREADYTAKEN9OURSYSADMINS
MIGHTTELLYOUFOREXAMPLETHATYOUCANTUSEANYPORT
NUMBERBELOWSAY
23
21
25
37
110
FTP
Telnet
POP3
SMTP
Time
80
443
HTTP
HTTPS
6HUYHU
:HOONQRZQ7&3SRUWQXPEHUV
IRUFRPPRQVHUYHUDSSOLFDWLRQV
7KH7&3SRUWQXPEHUV
IURPWRDUH
UHVHUYHGIRUZHOONQRZQ
VHUYLFHVLQFOXGLQJWKH
%LJ2QHZHFDUHDERXW³
SRUW'RQ·WXVHWKHVH
SRUWVIRU\RXURZQ
FXVWRPVHUYHUSURJUDPV

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\]ZMNWZI[QUXTM)XIKPM_MJ[Q\M
7ELLTALKMOREABOUT!PACHEAND4OMCATLATERBUTFORNOWLETSASSUMETHATOURSIMPLE
WEBSITEISUSING!PACHETHEEXTREMELYPOPULAROPENSOURCEWEBSERVERYOUREPROBABLY
ALREADYUSING7HATWOULDTHEDIRECTORYSTRUCTURELOOKLIKEFORAWEBSITECALLEDWWW
WICKEDLYSMARTCOMHOSTINGTWOAPPLICATIONSONEGIVINGSKIINGADVICEANDTHEOTHER
BEERRELATEDADVICE)MAGINETHATTHE!PACHEAPPLICATIONISRUNNINGONPORT
4HEHTMLPAGESAREEACHMARKEDWITHALETTER!"#$FORTHEEXERCISEONTHE
OPPOSITEPAGE
,QGH[KWPO

KWPO!



KWPO!
VHOHFW%HHUKWPO

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!
)
*
+
,
ZHEVLWHGLUHFWRU\
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
)

0DSSLQJ85/VWRFRQWHQW
/RRNDWWKHGLUHFWRU\VWUXFWXUHRQWKHRSSRVLWHSDJHWKHQZULWHLQ
D85/WKDWZRXOGJHW\RXWRHDFKRIWKHIRXUKWPOSDJHVPDUNHG
ZLWKWKH$%&DQG':HGLGWKHILUVWRQH$IRU\RXEHFDXVH
WKDW·VWKHNLQGRISHRSOHZHDUH)RUWKHH[HUFLVHDVVXPH$SDFKHLV
UXQQLQJRQSRUW7KHDQVZHUVDUHDWWKHERWWRPRIWKHQH[WSDJH
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
*
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHLQGH[KWPOSDJHDWORFDWLRQ"
+
ZLOOFDXVHWKHVHUYHUWRUHWXUQWR\RXWKHVHOHFW%HHUKWPOSDJHDWORFDWLRQ
,
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_MJXIOM[
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!
KWPO!
KWPO!
KHDG!
KHDG!
ERG\!

ERG\!

KWPO!
7KLVLVZKDW,GR
$VNPHIRUDSDJH,ÀQGLW
DQG,KDQGLWEDFN:LWKDIHZ
KHDGHUV%XWWKDW·VLW'R127
DVNPHWROLNH
GR
DQ\WKLQJ
WRWKHSDJH
$VWDWLFSDJHMXVWVLWVWKHUHLQD
GLUHFWRU\7KHVHUYHUILQGVLWDQG
KDQGVLWEDFNWRWKHFOLHQWDVLV
(YHU\FOLHQWVHHVWKHVDPHWKLQJ
%XWZKDWLI,ZDQWVD\WKH
FXUUHQWWLPHWRVKRZXSRQ
P\SDJH":KDWLI,ZDQWDSDJH
WKDWKDVVRPHWKLQJ
G\QDPLF
"
&DQ·W,KDYHVRPHWKLQJOLNHD
YDULDEOH
LQVLGHP\+70/"
KWPO!
ERG\!
7KHFXUUHQWWLPHLV>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.
ZHEVHUYHU
DSSOLFDWLRQ
ZHEVHUYHU
PDFKLQH
%ZZZZLFNHGO\VPDUWFRPVNLLQJ$GYLFH
&ZZZZLFNHGO\VPDUWFRPEHHU$GYLFH
'ZZZZLFNHGO\VPDUWFRPEHHU$GYLFHVHOHFWVHOHFW%HHUKWPO
VWDWLFSDJHV
$QVZHUVIURPSUHYLRXVSDJH
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.
LQWURDQGDUFKLWHFWXUH
\RXDUHKHUH

*]\[WUM\QUM[aW]VMMLUWZM
\PIVR][\\PM_MJ[MZ^MZ
,·PDZHEVHUYHU
DSSOLFDWLRQ,6(59(WKLQJV,
GRQ·WGRFRPSXWDWLRQRQWKHWKLQJV
,VHUYH%XW,NQRZDUHDOQLFH
SURJUDPRQWKHVDPHPDFKLQH

WKDW&$1KHOS\RXRXW
:HEVHUYHU
DSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ
RQWKHVHUYHU
,FDQKDQGOH
WKDWGDWHWKLQJ
IRU\RX
%XWKRZGRHVWKDWKHOS"
0\FOLHQWVDUHDOO
ZHE
FOLHQWV
7KHEURZVHUNQRZVRQO\DERXWWKH
ZHEVHUYHUVRLWZRQ·WEHDEOH
WRFDOOWKDWRWKHUDSSOLFDWLRQ
ZHEVHUYHU
DSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ
RQWKHVHUYHU
7KDW·VQRWDSUREOHP
,·OOWDNHFDUHRIJHWWLQJWKH
UHTXHVWWRWKHULJKWKHOSHUDSSWKHQ
,·OOWDNHWKDWDSS·VUHVSRQVHDQGVHQG
LWEDFNWRWKHFOLHQW,QIDFWWKH
FOLHQWQHYHUQHHGVWRNQRZWKDW
VRPHRQHHOVHGLGVRPH
RIWKHZRUN




ZHEVHUYHU
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.

×