Chapter 1. Why use Servlets & JSPs?
LQWURDQGRYHUYLHZ
:K\XVH6HUYOHWV -63V" +DK,NQRZ &*,0\ZHEVLWHZLOO UXOHWKHZRUOG
LQVHUW7LPH2Q6HUYHU@ ERG\! KWPO!
%ZZZZLFNHGO\VPDUWFRPVNLLQJ$GYLFH &ZZZZLFNHGO\VPDUWFRPEHHU$GYLFH 'ZZZZLFNHGO\VPDUWFRPEHHU$GYLFHVHOHFWVHOHFW%HHUKWPO $QVZHUVIURPSUHYLRXVSDJH
FKDSWHU
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:
[email protected] 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.
Chapter 1. Why use Servlets & JSPs?
LQWURDQGDUFKLWHFWXUH
*]\[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
,FDQKDQGOH WKDWGDWHWKLQJ IRU\RX
%XWKRZGRHVWKDWKHOS" 0\FOLHQWVDUHDOOZHEFOLHQWV 7KHEURZVHUNQRZVRQO\DERXWWKH ZHEVHUYHUVRLWZRQ·WEHDEOH WRFDOOWKDWRWKHUDSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ RQWKHVHUYHU
ZHEVHUYHU PDFKLQH
7KDW·VQRWDSUREOHP ,·OOWDNHFDUHRIJHWWLQJWKH UHTXHVWWRWKHULJKWKHOSHUDSSWKHQ ,·OOWDNHWKDWDSS·VUHVSRQVHDQGVHQG LWEDFNWRWKHFOLHQW,QIDFWWKH FOLHQWQHYHUQHHGVWRNQRZWKDW VRPHRQHHOVHGLGVRPH RIWKHZRUN
ZHEVHUYHU DSSOLFDWLRQ
DQRWKHUDSSOLFDWLRQ RQWKHVHUYHU \RXDUHKHUH
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:
[email protected] 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.
Chapter 1. Why use Servlets & JSPs?
ZKHQDZHEVHUYHULVQ¶WHQRXJK
+ JMMZIL^QKM_MJIXXTQKI\QWV :KDW·VZRUNLQJVRIDU 7KHEURZVHUVHQGVWKH UHTXHVWGDWDWRWKH&RQWDLQHU
&RQWDLQHU
:HE EURZVHU
&RQWDLQHU ORJLF
7KH&RQWDLQHUILQGVWKH FRUUHFWVHUYOHWEDVHGRQWKH 85/DQGSDVVHVWKHUHTXHVWWR WKHVHUYOHW
&OLHQW
7KHVHUYOHWFDOOVWKH %HHU([SHUWIRUKHOS
VHUYOHW
7KHVHUYOHWRXWSXWVWKH UHVSRQVHZKLFKSULQWVWKH DGYLFH %HHU([SHUW FRPSRQHQW
7KH&RQWDLQHUUHWXUQVWKH SDJHWRWKHKDSS\XVHU
7KHEURZVHUVHQGVWKH UHTXHVWGDWDWRWKH&RQWDLQHU
:KDWZH:$17
7KH&RQWDLQHUILQGVWKH FRUUHFWVHUYOHWEDVHGRQWKH 85/DQGSDVVHVWKHUHTXHVWWR WKHVHUYOHW
&RQWDLQHU
7KHVHUYOHWFDOOVWKH %HHU([SHUWIRUKHOS
&OLHQW
7KHH[SHUWFODVVUHWXUQVDQ DQVZHUZKLFKWKHVHUYOHWDGGV WRWKHUHTXHVWREMHFW
KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!
UHVXOWMVS
VHUYOHW
7KHVHUYOHWIRUZDUGVWKH UHTXHVWWRWKH-63
UHTXHVW
%HHU([SHUW FRPSRQHQW
7KH-63JHWVWKHDQVZHU IURPWKHUHTXHVWREMHFW 7KH-63JHQHUDWHVDSDJH IRUWKH&RQWDLQHU 7KH&RQWDLQHUUHWXUQVWKH SDJHWRWKHKDSS\XVHU
FKDSWHU
Chapter 3. Mini MVC Tutorial Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
[email protected] Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, 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.
Chapter 3. Mini MVC Tutorial
KDQGVRQ09&
+ZMI\M\PM2;8¹^QM_º\PI\OQ^M[\PMIL^QKM $ONTGETYOURHOPESUP9OUREGOINGTOHAVETOWAITFORAFEW CHAPTERSBEFOREWEREALLYSTARTTALKINGABOUT*30S4HIS*30ISNT ACTUALLYAPARTICULARLYGOODONE EITHERBECAUSEOFITSSCRIPTLETCODE WHICHWELLTALKABOUTLATERINTHEBOOK &ORNOWITSHOULDBEPRETTY EASYTOREAD ANDIFYOUWANTTOEXPERIMENTALITTLE GOFORIT!LTHOUGH WECOULDTESTTHIS*30NOWFROMTHEBROWSER WELLWAITUNTILAFTERWE MODIFYTHESERVLETVERSIONTHREE TOSEEIFITWORKS
+HUH·VWKH-63
This is a Òpage directiveÓ (weÕre thinking itÕs pretty obvious what this one does). Some standard HTML (which is known as Òtemplate textÓ in the JSP world). ´FHQWHU´!%HHU5HFRPPHQGDWLRQV-63K!
#SDJHLPSRUW ´MDYDXWLO ´! KWPO! ERG\! KDOLJQ S!
/LVWVW\OHV /LVW UHTXHVWJHW$WWULEXWH³VW\OHV´ ,WHUDWRULW VW\OHVLWHUDWRU ZKLOHLWKDV1H[W ^ RXWSULQW³EU!WU\³LWQH[W ` ! Some standard Java sitting ERG\! KWPO!
inside tags (this is known as scriptlet code).
Here weÕre getting an attribute from the request object. A little later in the book, weÕll explain everything about attributes and how we managed to get the request object...
'HSOR\LQJWKH-63
WRPFDW
:HGRQ¶WFRPSLOHWKH-63WKH&RQWDLQHUGRHVWKDWDW¿UVWUHTXHVW %XWZHGRKDYHWR
ZHEDSSV
1DPHLW³UHVXOWMVS´
%HHUY
6DYHLWLQWKHGHYHORSPHQWHQYLURQPHQWLQZHE 0RYHDFRS\RILWWRWKHGHSOR\PHQWHQYLURQPHQWLQ%HHUY
!
UHVXOWMVS
KWPO! ERG\! ERG\! KWPO!
IRUPKWPO
\RXDUHKHUH
Chapter 3. Mini MVC Tutorial Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
[email protected] Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, 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.
Chapter 3. Mini MVC Tutorial
GLVSDWFKLQJDUHTXHVWWRD-63
-VPIVKQVO\PM[MZ^TM\\W¹KITTº\PM2;8^MZ[QWV\PZMM )NTHISSTEPWEREGOINGTOMODIFYTHESERVLETTOhCALLvTHE*30TOPRODUCETHEOUTPUT VIEW 4HE#ONTAINERPROVIDESAMECHANISMCALLEDhREQUESTDISPATCHINGvTHAT ALLOWSONE#ONTAINER MANAGEDCOMPONENTTOCALLANOTHER ANDTHATSWHATWELL USETHESERVLETWILLGETTHEINFOFROMTHEMODEL SAVEITINTHEREQUESTOBJECT THEN DISPATCHTHEREQUESTTOTHE*30
7KHLPSRUWDQWFKDQJHVZHPXVW PDNHWRWKHVHUYOHW $GGWKHPRGHOFRPSRQHQW¶VDQVZHUWRWKHUHTXHVWREMHFWVRWKDW WKH-63FDQDFFHVVLW6WHS $VNWKH&RQWDLQHUWRIRUZDUGWKHUHTXHVWWR³UHVXOWMVS´6WHS
7KH&RQWDLQHUILQGVWKH FRUUHFWVHUYOHWEDVHGRQWKH 85/DQGSDVVHVWKHUHTXHVWWR WKHVHUYOHW
&RQWDLQHU
7KHVHUYOHWFDOOVWKH %HHU([SHUWIRUKHOS
&OLHQW
7KHH[SHUWFODVVUHWXUQVDQ DQVZHUZKLFKWKHVHUYOHWDGGV WRWKHUHTXHVWREMHFW
KWPO! KHDG! KHDG ERG\! ERG\! KWPO!
UHVXOWMVS
7KHEURZVHUVHQGVWKH UHTXHVWGDWDWRWKHFRQWDLQHU
VHUYOHW
7KHVHUYOHWGLVSDWFKHVWR WKH-63
UHTXHVW
%HHU([SHUW FRPSRQHQW
7KH-63JHWVWKHDQVZHU IURPWKHUHTXHVWREMHFW 7KH-63JHQHUDWHVDSDJH IRUWKH&RQWDLQHU 7KH&RQWDLQHUUHWXUQVWKH SDJHWRWKHKDSS\XVHU
FKDSWHU
Chapter 3. Mini MVC Tutorial Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
[email protected] Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, 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.
Chapter 3. Mini MVC Tutorial
KDQGVRQ09&
+WLMNWZ[MZ^TM\^MZ[QWV\PZMM (ERESHOWWEMODIlEDTHESERVLETTOADDTHEMODEL COMPONENTSANSWERTOTHEREQUESTOBJECTSOTHE*30 CANRETRIEVEIT ANDHOWWEASKEDTHE#ONTAINERTO DISPATCHTOTHE*30 SDFNDJHFRPH[DPSOHZHE LPSRUWFRPH[DPSOHPRGHO LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS LPSRUWMDYDLR LPSRUWMDYDXWLO SXEOLFFODVV%HHU6HOHFWH[WHQGV+WWS6HUYOHW^ SXEOLFYRLGGR3RVW+WWS6HUYOHW5HTXHVWUHTXHVW +WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^
UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´
UHPRYHWKHROGWHVWRXWSXW 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³%HHU6HOHFWLRQ$GYLFHEU!´
6WULQJF UHTXHVWJHW3DUDPHWHU³FRORU´
Now that the JSP is going to produce the output, we should remove the test output from the servlet. We commented it out so that you could still see it here.
RXWSULQWOQ³EU!*RWEHHUFRORU³F %HHU([SHUWEH QHZ%HHU([SHUW /LVWUHVXOW EHJHW%UDQGVF
UHTXHVWVHW$WWULEXWH³VW\OHV´UHVXOW
Add an attribute to the request object for the JSP to use. Notice the JSP is looking for ÒstylesÓ.
5HTXHVW'LVSDWFKHUYLHZ UHTXHVWJHW5HTXHVW'LVSDWFKHU³UHVXOWMVS´ YLHZIRUZDUGUHTXHVWUHVSRQVH ` `
Instantiate a dispatcher forrethquest e JSP.
Use the request dispatcher to ask the Container to crank up the JSP, sending it the request and response. \RXDUHKHUH
Chapter 3. Mini MVC Tutorial Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID:
[email protected] Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, 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.
Chapter 3. Mini MVC Tutorial
FRPSLOHGHSOR\DQGWHVW
+WUXQTMLMXTWaIVL\M[\\PMNQVITIXX )NTHISCHAPTERWEVEBUILTANENTIREALBEITTINY -6#APPLICATION USING(4-, SERVLETSAND*30S9OUCANADDTHISTOYOURRESUME
&RPSLOLQJWKHVHUYOHW :H¶OOXVHWKHVDPHFRPSLOHUFRPPDQGWKDWZHXVHGHDUOLHU )LOH(GLW:LQGRZ+HOS5XQ,WV$7UDS
FGEHHU9 MDYDFFODVVSDWK8VHUVEHUW$SSOLFDWLRQVWRPFDWFRPPRQOLE VHUYOHWDSLMDUFODVVHVGFODVVHVVUFFRPH[DPSOHZHE%HHU6HOHFWMDYD
'HSOR\LQJDQGWHVWLQJWKHZHEDSS 1RZLW¶VWLPHWRUHGSOR\WKHVHUYOHW 0RYHDFRS\RIWKHVHUYOHW¶VFODVV¿OHWR%HHUY:(%,1)FODVVHVFRPH[DPSOHZHE DJDLQWKLVZLOOUHSODFHWKHSUHYLRXVYHUVLRQWZRFODVV¿OH 6KXWGRZQDQGUHVWDUWWRPFDW
)LOH(GLW:LQGRZ+HOS6DYH