Head first servlets and jsp part 001

Page 1

Chapter 1. Why use Servlets & JSPs?

LQWUR DQG RYHUYLHZ

:K\ XVH 6HUYOHWV -63V" +DK , NQRZ &*, 0\ ZHEVLWH ZLOO UXOH WKH ZRUOG

<RX IRRO <RX PXVW XVH 6HUYOHWV DQG -63V ,I \RX FRQWLQXH WR ZULWH 3HUO VFULSWV , ZLOO GHVWUR\ \RX

: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

WKLV LV D QHZ 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: stephengoss@gmx.net 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?

RI¿FLDO 6XQ H[DP REMHFWLYHV

6HUYOHWV -63 RYHUYLHZ

&RYHUDJH 1RWHV

)RU HDFK RI WKH +773 0HWKRGV VXFK DV *(7

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

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

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

-^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 ofilla or clients (using browsers like Moz server Safari) and servers (using web through apps like Apache) connected Our goal is wires and wireless networks. clients that ion icat appl to build a web to around the globe can access. And become obscenely rich.

:HE EURZVHU :HE EURZVHU

&OLHQW 6HUYHU

&OLHQW

:HE EURZVHU :HE EURZVHU

&OLHQW

&OLHQW

6HUYHU

:HE EURZVHU

&OLHQW

6HUYHU

the earth

\RX DUH KHUH

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: stephengoss@gmx.net 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?

ZHE VHUYHU

?PI\ LWM[ aW]Z _MJ [MZ ^MZ LW' $ ZHE VHUYHU WDNHV D FOLHQW UHTXHVW DQG JLYHV VRPHWKLQJ EDFN WR WKH FOLHQW ! 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

The clientÕs request contains the name and address (the URL), of the thing the client is looking for. P\ UHTXHVW LV [\]

UHTXHVW KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO!

:HE EURZVHU

ERG\! KWPO!

UHVSRQVH KWPO! KHDG! KHDG!

&OLHQW

ERG\! ERG\! KWPO!

ually has The server us tentÓ that on Òc of lots clients. it can send tot can be en nt That co JPEGs, and web pages, ces. other resour

6HUYHU

The serverÕs respo actual document thnse contains the at the client requested (or an er request could not ror code if the be processed).

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

?PI\ LWM[ I _MJ KTQMV\ LW' $ 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

8VHU FOLFNV D OLQN LQ WKH EURZVHU

%URZVHU IRUPDWV WKH UHTXHVW DQG VHQGV LW WR WKH VHUYHU

6HUYHU À QGV WKH UHTXHVWHG SDJH

KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO!

P\ UHTXHVW LV [\]

FOLFN

ERG\! KWPO!

8VHU

6HUYHU

%URZVHU

%URZVHU JHWV WKH +70/ DQG UHQGHUV LW LQWR D GLVSOD\ IRU WKH XVHU KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO!

KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

8VHU

%URZVHU

6HUYHU IRUPDWV WKH UHVSRQVH DQG VHQGV LW WR WKH FOLHQW EURZVHU

ERG\! KWPO!

6HUYHU

\RX DUH KHUH

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: stephengoss@gmx.net 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?

+70/ DQG +773

+TQMV\[ IVL [MZ ^MZ[ SVW_ 0<54 IVL 0<<8 +70/ 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-,

%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/

+70/ WHOOV WKH EURZVHU KRZ WR GLVSOD\ WKH FRQWHQW WR WKH XVHU

+773 -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

+773 LV WKH SURWRFRO FOLHQWV DQG VHUYHUV XVH RQ WKH ZHE WR FRPPXQLFDWH 7KH VHUYHU XVHV +773 WR VHQG +70/ WR WKH FOLHQW

&9) (440 STANDS FOR (YPER4EXT 4RANSPORT 0ROTOCOL

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

<_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

7DJ

'HVFULSWLRQ

!

WHERE YOU PUT YOUR COMMENTS

D!

ANCHOR USUALLY FOR PUTTING IN A HYPERLINK

DOLJQ!

ALIGN THE CONTENTS LEFT RIGHT CENTERED OR JUSTIlED

ERG\!

DElNE THE BOUNDARIES OF THE DOCUMENT S BODY

EU!

A LINE BREAK

FHQWHU!

CENTER THE CONTENTS

IRUP!

DElNE A FORM WHICH USUALLY PROVIDES INPUT lELDS

K !

THE lRST LEVEL HEADING

KHDG!

DElNE THE BOUNDARIES OF THE DOCUMENT S HEADER

KWPO!

DElNE THE BOUNDARIES OF THE (4-, DOCUMENT

LQSXW W\SH!

DElNES AN INPUT WIDGET TO A FORM

S!

A NEW PARAGRAPH

WLWOH!

THE (4-, DOCUMENT S TITLE

and <align> tags (Technically, the <center>HTM L 4.0, but weÕre have been deprecated in examples because itÕs our using them in some of alternative, and youÕre simpler to read than the anyway.) not here to learn HTML

\RX DUH KHUH

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: stephengoss@gmx.net 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?

ZULWLQJ +70/

?PI\ aW] _ZQ\M \PM 0<54 )MAGINE YOU RE CREATING A LOGIN PAGE 4HE SIMPLE (4-, MIGHT LOOK SOMETHING LIKE THIS

An HTML

KWPO!

comment

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 ´!

'

to send The servletst to. ue the req

g is nested The <img> tagr aph <align> ra pa a e insid place to r de or tag in y where hl ug ro e ag the im ember, em (R . it we want precated, but de <align> is cause itÕs weÕre using it be .) ad re simple to

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!

itÓ button The Òsubmrm. fo in the

WeĂ•ll talk more about forms later, but brieĂ&#x;y, the browser can collect the userĂ•s input and return it to the server.

The <br> tags cause line breaks.

<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-,

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

?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

%

&

'

(

\RX DUH KHUH

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: stephengoss@gmx.net 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?

+773 SURWRFRO

?PI\ Q[ \PM 0<<8 XZW\WKWT' (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 .H\ HOHPHQ WV RI WKH U DESTINATION EVEN THOUGH THE l LE IS SPLIT INTO CHUNKS WHEN HTXHVW VWUHDP IT S SENT )0 IS THE UNDERLYING PROTOCOL THAT MOVES ROUTES +773 PHW THE CHUNKS PACKETS FROM ONE HOST TO ANOTHER ON THEIR KRG WKH DF EH SHUIRUP WLRQ WR WAY TO THE DESTINATION (440 THEN IS ANOTHER NETWORK HG

PROTOCOL THAT HAS 7EB SPECIl C FEATURES BUT IT DEPENDS 7KH SDJH WR DFFHVV D ON 4#0 )0 TO GET THE COMPLETE REQUEST AND RESPONSE 85/

)RUP SDUDP HWHUV OLNH FROM ONE PLACE TO ANOTHER 4HE STRUCTURE OF AN (440 DUJXPHQWV WR D PHWKRG

CONVERSATION IS A SIMPLE 2EQUEST 2ESPONSE SEQUENCE A BROWSER REQUESTS AND A SERVER RESPONDS

+773 UHTXHVW :HE EURZVHU

+773 UHVSRQVH

&OLHQW

.H\ HOHPHQWV RI WKH UHVSRQVH VWUHDP

6HUYHU

ZKHWKHU $ VWDWXV FRGH IRUFFH VVIXO

WKH UHTXHVW ZDV VX UH &RQWHQW W\SH WH[W SLFWX +70/ HWF

O 7KH FRQWHQW WKH DFWXD +70/ LPDJH HWF

<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

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

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 :HE EURZVHU

+773 UHVSRQVH

6HUYHU

&OLHQW

HTTP header

When the browser Ăžnds the opening <html> tag it goes into HTML-rendering mode and displays the page to the user. When the browser gets to anher image tag, it generates anot HTTP request to go get thecase resource described. In this nd the browser will make a secopicture HTTP request to get the . referenced in the <img> tag

+773 KHDGHU LQI R KWPO! KHDG! KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

HTTP body

\RX DUH KHUH

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: stephengoss@gmx.net 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?

+773 PHWKRGV

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

*(7 8VHU FOLFNV D OLQN WR D QHZ SDJH

%URZVHU VHQGV DQ +773 *(7 WR WKH VHUYHU DVNLQJ WKH VHUYHU WR *(7 WKH SDJH

KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO!

*(7

ERG\! KWPO!

8VHU

%URZVHU

6HUYHU

3267 %URZVHU VHQGV DQ +773 3267 WR WKH VHUYHU JLYLQJ WKH VHUYHU ZKDW WKH XVHU W\SHG LQWR WKH IRUP

8VHU W\SHV LQ D IRUP DQG KLWV WKH 6XEPLW EXWWRQ

KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO!

3267

ERG\! KWPO!

8VHU

%URZVHU

6HUYHU

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

/-< 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

\RX DUH KHUH

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: stephengoss@gmx.net 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?

+773 *(7

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 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 "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

The original URL before the extra parameters.

The Ò?Ó separates the path and the paramete (the extra data). Th rs e amount of data you can send along with the GE T is limited, and itÕs exposed up her in the browser bar for everyone to see. Toge eth er, the entire Strin the URL that is sent with the request. g is

And if you need help with the exam, check out javaranch which also includes 100% unbiased recommendations to buy whatever books the authors wrote.

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

)VI\WUa WN IV 0<<8 /-< ZMY]M[\ 4HE PATH TO THE RESOURCE AND ANY PARAMETERS ADDED TO THE 52, ARE ALL INCLUDED ON THE hREQUEST LINEv

uest line. The Req

The HTTP Method.

(if meters st, parapended to L, e u q e r T R ap In a GEare any) are the request Us are The path ere part of Parameter Âľ. The prot h t resourc to the the Ăžrst with a Ă’?Ă“. mpersand Ă’ that the ocol version g web servee on the is requestinweb browser startinted with an a r. g. separa

*(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

The Request headers.

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

+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

:HE EURZVHU

&OLHQW

6XUH ,¡OO JR *(7 WKDW SDJH DQG WKDQNV IRU WKH SDUDPHWHUV $QG MXVW )<, ´KXUU\ LW XS¾ LV QRW SDUW RI WKH +773 SURWRFRO

+773 UHTXHVW *(7

6HUYHU \RX DUH KHUH

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: stephengoss@gmx.net 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?

+773 3267

)VI\WUa WN IV 0<<8 87;< ZMY]M[\ (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

The HTTP Method.

ue The Req

st line.

version The protocolb we e th at th esting. browser is requ

The pat on the wh to the resource eb server .

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

The Request headers.

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

, age body The messes called sometim yloadÓ. the Òpa

FRORU GDUN WDVWH PDOW\

This time, the paramete in the body, so they arers are down here nÕt limited the way they are if you use to put them in the Req a GET and have uest line.

+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

:HE EURZVHU

&OLHQW

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

+773 UHTXHVW 3267

6HUYHU

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

)VI\WUa WN IV 0<<8 ZM[XWV[M IVL _PI\ \PM PMKS Q[ I š515- \aXMº' .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

e at status cod sion th col veris using. The HTTRPesponse. o t o r p A text version of The eb server for the the status code. the w +773 2. 6HW &RRNLH -6(66,21,' $$% & '( ( ( ) &) %)&$ & 3DWK WHVW(/ &RQWHQW 7\SH WH[W KWPO

HTTP Response headers.

&RQWHQW /HQJWK 'DWH :HG 1RY *07 6HUYHU $SDFKH &R\RWH &RQQHFWLRQ FORVH

The body holds the HTML, or other content to be rendered...

:HE EURZVHU

KWPO! KWPO!

+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

The content-type response head value is known as a MIME type. erÕs The MIME type tells the browser wha kind of data the browser is abou t receive so that the browser will t to know how to render it. Notice that the MIME type e relates to the values listed in valu HTTP requestÕs ÒAcceptÓ headthe (Go look at the Accept head er. from the previous pageÕs POST requerest. )

+773 UHVSRQVH +773 2.

&OLHQW

KWPO! KHDG! KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

6HUYHU

\RX DUH KHUH

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: stephengoss@gmx.net 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?

UHTXHVW DQG UHVSRQVH

)TT \PM XQMKM[ 7V WVM XIOM

The user URL. types a

KWWS ZZZ ZLFNHGO\VPDUW FRP WHVW %HHU KWPO

The browser creates an HTTP GET request.

s ET i TP G server. T H The t to the sen

73 *(7 WHVW %HHU KWPO +7 P UW FR VPD HGO\ +RVW ZZZ ZLFN 0DFLQWRVK 8VHU $JHQW 0R]LOOD

The server the page... Ăžnds 6HUYHU

And generates an HTTP response.

*MMZ P\UT

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!

+773 2. 6HW &RRNLH

P HTT s sent . Thesponse bi rowser re the to :HE EURZVHU

The browse renders ther HTML.

&OLHQW

KWPO! ERG\! K DOLJQ FHQWHU!%HHU /RJLQ 3DJH K ! IRUP! 6HOHFW D EHHU W\SH RU EX\ EHHU

Client look forward tos a successful transaction.beer

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

6KDUSHQ \RXU SHQFLO *(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

3267 *(7

$ XVHU LV UHWXUQLQJ D ORJLQ QDPH DQG SDVVZRUG

3267 *(7

$ XVHU LV UHTXHVWLQJ D QHZ SDJH YLD D K\SHUOLQN

3267 *(7

$ FKDW URRP XVHU LV VHQGLQJ D ZULWWHQ UHVSRQVH

3267 *(7

$ XVHU KLWV WKH µQH[W¶ EXWWRQ WR VHH WKH QH[W SDJH

3267 *(7

$ XVHU KLWV WKH µORJ RXW¶ EXWWRQ RQ D VHFXUH EDQNLQJ VLWH

3267 *(7

$ XVHU KLWV WKH µEDFN¶ EXWWRQ RQ WKH EURZVHU

3267 *(7

$ XVHU VHQGV D QDPH DQG DGGUHVV IRUP WR WKH VHUYHU

3267 *(7

$ XVHU PDNHV D UDGLR EXWWRQ VHOHFWLRQ

\RX DUH KHUH

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: stephengoss@gmx.net 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?

DQDWRP\ RI D 85/

=: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

Protocol: Tells the server which communications protocol (in this case HTTP) will be used.

is the URL is part osef rver supports h T : t r Po nal. A single application is optio rts. A server you donÕt many po ed by a port. If RL, then U iÞ r t u iden a port in yo nd as luck specify is the default, ea default 0 h 8 t port ave it, thatÕs would h r web servers. port fo

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.

KWWS ZZZ ZLFNHGO\VPDUW FRP EHHUDGYLFH VHOHFW EHHU KWPO

The path to the location, urce on the server, of the reso most of being requested. Becauseweb ran the on s ver ser ly ear the to used l stil is tax syn x Uni x, Uni hies arc hier ry cto dire the e describ on the web server.

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:

Not sho w

n:

Optional Que

ry String: Remember, GET request,if this was a (parameter the extra info to the end s)ofwould be appended with a questi this URL, starting on mark Ă’?Ă“, each paramet and wit separated by er (name/value pair) h an ampersand Ă’ Ă“.

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

:HOO NQRZQ 7&3 SRUW QXPEHUV IRU FRPPRQ VHUYHU DSSOLFDWLRQV

$ 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

FTP

Telnet

SMTP

21 23 25 6HUYHU

443 HTTPS

37

Time

110 80 POP3

HTTP

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).

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

\RX DUH KHUH

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: stephengoss@gmx.net 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?

ZHE VLWH GLUHFWRU\

,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 $SDFKHBKRPH

Òindex.htmlÓ is the default page that will be returned to a user who keys Òwww.wickedlysmart.com/Ó into his browser.

An Apache server will assume that ÒhtdocsÓ is the directory that is the root for all of the serverÕs web applications.

KWGRFV

lder The root foerAdvice be he t r fo application.

)

KWPO! KWPO!

VNLLQJ$GYLFH

The two applications on this server.

EHHU$GYLFH

,QGH[ KWPO

*

KWPO! KWPO!

VHOHFW

,QGH[ KWPO

+

KWPO! KWPO!

,QGH[ KWPO

Òindex.htmlÓ is default page the r the beerAdvice apfo plication.

The folders for the beerAdvice appÕs two actions.

,

KWPO! KWPO!

VHOHFW%HHU KWPO

FKHFNRXW

VHOHFW

An HTML page that gives the user some advice.

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

6KDUSHQ \RXU SHQFLO 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

http://www.wickedlysmart.com 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 LQGH[ KWPO SDJH DW ORFDWLRQ " +

ZLOO FDXVH WKH VHUYHU WR UHWXUQ WR \RX WKH VHOHFW%HHU KWPO SDJH DW ORFDWLRQ , \RX DUH KHUH

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: stephengoss@gmx.net 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?

VWDWLF SDJHV

?MJ [MZ ^MZ[ TW^M [MZ ^QVO [\I\QK _MJ XIOM[ 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

ZHE VHUYHU DSSOLFDWLRQ

KWPO! KHDG! KHDG! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KWPO! ERG\! KHDG! KHDG! ERG\! KWPO! KHDG! KWPO! ERG\! KHDG! ERG\! ERG\! KWPO! ERG\! KWPO!

$ 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 These pages go straight to the client just re exactly as they we put on the server.

ZHE VHUYHU PDFKLQH %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/"

ck What if we want toinssti something variable ide the HTML page? KWPO! ERG\! 7KH FXUUHQW WLPH LV >LQVHUW7LPH2Q6HUYHU@ ERG\! KWPO!

% ZZZ ZLFNHGO\VPDUW FRP VNLLQJ$GYLFH & ZZZ ZLFNHGO\VPDUW FRP EHHU$GYLFH ' ZZZ ZLFNHGO\VPDUW FRP EHHU$GYLFH VHOHFW VHOHFW%HHU KWPO $QVZHUV IURP SUHYLRXV SDJH

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

*]\ [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

, 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

DQRWKHU DSSOLFDWLRQ RQ WKH VHUYHU

ZHE VHUYHU PDFKLQH

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 DSSOLFDWLRQ

DQRWKHU DSSOLFDWLRQ RQ WKH VHUYHU \RX DUH KHUH

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: stephengoss@gmx.net 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?

ZKHQ D ZHE VHUYHU LVQ¶W HQRXJK

<_W \PQVO[ \PM _MJ [MZ ^MZ ITWVM _WV¼\ LW )F YOU NEED JUST IN TIME PAGES DYNAMICALLY CREATED PAGES THAT DON T EXIST BEFORE THE REQUEST AND THE ABILITY TO WRITE SAVE DATA ON THE SERVER WHICH MEANS WRITING TO A l LE OR DATABASE YOU CAN T RELY ON THE WEB SERVER ALONE

'\QDPLF FRQWHQW 4HE WEB SERVER APPLICATION SERVES ONLY STATIC PAGES BUT A SEPARATE hHELPERv APPLICATION THAT THE WEB SERVER CAN COMMUNICATE WITH CAN BUILD NON STATIC JUST IN TIME PAGES ! DYNAMIC PAGE COULD BE ANYTHING FROM A CATALOG TO A WEBLOG OR EVEN JUST A PAGE THAT RANDOMLY CHOOSES PICTURES TO DISPLAY :KHQ LQVWHDG RI WKLV

<RX ZDQW WKLV

KWPO! ERG\! 7KH FXUUHQW WLPH LV DOZD\V 30 RQ WKH VHUYHU ERG\! KWPO!

KWPO! ERG\! 7KH FXUUHQW WLPH LV >LQVHUW7LPH2Q6HUYHU@ RQ WKH VHUYHU ERG\! KWPO!

-XVW LQ WLPH SDJHV GRQ·W H[LVW EHIRUH WKH UHTXHVW FRPHV LQ ,W·V OLNH PDNLQJ DQ +70/ SDJH RXW RI WKLQ DLU 7KH UHTXHVW FRPHV LQ WKH KHOSHU DSS ´ZULWHVµ WKH +70/ DQG WKH ZHE VHUYHU JHWV LW EDFN WR WKH FOLHQW

6DYLQJ GDWD RQ WKH VHUYHU 7HEN THE USER SUBMITS DATA IN A FORM THE WEB SERVER SEES THE FORM DATA AND THINKS h3O ,IKE ) CARE v 4O PROCESS THAT FORM DATA EITHER TO SAVE IT TO A l LE OR DATABASE OR EVEN JUST TO USE IT TO GENERATE THE RESPONSE PAGE YOU NEED ANOTHER APP 7HEN THE WEB SERVER SEES A REQUEST FOR A HELPER APP THE WEB SERVER ASSUMES THAT PARAMETERS ARE MEANT FOR THAT APP 3O THE WEB SERVER HANDS OVER THE PARAMETERS AND GIVES THE APP A WAY TO GENERATE A RESPONSE TO THE CLIENT

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

<PM VWV 2I^I \MZU NWZ I _MJ [MZ ^MZ PMTXMZ IXX Q[ ยน+/1ยบ XZWOZIU -OST #') PROGRAMS ARE WRITTEN AS 0ERL SCRIPTS BUT MANY OTHER LANGUAGES CAN WORK INCLUDING # 0YTHON AND 0(0 #') STANDS FOR #OMMON 'ATEWAY )NTERFACE AND WE DON T CARE WHY IT S CALLED THAT 5SING #') HERE S HOW IT MIGHT WORK FOR A DYNAMIC WEB PAGE THAT HAS THE CURRENT SERVER DATE ZHE VHUYHU PDFKLQH

8VHU FOLFNV D OLQN WKDW KDV D 85/ WR D &*, SURJUDP LQVWHDG RI D VWDWLF SDJH

:HE EURZVHU

ZHE VHUYHU DSS

&OLHQW

ZHE VHUYHU PDFKLQH

:HE EURZVHU

SDUDPV

ZHE VHUYHU DSS

&OLHQW

KHOSHU DSS

ZHE VHUYHU PDFKLQH

:HE EURZVHU

KWPO! KHDG! KHDG!

ZHE VHUYHU DSS

&OLHQW

ERG\! ERG\! KWPO!

KHOSHU DSS

ZHE VHUYHU PDFKLQH

:HE EURZVHU

&OLHQW

+773 KHDGHU LQIR KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

ZHE VHUYHU DSS

:HE VHUYHU DSSOLFDWLRQ ยดVHHVยต WKDW WKH UHTXHVW LV IRU D KHOSHU SURJUDP VR WKH ZHE VHUYHU ODXQFKHV DQG UXQV WKH SURJUDP 7KH ZHE VHUYHU DSS VHQGV DORQJ DQ\ SDUDPHWHUV IURP D *(7 RU 3267

7KH KHOSHU DSS FRQVWUXFWV WKH EUDQG QHZ SDJH WKDW KDV WKH FXUUHQW GDWH LQVHUWHG DQG VHQGV WKH +70/ EDFN WR WKH VHUYHU $V IDU DV WKH ZHE VHUYHU LV FRQFHUQHG WKH +70/ IURP WKH KHOSHU DSS LV D VWDWLF SDJH 7KH KHOSHU DSSOLFDWLRQ LV VKXW GRZQ DQG WKH FOLHQW JHWV EDFN DQ +70/ SDJH WKDW KDV WKH FXUUHQW GDWH DV SDUW RI LWV QRZ VWDWLF FRQWHQW

\RX DUH KHUH

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: stephengoss@gmx.net 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?

WZR VLGHV &*, DQG 6HUYOHWV

;MZ ^TM\[ IVL +/1 JW\P XTIa \PM ZWTM WN I PMTXMZ IXX QV \PM _MJ [MZ ^MZ

&*,

#') IS BETTER THAN 3ERVLETS 7E WRITE #') SCRIPTS IN 0ERL AT OUR SHOP BECAUSE EVERYBODY KNOWS 0ERL ) GUESS THAT S l NE IF YOU USE *AVA SINCE YOU KNOW IT "UT IT S CERTAINLY NOT WORTH IT FOR US TO SWITCH TO *AVA 4HERE S NO ADVANTAGE

,ISTEN IN AS OUR TWO BLACK BELTS DISCUSS THE PROS AND CONS OF #') AND 3ERVLETS

6HUYOHWV

) DOUBT EVERYBODY KNOWS 0ERL ) LIKE 0ERL BUT WE RE ALL *AVA PROGRAMMERS IN OUR SHOP SO WE PREFER *AVA 7ITH MUCH RESPECT MASTER THERE ARE MANY ADVANTAGES TO USING *AVA OVER 0ERL FOR THE THINGS YOU WANT TO DO WITH #')

9OU CHALLENGE ME /N WHAT GROUNDS

4HIS IS NO DIFFERENT FROM *AVA WHAT DO YOU CALL THE *6- )S NOT EVERY INSTANCE OF THE *6- A HEAVY WEIGHT PROCESS

) SEE YOU HAVE FORGOTTEN MUCH 7EB SERVERS NOW ARE ABLE TO KEEP A SINGLE 0ERL PROGRAM RUNNING BETWEEN CLIENT REQUESTS 3O THE ADDITIONAL OVERHEAD ARGUMENT IS WORTHLESS

7HAT ARE YOU TALKING ABOUT !NY #/2"! COMPLIANT THING CAN BE A * %% CLIENT

3TOPˆ) M LATE FOR MY 0ILATES CLASS "UT THIS IS NOT OVER 7E LL HAVE TO l NISH IT LATER

0ERFORMANCE FOR ONE THING 7ITH 0ERL THE SERVER HAS TO LAUNCH A HEAVY WEIGHT PROCESS FOR EACH AND EVERY REQUEST FOR THAT RESOURCE !H YES BUT YOU SEE 3ERVLETS STAY LOADED AND CLIENT REQUESTS FOR A 3ERVLET RESOURCE ARE HANDLED AS SEPARATE THREADS OF A SINGLE RUNNING 3ERVLET 4HERE S NO OVERHEAD OF STARTING THE *6- LOADING THE CLASS AND ALL THAT

) HAVE NOT FORGOTTEN MASTER "UT IT IS NOT ALL WEB SERVERS THAT CAN DO THAT 9OU ARE TALKING ABOUT A SPECIAL CASE WHICH DOES NOT APPLY TO ALL 0ERL #') PROGRAMS "UT 3ERVLETS WILL ALWAYS BE MORE EFl CIENT IN THAT WAY !ND LET S NOT FORGET THAT A 3ERVLET CAN BE A * %% CLIENT WHILE A 0ERL #') PROGRAM CANNOT ) DO NOT MEAN A CLIENT TO A * %% PROGRAM ) MEAN A CLIENT THAT IS * %% ! 3ERVLET RUNNING IN A * %% WEB CONTAINER CAN PARTICIPATE IN SECURITY AND TRANSACTIONS RIGHT ALONG WITH ENTERPRISE BEANS AND THERE AREˆ TO BE CONTINUED

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

6KDUSHQ \RXU SHQFLO The user URL. types a

5HTXHVW 5HVSRQVH )LOO LQ WKH ER[HV ZLWK D GHVFULSWLRQ RI ZKDW KDSSHQV GXULQJ WKDW VWHS LQ WKH SURFHVV 7KLV LV D GXSOLFDWH RI SDJH VR ZKHQ \RX·UH ILQLVKHG IOLS EDFN WR WKDW SDJH WR FRPSDUH \RXU DQVZHUV

KWWS ZZZ ZLFNHGO\VPDUW FRP WHVW %HHU KWPO

73 *(7 WHVW %HHU KWPO +7 P UW FR VPD HGO\ ZLFN +RVW ZZZ 0DFLQWRVK 8VHU $JHQW 0R]LOOD

6HUYHU

*MMZ P\UT 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!

+773 2. 6HW &RRNLH KWPO! ERG\! K DOLJQ FHQWHU!%HHU /RJLQ 3DJH K ! IRUP! 6HOHFW D EHHU W\SH RU EX\ EHHU

:HE EURZVHU

&OLHQW

Client look forward tos a successful transaction.beer

\RX DUH KHUH

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: stephengoss@gmx.net 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?

TXLFNLH ORRN DW VHUYOHWV

;MZ ^TM\[ ,MUa[\QNQML _ZQ\M LMXTWa Z]V *UST SO THOSE NEW TO SERVLETS CAN STOP HOLDING THEIR BREATH HERE S A QUICK GUIDE TO WRITING DEPLOYING AND RUNNING A SERVLET 4HIS MIGHT CREATE MORE QUESTIONS THAN IT ANSWERSˆDON T PANIC YOU DON T HAVE TO DO THIS RIGHT NOW )T S JUST A QUICK DEMONSTRATION FOR THOSE WHO CAN T WAIT 4HE NEXT CHAPTER INCLUDES A MORE THOROUGH TUTORIAL

SURMHFW

FODVVHV

VUF

%XLOG WKLV GLUHFWRU\ WUHH VRPHZKHUH QRW XQGHU WRPFDW

:ULWH D VHUYOHW QDPHG &K 6HUYOHW MDYD DQG SXW LW LQ WKH VUF GLUHFWRU\ WR NHHS WKLV H[DPSOH VLPSOH ZH DUHQ¡W SXWWLQJ WKH VHUYOHW LQ D SDFNDJH EXW DIWHU WKLV DOO RWKHU VHUYOHW H[DPSOHV LQ WKH ERRN ZLOO EH LQ SDFNDJHV

HWF

6HUYOHW ^ GR*HW `

&K 6HUYOHW MDYD

ZHEDSS! ZHEDSS!

ZHE [PO

LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV &K 6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ ^

Standard servlet declarations (there will be about 400 pages describing this stuff).

3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU MDYD XWLO 'DWH WRGD\ QHZ MDYD XWLO 'DWH RXW SULQWOQ ³ KWPO! ³ ³ ERG\!´ ³ K DOLJQ FHQWHU!+)?œV &KDSWHU 6HUYOHW K !´ ³ EU!´ WRGD\ ³ ERG\!´ ³ KWPO!´ ` `

&UHDWH D GHSOR\PHQW GHVFULSWRU '' QDPHG ZHE [PO SXW LW LQ WKH HWF GLUHFWRU\

ZHE DSS [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE DSSB B [VG´ YHUVLRQ ´ ´! VHUYOHW! VHUYOHW QDPH!&KDSWHU 6HUYOHW VHUYOHW QDPH! VHUYOHW FODVV!&K 6HUYOHW VHUYOHW FODVV! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!&KDSWHU 6HUYOHW VHUYOHW QDPH! XUO SDWWHUQ! 6HUY XUO SDWWHUQ! VHUYOHW PDSSLQJ! ZHE DSS!

bedded in a HTML imra m. Looks og Java pr snÕt it? oe lovely, d

Highlights:

lication. -One DD per web app ny servlets. -A DD can declare ma ties the - A <servlet-name>to the <servlet<servlet> element mapping> element. the Java class. - A <servlet-class> is the name the - A <url-pattern>e isrequest. th for s use nt clie

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

%XLOG WKLV GLUHFWRU\ WUHH XQGHU WKH H[LVWLQJ WRPFDW GLUHFWRU\

)URP WKH SURMHFW GLUHFWRU\ FRPSLOH WKH VHUYOHW

WRPFDW

The name of the webapp.

ZHEDSSV

MDYDF FODVVSDWK \RXU SDWK WRPFDW FRPPRQ OLE VHUYOHW DSL MDU G FODVVHV VUF &K 6HUYOHW MDYD FK

(This is all one command.) WKH &K 6HUYOHW FODVV ILOH ZLOO HQG XS LQ SURMHFW FODVVHV

:(% ,1)

ZHEDSS! ZHEDSS!

&RS\ WKH &K 6HUYOHW FODVV ILOH WR :(% ,1) FODVVHV DQG FRS\ WKH ZHE [PO ILOH WR :(% ,1)

ZHE [PO

FODVVHV

)URP WKH WRPFDW GLUHFWRU\ VWDUW 7RPFDW ELQ VWDUWXS VK

/DXQFK \RXU EURZVHU DQG W\SH LQ

&K 6HUYOHW FODVV

h1Õ and is named Ôc The webaptp is named ÔServ1Õ. the servle

KWWS ORFDOKRVW FK 6HUY

LW VKRXOG GLVSOD\

7XH $SULO 067

!

ay vary...

m Your date

)RU QRZ HYHU\ WLPH \RX XSGDWH HLWKHU D VHUYOHW FODVV RU WKH GHSOR\PHQW GHVFULSWRU VKXWGRZQ 7RPFDW ELQ VKXWGRZQ VK \RX DUH KHUH

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: stephengoss@gmx.net 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?

+70/ LQ D SULQWOQ VXFNV

1R RIIHQVH KHUH EXW WKHUH¡V VRPHWKLQJ 6(5,286/< ZURQJ ZLWK WKLV VHUYOHWV SLFWXUH WU\LQJ WR VWXII +70/ LQVLGH D SULQWOQ "" 7KDW FDQ¡W EH ULJKW

dynamic web u create a ve to print yo w ho is s Thi ream rvlet. You ha page in a sething to an output Pstresponse le T ho T H the w ). part of the (itÕs reallyhat youÕre printing to t stream

RXW SULQWOQ ³ KWPO! ³ ³ ERG\!´ ³ K !6N\OHU?œV /RJLQ 3DJH K !´ ³ EU!´ WRGD\ ³ ERG\!´ ³ KWPO!´

)K\]ITTa \ZaQVO \W NWZUI\ 0<54 QV[QLM I [MZ ^TM\Âź[ W]\ XZQV\TV XZM\\a U]KP []KS[ 4HIS IS ONE OF THE WORST PARTS NO THE WORST PART OF SERVLETS 3TUFlNG PROPERLY FORMATTED (4-, TAGS INTO THE PRINTLN JUST SO THAT YOU CAN INSERT VARIABLES AND METHOD CALLS IS JUST BRUTAL $ON T EVEN THINK ABOUT DOING ANYTHING THE LEAST BIT SOPHISTICATED

WKHUH DUH QR

4

'XPE 4XHVWLRQV

)T CAN T BE THAT BAD WHY CAN T ) JUST COPY A WHOLE PAGE OF (4-, FROM MY WEB PAGE EDITOR LIKE $REAMWEAVER AND PASTE IT INTO THE PRINTLN )T S NOT LIKE ) HAVE TO BE ABLE TO READ THE CODE IN THERE

$

/BVIOUSLY YOU HAVEN T TRIED THIS YET )T SOUNDS GOOD 9ES ) LL JUST MAKE MY PAGE IN A DECENT WEB PAGE EDITOR OR EVEN A SIMPLE TEXT FILE WOULD BE EASIER THAN IN MY *AVA CODE AND THEN A QUICK COPY AND PASTE INTO THE PRINTLN AND VOILA %XCEPT YOU GET ABOUT COMPILER ERRORS 2EMEMBER YOU CAN T HAVE A CARRIAGE RETURN A REAL ONE INSIDE A 3TRING LITERAL !ND WHILE WE RE TALKING ABOUT 3TRINGS WHAT ABOUT ALL YOUR (4-, THAT HAS DOUBLE QUOTE MARKS IN IT

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

2K LI RQO\ WKHUH ZHUH D ZD\ WR SXW -DYD LQVLGH DQ +70/ SDJH LQVWHDG RI SXWWLQJ +70/ LQVLGH D -DYD FODVV

;PM LWM[VÂź\ SVW_ IJW]\ 2;8 KWPO! ERG\! K !6N\OHUÂśV /RJLQ 3DJH K ! EU! QHZ MDYD XWLO 'DWH ! ERG\! KWPO!

ks like a Whoa! This looht in the rig , va Ja e littl !? L M HT of middle

VN\OHUORJLQ MVS ! *30 PAGE LOOKS JUST LIKE AN (4-, PAGE EXCEPT YOU CAN PUT *AVA AND *AVA RELATED THINGS INSIDE THE PAGE 3O IT REALLY IS LIKE INSERTING A VARIABLE INTO YOUR (4-,

\RX DUH KHUH

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: stephengoss@gmx.net 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?

-DYD PHHWV +70/ -63

2;8 Q[ _PI\ PIXXMVML _PMV [WUMJWLa QV\ZWL]KML 2I^I \W 0<54 +H\ EDE\ QLFH WDJV

0UTTING *AVA INTO (4-, IS A SOLUTION FOR TWO ISSUES

1RW DOO +70/ SDJH GHVLJQHUV NQRZ -DYD !PP DEVELOPERS KNOW *AVA 7EB PAGE DESIGNERS KNOW (4-, 7ITH *30 *AVA DEVELOPERS CAN DO *AVA AND (4-, DEVELOPERS CAN DO WEB PAGES

KWPO! ERG\! HQW 7KH FXUU ZD\V DO WLPH LV 30 HUYHU V KH RQ W ERG\! KWPO!

FODVV )R R YRLG ED ^ U [ QHZ ^ 'DWH ` `

)RUPDWWLQJ +70/ LQWR D 6WULQJ OLWHUDO LV 5($//< XJO\ 0UTTING EVEN MARGINALLY COMPLEX (4-, INTO THE ARGUMENT TO A PRINTLN IS A COMPILER ERROR WAITING TO HAPPEN 9OU MIGHT HAVE TO DO A TON OF WORK TO GET THE (4-, FORMATTED PROPERLY IN A WAY THAT STILL WORKS IN THE CLIENT S BROWSER YET SATISlES *AVA RULES FOR WHAT S ALLOWED IN A 3TRING LITERAL 9OU CAN T HAVE CARRIAGE RETURNS FOR EXAMPLE YET MOST OF THE (4-, YOU LL PULL FROM A WEB PAGE EDITOR WILL HAVE REAL CARRIAGE RETURNS IN THE SOURCE 1UOTES CAN BE A PROBLEM TOOˆA LOT OF (4-, TAGS USE QUOTES AROUND ATTRIBUTE VALUES FOR EXAMPLE !ND YOU KNOW WHAT HAPPENS WHEN THE COMPILER SEES A DOUBLE QUOTE IT THINKS h4HIS MUST BE THE END OF THE 3TRING LITERAL v 3URE YOU CAN GO BACK AND REPLACE EACH OF YOUR DOUBLE QUOTES WITH ESCAPE CODES BUT IT ALL GETS INSANELY ERROR PRONE

KWPO! ERG\! 7KH FXUUHQW WLPH RQ WKH VHUYHU LV QHZ MDYD XWLO 'DWH ! ERG\! KWPO!

4

7AIT THERE S STILL SOMETHING WRONG HERE "ENElT NUMBER ONE SAYS h.OT ALL PAGE DESIGNERS KNOW *AVA v BUT THE (4-, PAGE DESIGNER STILL HAS TO WRITE *AVA INSIDE THE *30 PAGE *30 LETS THE *AVA PROGRAMMER OFF THE HOOK FOR WRITING (4-, BUT IT DOESN T REALLY HELP THE (4-, DESIGNER )T MIGHT BE EASIER TO WRITE (4-, IN A *30 RATHER THAN IN A PRINTLN BUT THE (4-, DEVELOPER STILL HAS TO KNOW *AVA

$

,OOKS THAT WAY DOESN T IT "UT WITH THE NEW *30 SPEC AND BY FOLLOWING BEST PRACTICES THE PAGE DESIGNER SHOULD BE PUTTING VERY LITTLE OR NO REAL *AVA INTO A *30 4HEY DO HAVE TO LEARN SOMETHING BUT IT S MORE LIKE PUTTING IN LABELS THAT CALL REAL *AVA METHODS RATHER THAN EMBEDDING THE ACTUAL *AVA CODE INTO THE PAGE ITSELF 4HEY HAVE TO LEARN *30 SYNTAX BUT NOT THE *AVA LANGUAGE

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: stephengoss@gmx.net 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?

LQWUR DQG DUFKLWHFWXUH

:MDD=L HGAFLK

ƒ +773 VWDQGV IRU +\SHU7H[W 7UDQVIHU 3URWRFRO DQG LV WKH QHWZRUN SURWRFRO XVHG RQ WKH :HE ,W UXQV RQ WRS RI 7&3 ,3

ƒ +773 XVHV D UHTXHVW UHVSRQVH PRGHOÂłWKH FOLHQW PDNHV DQ +773 UHTXHVW DQG WKH ZHE VHUYHU JLYHV EDFN DQG +773 UHVSRQVH WKDW WKH EURZVHU WKHQ ILJXUHV RXW KRZ WR KDQGOH GHSHQGLQJ RQ WKH FRQWHQW W\SH RI WKH UHVSRQVH

ƒ ,I WKH UHVSRQVH IURP WKH VHUYHU LV DQ +70/ SDJH WKH +70/ LV DGGHG WR WKH +773 UHVSRQVH ƒ $Q +773 UHTXHVW LQFOXGHV WKH UHTXHVW 85/ WKH UHVRXUFH WKH FOLHQW LV WU\LQJ WR DF FHVV WKH +773 PHWKRG *(7 3267 HWF DQG RSWLRQDOO\ IRUP SDUDPHWHU GDWD DOVR FDOOHG WKH ´TXHU\ VWULQJÂľ

ƒ $Q +773 UHVSRQVH LQFOXGHV D VWDWXV FRGH WKH FRQWHQW W\SH DOVR NQRZQ DV 0,0( W\SH DQG WKH DFWXDO FRQWHQW RI WKH UHVSRQVH +70/ LPDJH HWF

ƒ $ *(7 UHTXHVW DSSHQGV IRUP GDWD WR WKH HQG RI WKH 85/ ƒ $ 3267 UHTXHVW LQFOXGHV IRUP GDWD LQ WKH ERG\ RI WKH UHTXHVW ƒ $ 0,0( W\SH WHOOV WKH EURZVHU ZKDW NLQG RI GDWD WKH EURZVHU LV DERXW WR UHFHLYH VR WKDW WKH EURZVHU ZLOO NQRZ ZKDW WR GR ZLWK LW UHQGHU WKH +70/ GLVSOD\ WKH JUDSKLF SOD\ WKH PXVLF HWF

ƒ 85/ VWDQGV IRU 8QLIRUP 5HVRXUFH /RFDWRU (YHU\ UHVRXUFH RQ WKH ZHE KDV LWV RZQ XQLTXH DGGUHVV LQ WKLV IRUPDW ,W VWDUWV ZLWK D SURWRFRO IROORZHG E\ WKH VHUYHU QDPH DQ RSWLRQDO SRUW QXPEHU DQG XVXDOO\ D VSHFLILF SDWK DQG UHVRXUFH QDPH ,W FDQ DOVR LQFOXGH DQ RSWLRQDO TXHU\ VWULQJ LI WKH 85/ LV IRU D *(7 UHTXHVW

ƒ :HE VHUYHUV DUH JRRG DW VHUYLQJ VWDWLF +70/ SDJHV EXW LI \RX QHHG G\QDPLFDOO\ JHQHUDWHG GDWD LQ WKH SDJH WKH FXUUHQW WLPH IRU H[DPSOH \RX QHHG VRPH NLQG RI KHOSHU DSS WKDW FDQ ZRUN ZLWK WKH VHUYHU 7KH QRQ -DYD WHUP IRU WKHVH KHOSHU DSSV PRVW RIWHQ ZULWWHQ LQ 3HUO LV &*, ZKLFK VWDQGV IRU &RPPRQ *DWHZD\ ,QWHUIDFH

ƒ 3XWWLQJ +70/ LQVLGH D SULQWOQ VWDWHPHQW LV XJO\ DQG HUURU SURQH EXW -63V VROYH WKDW SUREOHP E\ OHWWLQJ \RX SXW -DYD LQWR DQ +70/ SDJH UDWKHU WKDQ SXWWLQJ +70/ LQWR -DYD FRGH

\RX DUH KHUH

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: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO RYHUYLHZ

:HE $SS $UFKLWHFWXUH %HKROG WKH SRZHU RI P\ &RQWDLQHU D WKRXVDQG VLPXOWDQHRXV KLWV ZLOO QRW EULQJ PH WR P\ NQHHV

8K RK DQRWKHU YLFWLP RI - (( IHYHU

6HUYOHWV QHHG KHOS :KHQ D UHTXHVW FRPHV LQ VRPHERG\ KDV WR LQVWDQWLDWH WKH VHUYOHW RU DW OHDVW PDNH D QHZ WKUHDG WR KDQGOH WKH UHTXHVW 6RPHERG\ KDV WR FDOO WKH VHUYOHW¶V GR3RVW RU GR*HW PHWKRG $QG RK \HV WKRVH PHWKRGV KDYH FUXFLDO DUJXPHQWV²WKH +773 UHTXHVW DQG +773 UHVSRQVH REMHFWV 6RPHERG\ KDV WR JHW WKH UHTXHVW DQG WKH UHVSRQVH WR WKH VHUYOHW 6RPHERG\ KDV WR PDQDJH WKH OLIH GHDWK DQG UHVRXUFHV RI WKH VHUYOHW 7KDW VRPHERG\ LV WKH ZHE &RQWDLQHU ,Q WKLV FKDSWHU ZH¶OO ORRN DW KRZ \RXU ZHE DSSOLFDWLRQ UXQV LQ WKH &RQWDLQHU DQG ZH¶OO WDNH D ¿ UVW ORRN DW WKH VWUXFWXUH RI D ZHE DSS XVLQJ WKH 0RGHO 9LHZ &RQWUROOHU 09& GHVLJQ SDWWHUQ

WKLV LV D QHZ FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

RI¿FLDO 6XQ H[DP REMHFWLYHV

+LJK OHYHO :HE $SS $FKLWHFWXUH

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 )RU HDFK RI WKH +773 0HWKRGV VXFK DV *(7

3267 +($' DQG VR RQ GHVFULEH WKH SXUSRVH RI WKH PHWKRG DQG WKH WHFKQLFDO FKDUDFWHULVWLFV RI WKH +773 0HWKRG SURWRFRO OLVW WULJJHUV WKDW PLJKW FDXVH D FOLHQW XVXDOO\ D :HE EURZVHU WR XVH WKH 0HWKRG DQG LGHQWLI\ WKH +WWS6HUYOHW PHWKRG WKDW FRUUHVSRQGV WR WKH +773 0HWKRG

'HVFULEH WKH SXUSRVH DQG HYHQW VHTXHQFH RI WKH

VHUYOHW OLIH F\FOH VHUYOHW FODVV ORDGLQJ VHUYOHW LQVWDQWLDWLRQ FDOO WKH LQLW PHWKRG FDOO WKH VHUYLFH PHWKRG DQG FDOO WKH GHVWUR\ PHWKRG

&RQVWUXFW WKH ¿OH DQG GLUHFWRU\ VWUXFWXUH RI D :HE $SSOLFDWLRQ WKDW PD\ FRQWDLQ D VWDWLF FRQWHQW E -63 SDJHV F VHUYOHW FODVVHV G WKH GHSOR\PHQW GHVFULSWRU H WDJ OLEUDULHV I -$5 ¿OHV DQG J -DYD FODVV ¿OHV DQG GHVFULEH KRZ WR SURWHFW UHVRXUFH ¿OHV IURP +773 DFFHVV

!LL OF THE OBJECTIVES IN THIS SECTION ARE COVERED COMPLETELY IN OTHER CHAPTERS 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 9OU WON T HAVE ANY MOCK EXAM QUESTIONS ON THESE TOPICS UNTIL YOU GET TO THE MORE SPECIlC CHAPTER WHERE THOSE TOPICS ARE COVERED %NJOY THIS NICE SIMPLE BACKGROUND MATERIAL WHILE YOU CAN "54 YOU DO NEED TO KNOW THIS STUFF BEFORE MOVING ON )F YOU ALREADY HAVE SOME SERVLET EXPERIENCE YOU CAN PROBABLY JUST SKIM THE PAGES LOOK AT THE PICTURES DO THE EXERCISES AND MOVE ON TO CHAPTER

'HVFULEH WKH SXUSRVH DQG VHPDQWLFV IRU HDFK RI WKH IROORZLQJ GHSOR\PHQW GHVFULSWRU HOHPHQWV VHUYOHW LQVWDQFH VHUYOHW QDPH VHUYOHW FODVV VHUYOHW LQLWLDOL]DWLRQ SDUDPHWHUV DQG 85/ WR QDPHG VHUYOHW PDSSLQJ

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

?PI\ Q[ I +WV\IQVMZ' 6HUYOHWV GRQ·W KDYH D PDLQ PHWKRG 7KH\·UH XQGHU WKH FRQWURO RI DQRWKHU -DYD DSSOLFDWLRQ FDOOHG D &RQWDLQHU 4OMCAT IS AN EXAMPLE OF A #ONTAINER 7HEN YOUR WEB SERVER APPLICATION LIKE !PACHE GETS A REQUEST FOR A SERVLET AS OPPOSED TO SAY A PLAIN OLD STATIC (4-, PAGE THE SERVER HANDS THE REQUEST NOT TO THE SERVLET ITSELF BUT TO THE #ONTAINER IN WHICH THE SERVLET IS DEPLOYED )T S THE #ONTAINER THAT GIVES THE SERVLET THE (440 REQUEST AND RESPONSE AND IT S THE #ONTAINER THAT CALLS THE SERVLET S METHODS LIKE DO0OST OR DO'ET

ZHE VHUYHU PDFKLQH UHTXHVW

*(7

*(7

:HE EURZVHU

ZHE VHUYHU DSS

&OLHQW

Java code

*(7

Java

ZHE code &RQWDLQHU DSS

VHUYOHW

ZHE VHUYHU PDFKLQH +773 2. KWPO! KHDG!

:HE EURZVHU

KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

UHVSRQVH

&OLHQW

KWPO! KHDG! KHDG!

ZHE VHUYHU DSS

KWPO! KHDG! KHDG!

ERG\! ERG\! KWPO!

ZHE &RQWDLQHU DSS

ERG\! ERG\! KWPO!

VHUYOHW

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

OLIH ZLWKRXW VHUYOHWV

?PI\ QN aW] PIL 2I^I J]\ VW [MZ ^TM\[ WZ +WV\IQVMZ[' 7HAT IF YOU HAD TO WRITE A *AVA PROGRAM TO HANDLE DYNAMIC REQUESTS THAT COME TO A WEB SERVER APPLICATION LIKE !PACHE BUT WITHOUT A #ONTAINER LIKE 4OMCAT )N OTHER WORDS IMAGINE THERE S NO SUCH THING AS SERVLETS AND ALL YOU HAVE ARE THE CORE * 3% LIBRARIES /F COURSE YOU CAN ASSUME YOU HAVE THE CAPABILITY OF CONl GURING THE WEB SERVER APPLICATION SO THAT IT CAN INVOKE YOUR *AVA APPLICATION )T S /+ IF YOU DON T YET KNOW MUCH ABOUT WHAT THE #ONTAINER DOES *UST IMAGINE YOU NEED SERVER SIDE SUPPORT FOR A WEB APPLICATION AND ALL YOU HAVE IS PLAIN OLD *AVA

$ WUXH ZDUULRU ZRXOG QRW XVH D &RQWDLQHU +H ZRXOG ZULWH HYHU\WKLQJ XVLQJ RQO\ - 6( DQG KLV EDUH KDQGV

/LVW VRPH RI WKH IXQFWLRQV \RX ZRXOG KDYH WR LPSOHPHQW LQ D - 6( DSSOLFDWLRQ LI QR &RQWDLQHU H[LVWHG

* Create a socket connection with the server, and create a listener for the socket.

0OSSIBLE ANSWERS CREATE A THREAD MANAGER IMPLEMENT SECURITY HOW ABOUT l LTER ING FOR THINGS LIKE LOGGING *30 SUPPORT YIKES MEMORY MANAGEMENT

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

?PI\ LWM[ \PM +WV\IQVMZ OQ^M aW]' 7E KNOW THAT IT S THE #ONTAINER THAT MANAGES AND RUNS THE SERVLET BUT WHY )S IT WORTH THE EXTRA OVERHEAD &RPPXQLFDWLRQV VXSSRUW 4HE CONTAINER PROVIDES AN EASY WAY FOR YOUR SERVLETS TO TALK TO YOUR WEB SERVER 9OU DON T HAVE TO BUILD A 3ERVER3OCKET LISTEN ON A PORT CREATE STREAMS ETC 4HE #ONTAINER KNOWS THE PROTOCOL BETWEEN THE WEB SERVER AND ITSELF SO THAT YOUR SERVLET DOESN T HAVE TO WORRY ABOUT AN !0) BETWEEN SAY THE !PACHE WEB SERVER AND YOUR OWN WEB APPLICATION CODE !LL YOU HAVE TO WORRY ABOUT IS YOUR OWN BUSINESS LOGIC THAT GOES IN YOUR 3ERVLET LIKE ACCEPTING AN ORDER FROM YOUR ONLINE STORE /LIHF\FOH 0DQDJHPHQW 4HE #ONTAINER CONTROLS THE LIFE AND DEATH OF YOUR SERVLETS )T TAKES CARE OF LOADING THE CLASSES INSTANTIATING AND INITIALIZING THE SERVLETS INVOKING THE SERVLET METHODS AND MAKING SERVLET INSTANCES ELIGIBLE FOR GARBAGE COLLECTION 7ITH THE #ONTAINER IN CONTROL YOU DON T HAVE TO WORRY AS MUCH ABOUT RESOURCE MANAGEMENT 0XOWLWKUHDGLQJ 6XSSRUW 4HE #ONTAINER AUTOMATICALLY CREATES A NEW *AVA THREAD FOR EVERY SERVLET REQUEST IT RECEIVES 7HEN THE SERVLET S DONE RUNNING THE (440 SERVICE METHOD FOR THAT CLIENT S REQUEST THE THREAD COMPLETES I E DIES 4HIS DOESN T MEAN YOU RE OFF THE HOOK FOR THREAD SAFETYˆYOU CAN STILL RUN INTO SYNCHRONIZATION ISSUES "UT HAVING THE SERVER CREATE AND MANAGE THREADS FOR MULTIPLE REQUESTS STILL SAVES YOU A LOT OF WORK

7KDQNV WR WKH &RQWDLQHU <28 JHW WR FRQFHQWUDWH PRUH RQ \RXU RZQ EXVLQHVV ORJLF LQVWHDG RI ZRUU\LQJ DERXW ZULWLQJ FRGH IRU WKUHDGLQJ VHFXULW\ DQG QHWZRUNLQJ <RX JHW WR IRFXV DOO \RXU HQHUJ\ RQ PDNLQJ D IDEXORXV RQOLQH EXEEOH ZUDS VWRUH DQG OHDYH WKH XQGHUO\LQJ VHUYLFHV OLNH VHFXULW\ DQG -63 SURFHVVLQJ XS WR WKH FRQWDLQHU 1RZ DOO , KDYH WR ZRUU\ DERXW LV KRZ WR VHOO P\ VFUDWFK Q VQLII EXEEOH ZUDS LQVWHDG RI KDYLQJ WR ZULWH DOO WKDW FRGH IRU WKH WKLQJV WKH &RQWDLQHU·V JRQQD GR IRU PH

'HFODUDWLYH 6HFXULW\ 7ITH A #ONTAINER YOU GET TO USE AN 8-, DEPLOYMENT DESCRIPTOR TO CONlGURE AND MODIFY SECURITY WITHOUT HAVING TO HARD CODE IT INTO YOUR SERVLET OR ANY OTHER CLASS CODE 4HINK ABOUT THAT 9OU CAN MANAGE AND CHANGE YOUR SECURITY WITHOUT TOUCHING AND RECOMPILING YOUR *AVA SOURCE lLES -63 6XSSRUW 9OU ALREADY KNOW HOW COOL *30S ARE 7ELL WHO DO YOU THINK TAKES CARE OF TRANSLATING THAT *30 CODE INTO REAL *AVA /F COURSE 4HE #ONTAINER \RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

WKH &RQWDLQHU

0W_ \PM +WV\IQVMZ PIVLTM[ I ZMY]M[\ 7E LL SAVE SOME OF THE JUICIER BITS FOR LATER IN THE BOOK BUT HERE S A QUICK LOOK

+773 UHTXHVW *(7

:HE EURZVHU

FRQWDLQHU

VHUYOHW

8VHU FOLFNV D OLQN WKDW KDV D 85/ WR D VHUYOHW LQVWHDG RI D VWDWLF SDJH

VHUYOHW

7KH FRQWDLQHU ´VHHVµ WKDW WKH UHTXHVW LV IRU D VHUYOHW VR WKH FRQWDLQHU FUHDWHV WZR REMHFWV

&OLHQW

:HE EURZVHU

&OLHQW

FRQWDLQHU

+WWS6HUYOHW5HVSRQVH UHTXHVW

+WWS6HUYOHW5HTXHVW

UHVSRQVH

:HE EURZVHU

&OLHQW

UHTXHVW UHVSRQVH

VHUYOHW

FRQWDLQHU

thread

7KH FRQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ LQ WKH UHTXHVW FUHDWHV RU DOORFDWHV D WKUHDG IRU WKDW UHTXHVW DQG SDVVHV WKH UHTXHVW DQG UHVSRQVH REMHFWV WR WKH VHUYOHW WKUHDG

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

:HE EURZVHU

VHUYOHW

FRQWDLQHU &OLHQW UHTXHVW UHVSRQVH

:HE EURZVHU

VHUYOHW

FRQWDLQHU &OLHQW

UHVSRQVH KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

VHUYLFH

:HE EURZVHU

&OLHQW

FRQWDLQHU

+773 UHVSRQVH UHTXHVW

)RU WKLV H[DPSOH ZH·OO DVVXPH WKH UHTXHVW ZDV DQ +773 *(7

7KH GR*HW PHWKRG JHQHUDWHV WKH G\QDPLF SDJH DQG VWXIIV WKH SDJH LQWR WKH UHVSRQVH REMHFW 5HPHPEHU WKH FRQWDLQHU VWLOO KDV D UHIHUHQFH WR WKH UHVSRQVH REMHFW

VHUYLFH

GR*HW

+773 KHDGHU LQIR KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

7KH FRQWDLQHU FDOOV WKH VHUYOHW·V VHUYLFH PHWKRG 'HSHQGLQJ RQ WKH W\SH RI UHTXHVW WKH VHUYLFH PHWKRG FDOOV HLWKHU WKH GR*HW RU GR3RVW PHWKRG

VHUYOHW

no thread

7KH WKUHDG FRPSOHWHV WKH FRQWDLQHU FRQYHUWV WKH UHVSRQVH REMHFW LQWR DQ +773 UHVSRQVH VHQGV LW EDFN WR WKH FOLHQW WKHQ GHOHWHV WKH UHTXHVW DQG UHVSRQVH REMHFWV

UHVSRQVH

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

VHUYOHW FRGH

0W_ Q\ TWWS[ QV KWLM _PI\ UISM[ I [MZ ^TM\ I [MZ ^TM\ In the real world, 99.9% of all t() servlets override either the doGe or doPost() method. LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR

all servlets 99.9999 % ofrvlets. Se are Http

SXEOLF FODVV &K 6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ ^ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU MDYD XWLO 'DWH WRGD\ QHZ MDYD XWLO 'DWH RXW SULQWOQ ³ KWPO! ³ ³ ERG\!´ ³ K VW\OH ´WH[W DOLJQ FHQWHU!´ ³+)?œV &KDSWHU 6HUYOHW K !´ ³ EU!´ WRGD\ ³ ERG\!´ ³ KWPO!´ ` `

Notice... no main( ) The servletÕs lif method. ecyc methods (like do le Ge called by the Co t()) are ntainer.

This is where your servlet gets references to the request and response objects which the container creates.

You can get a PrintWriter from the response object your servlet gets from the Container. Use the PrintWriter to write HTML text to the response object. (You can get other output options, besides PrintWriter, for writing, say, a picture instead of HTML text.)

WKHUH DUH QR

'XPE 4XHVWLRQV

4

) REMEMBER SEEING DO'ET AND DO0OST BUT ON THE PREVIOUS PAGE YOU SHOW A SERVICE METHOD 7HERE DID THE SERVICE METHOD COME FROM

$

9OUR SERVLET INHERITED IT FROM (TTP3ERVLET WHICH INHERITED IT FROM 'ENERIC3ERVLET WHICH INHERITED IT FROM AHHH WE LL DO CLASS HIERARCHIES TO DEATH IN THE "EING A 3ERVLET CHAPTER SO YOU JUST NEED A LITTLE MORE PATIENCE

4

9OU WIMPED OUT ON EXPLAINING HOW THE CONTAINER FOUND THE CORRECT SERVLET LIKE HOW DOES A 52, RELATE TO A SERVLET $OES THE USER HAVE TO TYPE IN THE EXACT PATH AND CLASS l LE NAME OF THE SERVLET

$

.O 'OOD QUESTION THOUGH "UT IT POINTS TO A 2EALLY "IG 4OPIC SERVLET MAPPING AND 52, PAT TERNS SO WE LL TAKE ONLY A QUICK LOOK ON THE NEXT FEW PAGES BUT GO INTO MUCH MORE DETAIL LATER IN THE BOOK IN THE $EPLOYMENT CHAPTER

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

AW]¼ZM _WVLMZQVO PW_ \PM +WV\IQVMZ NW]VL \PM ;MZ ^TM\ 3OMEHOW THE 52, THAT COMES IN AS PART OF THE REQUEST FROM THE CLIENT IS MAPPED TO A SPECIlC SERVLET ON THE SERVER 4HIS MAPPING OF 52,S TO SERVLETS MIGHT BE HANDLED IN A NUMBER OF DIFFERENT WAYS AND IT S ONE OF THE MOST FUNDAMENTAL ISSUES YOU LL FACE AS A WEB APP DEVELOPER 4HE USER REQUEST MUST MAP TO A PARTICULAR SERVLET AND IT S UP TO YOU TO UNDERSTAND AND USUALLY CONlGURE THAT MAPPING 7HAT DO YOU THINK

# 4X3 X9 8 +RZ VKRXOG WKH &RQWDLQHU PDS VHUYOHWV WR 85/V" 7KH XVHU GRHV VRPHWKLQJ LQ WKH EURZVHU FOLFNV D OLQN KLWV WKH ´6XEPLWµ EXWWRQ HQWHUV D 85/ HWF DQG WKDW VRPHWKLQJ LV VXSSRVHG WR VHQG WKH UHTXHVW WR D VSHFLILF VHUYOHW RU RWKHU ZHE DSS UHVRXUFH OLNH D -63 \RX EXLOW +RZ PLJKW WKDW KDSSHQ" )RU HDFK RI WKH IROORZLQJ DSSURDFKHV WKLQN DERXW WKH SURV DQG FRQV +DUGFRGH WKH PDSSLQJ LQWR \RXU +70/ SDJH ,Q RWKHU ZRUGV WKH FOLHQW LV XVLQJ WKH H[DFW SDWK DQG ¿OH FODVV QDPH RI WKH VHUYOHW

3526

&216

8VH \RXU &RQWDLQHU YHQGRU¶V WRRO WR GR WKH PDSSLQJ

3526

&216

8VH VRPH VRUW RI SURSHUWLHV WDEOH WR VWRUH WKH PDSSLQJV

3526

&216 \RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

PDSSLQJ 85/6 WR VHUYOHWV

) [MZ ^TM\ KIV PI^M <0:-- VIUM[ ! SERVLET HAS A l LE PATH NAME OBVIOUSLY LIKE CLASSES REGISTRATION 3IGN5P3ERVLET CLASS A PATH TO AN ACTUAL CLASS l LE 4HE ORIGINAL DEVELOPER OF THE SERVLET CLASS CHOSE THE CLASS NAME AND THE PACKAGE NAME THAT DEl NES PART OF THE DIRECTORY STRUCTURE AND THE LOCATION ON THE SERVER DEl NES THE FULL PATH NAME "UT ANYONE WHO DEPLOYS THE SERVLET CAN ALSO GIVE IT A SPECIAL DEPLOYMENT NAME ! DEPLOYMENT NAME IS SIMPLY A SECRET INTERNAL NAME THAT DOESN T HAVE TO BE THE SAME AS THE CLASS OR l LE NAME )T CAN BE THE SAME AS THE SERVLET CLASS NAME REGISTRATION 3IGN5P3ERVLET OR THE RELATIVE PATH TO THE CLASS l LE CLASSES REGISTRATION 3IGN5P3ERVLET CLASS BUT IT CAN ALSO BE SOMETHING COMPLETELY DIFFERENT LIKE %NROLL3ERVLET &INALLY THE SERVLET HAS A PUBLIC 52, NAMEˆTHE NAME THE CLIENT KNOWS ABOUT )N OTHER WORDS THE NAME CODED INTO THE (4-, SO THAT WHEN THE USER CLICKS A LINK THAT S SUPPOSED TO GO TO THAT SERVLET THIS PUBLIC 52, NAME IS SENT TO THE SERVER IN THE (440 REQUEST

,·OO FOLFN WKH OLQN WR WKH ´UHJLVWHU UHJLVWHU0Hµ VHUYOHW

&OLHQW NQRZQ 85/ QDPH 7KH FOLHQW VHHV D 85/ IRU WKH VHUYOHW LQ WKH +70/ EXW GRHVQ·W UHDOO\ NQRZ KRZ WKDW VHUYOHW QDPH PDSV WR UHDO GLUHFWRULHV DQG À OHV EDFN RQ WKH VHUYHU 7KH SXEOLF 85/ QDPH LV D IDNH QDPH PDGH XS IRU FOLHQWV

FODVVHV

,·P JRQQD FDOO WKLV VHUYOHW WKH ´(QUROO6HUYOHWµ

UHJLVWUDWLRQ

6LJQ8S6HUYOHW FODVV

'HSOR\HU NQRZQ VHFUHW LQWHUQDO QDPH 7KH GHSOR\HU FDQ FUHDWH D QDPH WKDW·V NQRZQ RQO\ WR WKH GHSOR\HU DQG RWKHUV LQ WKH UHDO RSHUDWLRQDO HQYLURQPHQW 7KLV QDPH WRR LV D IDNH QDPH PDGH XS MXVW IRU WKH GHSOR\PHQW RI WKH VHUYOHW ,W GRHVQ·W KDYH WR PDWFK WKH SXEOLF 85/ XVHG E\ WKH FOLHQW 25 WKH UHDO À OH DQG SDWK QDPH RI WKH VHUYOHW FODVV

$FWXDO À OH QDPH 7KH GHYHORSHU·V VHUYOHW FODVV KDV D IXOO\ TXDOLÀ HG QDPH WKDW LQFOXGHV ERWK WKH FODVV QDPH DQG WKH SDFNDJH QDPH 7KH VHUYOHW FODVV À OH KDV D UHDO SDWK DQG À OH QDPH GHSHQGLQJ RQ ZKHUH WKH SDFNDJH GLUHFWRU\ VWUXFWXUH OLYHV RQ WKH VHUYHU

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

:HOO LVQ¡W WKDW VSHFLDO KRZ HYHU\RQH JHWV WR H[SUHVV WKHLU FUHDWLYLW\ DQG FRPH XS ZLWK WKHLU YHU\ RZQ QDPH IRU WKH VDPH GDUQ WKLQJ %XW ZKDW¡V WKH SRLQW " 5HDOO\" :K\ GRQ¡W ZH DOO MXVW XVH WKH RQH UHDO QRQ FRQIXVLQJ Ă€OH QDPH"

0DSSLQJ VHUYOHW QDPHV LPSURYHV \RXU DSS¡V à H[LELOLW\ DQG VHFXULW\ 4HINK ABOUT IT 3O YOU VE HARD CODED THE REAL PATH AND lLE NAME INTO ALL THE *30S AND OTHER (4-, PAGES THAT USE THAT SERVLET 'REAT .OW WHAT HAPPENS WHEN YOU NEED TO REORGANIZE YOUR APPLICATION AND POSSIBLY MOVE THINGS INTO DIFFERENT DIRECTORY STRUCTURES $O YOU REALLY WANT TO FORCE EVERYONE WHO USES THAT SERVLET TO KNOW AND FOREVER FOLLOW THAT SAME DIRECTORY STRUCTURE "Y MAPPING THE NAME INSTEAD OF CODING IN THE REAL lLE AND PATH NAME YOU HAVE THE mEXIBILITY TO MOVE THINGS AROUND WITHOUT HAVING THE MAINTENANCE NIGHTMARE OF TRACKING DOWN AND CHANGING CLIENT CODE THAT REFERS TO THE OLD LOCATION OF THE SERVLET lLES !ND WHAT ABOUT SECURITY $O YOU REALLY WANT THE CLIENT TO KNOW EXACTLY HOW THINGS ARE STRUCTURED ON YOUR SERVER $O YOU WANT THEM TO SAY ATTEMPT TO NAVIGATE DIRECTLY TO THE SERVLET WITHOUT GOING THROUGH THE RIGHT PAGES OR FORMS "ECAUSE IF THE END USER CAN SEE THE REAL PATH SHE CAN TYPE IT INTO HER BROWSER AND TRY TO ACCESS IT DIRECTLY

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

VHUYOHW PDSSLQJ LQ WKH ''

=[QVO \PM ,MXTWaUMV\ ,M[KZQX\WZ \W UIX =:4[ \W [MZ ^TM\[ 7HEN YOU DEPLOY YOUR SERVLET INTO YOUR WEB #ONTAINER YOU LL CREATE A FAIRLY SIMPLE 8-, DOCUMENT CALLED THE $EPLOYMENT $ESCRIPTOR $$ TO TELL THE #ONTAINER HOW TO RUN YOUR SERVLETS AND *30S !LTHOUGH YOU LL USE THE $$ FOR MORE THAN JUST MAPPING NAMES YOU LL USE TWO 8-, ELEMENTS TO MAP 52,S TO SERVLETSˆONE TO MAP THE CLIENT KNOWN PUBLIC 52, NAME TO YOUR OWN INTERNAL NAME AND THE OTHER TO MAP YOUR OWN INTERNAL NAME TO A FULLY QUALIlED CLASS NAME

7KH WZR '' HOHPHQWV IRU 85/ PDSSLQJ VHUYOHW! PDSV LQWHUQDO QDPH WR IXOO\ TXDOLÂżHG FODVV QDPH VHUYOHW PDSSLQJ! PDSV LQWHUQDO QDPH WR SXEOLF 85/ QDPH

This web app has two servlets.

goes OT more that There is a Lening <web-app> tag, into this op t want to show it but we donÕ(thereÕs an example right now of this chapter). at the end

ZHE DSS !

The <servlet> element tells the Container which class Ăžles belong to a particular web application.

Think of the <servlet-mapping> element as what the Container uses at runtime when a request comes in, to ask, Ă’which servlet should I invoke for this requested URL?Ă“.

is used to The <servlet-name> elemtoenta speciÞc tie a <servlet> elementent. The end<servlet-mapping> elemname; itÕs used user NEVER sees this the DD. only in other parts of

VHUYOHW! VHUYOHW QDPH!,QWHUQDO QDPH VHUYOHW QDPH! VHUYOHW FODVV!IRR 6HUYOHW VHUYOHW FODVV! VHUYOHW!

e fullyYou put in thme of the na ed qualiÞ you donÕt add class (but extension). the Ò.classÓ

VHUYOHW! VHUYOHW QDPH!,QWHUQDO QDPH VHUYOHW QDPH! VHUYOHW FODVV!IRR 6HUYOHW VHUYOHW FODVV! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!,QWHUQDO QDPH VHUYOHW QDPH! XUO SDWWHUQ! 3XEOLF XUO SDWWHUQ! VHUYOHW PDSSLQJ! This is the what the client

sees (and uses) a made-up to get to the servlet... but itÕs actual name that is NOT the name of the servlet class.

VHUYOHW PDSSLQJ! VHUYOHW QDPH!,QWHUQDO QDPH VHUYOHW QDPH! XUO SDWWHUQ! 3XEOLF XUO SDWWHUQ! VHUYOHW PDSSLQJ! ItÕs possible to use wildcards in ZHE DSS! the <url-patte

rn> element. on that and paths later. .. more

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

*]\ _IQ\ <PMZM¼[ UWZM aW] KIV LW _Q\P \PM ,, "ESIDES MAPPING 52,S TO ACTUAL SERVLETS YOU CAN USE THE $$ TO CUSTOMIZE OTHER ASPECTS OF YOUR WEB APPLICATION INCLUDING SECURITY ROLES ERROR PAGES TAG LIBRARIES INITIAL CONlGURATION INFORMATION AND IF IT S A FULL * %% SERVER YOU CAN EVEN DECLARE THAT YOU LL BE ACCESSING SPECIlC ENTERPRISE JAVABEANS $ON T WORRY ABOUT THE DETAILS YET 4HE CRUCIAL POINT FOR NOW IS THAT THE $$ GIVES YOU A WAY TO DECLARATIVELY MODIFY YOUR APPLICATION WITHOUT CHANGING SOURCE CODE 4HINK ABOUT THIS IT MEANS THAT EVEN THOSE WHO AREN T *AVA PROGRAMMERS CAN CUSTOMIZE YOUR *AVA WEB APPLICATION WITHOUT HAVING TO DRAG YOU BACK FROM YOUR TROPICAL VACATION

7KH GHSOR\PHQW GHVFULSWRU '' SURYLGHV D ´GHFODUDWLYHµ PHFKDQLVP IRU FXVWRPL]LQJ \RXU ZHE DSSOLFDWLRQV ZLWKRXW WRXFKLQJ VRXUFH FRGH

<< :]f]Úlk

0LQLPL]HV WRXFKLQJ VRXUFH FRGH WKDW WKHUH DUH QR

'XPE 4XHVWLRQV

4

) M CONFUSED ,OOKING AT THE $$ YOU STILL DON T HAVE ANYTHING THAT INDICATES THE ACTUAL PATH NAME OF THE SERVLET )T JUST SAYS THE CLASS NAME 4HIS STILL DOESN T ANSWER THE QUESTION OF HOW THE #ONTAINER USES THAT CLASS NAME TO lND A SPECIlC SERVLET CLASS lLE )S THERE YET !./4(%2 MAPPING SOMEWHERE THAT SAYS THAT SUCH AND SUCH A CLASS NAME MAPS TO SUCH AND SUCH A lLE IN SUCH AND SUCH A LOCATION

$

9OU NOTICED 9OU RE RIGHT THAT WE PUT ONLY THE CLASS NAME FULLY QUALIFIED TO INCLUDE THE PACKAGE NAME INTO THE SERVLET CLASS ELEMENT 4HAT S BECAUSE THE #ONTAINER HAS A SPECIFIC PLACE IT WILL LOOK FOR ALL SERVLETS FOR WHICH YOU VE SPECIFIED A MAPPING IN THE $$

KDV DOUHDG\ EHHQ WHVWHG

/HWV \RX ILQH WXQH \RXU DSS·V FDSDELOLWLHV HYHQ LI \RX GRQ·W KDYH WKH VRXUFH FRGH /HWV \RX DGDSW \RXU DSSOLFDWLRQ WR GLIIHUHQW UHVRXUFHV OLNH GDWDEDVHV ZLWKRXW KDYLQJ WR UHFRPSLOH DQG WHVW DQ\ FRGH 0DNHV LW HDVLHU IRU \RX WR PDLQWDLQ G\QDPLF VHFXULW\ LQIR OLNH DFFHVV FRQWURO OLVWV DQG VHFXULW\ UROHV /HWV QRQ SURJUDPPHUV PRGLI\ DQG GHSOR\ \RXU ZHE DSSOLFDWLRQV ZKLOH \RX FDQ IRFXV RQ WKH PRUH LQWHUHVWLQJ WKLQJV /LNH KRZ DSSURSULDWH \RXU ZDUGUREH LVQ·W IRU D WULS WR +DZDLL

)N FACT THE #ONTAINER USES A SOPHISTICATED SET OF RULES FOR FINDING A MATCH BETWEEN THE 52, THAT COMES IN FROM THE CLIENT REQUEST AND AN ACTUAL *AVA CLASS SITTING SOMEWHERE ON THE SERVER "UT WE LL GET INTO THAT IN A LATER CHAPTER ON $EPLOYMENT 2IGHT NOW THE KEY POINT TO REMEMBER IS THAT YOU CAN DO THIS MAPPING

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

%REÂśV PDWFKPDNLQJ VLWH

;\WZa" *WJ *]QTL[ I 5I\KPUISQVO ;Q\M $ATING IS TOUGH TODAY 7HO HAS THE TIME WHEN THERE S ALWAYS ANOTHER DISK TO DEFRAG "OB WHO WANTS A PIECE OF THE DOT COM ACTION WHAT S LEFT OF IT ANYWAY BELIEVES THAT CREATING A GEEK SPECIl C DATING SITE IS HIS TICKET OUT OF THE $ILBERTIAN JOB HE HAS NOW 4HE PROBLEM IS "OB S BEEN A SOFTWARE MANAGER FOR SO LONG THAT HE S UM A LITTLE OUT OF TOUCH WITH CONTEMPORARY SOFTWARE ENGINEERING PRACTICES "UT HE KNOWS SOME BUZZWORDS AND SOME *AVA AND HE S READ A LITTLE ABOUT SERVLETS SO HE MAKES A QUICK DESIGN AND STARTS TO CODE

, ZDQW DQ $JLOH 'DWLQJ VLWH ZKHUH JHHNV FDQ PHHW DQG KRRN XS %HFDXVH QRW HYHU\ERG\ JHWV OXFN\ DW D /LQX[ ,QVWDOODWKRQ

,QSXW \RXU VWDWH @Yf\d]

*HHN'DWHV

9_] GK

´ RI RXU WUDQVDFWLRQV HQG LQ FRPPLW ¾

9lljaZml]k =p[]hlagfk

Bgaf

Lqh] \][dYjYlagf

Afk]jl

<ID im]jq J]^Y[lgj eq HjgĂš d]

4XHU\ ;gehgk] qgmj <Ylaf_ Im]jq DYf_mY_] <ID! kljaf_ `]j]2

'4/ 4XHU\

5HIDFWRU Eg\a^q qgmj hjgĂš d]2

>SURÂż OH KHUH@

Aehjgn] al

<g al

Egj]

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

0M [\IZ\[ \W J]QTL I J]VKP WN [MZ ^TM\[ WVM NWZ MIKP XIOM (E CONSIDERED HAVING JUST A SINGLE SERVLET WITH A BUNCH OF IF TESTS BUT DECIDED THAT SEPARATE SERVLETS WOULD BE MORE //ˆEACH SERVLET SHOULD HAVE ONE RESPONSIBILITY LIKE THE QUERY PAGE THE SIGN UP PAGE THE SEARCH RESULTS PAGE ETC %ACH SERVLET WILL HAVE ALL THE BUSINESS LOGIC IT NEEDS TO MODIFY OR READ THE DATABASE AND PRINTS THE (4-, TO THE RESPONSE STREAM BACK TO THE CLIENT

7KLV LV D JUHDW 22 GHVLJQ $OO P\ VHUYOHWV KDYH H[DFWO\ RQH MRE

LPSRUW VWDWHPHQWV SXEOLF FODVV 'DWLQJ6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ ^ EXVLQHVV ORJLF JRHV KHUH GHSHQGLQJ RQ ZKDW WKLV VHUYOHW LV VXSSRVHG WR GR ZULWH WR WKH GDWDEDVH GR WKH TXHU\ HWF

3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU FRPSRVH WKH G\QDPLF +70/ SDJH RXW SULQWOQ ³VRPHWKLQJ UHDOO\ XJO\ JRHV KHUH´ ` `

ZHE VHUYHU PDFKLQH

,QSXW'4/6HUYOHW 'R'4/4XHU\6HUYOHW

6LJQXS6HUYOHW

'%

ZHE VHUYHU FRQWDLQHU

0DLQ3DJH6HUYOHW

,QSXW3URILOH&KDQJHV6HUYOHW

$FFHSW6LJQXS6HUYOHW

atever The servlet does whprocess to do to ds it nee ert or the request (like insse) and search the databa page in returns the HTML the HTTP response. ic All of the business log ML AND the client HTide the page response is ins e. cod t vle ser

$FFHSW3URILOH&KDQJHV6HUYOHW

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

%RE DGGV -63V

*]\ \PMV Q\ OM\[ ]OTa [W PM ILL[ 2;8[ 4HOSE PESKY PRINTLN STATEMENTS FOR THE OUTPUT RESPONSE GET REALLY UGLY REALLY QUICKLY 3O HE READS UP ON *30S AND DECIDES TO HAVE EACH SERVLET DO WHATEVER BUSINESS LOGIC IT NEEDS TO DO QUERY THE DATABASE INSERT OR UPDATE A NEW RECORD ETC THEN FORWARD THE REQUEST TO A *30 TO DO THE (4-, FOR THE RESPONSE 4HIS ALSO SEPARATES THE BUSINESS LOGIC FROM THE PRESENTATION AND SINCE HE S BEEN READING UP ON DESIGN HE KNOWS THAT SEPARATION OF CONCERNS IS A 'OOD 4HING

7KLV -63 GHVLJQ LV PXFK FRROHU 1RZ WKH VHUYOHW FRGH LV FOHDQHU HDFK VHUYOHW UXQV LWV RZQ EXVLQHVV ORJLF DQG WKHQ LQYRNHV D VSHFLĂ€ F -63 WR KDQGOH WKH +70/ IRU WKH UHVSRQVH VHSDUDWLQJ EXVLQHVV ORJLF IURP SUHVHQWDWLRQ

LPSRUW VWDWHPHQWV SXEOLF FODVV 'DWLQJ6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ ^ EXVLQHVV ORJLF JRHV KHUH GHSHQGLQJ RQ ZKDW WKLV VHUYOHW LV VXSSRVHG WR GR ZULWH WR WKH GDWDEDVH GR WKH TXHU\ HWF

IRUZDUG WKH UHTXHVW WR D VSHFLÂż F -63 SDJH LQVWHDG RI WU\LQJ WR SULQW WKH +70/ WR WKH RXWSXW VWUHDP ` `

,QSXW6LJQXS6HUYOHW

$FFHSW6LJQXS6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW3URILOH&KDQJHV6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW6LJQXS-63

$FFHSW3URILOH&KDQJHV6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

$FFHSW6LJQXS-63

0DLQ3DJH6HUYOHW

,QSXW'4/6HUYOHW

'R'4O4XHU\6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW3URILOH&KDQJHV-63

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

$FFHSW3URILOH&KDQJHV-63

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

0DLQ3DJH-63

,QSXW'4/-63

:HE EURZVHU

3267 '

R'4/4XH

+70/ UHVS RQVH

&OLHQW

U\

ZHE VHUYHU FRQWDLQHU

X

/4

'4

'R

HU\

'R'4/4XHU\6HUYOHW

6HOHFW

'%

OWV UHVX '4/ UHVXOWV

+70/ UHVSRQVH

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

'R'4/4XHU\-63

'R'4/4XHU\-63

Client Þlls out the DQL query form and clicks the ÒDo itÓ button. This sends an HTTP POST request for the DoDQLQuery. The web server invokes the servlet, the servlet runs the query on the database, then the request is forwarded to the appropriate JSP. The JSP builds the response HTML and sends it back.

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

*]\ \PMV PQ[ NZQMVL [Ia[ ยนAW] ):- ][QVO 5>+ ZQOP\ 'ยบ +IM WANTS TO KNOW IF THE DATING SERVICE CAN BE ACCESSED FROM A 3WING '5) APPLICATION "OB SAYS h.O ) HADN T THOUGHT OF THAT v 3O +IM SAYS h7ELL IT S NOT A PROBLEM BECAUSE ) M SURE YOU USED -6# SO WE CAN JUST WHIP UP A 3WING '5) CLIENT THAT CAN ACCESS THE BUSINESS LOGIC CLASSES v

:KDW LI \RX ZDQW WR PDNH D 6ZLQJ *8, DSS IRU WKH GDWLQJ VHUYLFH DQG LW XVHV WKH VDPH EXVLQHVV ORJLF"

!ND "OB SAYS h'ULP v !ND +IM SAYS h$ON T TELL ME YOU DID NOT USE -6# v !ND "OB SAYS h7ELL ) DID SEPARATE OUT THE PRESENTATION FROM THE BUSINESS LOGIC v +IM SAYS h4HAT S A START BUT LET ME GUESS YOUR BUSINESS LOGIC IS ALL INSIDE SERVLETS v "OB REALIZES SUDDENLY WHY HE WENT INTO MANAGEMENT "UT HE S DETERMINED TO DO THIS RIGHT SO HE ASKS +IM TO GIVE HIM A QUICK CRASH OVERVIEW OF -6#

:LWK 09& WKH EXVLQHVV ORJLF LV QRW RQO\ VHSDUDWH IURP WKH SUHVHQWDWLRQ LW GRHVQยทW HYHQ NQRZ WKDW WKHUH ,6 D SUHVHQWDWLRQ 4HE ESSENCE OF -6# IS THAT YOU SEPARATE THE BUSINESS LOGIC FROM THE PRESENTATION BUT PUT SOMETHING BETWEEN THEM SO THAT THE BUSINESS LOGIC CAN STAND ON ITS OWN AS A REUSABLE *AVA CLASS AND DOESN T HAVE TO KNOW ANYTHING ABOUT THE VIEW "OB WAS PARTLY THERE BY SEPARATING OUT THE BUSINESS LOGIC FROM THE PRESENTATION BUT HIS BUSINESS LOGIC STILL HAS AN INTIMATE CONNECTION TO THE VIEW )N OTHER WORDS HE MIXED THE BUSINESS LOGIC INTO A SERVLET AND THAT MEANS HE CAN T REUSE HIS BUSINESS LOGIC FOR SOME OTHER KIND OF VIEW LIKE A 3WING '5) OR EVEN A WIRELESS APP (IS BUSINESS LOGIC IS STUCK IN A SERVLET WHEN IT SHOULD BE IN A STANDALONE *AVA CLASS HE CAN REUSE

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

09& GHVLJQ SDWWHUQ

<PM 5WLMT >QM_ +WV\ZWTTMZ 5>+ ,M[QOV 8I\\MZV NQ`M[ \PQ[ )F "OB HAD UNDERSTOOD THE -6# DESIGN PATTERN HE WOULD HAVE KNOWN THAT THE BUSINESS LOGIC SHOULDN T BE STUFFED INSIDE A SERVLET (E WOULD HAVE REALIZED THAT WITH THE BUSINESS LOGIC EMBEDDED IN A SERVLET HE D BE SCREWED IF HE ONE DAY NEEDED A DIFFERENT WAY TO ACCESS THE DATING SERVICE ,IKE FROM A 3WING '5) APP 7E LL TALK A LOT MORE ABOUT -6# AND OTHER PATTERNS LATER IN THE BOOK BUT YOU NEED A QUICK UNDERSTANDING NOW BECAUSE THE TUTORIAL APP WE BUILD AT THE END OF THIS CHAPTER USES -6#

0RGHO 9LHZ &RQWUROOHU 09& WDNHV WKH EXVLQHVV ORJLF RXW RI WKH VHUYOHW DQG SXWV LW LQ D ´0RGHOµ³ D UHXVDEOH SODLQ ROG -DYD FODVV 7KH 0RGHO LV D FRPELQDWLRQ RI WKH EXVLQHVV GDWD OLNH WKH VWDWH RI D 6KRSSLQJ &DUW DQG WKH PHWKRGV UXOHV WKDW RSHUDWH RQ WKDW GDWD

)F YOU RE ALREADY FAMILIAR WITH IT THEN YOU KNOW THAT -6# IS NOT SPECIlC TO SERVLETS AND *30SˆTHE CLEAN SEPARATION OF BUSINESS LOGIC AND PRESENTATION IS JUST AS VALID IN ANY OTHER KIND OF APPLICATION "UT WITH WEB APPS IT S REALLY IMPORTANT BECAUSE YOU SHOULD NEVER ASSUME THAT YOUR BUSINESS LOGIC WILL BE ACCESSED ONLY FROM THE WEB 7E RE SURE YOU VE WORKED IN THIS BUSINESS LONG ENOUGH TO KNOW THE ONLY GUARANTEE IN SOFTWARE DEVELOPMENT THE SPEC ALWAYS CHANGES

09& LQ WKH 6HUYOHW -63 ZRUOG

&21752//(5 7DNHV XVHU LQSXW IURP WKH UHTXHVW DQG ÀJXUHV RXW ZKDW LW PHDQV WR WKH PRGHO

Servlet

7HOOV WKH PRGHO WR XSGDWH LWVHOI DQG PDNHV WKH QHZ PRGHO VWDWH DYDLODEOH IRU WKH YLHZ WKH -63

&RQWUROOHU

02'(/

9,(: 5HVSRQVLEOH IRU WKH SUHVHQWDWLRQ ,W JHWV WKH JSP VWDWH RI WKH PRGHO IURP WKH &RQWUROOHU DOWKRXJK QRW GLUHFWO\ WKH &RQWUROOHU SXWV WKH PRGHO GDWD LQ D SODFH ZKHUH WKH 9LHZ FDQ ÀQG LW ,W·V DOVR WKH SDUW WKDW JHWV WKH XVHU LQSXW WKDW JRHV EDFN WR WKH &RQWUROOHU

+ROGV WKH UHDO EXVLQHVV ORJLF DQG WKH VWDWH ,Q RWKHU ZRUGV LW NQRZV WKH UXOHV IRU JHWWLQJ DQG XSGDWLQJ WKH VWDWH

JSP Plain old Java

FODVV )RR ^ YRLG EDU ^ GR%DU ` `

$ 6KRSSLQJ &DUW·V FRQWHQWV DQG WKH UXOHV IRU ZKDW WR GR ZLWK LW ZRXOG EH SDUW RI WKH 0RGHO LQ 09&

9LHZ

0RGHO '%

,W·V WKH RQO\ SDUW RI WKH V\VWHP WKDW WDONV WR WKH GDWDEDVH DOWKRXJK LW SUREDEO\ XVHV DQRWKHU REMHFW IRU WKH DFWXDO '% FRPPXQLFDWLRQ EXW ZH·OO VDYH WKDW SDWWHUQ IRU ODWHU

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

)XXTaQVO \PM 5>+ XI\\MZV \W \PM UI\KPUISQVO _MJ IXX 3O "OB KNOWS WHAT HE HAS TO DO 3EPARATE OUT THE BUSINESS LOGIC FROM THE SERVLETS AND CREATE A REGULAR *AVA CLASS FOR EACH ONE TO REPRESENT THE -ODEL

w in the app, he nova For each pageCo a Ja r, lle ro nt has a servlet d a JSP View. class Model, an

4HEN THE ORIGINAL SERVLET WILL BE THE #ONTROLLER THE NEW BUSINESS LOGIC CLASS WILL BE THE -ODEL AND THE *30 WILL BE THE 6IEW

,QSXW6LJQXS0RGHO

,QSXW6LJQXS6HUYOHW

$FFHSW6LJQXS0RGHO

$FFHSW6LJQXS6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW6LJQXS-63

,QSXW3URILOH&KDQJHV0RGHO

,QSXW3URILOH&KDQJHV6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

$FFHSW6LJQXS-63

$FFHSW3URILOH&KDQJHV0RGHO

$FFHSW3URILOH&KDQJHV6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

0DLQ3DJH0RGHO

,QSXW'4/0RGHO

'R'4O4XHU\0RGHO

0DLQ3DJH6HUYOHW

,QSXW'4/6HUYOHW

'R'4O4XHU\6HUYOHW

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW3URILOH&KDQJHV-63

$FFHSW3URILOH&KDQJHV-63

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

0DLQ3DJH-63

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

KWPO! ERG\! 5HIDFWRU QHZ )RR ! PRUH KHUH ! PRUH KHUH ERG\! KWPO!

,QSXW'4/-63

'R'4/4XHU\-63

:KDW GR \RX WKLQN" $P , JRRG RU DP , JRRG" $ SHUIHFW 09& GHVLJQ

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

\HDK EXW LV WKLV D JRRG GHVLJQ"

*]\ \PMV PQ[ NZQMVL 3QU \ISM[ I TWWS +IM COMES IN AND SAYS THAT WHILE IT )3 AN -6# DESIGN IT S A DUMB ONE 3URE THE BUSINESS LOGIC HAS BEEN PULLED OUT INTO A -ODEL AND THE SERVLETS ACT AS THE #ONTROLLERS WORKING BETWEEN THE -ODELS AND 6IEWS SO THAT THE -ODELS CAN BE BRAIN DEAD ABOUT THE 6IEWS 4HAT S ALL GOOD "UT LOOK AT ALL THOSE LITTLE SERVLETS 7HAT DO THEY EVEN DO .OW THAT THE BUSINESS LOGIC IS SAFELY TUCKED AWAY IN THE -ODEL THE SERVLET #ONTROLLER ISN T DOING MUCH EXCEPT SOME GENERIC APPLICATION STUFF FOR THIS APP AND OH YEAH IT DOES UPDATE THE -ODEL AND THEN IT KICKS THE 6IEW INTO GEAR

:KDW D FRPSOHWHO\ ODPH GHVLJQ /RRN DW DOO WKH GXSOLFDWH FRGH LQ HDFK VHUYOHW <RX KDYH WR DGG WKH VDPH RYHUDOO DSSOLFDWLRQ FRGH OLNH VHFXULW\ LQ DOPRVW HYHU\ VHUYOHW

"UT THE WORST PART IS THAT ALL THAT GENERIC APPLICATION LOGIC IS DUPLICATED IN EVERY SINGLE FRICKIN SERVLET )F ONE THING NEEDS TO CHANGE IT HAS TO CHANGE EVERYWHERE ! MAINTENANCE TRAIN WRECK WAITING TO HAPPEN h9EAH ) FELT A LITTLE WEIRD ABOUT THE DUPLICATE CODE h SAYS "OB hBUT WHAT ELSE CAN ) DO 3URELY YOU DON T MEAN FOR ME TO PUT EVERYTHING IN A SINGLE SERVLET AGAIN (OW COULD THAT BE GOOD v

&RPH RQ \RX GRQ·W 6(5,286/< H[SHFW PH WR SXW LW DOO EDFN LQ RQH QRQ 22 VHUYOHW

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

1[ \PMZM IV IV[_MZ ' 6KRXOG %RE JR EDFN WR MXVW RQH VHUYOHW &RQWUROOHU WR DYRLG GXSOLFDWH FRGH" :RXOG WKDW EH EDG 22 EHFDXVH WKH VHUYOHWV UHDOO\ DUH GRLQJ GLIIHUHQW WKLQJV" 'RHV .HDQX 5HHYHV UHDOO\ NQRZ .XQJ )X"

# 4X3 X9 8 /HDYH WKLV IRU \RX WR SRQGHU ZH ZLOO :KDW GR \RX WKLQN" 'R \RX NQRZ WKH DQVZHU" ,6 WKHUH DQ DQVZHU" :RXOG \RX DJUHH ZLWK %RE DQG OHDYH WKH VHUYOHWV DV WKH\ DUH RU ZRXOG \RX SXW WKH FRGH LQWR MXVW RQH VHUYOHW &RQWUROOHU" $QG LI \RX GR XVH MXVW RQH &RQWUROOHU IRU HYHU\WKLQJ KRZ ZLOO WKH &RQWUROOHU NQRZ ZKLFK 0RGHO DQG 9LHZ WR FDOO" 7KH DQVZHU WR WKLV TXHVWLRQ ZRQ¶W FRPH XQWLO WKH YHU\ HQG RI WKH ERRN VR WKLQN DERXW WKLV IRU D IHZ PRPHQWV WKHQ SXW LW LQ D PHQWDO EDFNJURXQG WKUHDG \RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

FKDSWHU UHĂ€HFWLRQ

6KDUSHQ \RXU SHQFLO

8VLQJ 09& LQ D VHUYOHW -63 ZRUOG HDFK RI WKHVH WKUHH FRPSRQHQWV -63 -DYD FODVV 6HUYOHW SOD\V RQH RI WKH WKUHH 09& UROHV &LUFOH WKH ´0¾ WKH ´9¾ RU WKH ´&¾ GHSHQGLQJ RQ ZKLFK 09& SDUW WKDW FRPSRQHQW SOD\V &LUFOH RQO\ RQH OHWWHU SHU FRPSRQHQW

0 9 & -63

FODVV )RR ^ YRLG EDU ^ GR%DU ` `

0 9 &

QRQ VHUYOHW -DYD FODVV

:MDD=L HGAFLK

ƒ 7KH &RQWDLQHU JLYHV \RXU ZHE DSS FRPPXQLFDWLRQV VXSSRUW OLIHF\FOH PDQDJHPHQW PXOWLWKUHDGLQJ VXSSRUW GHFODUDWLYH VHFXULW\ DQG VXSSRUW IRU -63V VR WKDW \RX FDQ FRQFHQWUDWH RI \RXU RZQ EXVLQHVV ORJLF

ƒ 7KH &RQWDLQHU FUHDWHV D UHTXHVW DQG UHVSRQVH REMHFW WKDW VHUYOHWV DQG RWKHU SDUWV RI WKH ZHE DSS FDQ XVH WR JHW LQIRUPDWLRQ DERXW WKH UHTXHVW DQG VHQG LQIRUPDWLRQ WR WKH FOLHQW

ƒ $ W\SLFDO VHUYOHW LV D FODVV WKDW H[WHQGV

6HUYOHW

0 9 &

+WWS6HUYOHW5HTXHVW DQG RYHUULGHV RQH RU PRUH VHUYLFH PHWKRGV WKDW FRUUHVSRQG WR +773 PHWKRGV LQYRNHG E\ WKH EURZVHU GR*HW GR3RVW HWF

ƒ 7KH GHSOR\HU FDQ PDS D VHUYOHW FODVV WR D 85/ WKDW WKH FOLHQW FDQ XVH WR UHTXHVW WKDW VHUYOHW 7KH QDPH PD\ KDYH QRWKLQJ WR GR ZLWK WKH DFWXDO FODVV ILOH QDPH

:KDW GR WKH OHWWHUV 09& UHSUHVHQW LQ WKH 09& GHVLJQ SDWWHUQ"

0 VWDQGV IRU BBBBBBBBBBBBBBBBBBBBBBBB 9 VWDQGV IRU BBBBBBBBBBBBBBBBBBBBBBBB & VWDQGV IRU BBBBBBBBBBBBBBBBBBBBBBBB

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

6KDUSHQ \RXU SHQFLO

:KR·V UHVSRQVLEOH" )LOO LQ WKH WDEOH EHORZ LQGLFDWLQJ ZKHWKHU WKH ZHE VHUYHU WKH ZHE FRQWDLQHU RU D VHUYOHW LV PRVW UHVSRQVLEOH IRU WKH WDVN OLVWHG ,Q D IHZ FDVHV PRUH WKDQ RQH DQVZHU PD\ EH WUXH IRU D JLYHQ WDVN )RU H[WUD FUHGLW DGG D EULHI FRPPHQW GHVFULELQJ WKH SURFHVV

7DVN

:HE VHUYHU

&RQWDLQHU

6HUYOHW

&UHDWHV WKH UHTXHVW UHVSRQVH REMHFWV

&DOOV WKH VHUYLFH PHWKRG

6WDUWV D QHZ WKUHDG WR KDQGOH UHTXHVWV

&RQYHUWV D UHVSRQVH REMHFW WR DQ +773 UHVSRQVH

.QRZV +773

$GGV +70/ WR WKH UHVSRQVH REMHFW

+DV D UHIHUHQFH WR WKH UHVSRQVH REMHFWV

)LQGV 85/V LQ WKH '' 'HOHWHV WKH UHTXHVW DQG UHVSRQVH REMHFWV

&RRUGLQDWHV PDNLQJ G\QDPLF FRQWHQW 0DQDJHV OLIHF\FOHV +DV D QDPH WKDW PDWFKHV WKH VHUYOHW FODVV! HOHPHQW LQ WKH ''

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

VHUYOHW DQG '' H[HUFLVH

&RGH 0DJQHWV ! WORKING SERVLET AND ITS $$ ARE SCRAMBLED UP ON THE FRIDGE #AN YOU ADD THE CODE SNIPPETS ON THE RIGHT TO THE INCOMPLETE LISTINGS ON THE LEFT TO MAKE A WORKING SERVLET AND $$ WHOSE 52, ENDS WITH $ICE 4HERE MIGHT BE SOME EXTRA MAGNETS ON THE RIGHT THAT YOU WON T USE AT ALL

([HUFLVH 6HUYOHW SXEOLF FODVV

H[WHQGV +WWS6HUYOHW ^

SXEOLF YRLG GR*HW

WKURZV ,2([FHSWLRQ ^

6WULQJ G ,QWHJHU WR6WULQJ LQW 0DWK UDQGRP

6WULQJ G ,QWHJHU WR6WULQJ LQW 0DWK UDQGRP

RXW SULQWOQ ³ KWPO! ERG\!´ ³ K DOLJQ FHQWHU!+)?œV &KDS 'LFH 5ROOHU K !´ ³ S!´ G ³ DQG ³ G ³ ZHUH UROOHG´ ³ ERG\! KWPO!´ ` `

'' ZHE DSS !

(Remember, this isnÕt the complete <web-app> opening tag--a complete example is at the end of this chapter. It doesnÕt affect this exercise.) & GLFH VHUYOHW QDPH!

ZHE DSS!

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

&RGH 0DJQHWV FRQWLQXHG LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR

SXEOLF YRLG VHUYLFH

XUO SDWWHUQ!

& GLFH

&K 'LFH VHUYOHW QDPH!

6HUYOHW5HTXHVW UHTXHVW

3ULQW:ULWHU RXW UHV SRQVH JHW:ULWHU

+WWS6HUYOHW5HVSRQVH UHVSRQVH

VHUYOHW PDSSLQJ! & GLFH

VSRQVH 6HUYOHW5HVSRQVH UH

VHUYOHW QDPH!

VHUYOHW FODVV!

'LFH

&K 'LFH

+WWS6HUYOHW5HTXHVW UHTXHVW

VHUYOHW!

3ULQW:ULWHU RXW UHTXHVW JHW:ULWHU 'LFH

&K 'LFH

! VHUYOHW FODVV

VHUYOHW QDPH! XUO SDWWHUQ! VHUYOHW!

VHUYOHW PDSSLQJ!

\RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

UHVSRQVLELOLW\ H[HUFLVH VROXWLRQ

([HUFLVH 6ROXWLRQV

:HE VHUYHU

7DVN

&DOOV WKH VHUYLFH PHWKRG

6WDUWV D QHZ WKUHDG WR KDQGOH UHTXHVWV

&RQYHUWV D UHVSRQVH REMHFW WR DQ +773 UHVSRQVH

.QRZV +773

&RQWDLQHU

Gets the response from the container. Uses it to talk to the client browser.

$GGV +70/ WR WKH UHVSRQVH REMHFW

+DV D UHIHUHQFH WR WKH UHVSRQVH REMHFWV

Container gives it the servlet.

)LQGV 85/V LQ WKH ''

To Þnd the correct servlet for the request.

'HOHWHV WKH UHTXHVW DQG UHVSRQVH REMHFWV

&RRUGLQDWHV PDNLQJ G\QDPLF FRQWHQW 0DQDJHV OLIHF\FOHV +DV D QDPH WKDW PDWFKHV WKH VHUYOHW FODVV! HOHPHQW LQ WKH ''

6HUYOHW

Just before starting the thread. Then service() method calls doGet() or doPost(). Starts a servlet thread.

&UHDWHV WKH UHTXHVW UHVSRQVH REMHFWV

Knows how to forward to the Container.

The dynamic content for the client. Uses it to print a response.

Once the servlet is Þnished. Knows who to call. Calls service method (and others youÕll see). public class Whatever

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

([HUFLVH 6ROXWLRQV FRQWLQXHG

6HUYOHW LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV

&K 'LFH

SXEOLF YRLG GR*HW

H[WHQGV +WWS6HUYOHW ^

+WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ ^

3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU 6WULQJ G ,QWHJHU WR6WULQJ LQW 0DWK UDQGRP

6WULQJ G ,QWHJHU WR6WULQJ LQW 0DWK UDQGRP

RXW SULQWOQ ³ KWPO! ERG\!´ ³ K DOLJQ FHQWHU!+)?¶V &KDS 'LFH 5ROOHU K !´ ³ S!´ G ³ DQG ³ G ³ ZHUH UROOHG´ ³ ERG\! KWPO!´ ` `

'' ZHE DSS ! VHUYOHW! VHUYOHW QDPH! VHUYOHW FODVV!

& GLFH VHUYOHW QDPH! &K 'LFH

VHUYOHW FODVV!

VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH! XUO SDWWHUQ!

& GLFH

VHUYOHW QDPH!

'LFH

XUO SDWWHUQ!

VHUYOHW PDSSLQJ! ZHE DSS! \RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

WZR REMHFWV WZR KHDSV

) š_WZSQVOº ,MXTWaUMV\ ,M[KZQX\WZ ,, $ON T WORRY ABOUT WHAT ANY OF THIS REALLY MEANS YOU LL SEE AND BE TESTED ON THIS IN OTHER CHAPTERS (ERE WE JUST WANTED TO SHOW YOU A WEB XML $$ THAT ACTUALLY WORKS 4HE OTHER EXAMPLES IN THIS CHAPTER WERE MISSING A LOT OF THE PIECES THAT GO INTO THE OPENING WEB APP TAG 9OU CAN SEE WHY WE DON T USUALLY INCLUDE IT IN OUR EXAMPLES 7KH ZD\ ZH XVXDOO\ VKRZ LW LQ WKH ERRN ZHE DSS !

eb-app> This opening <wlet e. tag isnÕt comp

VHUYOHW! VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! VHUYOHW FODVV!FRP H[DPSOH ZHE %HHU6HOHFW VHUYOHW FODVV! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! XUO SDWWHUQ! 6HOHFW%HHU GR XUO SDWWHUQ! VHUYOHW PDSSLQJ! ZHE DSS!

7KH ZD\ LW 5($//< ZRUNV

of this to memorize any You do NOT haerve. Just copy it in when opening tag, ev ntainer thatÕs compliant youÕre using a Coc 2.4 (like Tomcat 5). with servlet spe

ZHE DSS [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE DSSB B [VG´ YHUVLRQ ´ ´! VHUYOHW! VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! VHUYOHW FODVV!FRP H[DPSOH ZHE %HHU6HOHFW VHUYOHW FODVV! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! XUO SDWWHUQ! 6HOHFW%HHU GR XUO SDWWHUQ! VHUYOHW PDSSLQJ! ZHE DSS!

FKDSWHU

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 2. Web App Architecture

KLJK OHYHO DUFKLWHFWXUH

0W_ 2 -- NQ\[ QV\W ITT \PQ[ 4HE *AVA %NTERPRISE %DITION IS KIND OF A SUPER SPECˆIT INCORPORATES OTHER SPECIFCATIONS INCLUDING THE 3ERVLETS SPEC AND THE *30 SPEC 4HAT S FOR THE WEB #ONTAINER "UT THE * %% SPEC ALSO INCLUDES THE %NTERPRISE *AVA"EAN SPECIlCATION FOR THE %*" #ONTAINER )N OTHER WORDS THE WEB #ONTAINER IS FOR WEB COMPONENTS 3ERVLETS AND *30S AND THE %*" #ONTAINER IS FOR BUSINESS COMPONENTS ! FULLY COMPLIANT * %% APPLICATION SERVER MUST HAVE BOTH A WEB #ONTAINER AND AN %*" #ONTAINER PLUS OTHER THINGS INCLUDING A *.$) AND *-3 IMPLEMENTATION 4OMCAT IS JUST A WEB #ONTAINER )T IS STILL COMPLIANT WITH THE PORTIONS OF THE * %% SPEC THAT ADDRESS THE WEB #ONTAINER 4OMCAT IS A WEB #ONTAINER NOT A FULL * %% APPLICATION SERVER BECAUSE 4OMCAT DOES NOT HAVE AN %*" #ONTAINER

$ - (( DSSOLFDWLRQ VHUYHU LQFOXGHV ERWK D ZHE &RQWDLQHU $1' DQ (-% &RQWDLQHU 7RPFDW LV D ZHE &RQWDLQHU EXW 127 D IXOO - (( DSSOLFDWLRQ VHUYHU $ - (( VHUYHU LQFOXGHV WKH 6HUYOHW VSHF -63 VSHF DQG (-% VSHF

- (( $SSOLFDWLRQ 6HUYHU :HE &RQWDLQHU

(-% &RQWDLQHU

6HUYOHW KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

6HUYOHWV -63V

4

3O 4OMCAT IS A STANDALONE WEB #ONTAINER DOES THAT MEAN THERE ARE STANDALONE %*" #ONTAINERS

$

)N THE OLD DAYS SAY THE YEAR YOU COULD FIND COMPLETE * %% APPLICATION SERVERS STANDALONE WEB #ON TAINERS AND STANDALONE %*" #ONTAINERS "UT TODAY VIRTUALLY ALL %*" #ONTAINERS ARE PART OF FULL * %% SERVERS ALTHOUGH THERE ARE STILL A FEW STANDLONE WEB #ONTAINERS INCLUDING

'%

(QWHUSULVH -DYD%HDQV

4OMCAT AND 2ESIN 3TANDALONE WEB #ONTAINERS ARE USUALLY CONFIGURED TO WORK WITH AN (440 WEB SERVER LIKE !PACHE ALTHOUGH THE 4OMCAT #ONTAINER DOES HAVE THE ABILITY TO ACT AS A BASIC (440 SERVER "UT FOR (440 SERVER CAPABILITY 4OMCAT IS NOT NEARLY AS ROBUST AS !PACHE SO THE MOST COM MON NON %*" WEB APPS USUALLY USE !PACHE AND 4OMCAT CONFIGURED TOGETHERˆWITH !PACHE AS THE (440 WEB 3ERVER AND 4OMCAT AS THE WEB #ONTAINER 3OME OF THE MOST COMMON * %% SERVERS ARE "%! S 7EB,OGIC THE OPEN SOURCE *"OSS !3 AND )"- S 7EB3PHERE \RX DUH KHUH

Chapter 2. Web App Architecture Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 3. Mini MVC Tutorial

KDQGV RQ 09&

0LQL 09& 7XWRULDO +H PD\ ORRN WRXJK EXW KH KDV QHYHU FUHDWHG DQG GHSOR\HG DQ 09& ZHE DSS , KHDUG WKDW KH VWLOO FRGHV DOO KLV SUHVHQWDWLRQ ORJLF LQ VHUYOHWV -XVW WKLQN KH·G EH D EODFN EHOW E\ QRZ LI KH·G EHHQ XVLQJ -63V

&UHDWH DQG GHSOR\ DQ 09& ZHE DSS ,W¶V WLPH WR JHW \RXU KDQGV GLUW\ ZULWLQJ DQ +70/ IRUP D VHUYOHW FRQWUROOHU D PRGHO SODLQ ROG -DYD FODVV DQ ;0/ GHSOR\PHQW GHVFULSWRU DQG D -63 YLHZ 7LPH WR EXLOG LW GHSOR\ LW DQG WHVW LW %XW ¿ UVW \RX QHHG WR VHW XS \RXU GHYHORSPHQW HQYLURQPHQW²D SURMHFW GLUHFWRU\ VWUXFWXUH WKDW¶V VHSDUDWH IURP \RXU DFWXDO GHSOR\HG DSS 1H[W \RX QHHG WR VHW XS \RXU GHSOR\PHQW HQYLURQPHQW IROORZLQJ WKH VHUYOHW DQG -63 VSHFV DQG 7RPFDW UHTXLUHPHQWV 7KHQ \RX¶UH UHDG\ WR VWDUW ZULWLQJ FRPSLOLQJ GHSOR\LQJ DQG UXQQLQJ 7UXH WKLV LV D YHU\ VPDOO DSS ZH¶UH EXLOGLQJ %XW WKHUH¶V DOPRVW 12 DSS WKDW¶V WRR VPDOO WR XVH 09& %HFDXVH WRGD\¶V VPDOO DSS LV WRPRUURZ¶V GRW FRP VXFFHVV

WKLV LV D QHZ 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: stephengoss@gmx.net 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

RI¿FLDO 6XQ H[DP REMHFWLYHV

:HE $SSOLFDWLRQ 'HSOR\PHQW

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 &RQVWUXFW WKH ¿OH DQG GLUHFWRU\ VWUXFWXUH RI D ZHE

DSSOLFDWLRQ WKDW PD\ FRQWDLQ D VWDWLF FRQWHQW E -63 SDJHV F VHUYOHW FODVVHV G WKH GHSOR\PHQW GHVFULSWRU H WDJ OLEUDULHV I -$5 ¿OHV DQG J -DYD FODVV ¿OHV 'HVFULEH KRZ WR SURWHFW UHVRXUFH ¿OHV IURP +773 DFFHVV

'HVFULEH WKH SXUSRVH DQG VHPDQWLFV IRU HDFK RI WKH IROORZLQJ GHSOR\PHQW GHVFULSWRU HOHPHQWV HUURU SDJH LQLW SDUDP PLPH PDSSLQJ VHUYOHW VHUYOHW FODVV VHUYOHW PDSSLQJ VHUYOHW QDPH DQG ZHOFRPH ¿OH

!LL OF THE OBJECTIVES IN THIS SECTION ARE COVERED COMPLETELY IN THE $EPLOYMENT CHAPTER THIS IS JUST A lRST LOOK 4HIS CHAPTER IS THE ONLY COMPLETE START TO lNISH TUTORIAL IN THE BOOK SO IF YOU SKIP IT YOU MIGHT HAVE TROUBLE LATER TESTING SOME OF THE OTHER EXAMPLES IN LATER CHAPTERS WHERE WE DON T GO THROUGH EVERY DETAIL AGAIN !S WITH THE PREVIOUS TWO CHAPTERS YOU DON T NEED TO FOCUS ON MEMORIZING THE CONTENT IN THIS CHAPTER *UST GET IN THERE AND DO IT

&RQVWUXFW WKH FRUUHFW VWUXFWXUH IRU HDFK RI WKH IROORZLQJ GHSOR\PHQW GHVFULSWRU HOHPHQWV HUURU SDJH LQLW SDUDP PLPH PDSSLQJ VHUYOHW VHUYOHW FODVV VHUYOHW QDPH DQG ZHOFRPH ¿OH

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: stephengoss@gmx.net 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

KDQGV RQ 09&

4M\¼[ J]QTL I ZMIT [UITT _MJ IXXTQKI\QWV 7E LOOKED AT THE ROLE OF A CONTAINER WE TALKED A BIT ABOUT DEPLOYMENT DESCRIPTORS AND WE TOOK A l RST LOOK AT THE -ODEL -6# ARCHITECTURE "UT YOU CAN T JUST SIT HERE AND READ ALL DAYˆ NOW IT S TIME TO ACTUALLY DO SOMETHING

7KH IRXU VWHSV ZH·OO IROORZ %HHU 5HFRPPHQGDWLRQV -63

5HYLHZ WKH XVHU¶V YLHZV ZKDW WKH EURZVHU

WU\ -DFN·V 3DOH $OH WU\ *RXW 6WRXW

ZLOO GLVSOD\ DQG WKH KLJK OHYHO DUFKLWHFWXUH

0\3URMHFWV

&UHDWH WKH GHYHORSPHQW HQYLURQPHQW WKDW ZH GDWLQJ$SS

ZLOO XVH IRU WKLV SURMHFW ZKLFK \RX FDQ XVH IRU DQ\ RWKHU H[DPSOH LQ WKH ERRN HWF

EHHU9

ZHE

FODVVHV

VUF

OLE

ZHEDSS! ZHEDSS!

FRP

FRP

UHVXOW MVS

ZHE [PO

H[DPSOH

WRPFDW WRPFDW

ZHE

&UHDWH WKH GHSOR\PHQW

ZHEDSSV

HQYLURQPHQW WKDW ZH ZLOO XVH IRU WKLV SURMHFW ZKLFK \RX FDQ XVH IRU DQ\ RWKHU H[DPSOH LQ WKH ERRN

%HHU Y

KWPO! ERG\! ERG\! KWPO!

:(% ,1)

IRUP KWPO

!

UHVXOW MVS

KWPO! ERG\! ERG\! KWPO!

!

SXEOLF FODVV

6HUYOHW H[WHQGV +WWS6HUY OHW ^ `

%HHU6HOHFW MDYD

IRUP KWPO

H[DPSOH

PRGHO

ZHE

PRGHO

JHW %UDQGV ^ `

%HHU6HOHFW FODVV

%HHU([SHUW FODVV

%HHU([SHUW MDYD

OLE

FODVVHV

ZHEDSS! ZHEDSS!

ZHE [PO FRP

H[DPSOH

ZHE

PRGHO

desi

%HHU6HOHFW FODVV

%HHU([SHUW FODVV

gn

3HUIRUP LWHUDWLYH GHYHORSPHQW DQG WHVWLQJ RQ WKH

code

1RWH :H UHFRPPHQG LWHUDWLYH GHYHORSPHQW DQG WHVWLQJ DOWKRXJK ZH ZRQ¶W DOZD\V VKRZ DOO WKH VWHSV LQ WKLV ERRN

test

YDULRXV FRPSRQHQWV RI RXU ZHE DSSOLFDWLRQ 2. WKLV LV PRUH RI D VWUDWHJ\ WKDQ D VWHS

\RX DUH KHUH

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: stephengoss@gmx.net 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

XVHU YLHZV

<PM =[MZŸ[ >QM_ WN \PM _MJ IXXTQKI\QWV¸ I *MMZ )L^Q[WZ /UR WEB APPLICATION IS A "EER !DVISOR 5SERS WILL BE ABLE TO SURF TO OUR APP ANSWER A QUESTION AND GET BACK STUNNINGLY USEFUL BEER ADVICE

This page will be written in HTML, and will generate an HTTP Post request, sending the userÕs color selection as a parameter.

This page will be a JSP that Ă•sgives ce. the advice based on the user choi %HHU 5HFRPPHQGDWLRQV -63 WU\ -DFN¡V 3DOH $OH WU\ *RXW 6WRXW

4

7HY ARE WE WRITING A WEB APPLICATION THAT GIVES BEER ADVICE

$

!FTER AN EXHAUSTIVE MARKETING RESEARCH EFFORT WE CONCLUDED THAT OF OUR READERS APPRECIATE BEER 4HE OTHER CAN SIMPLY SUBSTITUTE THE WORD hCOFFEEv FOR hBEERv

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: stephengoss@gmx.net 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

KDQGV RQ 09&

0MZMÂź[ \PM IZKPQ\MK\]ZM %VEN THOUGH THIS IS A TINY APPLICATION WE LL BUILD IT USING A SIMPLE -6# ARCHITECTURE 4HAT WAY WHEN IT BECOMES 4(% HOTTEST SITE ON THE WEB WE LL BE READY TO EXTEND THE APPLICATION

:HE 6HUYHU :HE EURZVHU

&RQWDLQHU

:HE 6HUYHU ORJLF

&OLHQW

7KH FOLHQW PDNHV D UHTXHVW IRU WKH IRUP KWPO SDJH

&RQWDLQHU ORJLF

KWPO! KHDG! KHDG!

7KH &RQWDLQHU UHWULHYHV WKH IRUP KWPO SDJH

Controller Model

VHUYOHW

ERG\! ERG\! KWPO! KWPO! KHDG! KHDG!

IRUP KWPO

ERG\! ERG\! KWPO!

View

%HHU([SHUW FRPSRQHQW

UHVXOW MVS

7KH &RQWDLQHU UHWXUQV WKH SDJH WR WKH EURZVHU ZKHUH WKH XVHU DQVZHUV WKH TXHVWLRQV RQ WKH IRUP DQG

(Plain Just a POJObject). O a av Old J 7KH EURZVHU VHQGV WKH UHTXHVW GDWD WR WKH FRQWDLQHU 7KH &RQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ DQG SDVVHV WKH UHTXHVW WR WKH VHUYOHW

&RQWDLQHU :HE EURZVHU

&RQWDLQHU ORJLF

7KH VHUYOHW FDOOV WKH %HHU([SHUW IRU KHOS

&OLHQW

7KH H[SHUW FODVV UHWXUQV DQ DQVZHU ZKLFK WKH VHUYOHW DGGV WR WKH UHTXHVW REMHFW

KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

UHVXOW MVS

From here on out when you donÕt see the web server, assume itÕs there.

VHUYOHW

%HHU([SHUW FRPSRQHQW

UHTXHVW

7KH VHUYOHW IRUZDUGV WKH UHTXHVW WR WKH -63 7KH -63 JHWV WKH DQVZHU IURP WKH UHTXHVW REMHFW 7KH -63 JHQHUDWHV D SDJH IRU WKH &RQWDLQHU 7KH FRQWDLQHU UHWXUQV WKH SDJH WR WKH KDSS\ XVHU \RX DUH KHUH

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: stephengoss@gmx.net 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

GHYHORSPHQW HQYLURQPHQW

+ZMI\QVO aW]Z LM^MTWXUMV\ MV^QZWVUMV\ 4HERE ARE LOTS OF WAYS YOU COULD ORGANIZE YOUR DEVELOPMENT DIRECTORY STRUCTURE BUT HERE S WHAT WE RECOMMEND FOR SMALL AND MEDIUM SIZED PROJECTS 7HEN IT S TIME TO DEPLOY THE WEB APP WE LL COPY A PORTION OF THIS INTO WHEREVER OUR PARTICULAR #ONTAINER WANTS THE PIECES TO GO )N THIS TUTORIAL WE RE USING 4OMCAT

Somewhere on your hard drive... (You donÕt need this directory for the tutorial.)

HWF

ZHEDSS! ZHEDSS!

0\3URMHFWV

GDWLQJ$SS

EHHU9

VUF

OLE

This is where you put 3rd party JAR Ăžles (from servlets-R-us).

SXEOLF FODVV

6HUYOHW H[WHQGV +WWS6HUY OHW ^ `

%HHU6HOHFW MDYD

!

FRP

y This directisorderived structure mpile when you coclasses your Java (using -d ).

H[DPSOH

ZHE

ZHE

FRP

This is where your conĂžguration Ăžle goes.

Your static and dynamic view components go here.

FODVVHV

All of your Java code lives under the src directory.

ZHE [PO

WeÕre of course using a standard package structure so that we get all the normal beneÞts of packages: - project organization - namespace management - portability and reusability

Put each web app in its own project directory.

PRGHO

UHVXOW MVS

IRUP KWPO

PRGHO

JHW %UDQGV ^ `

%HHU6HOHFW FODVV

%HHU([SHUW FODVV

%HHU([SHUW MDYD

KWPO! ERG\! ERG\! KWPO!

Examples of some view components.

H[DPSOH

ZHE

Notice that weÕre separating the controller components from the model components.

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: stephengoss@gmx.net 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

KDQGV RQ 09&

+ZMI\QVO \PM LMXTWaUMV\ MV^QZWVUMV\

This is the directory; itTomimcat home named somethingght be else like: jakarta-tomcat -5.0.19.

$EPLOYING A WEB APP INVOLVES BOTH #ONTAINER SPECIl C RULES AND REQUIREMENTS OF THE 3ERVLETS AND *30 SPECIl CATIONS )F YOU RE NOT DEPLOYING TO 4OMCAT YOU LL HAVE TO l GURE OUT EXACTLY WHERE YOUR WEB APP SHOULD BE RELATIVE TO YOUR #ONTAINER )N OUR EXAMPLE EVERYTHING BELOW THE h"EER V v DIRECTORY IS THE SAME REGARDLESS OF YOUR #ONTAINER WRPFDW WRPFDW

7RPFDW VSHFLĂ€ F

This part of the directory structure is required by Tomcat, and it must be directly inside the Tomcat home directory.

ZHEDSSV

This directory name also represents the Òcontext rootÓ which Tomcat uses when resolving URLs. WeÕll explore this concept in great detail in the deployment chapter.

The name of the web app.

%HHU Y

Everything BELOW this dotted line IS the webapp, and will be the same regardless of your Container vendor.

:(% ,1)

3DUW RI WKH 6HUYOHWV VSHFLĂ€ FDWLRQ

KWPO! ERG\! ERG\! KWPO!

!

IRUP KWPO

OLE

FODVVHV

ZHEDSS! ZHEDSS!

UHVXOW MVS

This web.xml Ăžle MUST be in WEB-INF

ZHE [PO FRP

This package structure is exactly what we used in the development environment. Unless youÕre deploying your classes in a JAR (weÕll talk about that later in the book), then you MUST put the package directory structure immediately under WEB-INF/classes.

$SSOLFDWLRQ VSHFLĂ€ F H[DPSOH

ZHE

PRGHO

%HHU6HOHFW FODVV

%HHU([SHUW FODVV

\RX DUH KHUH

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: stephengoss@gmx.net 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

EXLOGLQJ WKH DSS

7]Z ZWILUIX NWZ J]QTLQVO \PM IXX 7HEN WE STARTED THIS CHAPTER WE OUTLINED A FOUR STEP PROCESS FOR DEVELOPING OUR WEB APP 3O FAR WE VE REVIEWED THE USER VIEWS FOR OUR WEB APP LOOKED AT THE ARCHITECTURE SETUP THE DEVELOPMENT AND DEPLOYMENT ENVIRONMENTS FOR CREATING AND DEPLOYING THE APP .OW IT S TIME FOR STEP CREATING THE APP 7E BORROW FROM SEVERAL POPULAR DEVELOPMENT METHODOLOGIES A LITTLE FROM EXTREME PROGRAMMING ITERATIVE DEVELOPMENT AND MANGLE THEM FOR OUR OWN EVIL PURPOSES

7KH ILYH VWHSV ZH·OO IROORZ LQ VWHS

D

%XLOG DQG WHVW WKH +70/ IRUP WKDW WKH XVHU ZLOO ¿UVW UHTXHVW

E

%XLOG DQG WHVW YHUVLRQ RI WKH FRQWUROOHU VHUYOHW ZLWK WKH +70/ IRUP 7KLV YHUVLRQ LV LQYRNHG YLD WKH +70/ IRUP DQG SULQWV WKH SDUDPHWHU LW UHFHLYHV

F

%XLOG D WHVW FODVV IRU WKH H[SHUW PRGHO FODVV WKHQ EXLOG DQG WHVW WKH H[SHUW PRGHO FODVV LWVHOI

G

8SJUDGH WKH VHUYOHW WR YHUVLRQ 7KLV YHUVLRQ DGGV WKH FDSDELOLW\ RI FDOOLQJ WKH PRGHO FODVV WR JHW EHHU DGYLFH

H

%XLOG WKH -63 XSJUDGH WKH VHUYOHW WR YHUVLRQ ZKLFK DGGV WKH FDSDELOLW\ RI GLVSDWFKLQJ WR WKH -63 DQG WHVW WKH ZKROH DSS

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: stephengoss@gmx.net 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

KDQGV RQ 09&

<PM 0<54 NWZ \PM QVQ\QIT NWZU XIOM 4HE (4-, IS SIMPLEˆIT PUTS UP THE HEADING TEXT THE DROP DOWN LIST FROM WHICH THE USER SELECTS A BEER COLOR AND THE SUBMIT BUTTON

KWPO! ERG\! K DOLJQ ´FHQWHU´!%HHU 6HOHFWLRQ 3DJH K ! IRUP PHWKRG ´3267´ DFWLRQ ´6HOHFW%HHU GR´! 6HOHFW EHHU FKDUDFWHULVWLFV S! &RORU VHOHFW QDPH ´FRORU´ VL]H ´ ´!

Why did we choose POST instead of GET ? This is what the HTML thinks the servlet is called. There is NOTHING in your directory structure named ÒSelectBeer.doÓ! ItÕs a logical name...

RSWLRQ!OLJKW RSWLRQ!DPEHU RSWLRQ!EURZQ RSWLRQ!GDUN

This is how we created the pull down menu, your options may vary. (Did you Ăžgure out size=Ă’1Ă“ ?)

VHOHFW! EU! EU! FHQWHU! LQSXW W\SH ´68%0,7´! FHQWHU! IRUP! ERG\! KWPO!

4

7HY IS THE FORM SUBMITTING TO h3ELECT"EER DOv WHEN THERE IS ./ SERVLET WITH THAT NAME )N THE DIRECTORY STRUCTURES WE LOOKED AT EARLIER ) DIDN T SEE ANYTHING THAT HAD THE NAME h3ELECT"EER DOv !ND WHAT S WITH THE h DOv EXTENSION ANYWAY

$

3ELECT"EER DO IS A LOGICAL NAME NOT AN ACTUAL FILE NAME )T S SIMPLY THE NAME WE WANT THE CLIENT TO USE )N FACT THE CLIENT WILL .%6%2 HAVE DIRECT ACCESS TO THE SERVLET CLASS FILE SO YOU WON T FOR EXAMPLE CREATE AN (4-, PAGE WITH A LINK OR ACTION THAT INCLUDES A PATH TO A SERVLET CLASS FILE 4HE TRICK IS WE LL USE THE 8-, $EPLOYMENT $ESCRIPTOR WEB XML TO MAP FROM WHAT THE CLIENT REQUESTS h3ELECT"EER DOv TO AN ACTUAL SERVLET CLASS FILE THE #ONTAINER WILL USE WHEN A REQUEST COMES IN FOR h3ELECT"EER DOv &OR NOW THINK OF THE h DOv EXTENSION AS SIMPLY PART OF THE LOGICAL NAME AND NOT A REAL FILE TYPE ,ATER IN THE BOOK YOU LL LEARN ABOUT OTHER WAYS IN WHICH YOU CAN USE EXTENSIONS REAL OR MADE UP LOGICAL IN YOUR SERVLET MAPPINGS

\RX DUH KHUH

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: stephengoss@gmx.net 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

GHSOR\LQJ DQG WHVWLQJ

,MXTWaQVO IVL \M[\QVO \PM WXMVQVO XIOM 4O TEST IT YOU NEED TO DEPLOY IT INTO THE #ONTAINER 4OMCAT DIRECTORY STRUCTURE START 4OMCAT AND BRING UP THE PAGE IN A BROWSER WRPFDW

&UHDWH WKH +70/ LQ \RXU GHYHORSPHQW HQYLURQPHQW &UHDWH WKLV +70/ ¿ OH FDOO LW IRUP KWPO DQG VDYH LW LQ \RXU GHYHORSPHQW HQYLURQPHQW XQGHU WKH EHHU9 ZHE GLUHFWRU\

%HHU Y

&RS\ WKH À OH LQWR WKH GHSOR\PHQW HQYLURQPHQW 3ODFH D FRS\ RI WKH IRUP KWPO ¿ OH LQWR WRPFDW ZHEDSSV %HHU Y 5HPHPEHU \RXU WRPFDW KRPH GLUHFWRU\ PLJKW KDYH D GLIIHUHQW QDPH

6WDUW 7RPFDW 7KURXJKRXW WKLV ERRN ZH¶UH XVLQJ 7RPFDW DV ERWK WKH ZHE 6HUYHU DQG WKH ZHE &RQWDLQHU ,Q WKH UHDO ZRUOG \RX SUREDEO\ XVH D PRUH UREXVW :HE 6HUYHU OLNH $SDFKH FRQ¿ JXUHG ZLWK D :HE &RQWDLQHU OLNH 7RPFDW %XW 7RPFDW PDNHV D SHUIHFWO\ GHFHQW :HE 6HUYHU IRU HYHU\WKLQJ ZH QHHG WR GR LQ WKLV ERRN 7R VWDUW 7RPFDW FG LQWR WKH WRPFDW KRPH GLUHFWRU\ DQG UXQ ELQ VWDUWXS VK

ZHEDSSV

KWPO! ERG\! ERG\! KWPO!

IRUP KWPO )LOH (GLW :LQGRZ +HOS 2SHQ6RXUFH,W

FG WRPFDW ELQ VWDUWXS VK

7HVW WKH SDJH 2SHQ WKH +70/ SDJH LQ \RXU EURZVHU DQG W\SH

KWWS ORFDOKRVW %HHU Y IRUP KWPO

KWWS ORFDOKRVW %HHU Y IRUP KWPO <RX VKRXOG VHH VRPHWKLQJ OLNH WKH VFUHHQ VKRW KHUH

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: stephengoss@gmx.net 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

KDQGV RQ 09&

+ZMI\QVO \PM ,MXTWaUMV\ ,M[KZQX\WZ ,, 4HE MAIN JOB OF THIS $$ IS TO DEl NE THE MAPPING BETWEEN THE LOGICAL NAME THE CLIENT USES FOR THE REQUEST h3ELECT"EER DOv AND THE ACTUAL SERVLET CLASS l LE COM EXAMPLE WEB "EER3ELECT

&UHDWH WKH '' LQ \RXU GHYHORSPHQW HQYLURQPHQW &UHDWH WKLV ;0/ GRFXPHQW QDPH LW ZHE [PO DQG VDYH LW LQ \RXU GHSOR\PHQW HQYLURQPHQW XQGHU WKH EHHU9 HWF GLUHFWRU\

know You donÕt have to ans, what any of this me just type it in.

ZHE DSS [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE DSSB B [VG´ YHUVLRQ ´ ´!

p name This is a made-uON LY in use l uÕl yo at th e DD. th of s rt pa r he VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! ot

VHUYOHW!

VHUYOHW FODVV!FRP H[DPSOH ZHE %HHU6HOHFW VHUYOHW FODVV! P

of the Fully-qualiĂžed name le. Ăž ss cla t vle ser

VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!&K %HHU VHUYOHW QDPH! XUO SDWWHUQ! 6HOHFW%HHU GR XUO SDWWHUQ!

e client to refer to is how we want oÓthis just a convention. DonÕt forget to This ser Ò.d vlet. The start with a slash. the

VHUYOHW PDSSLQJ! ZHE DSS!

WRPFDW

ZHEDSSV

&RS\ WKH À OH LQWR WKH GHSOR\PHQW HQYLURQPHQW 3ODFH D FRS\ RI WKH ZHE [PO ¿ OH LQWR WRPFDW ZHEDSSV %HHU Y :(% ,1) <RX 0867 SODFH LW WKHUH RU WKH &RQWDLQHU ZRQœW ¿ QG LW DQG QRWKLQJ ZLOO ZRUN DQG \RXœOO EHFRPH GHSUHVVHG

%HHU Y

:(% ,1)

KWPO! ERG\! ERG\! KWPO!

IRUP KWPO

ZHEDSS! ZHEDSS!

ZHE [PO \RX DUH KHUH

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: stephengoss@gmx.net 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

VHUYOHW PDSSLQJ

5IXXQVO \PM TWOQKIT VIUM \W I [MZ ^TM\ KTI[[ NQTM

'LDQH À OOV RXW WKH IRUP DQG KLWV VXEPLW 7KH EURZVHU JHQHUDWHV WKH UHTXHVW 85/

%HHU Y 6HOHFW%HHU GR

The host The web app The logical resource name. server context root. root. 3267 %HHU Y 6HOHFW%HHU GR +773 +RVW ZZZ ZLFNHG

O\VPDUW FRP 8VHU $JHQW 0 R]LOOD 0DFLQ WRVK 8 33& 0DF 26 ; 0D FK 2 HQ 86 U Y *HFNR 1HWVFDSH $FFHSW WH[W [PO DSSOLFDWLRQ [PO DSSOLFDWLRQ [ KWPO [PO WH[ W KWPO T WH[W SODLQ T YLGHR [ PQJ LPDJH S QJ LPDJH MSHJ LPDJH JLI T T

&OLHQW

&RQWDLQHU

7KH &RQWDLQHU VHDUFKHV WKH '' DQG À QGV D VHUYOHW PDSSLQJ! ZLWK D XUO SDWWHUQ! WKDW PDWFKHV 6HOHFW%HHU GR ZKHUH WKH VODVK UHSUHVHQWV WKH FRQWH[W URRW RI WKH ZHE DSS DQG 6HOHFW%HHU GR LV WKH ORJLFDO QDPH RI D UHVRXUFH

ZHE DSS! ! VHUYOHW HW QDPH! VHUYO %HHU &K OHW QDPH! VHUY HW FODVV! W YO HU V %HHU6HOHF [DPSOH ZHE FRP H FODVV! W OH UY VH W! VHUYOH

&RQWDLQHU

7KH &RQWDLQHU VHHV WKDW WKH VHUYOHW QDPH! IRU WKLV XUO SDWWHUQ! LV ´&K %HHUÂľ %XW WKDW LVQ¡W WKH QDPH RI DQ DFWXDO VHUYOHW FODVV Ă€ OH ´&K %HHUÂľ LV WKH QDPH RI D VHUYOHW QRW D VHUYOHW FODVV 7R WKH &RQWDLQHU D VHUYOHW LV VRPHWKLQJ QDPHG LQ WKH '' XQGHU D VHUYOHW! WDJ 7KH QDPH RI WKH VHUYOHW LV VLPSO\ WKH QDPH XVHG LQ WKH '' VR WKDW RWKHU SDUWV RI WKH '' FDQ PDS WR LW

r-v1/Ó isnÕt it L, the Ò/Bee In the HTMe path. In the HTML, part of th just says: d=ÓPOSTÓ <form metho > ectBeer.doÓ action=ÓSel Ò/Beer-v1/Ó ser prependse thatÕs where ow br e th But quest, becaus . In on to the rerequest is coming fromoÓ in nt the clie ds, the ÒSelectBeer.d RL of other wor is relative to the U lative to the HTML s on. In this case, re r-v1Ó. the page itof the web app, Ò/Bee the root

PDSSLQJ! VHUYOHW HW QDPH! VHUYO %HHU &K OHW QDPH! UY VH DWWHUQ! S XUO HFW%HHU GR 6HO SDWWHUQ! O XU W PDSSLQJ! VHUYOH S! DS ZHE

&RQWDLQHU

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: stephengoss@gmx.net 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

KDQGV RQ 09&

7KH &RQWDLQHU ORRNV LQVLGH WKH VHUYOHW! WDJV IRU VRPHWKLQJ ZLWK WKH VHUYOHW QDPH! ´&K %HHUµ

&RQWDLQHU

ZHE DSS! ! VHUYOHW HW QDPH! VHUYO %HHU &K ! H! OHW QDPH VHUY HW FODVV! YO HU 6HOHFW V H ZHE %HHU SO DP H[ P FR OHW FODVV! VHUY W! VHUYOH PDSSLQJ! VHUYOHW ! PH! DP QD HW Q VHUYO U %HHU &K ! PH QD W OH VHUY DWWHUQ! XUO S GR U HH W% HF 6HO SDWWHUQ! XUO LQJ! SS PD W OH VHUY ZHE DSS!

7KH &RQWDLQHU XVHV WKH VHUYOHW FODVV! LQ WKH VHUYOHW! WDJ WR NQRZ ZKLFK VHUYOHW FODVV LV UHVSRQVLEOH IRU KDQGOLQJ WKLV UHTXHVW ,I WKH VHUYOHW KDV QRW EHHQ LQLWLDOL]HG WKH FODVV LV ORDGHG DQG WKH VHUYOHW LV LQLWLDOL]HG &RQWDLQHU

7KH &RQWDLQHU VWDUWV D QHZ WKUHDG WR KDQGOH WKH UHTXHVW DQG SDVVHV WKH UHTXHVW WR WKH WKUHDG WR WKH VHUYOHW·V VHUYLFH PHWKRG

VHUYOHW

UHTXHVW +773 2. KWPO! KHDG! KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

&RQWDLQHU UHVSRQVH

7KH &RQWDLQHU VHQGV WKH UHVSRQVH WKURXJK WKH :HE 6HUYHU RI FRXUVH EDFN WR WKH FOLHQW

+773 2. KWPO! KHDG! KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

&OLHQW

VHUYOHW

&RQWDLQHU

\RX DUH KHUH

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: stephengoss@gmx.net 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

VHUYOHW FRQWUROOHU YHUVLRQ RQH

<PM NQZ[\ ^MZ[QWV WN \PM KWV\ZWTTMZ [MZ^TM\ /UR PLAN IS TO BUILD THE SERVLET IN STAGES TESTING THE VARIOUS COMMUNICATION LINKS AS WE GO )N THE END REMEMBER THE SERVLET WILL ACCEPT A PARAMETER FROM THE REQUEST INVOKE A METHOD ON THE MODEL SAVE INFORMATION IN A PLACE THE *30 CAN l ND AND FORWARD THE REQUEST TO THE *30 "UT FOR THIS l RST VERSION OUR GOAL IS JUST TO MAKE SURE THAT THE (4-, PAGE CAN PROPERLY INVOKE THE SERVLET AND THAT THE SERVLET IS RECEIVING THE (4-, PARAMETER CORRECTLY

6HUYOHW FRGH SDFNDJH FRP H[DPSOH ZHE LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR

match the Be sure yout and deployment en m op rlier. el dev e created ea structures w

ervlet, nds GenericS interface... te ex et vl er et HttpS ements the Servl which impl

SXEOLF FODVV %HHU6HOHFW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ doPost to

WeĂ•ll use handle the HTTP request, because UHVSRQVH VHW&RQWHQW7\SH ÂłWH[W KWPO´ the HTML form 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU says: RXW SULQWOQ Âł%HHU 6HOHFWLRQ $GYLFH EU!´ method=POST 6WULQJ F UHTXHVW JHW3DUDPHWHU ÂłFRORU´

RXW SULQWOQ ³ EU!*RW EHHU FRORU ³ F ` ` WeÕre not giving back advice

here, just displaying test information.

This method comes from the ServletResponse interface. This method comes from the ServletRequest interface. Notice that the argument matches the value of the ÒnameÓ attribute in the HTMLÕs <select> tag.

.H\ $3,V LQWHUIDFH!! MDYD[ VHUYOHW 6HUYOHW

LQWHUIDFH!! MDYD[ VHUYOHW 6HUYOHW5HTXHVW

LQWHUIDFH!! MDYD[ VHUYOHW 6HUYOHW5HVSRQVH

MDYD[ VHUYOHW *HQHULF6HUYOHW

LQWHUIDFH!! MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HTXHVW

LQWHUIDFH!! MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HVSRQVH

MDYD[ VHUYOHW KWWS +WWS6HUYOHW

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: stephengoss@gmx.net 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

KDQGV RQ 09&

+ WUXQTQVO LMXTWaQVO IVL \M[\QVO \PM KWV\ZWTTMZ [MZ^TM\ /K WE VE BUILT DEPLOYED AND TESTED OUR (4-, AND WE VE BUILT AND DEPLOYED OUR $$ WELL WE PUT THE WEB XML INTO THE DEPLOYMENT ENVIRONMENT BUT TECHNICALLY THE $$ WON T BE DEPLOYED UNTIL WE RESTART 4OMCAT .OW IT S TIME TO COMPILE THE l RST VERSION OF THE SERVLET DEPLOY IT AND TEST IT VIA THE (4-, FORM .OW WE LL RESTART 4OMCAT TO MAKE SURE THAT IT hSEESv THE WEB XML AND SERVLET CLASS

&RPSLOLQJ WKH VHUYOHW &RPSLOH WKH VHUYOHW ZLWK WKH G À DJ WR SXW WKH FODVV LQ WKH GHYHORSPHQW HQYLURQPHQW

Adjust this to match your own directory path to your system! Everything after Ă’tomcat/Ă“ will be the same.

)LOH (GLW :LQGRZ +HOS 8SGDWH%UDLQ

FG 0\3URMHFWV EHHU9 MDYDF FODVVSDWK 8VHUV EHUW $SSOLFDWLRQV WRPFDW FRPPRQ OLE VHUYOHW DSL MDU FODVVHV G FODVVHV VUF FRP H[DPSOH ZHE %HHU6HOHFW MDYD

Use the -d option to tell the compiler to put the .class Ăžle into the classes directory within the correct package structure. Your .class Ăžle will end up in /beerV1/classes/com/example/web/.

WRPFDW

'HSOR\LQJ WKH VHUYOHW

ZHEDSSV

7R GHSOR\ WKH VHUYOHW PDNH D FRS\ RI WKH FODVV Âż OH DQG PRYH LW WR WKH %HHU Y :(% ,1) FODVVHV FRP H[DPSOH ZHE GLUHFWRU\ LQ WKH GHSOR\PHQW VWUXFWXUH

7HVWLQJ WKH VHUYOHW 5HVWDUW WRPFDW /DXQFK \RXU EURZVHU DQG JR WR KWWS ORFDOKRVW %HHU Y IRUP KWPO 6HOHFW D EHHU FRORU DQG KLW ³6XEPLW´ ,I \RXU VHUYOHW LV ZRUNLQJ \RX VKRXOG VHH WKH VHUYOHWœV UHVSRQVH LQ \RXU EURZVHU DV VRPHWKLQJ OLNH %HHU 6HOHFWLRQ $GYLFH *RW EHHU FRORU EURZQ

%HHU Y

)LOH (GLW :LQGRZ +HOS 6ODVKGRW0H

:(% ,1)

FG WRPFDW ELQ VKXWGRZQ VK ELQ VWDUWXS VK

FODVVHV

FRP

KWWS ORFDOKRVW %HHU Y IRUP KWPO H[DPSOH

ZHE

%HHU6HOHFW FODVV

\RX DUH KHUH

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: stephengoss@gmx.net 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

WKH PRGHO FODVV

*]QTLQVO IVL \M[\QVO \PM UWLMT KTI[[ )N -6# THE MODEL TENDS TO BE THE hBACK ENDv OF THE APPLICATION )T S OFTEN THE LEGACY SYSTEM THAT S NOW BEING EXPOSED TO THE WEB )N MOST CASES IT S JUST PLAIN OLD *AVA CODE WITH NO KNOWLEDGE OF THE FACT THAT IT MIGHT BE CALLED BY SERVLETS 4HE MODEL SHOULDN T BE TIED DOWN TO BEING USED BY ONLY A SINGLE WEB APP SO IT SHOULD BE IN ITS OWN UTILITY PACKAGES

7KH VSHFV IRU WKH PRGHO ,WV SDFNDJH VKRXOG EH FRP H[DPSOH PRGHO ,WV GLUHFWRU\ VWUXFWXUH VKRXOG EH :(% ,1) FODVVHV FRP PRGHO ,W H[SRVHV RQH PHWKRG JHW%UDQGV WKDW WDNHV D SUHIHUUHG EHHU FRORU DV D 6WULQJ DQG UHWXUQV DQ $UUD\/LVW RI UHFRPPHQGHG EHHU EUDQGV DOVR DV 6WULQJV

%XLOG WKH WHVW FODVV IRU WKH PRGHO &UHDWH WKH WHVW FODVV IRU WKH PRGHO \HV EHIRUH \RX EXLOG WKH PRGHO LWVHOI <RXœUH RQ \RXU RZQ KHUH ZH GRQœW KDYH RQH LQ WKLV WXWRULDO 5HPHPEHU WKH PRGHO ZLOO VWLOO EH LQ WKH GHYHORSPHQW HQYLURQPHQW ZKHQ \RX ¿UVW WHVW LW²LWœV MXVW OLNH DQ\ RWKHU -DYD FODVV DQG \RX FDQ WHVW LW ZLWKRXW 7RPFDW

%XLOG DQG WHVW WKH PRGHO 0RGHOV FDQ EH H[WUHPHO\ FRPSOLFDWHG 7KH\ RIWHQ LQYROYH FRQQHFWLRQV WR OHJDF\ GDWDEDVHV DQG FDOOV WR FRPSOH[ EXVLQHVV ORJLF +HUHœV RXU VRSKLVWLFDWHG UXOH EDVHG H[SHUW V\VWHP IRU WKH EHHU DGYLFH SDFNDJH FRP H[DPSOH PRGHO LPSRUW MDYD XWLO SXEOLF FODVV %HHU([SHUW ^ SXEOLF /LVW JHW%UDQGV 6WULQJ FRORU ^ /LVW EUDQGV QHZ $UUD\/LVW LI FRORU HTXDOV ³DPEHU´

^ EUDQGV DGG ³-DFN $PEHU´ EUDQGV DGG ³5HG 0RRVH´ ` HOVH ^ EUDQGV DGG ³-DLO 3DOH $OH´ EUDQGV DGG ³*RXW 6WRXW´ ` UHWXUQ EUDQGV ` `

ed complex, Notice how weÕve captur beer paradigm the of dge wle expert kno expressions. al ion dit con ed using advanc

this to use DonÕt forget to changethe tomcat home. to h pat c ciÞ spe UR YO

)LOH (GLW :LQGRZ +HOS 6NDWHERDUG

FG EHHU9 MDYDF FODVVSDWK 8VHUV EHUW $SSOLFDWLRQV WRPFDW FRPPRQ OLE VHUYOHW DSL MDU FODVVHV G FODVVHV VUF FRP H[DPSOH PRGHO %HHU([SHUW MDYD

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: stephengoss@gmx.net 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

KDQGV RQ 09&

-VPIVKQVO \PM [MZ^TM\ \W KITT \PM UWLMT [W \PI\ _M KIV OM\ :-)4 IL^QKM )N THIS VERSION TWO SERVLET WE LL ENHANCE THE DO0OST METHOD TO CALL THE MODEL FOR ADVICE VERSION THREE WILL MAKE THE ADVICE COME FROM A *30 4HE CODE CHANGES ARE TRIVIAL BUT THE IMPORTANT PART IS UNDERSTANDING THE REDEPLOYMENT OF THE ENHANCED WEB APP 9OU CAN TRY TO WRITE THE CODE RECOMPILE AND DEPLOY ON YOUR OWN OR YOU CAN TURN THE PAGE AND FOLLOW ALONG

6KDUSHQ \RXU SHQFLO

(QKDQFH WKH VHUYOHW YHUVLRQ WZR )RUJHW DERXW VHUYOHWV IRU D PLQXWH OHW·V MXVW WKLQN -DYD :KDW DUH WKH VWHSV ZH KDYH WR WDNH WR DFFRPSOLVK WKH IROORZLQJ" (QKDQFH WKH GR3RVW PHWKRG WR FDOO WKH PRGHO &RPSLOH WKH VHUYOHW 'HSOR\ DQG WHVW WKH XSGDWHG ZHE DSS

public class BeerSelect extends HttpServlet {

\RX DUH KHUH

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: stephengoss@gmx.net 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

FDOOLQJ WKH PRGHO IURP WKH VHUYOHW FRQWUROOHU

;MZ^TM\ ^MZ[QWV \_W KWLM 2EMEMBER THE MODEL IS JUST PLAIN OLD *AVA SO WE CALL IT LIKE WE D CALL ANY OTHER *AVA METHODˆINSTANTIATE THE MODEL CLASS AND CALL ITS METHOD SDFNDJH FRP H[DPSOH ZHE

LPSRUW FRP H[DPSOH PRGHO LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR LPSRUW MDYD XWLO

r the the import fois in. t DonÕt forget er xp rE ee B package that servlet, ng the original WeÕre modifayi new class. not making

SXEOLF FODVV %HHU6HOHFW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW

+WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^

UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´

3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU

RXW SULQWOQ ³%HHU 6HOHFWLRQ $GYLFH EU!´

6WULQJ F UHTXHVW JHW3DUDPHWHU ³FRORU´

%HHU([SHUW EH QHZ %HHU([SHUW

e BeerExper).t Instantiate th getBrands( class and call

/LVW UHVXOW EH JHW%UDQGV F ,WHUDWRU LW UHVXOW LWHUDWRU ZKLOH LW KDV1H[W

^ RXW SULQW Âł EU!WU\ Âł LW QH[W

` ` `

Print out the vic e (beer brand items in the Arad from the model).rayList returned version, the ad In the Ăžnal (third) e will be printed from a JSP insvic tead of the servle t.

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: stephengoss@gmx.net 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

KDQGV RQ 09&

3Ma [\MX[ NWZ [MZ^TM\ ^MZ[QWV \_W 7E HAVE TWO MAIN THINGS TO DO RECOMPILE THE SERVLET AND DEPLOY THE MODEL CLASS

&RPSLOLQJ WKH VHUYOHW :H¶OO XVH WKH VDPH FRPSLOHU FRPPDQG WKDW ZH XVHG ZKHQ ZH EXLOW WKH ¿ UVW YHUVLRQ RI WKH VHUYOHW

)LOH (GLW :LQGRZ +HOS 3OD\*R

FG EHHU9 MDYDF FODVVSDWK 8VHUV EHUW $SSOLFDWLRQV WRPFDW FRPPRQ OLE VHUYOHW DSL MDU FODVVHV G FODVVHV VUF FRP H[DPSOH ZHE %HHU6HOHFW MDYD 'HSOR\LQJ DQG WHVWLQJ WKH ZHE DSS 1RZ LQ DGGLWLRQ WR WKH VHUYOHW ZH DOVR KDYH WR GHSOR\ WKH PRGHO 7KH NH\ VWHSV DUH

WRPFDW

0RYH D FRS\ RI WKH VHUYOHW FODVV ¿ OH WR %HHU Y :(% ,1) FODVVHV FRP H[DPSOH ZHE 7KLV UHSODFHV WKH YHUVLRQ RQH VHUYOHW FODVV ¿ OH

ZHEDSSV

%HHU Y

0RYH D FRS\ RI WKH PRGHO¶V FODVV ¿ OH WR %HHU Y :(% ,1) FODVVHV FRP H[DPSOH PRGHO 6KXWGRZQ DQG UHVWDUW WRPFDW 7HVW WKH DSS YLD IRUP KWPO WKH ¿ QDO EURZVHU RXWSXW VKRXOG EH VRPHWKLQJ OLNH

%HHU 6HOHFWLRQ $GYLFH WU\ -DFN $PEHU WU\ 5HG 0RRVH

:(% ,1)

FODVVHV

)LOH (GLW :LQGRZ +HOS 6HOO+LJK

FG WRPFDW ELQ VKXWGRZQ VK ELQ VWDUWXS VK

FRP

H[DPSOH

KWWS ORFDOKRVW %HHU Y IRUP KWPO

ZHE

PRGHO

%HHU6HOHFW FODVV

%HHU([SHUW FODVV

\RX DUH KHUH

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: stephengoss@gmx.net 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

WKH 09& DSS

:M^QM_ \PM XIZ\QITTa KWUXTM\ML 5>+ JMMZ IL^QKM _MJ IXXTQKI\QWV :KDW·V ZRUNLQJ VR IDU 7KH EURZVHU VHQGV WKH UHTXHVW GDWD WR WKH &RQWDLQHU

&RQWDLQHU

:HE EURZVHU

&RQWDLQHU ORJLF

7KH &RQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ DQG SDVVHV WKH UHTXHVW WR WKH VHUYOHW

&OLHQW

7KH VHUYOHW FDOOV WKH %HHU([SHUW IRU KHOS

VHUYOHW

7KH VHUYOHW RXWSXWV WKH UHVSRQVH ZKLFK SULQWV WKH DGYLFH %HHU([SHUW FRPSRQHQW

7KH &RQWDLQHU UHWXUQV WKH SDJH WR WKH KDSS\ XVHU

7KH EURZVHU VHQGV WKH UHTXHVW GDWD WR WKH &RQWDLQHU

:KDW ZH :$17

7KH &RQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ DQG SDVVHV WKH UHTXHVW WR WKH VHUYOHW

&RQWDLQHU

7KH VHUYOHW FDOOV WKH %HHU([SHUW IRU KHOS

&OLHQW

7KH H[SHUW FODVV UHWXUQV DQ DQVZHU ZKLFK WKH VHUYOHW DGGV WR WKH UHTXHVW REMHFW

KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

UHVXOW MVS

VHUYOHW

7KH VHUYOHW IRUZDUGV WKH UHTXHVW WR WKH -63

UHTXHVW

%HHU([SHUW FRPSRQHQW

7KH -63 JHWV WKH DQVZHU IURP WKH UHTXHVW REMHFW 7KH -63 JHQHUDWHV D SDJH IRU WKH &RQWDLQHU 7KH &RQWDLQHU UHWXUQV WKH SDJH WR WKH KDSS\ 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: stephengoss@gmx.net 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

KDQGV RQ 09&

+ZMI\M \PM 2;8 š^QM_º \PI\ OQ^M[ \PM IL^QKM $ON T GET YOUR HOPES UP 9OU RE GOING TO HAVE TO WAIT FOR A FEW CHAPTERS BEFORE WE REALLY START TALKING ABOUT *30S 4HIS *30 ISN T ACTUALLY A PARTICULARLY GOOD ONE EITHER BECAUSE OF ITS SCRIPTLET CODE WHICH WE LL TALK ABOUT LATER IN THE BOOK &OR NOW IT SHOULD BE PRETTY EASY TO READ AND IF YOU WANT TO EXPERIMENT A LITTLE GO FOR IT !LTHOUGH WE COULD TEST THIS *30 NOW FROM THE BROWSER WE LL WAIT UNTIL AFTER WE MODIFY THE SERVLET VERSION THREE TO SEE IF IT WORKS

+HUH¡V WKH -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´!%HHU 5HFRPPHQGDWLRQV -63 K !

# SDJH LPSRUW ´MDYD XWLO ´ ! KWPO! ERG\! K DOLJQ S!

/LVW VW\OHV /LVW UHTXHVW JHW$WWULEXWH ³VW\OHV´ ,WHUDWRU LW VW\OHV LWHUDWRU ZKLOH LW KDV1H[W

^ RXW SULQW Âł EU!WU\ Âł LW QH[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\LQJ WKH -63

WRPFDW

:H GRQÂśW FRPSLOH WKH -63 WKH &RQWDLQHU GRHV WKDW DW Âż UVW UHTXHVW %XW ZH GR KDYH WR

ZHEDSSV

1DPH LW ³UHVXOW MVS´

%HHU Y

6DYH LW LQ WKH GHYHORSPHQW HQYLURQPHQW LQ ZHE 0RYH D FRS\ RI LW WR WKH GHSOR\PHQW HQYLURQPHQW LQ %HHU Y

!

UHVXOW MVS

KWPO! ERG\! ERG\! KWPO!

IRUP KWPO

\RX DUH KHUH

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: stephengoss@gmx.net 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

GLVSDWFKLQJ D UHTXHVW WR D -63

-VPIVKQVO \PM [MZ^TM\ \W ¹KITTº \PM 2;8 ^MZ[QWV \PZMM )N THIS STEP WE RE GOING TO MODIFY THE SERVLET TO hCALLv THE *30 TO PRODUCE THE OUTPUT VIEW 4HE #ONTAINER PROVIDES A MECHANISM CALLED hREQUEST DISPATCHINGv THAT ALLOWS ONE #ONTAINER MANAGED COMPONENT TO CALL ANOTHER AND THAT S WHAT WE LL USEˆTHE SERVLET WILL GET THE INFO FROM THE MODEL SAVE IT IN THE REQUEST OBJECT THEN DISPATCH THE REQUEST TO THE *30

7KH LPSRUWDQW FKDQJHV ZH PXVW PDNH WR WKH VHUYOHW $GG WKH PRGHO FRPSRQHQW¶V DQVZHU WR WKH UHTXHVW REMHFW VR WKDW WKH -63 FDQ DFFHVV LW 6WHS

$VN WKH &RQWDLQHU WR IRUZDUG WKH UHTXHVW WR ³UHVXOW MVS´ 6WHS

7KH &RQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ DQG SDVVHV WKH UHTXHVW WR WKH VHUYOHW

&RQWDLQHU

7KH VHUYOHW FDOOV WKH %HHU([SHUW IRU KHOS

&OLHQW

7KH H[SHUW FODVV UHWXUQV DQ DQVZHU ZKLFK WKH VHUYOHW DGGV WR WKH UHTXHVW REMHFW

KWPO! KHDG! KHDG ERG\! ERG\! KWPO!

UHVXOW MVS

7KH EURZVHU VHQGV WKH UHTXHVW GDWD WR WKH FRQWDLQHU

VHUYOHW

7KH VHUYOHW GLVSDWFKHV WR WKH -63

UHTXHVW

%HHU([SHUW FRPSRQHQW

7KH -63 JHWV WKH DQVZHU IURP WKH UHTXHVW REMHFW 7KH -63 JHQHUDWHV D SDJH IRU WKH &RQWDLQHU 7KH &RQWDLQHU UHWXUQV WKH SDJH WR WKH KDSS\ 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: stephengoss@gmx.net 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

KDQGV RQ 09&

+WLM NWZ [MZ^TM\ ^MZ[QWV \PZMM (ERE S HOW WE MODIl ED THE SERVLET TO ADD THE MODEL COMPONENT S ANSWER TO THE REQUEST OBJECT SO THE *30 CAN RETRIEVE IT AND HOW WE ASKED THE #ONTAINER TO DISPATCH TO THE *30 SDFNDJH FRP H[DPSOH ZHE LPSRUW FRP H[DPSOH PRGHO LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR LPSRUW MDYD XWLO SXEOLF FODVV %HHU6HOHFW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^

UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´

UHPRYH WKH ROG WHVW RXWSXW 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³%HHU 6HOHFWLRQ $GYLFH EU!´

6WULQJ F UHTXHVW JHW3DUDPHWHU ³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.

RXW SULQWOQ Âł EU!*RW EHHU FRORU Âł F %HHU([SHUW EH QHZ %HHU([SHUW /LVW UHVXOW EH JHW%UDQGV F

UHTXHVW VHW$WWULEXWH ³VW\OHV´ UHVXOW

Add an attribute to the request object for the JSP to use. Notice the JSP is looking for ÒstylesÓ.

5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH ` `

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. \RX DUH KHUH

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: stephengoss@gmx.net 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

FRPSLOH GHSOR\ DQG WHVW

+WUXQTM LMXTWa IVL \M[\ \PM NQVIT IXX )N THIS CHAPTER WE VE BUILT AN ENTIRE ALBEIT TINY -6# APPLICATION USING (4-, SERVLETS AND *30S 9OU CAN ADD THIS TO YOUR RESUME

&RPSLOLQJ WKH VHUYOHW :HÂśOO XVH WKH VDPH FRPSLOHU FRPPDQG WKDW ZH XVHG HDUOLHU )LOH (GLW :LQGRZ +HOS 5XQ,WV$7UDS

FG EHHU9 MDYDF FODVVSDWK 8VHUV EHUW $SSOLFDWLRQV WRPFDW FRPPRQ OLE VHUYOHW DSL MDU FODVVHV G FODVVHV VUF FRP H[DPSOH ZHE %HHU6HOHFW MDYD

'HSOR\LQJ DQG WHVWLQJ WKH ZHE DSS 1RZ LWÂśV WLPH WR UHGSOR\ WKH VHUYOHW 0RYH D FRS\ RI WKH VHUYOHWÂśV FODVV Âż OH WR %HHU Y :(% ,1) FODVVHV FRP H[DPSOH ZHE DJDLQ WKLV ZLOO UHSODFH WKH SUHYLRXV YHUVLRQ WZR FODVV Âż OH 6KXWGRZQ DQG UHVWDUW WRPFDW

)LOH (GLW :LQGRZ +HOS 6DYH<RXUVHOI

FG WRPFDW ELQ VKXWGRZQ VK ELQ VWDUWXS VK 7HVW WKH DSS YLD IRUP KWPO

HereÕs what you should

KWWS ORFDOKRVW %HHU Y IRUP KWPO

see !

%HHU 5HFRPPHQGDWLRQV -63 WU\ -DFN¡V 3DOH $OH WU\ *RXW 6WRXW

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: stephengoss@gmx.net 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

KDQGV RQ 09&

2. VR QRZ KH FDQ GR DQ 09& DSS EXW KH VWLOO KDV QR FOXH KRZ WR XVH WKH -63 H[SUHVVLRQ ODQJXDJH RU -67/ RU ZULWH D FXVWRP WDJ RU XVH D À OWHU DQG , FDXJKW KLP SOD\LQJ D :HH]HU &' DQG LW ZDV $)7(5 WKH JUHHQ DOEXP +H VWLOO KDV 62 PXFK WR OHDUQ

<PMZM Q[ [\QTT [W U]KP \W TMIZV 4HE PARTY S OVER 9OU HAD THREE WHOLE CHAPTERS TO CRUISE ALONG WRITE A LITTLE CODE REVIEW THE WHOLE (440 REQUEST RESPONSE THING "UT THERE S STILL MOCK EXAM QUESTIONS WAITING FOR YOU IN THIS BOOK AND THEY START WITH THE NEXT CHAPTER 5NLESS YOU RE ALREADY FAMILIAR WITH SERVLET DEVELOPMENT AND DEPLOYMENT YOU REALLY SHOULDN T TURN THE PAGE UNTIL AFTER YOU ACTUALLY DO THE TUTORIAL IN THIS CHAPTER .OT THAT WE RE TRYING TO PRESSURE YOU OR GUILT TRIP YOU OR ANYTHING

\RX DUH KHUH

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: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW $1' UHVSRQVH

%HLQJ D 6HUYOHW +H XVHG D *(7 UHTXHVW WR XSGDWH WKH GDWDEDVH 7KH SXQLVKPHQW ZLOO EH PRVW VHYHUH QR ´<RJD ZLWK 6X]\µ FODVVHV IRU GD\V

6HUYOHWV OLYH WR VHUYLFH FOLHQWV $ VHUYOHW¶V MRE LV WR WDNH D FOLHQW¶V UHTXHVW DQG VHQG EDFN D UHVSRQVH 7KH UHTXHVW PLJKW EH VLPSOH ³JHW PH WKH :HOFRPH SDJH ´ 2U LW PLJKW EH FRPSOH[ ³&RPSOHWH P\ VKRSSLQJ FDUW FKHFN RXW ´ 7KH UHTXHVW FDUULHV FUXFLDO GDWD DQG \RXU VHUYOHW FRGH KDV WR NQRZ KRZ WR ¿ QG LW DQG KRZ WR XVH LW 7KH UHVSRQVH FDUULHV WKH LQIR WKH EURZVHU QHHGV WR UHQGHU D SDJH RU GRZQORDG E\WHV DQG \RXU VHUYOHW FRGH KDV WR NQRZ KRZ WR VHQG LW 2U QRW \RXU VHUYOHW FDQ GHFLGH WR SDVV WKH UHTXHVW WR VRPHWKLQJ HOVH DQRWKHU SDJH VHUYOHW RU -63 LQVWHDG

WKLV LV D QHZ FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

RI¿FLDO 6XQ H[DP REMHFWLYHV

7KH 6HUYOHW 7HFKQRORJ\ 0RGHO

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 )RU HDFK RI WKH +773 0HWKRGV VXFK DV

*(7 3267 +($' DQG VR RQ GHVFULEH WKH SXUSRVH RI WKH PHWKRG DQG WKH WHFKQLFDO FKDUDFWHULVWLFV RI WKH +773 0HWKRG SURWRFRO OLVW WULJJHUV WKDW PLJKW FDXVH D FOLHQW XVXDOO\ D :HE EURZVHU WR XVH WKH 0HWKRG DQG LGHQWLI\ WKH +WWS6HUYOHW PHWKRG WKDW FRUUHVSRQGV WR WKH +773 0HWKRG

8VLQJ WKH +WWS6HUYOHW5HTXHVW LQWHUIDFH ZULWH FRGH WR UHWULHYH +70/ IRUP SDUDPHWHUV IURP WKH UHTXHVW UHWULHYH +773 UHTXHVW KHDGHU LQIRUPDWLRQ RU UHWULHYH FRRNLHV IURP WKH UHTXHVW

8VLQJ WKH +WWS6HUYOHW5HVSRQVH LQWHUIDFH ZULWH FRGH WR VHW DQ +773 UHVSRQVH KHDGHU VHW WKH FRQWHQW W\SH RI WKH UHVSRQVH DFTXLUH D WH[W VWUHDP IRU WKH UHVSRQVH DFTXLUH D ELQDU\ VWUHDP IRU WKH UHVSRQVH UHGLUHFW DQ +773 UHTXHVW WR DQRWKHU 85/ RU DGG FRRNLHV WR WKH UHVSRQVH

'HVFULEH WKH SXUSRVH DQG HYHQW VHTXHQFH RI WKH VHUYOHW OLIHF\FOH VHUYOHW FODVV ORDGLQJ VHUYOHW LQVWDQWLDWLRQ FDOO WKH LQLW PHWKRG FDOO WKH VHUYLFH PHWKRG DQG FDOO WKH GHVWUR\ PHWKRG

!LL OF THE OBJECTIVES IN THIS SECTION ARE COVERED COMPLETELY IN THIS CHAPTER WITH THE EXCEPTION OF THE COOKIES PART OF OBJECTIVE ! LOT OF THE CONTENT IN THIS CHAPTER WAS TOUCHED ON IN CHAPTER TWO BUT IN CHAPTER TWO WE SAID h$ON T WORRY ABOUT MEMORIZING IT v )N THIS CHAPTER YOU $/ HAVE TO SLOW DOWN REALLY STUDY AND MEMORIZE THE CONTENT .O OTHER CHAPTER WILL COVER THESE OBJECTIVES IN DETAIL SO THIS IS IT $O THE EXERCISES IN THE CHAPTER REVIEW THE MATERIAL THEN TAKE YOUR lRST MOCK EXAM AT THE END OF THE CHAPTER )F YOU DON T GET AT LEAST CORRECT GO BACK THROUGH THE CHAPTER TO lGURE OUT WHAT YOU MISSED "%&/2% YOU MOVE ON TO CHAPTER lVE 3OME OF THE MOCK EXAM QUESTIONS THAT BELONG WITH THESE OBJECTIVES HAVE BEEN MOVED INTO CHAPTERS AND BECAUSE THE QUESTIONS REQUIRES ADDITIONAL KNOWLEDGE OF SOME OF THE TOPICS WE DON T COVER UNTIL THOSE CHAPTERS 4HAT MEANS THERE ARE FEWER MOCK EXAM QUESTIONS IN THIS CHAPTER AND MORE IN LATER CHAPTERS TO AVOID TESTING YOU ON TOPICS YOU HAVEN T COVERED )MPORTANT NOTE WHILE THE lRST THREE CHAPTERS COVERED BACKGROUND INFORMATION FROM THIS PAGE FORWARD IN THE BOOK VIRTUALLY EVERYTHING YOU RE GOING TO SEE IS DIRECTLY RELATED TO OR EXPLICITLY PART OF THE EXAM

:H ZRQ·W VD\ PXFK DERXW WKH REMHFWLYHV UHODWHG WR FRRNLHV XQWLO WKH 6HVVLRQV FKDSWHU

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

;MZ ^TM\[ IZM KWV\ZWTTML Ja \PM +WV\IQVMZ )N CHAPTER TWO WE LOOKED AT THE #ONTAINER S OVERALL ROLE IN A SERVLET S LIFEˆIT CREATES THE REQUEST AND RESPONSE OBJECTS CREATES OR ALLOCATES A NEW THREAD FOR THE SERVLET AND CALLS THE SERVLET S SERVICE METHOD PASSING THE REQUEST AND RESPONSE REFERENCES AS ARGUMENTS (ERE S A QUICK REVIEW

*(7

:HE EURZVHU

8VHU FOLFNV D OLQN WKDW KDV D 85/ WR D VHUYOHW

VHUYOHW

FRQWDLQHU &OLHQW

7KH &RQWDLQHU ´VHHVµ WKDW WKH UHTXHVW LV IRU D VHUYOHW VR WKH FRQWDLQHU FUHDWHV WZR REMHFWV

:HE EURZVHU

VHUYOHW

FRQWDLQHU UHVSRQVH

&OLHQW

+WWS6HUYOHW5HVSRQVH +WWS6HUYOHW5HTXHVW

UHTXHVW

:HE EURZVHU

FRQWDLQHU

VHUYLF H UHTX HVW U HVSRQ VH

VHUYOHW

&OLHQW

thread

7KH &RQWDLQHU ILQGV WKH FRUUHFW VHUYOHW EDVHG RQ WKH 85/ LQ WKH UHTXHVW FUHDWHV RU DOORFDWHV D WKUHDG IRU WKDW UHTXHVW DQG FDOOV WKH VHUYOHW·V VHUYLFH PHWKRG SDVVLQJ WKH UHTXHVW DQG UHVSRQVH REMHFWV DV DUJXPHQWV

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

6HUYOHWV DQG WKH &RQWDLQHU

<PM [\WZa KWV\QV]M[

:HE EURZVHU

VHUYOHW

FRQWDLQHU &OLHQW

GR*HW UHTXHVW UHVSRQVH

:HE EURZVHU

+773 2. KWPO! KHDG!

FRQWDLQHU &OLHQW

KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

VHUYOHW

7KH VHUYLFH PHWKRG ILJXUHV RXW ZKLFK VHUYOHW PHWKRG WR FDOO EDVHG RQ WKH +773 0HWKRG *(7 3267 HWF VHQW E\ WKH FOLHQW 7KH FOLHQW VHQW DQ +773 *(7 UHTXHVW VR WKH VHUYLFH PHWKRG FDOOV WKH VHUYOHW·V GR*HW PHWKRG SDVVLQJ WKH UHTXHVW DQG UHVSRQVH REMHFWV DV DUJXPHQWV

7KH VHUYOHW XVHV WKH UHVSRQVH REMHFW WR ZULWH RXW WKH UHVSRQVH WR WKH FOLHQW 7KH UHVSRQVH JRHV EDFN WKURXJK WKH &RQWDLQHU

UHVSRQVH UHTXHVW

+773 2. KWPO! KHDG!

:HE EURZVHU

KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

FRQWDLQHU

VHUYOHW

no thread

&OLHQW

UHTXHVW

UHVSRQVH

7KH VHUYLFH PHWKRG FRPSOHWHV VR WKH WKUHDG HLWKHU GLHV RU UHWXUQV WR D &RQWDLQHU PDQDJHG WKUHDG SRRO 7KH UHTXHVW DQG UHVSRQVH REMHFW UHIHUHQFHV IDOO RXW RI VFRSH VR WKHVH REMHFWV DUH WRDVW UHDG\ IRU JDUEDJH FROOHFWLRQ 7KH FOLHQW JHWV WKH UHVSRQVH

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

*]\ \PMZMÂź[ UWZM \W I [MZ ^TM\Âź[ TQNM

GRHV QRW H[LVW

7E STEPPED INTO THE MIDDLE OF THE SERVLET S LIFE BUT THAT STILL LEAVES QUESTIONS WHEN WAS THE SERVLET CLASS LOADED 7HEN DID THE SERVLET S CONSTRUCTOR RUN (OW LONG DOES THE SERVLET OBJECT LIVE 7HEN SHOULD YOUR SERVLET INITIALIZE RESOURCES 7HEN SHOULD IT CLEAN UP ITS RESOURCES

FRQVWUXFWRU LQLW

4HE SERVLET LIFECYCLE IS SIMPLE THERE S ONLY ONE MAIN STATEˆINITIALIZED )F THE SERVLET ISN T INITIALIZED THEN IT S EITHER BEING INITIALIZED RUNNING ITS CONSTRUCTOR OR INIT METHOD BEING DESTROYED RUNNING ITS DESTROY METHOD OR IT SIMPLY DOES NOT EXIST

GHVWUR\

LQLWLDOL]HG

VHUYLFH

:HE &RQWDLQHU

6HUYOHW &ODVV

6HUYOHW 2EMHFW

&RQWDLQHU

/RDG FODVV

runs constructor;r just ass no-arag co to cl uc t le tr rv ns se r You . OT write (you shouldmNpiler-supplied default) co e th e us

$6HUYOHW FODVV

,QVWDQWLDWH VHUYOHW FRQVWUXFWRU UXQV

Called only ONCE in servletÕs life, and must completthebefo re Container can call serveice( ).

LQLW

VHUYLFH

GHVWUR\

This is where the servlet spends most of its life.

LQLWLDOL]HG

LQLWLDOL]HG

KDQGOH FOLHQW UHTXHVWV

doGet(), doPost(), etc. t runs in a (Each requteesthread .) ra pa se

Container calls to give the serv let a chance to clean up befo re the servlet is killed (i.e., mad e read garbage collection). Like init(), y for itÕs called only once. \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

WKH 6HUYOHW $3,

AW]Z [MZ ^TM\ QVPMZQ\[ \PM TQNMKaKTM UM\PWL[ LQWHUIDFH!!

6HUYOHW VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

LQLW 6HUYOHW&RQILJ

GHVWUR\

JHW6HUYOHW&RQILJ

JHW6HUYOHW,QIR

*HQHULF6HUYOHW VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

LQLW 6HUYOHW&RQILJ

LQLW

GHVWUR\

JHW6HUYOHW&RQILJ

JHW6HUYOHW,QIR

JHW,QLW3DUDPHWHU 6WULQJ

JHW,QLW3DUDPHWHU1DPHV

JHW6HUYOHW&RQWH[W

ORJ 6WULQJ

ORJ 6WULQJ 7KURZDEOH

+WWS6HUYOHW VHUYLFH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

GR*HW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR3RVW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR+HDG +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR2SWLRQV +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR3XW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR7UDFH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR'HOHWH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

JHW/DVW0RGLILHG +WWS6HUYOHW5HTXHVW

0\6HUYOHW GR3RVW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

P\%L]0HWKRG

6HUYOHW LQWHUIDFH MDYD[ VHUYOHW 6HUYOHW

T try to NOTE: do NlOof these memorize al t a feel for now ! Just geI works... how the AP

The Servlet interface says that all servlets have the Ăžve methods (the three inse bold are lifecycle methods).

*HQHULF6HUYOHW FODVV MDYD[ VHUYOHW *HQHULF6HUYOHW

GenericServlet is an abstract class that implements most of the basi methods youÕll need, includingc servlet the Servlet interface. You willthose from probably NEVER extend this class you of your servletÕs Òservlet beh rself. Most aviorÓ comes from this class.

+WWS6HUYOHW FODVV MDYD[ VHUYOHW KWWS +WWS6HUYOHW

tract class) implementss HttpServlet (also an abs reĂ&#x;ect the HTTPnes to the service() method service() method doesnĂ•t of the servlet--the vlet request and response, take just ANY old serc request and response. but an HTTP-speciĂž

0\6HUYOHW FODVV FRP ZLFNHGO\VPDUW IRR

Most of your servletness is handled by superclass methods. All you do is override the HTTP methods you need.

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

<PM <PZMM *QO 4QNMKaKTM 5WUMV\[

LQLW

VHUYLFH

GR*HW

DQG RU

GR3RVW

:KHQ LW·V FDOOHG

:KDW LW·V IRU

'R \RX RYHUULGH LW"

7KH &RQWDLQHU FDOOV LQLW RQ WKH VHUYOHW LQVWDQFH DIWHU WKH VHUYOHW LQVWDQFH LV FUHDWHG EXW EHIRUH WKH VHUYOHW FDQ VHUYLFH DQ\ FOLHQW UHTXHVWV

*LYHV \RX D FKDQFH WR LQLWLDOL]H \RXU VHUYOHW EHIRUH KDQGOLQJ DQ\ FOLHQW UHTXHVWV

3RVVLEO\ ,I \RX KDYH LQLWLDOL]DWLRQ FRGH OLNH JHWWLQJ D GDWDEDVH FRQ QHFWLRQ RU UHJLVWHULQJ \RXUVHOI ZLWK RWKHU REMHFWV WKHQ \RX·OO RYHUULGH WKH LQLW PHWKRG LQ \RXU VHUYOHW FODVV

:KHQ LW·V FDOOHG

:KDW LW·V IRU

'R \RX RYHUULGH LW"

:KHQ WKH ILUVW FOLHQW UHTXHVW FRPHV LQ WKH &RQWDLQHU VWDUWV D QHZ WKUHDG RU DOORFDWHV D WKUHDG IURP WKH SRRO DQG FDXVHV WKH VHUYOHW·V VHUYLFH PHWKRG WR EH LQYRNHG

7KLV PHWKRG ORRNV DW WKH UHTXHVW GHWHUPLQHV WKH +773 PHWKRG *(7 3267 HWF DQG LQYRNHV WKH PDWFKLQJ GR*HW GR3RVW HWF RQ WKH VHUYOHW

1R 9HU\ XQOLNHO\ <RX VKRXOG 127 RYHUULGH WKH VHUYLFH PHWKRG <RXU MRE LV WR RYHUULGH WKH GR*HW DQG RU GR3RVW PHWKRGV DQG OHW WKH VHUYLFH LPSOHPHQWDWLRQ IURP +7736HUYOHW ZRUU\ DERXW FDOOLQJ WKH ULJKW RQH

:KHQ LW·V FDOOHG

:KDW LW·V IRU

'R \RX RYHUULGH LW"

7KH VHUYLFH PHWKRG LQYRNHV GR*HW RU GR3RVW EDVHG RQ WKH +773 PHWKRG *(7 3267 HWF IURP WKH UHTXHVW

7KLV LV ZKHUH \RXU FRGH EHJLQV 7KLV LV WKH PHWKRG WKDW·V UHVSRQVLEOH IRU ZKDWHYHU WKH KHFN \RXU ZHE DSS LV VXS SRVHG WR EH '2,1*

$/:$<6 DW OHDVW 21( RI WKHP GR*HW RU GR3RVW

:H·UH LQFOXGLQJ RQO\ GR*HW DQG GR3RVW KHUH EHFDXVH WKRVH WZR DUH SUREDEO\ WKH RQO\ RQHV \RX·OO HYHU XVH

<RX FDQ FDOO RWKHU PHWKRGV RQ RWKHU REMHFWV RI FRXUVH EXW LW DOO VWDUWV IURP KHUH

:KLFKHYHU RQH V \RX RYHUULGH WHOOV WKH &RQWDLQHU ZKDW \RX VXSSRUW ,I \RX GRQ·W RYHUULGH GR3RVW IRU H[DPSOH WKHQ \RX·UH WHOOLQJ WKH &RQWDLQHU WKDW WKLV VHUYOHW GRHV QRW VXSSRUW +773 3267 UHTXHVWV

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

VHUYOHW WKUHDGV

, WKLQN , JRW WKLV VR WKH &RQWDLQHU FDOOV P\ VHUYOHW·V LQLW PHWKRG EXW LI , GRQ·W RYHUULGH LQLW WKH RQH IURP *HQHULF6HUYOHW UXQV 7KHQ ZKHQ D UHTXHVW FRPHV LQ WKH &RQWDLQHU VWDUWV RU DOORFDWHV D WKUHDG DQG FDOOV WKH VHUYLFH PHWKRG ZKLFK , GRQ·W RYHUULGH VR WKH VHUYLFH PHWKRG IURP +WWS6HUYOHW UXQV 7KH +WWS6HUYOHW VHUYLFH PHWKRG WKHQ FDOOV P\ RYHUULGGHQ GR*HW RU GR3RVW 6R HDFK WLPH P\ GR*HW RU GR3RVW UXQV LW·V LQ D VHSDUDWH PHWKRG

7KH VHUYLFH PHWKRG LV DOZD\V FDOOHG LQ LWV RZQ VWDFN 6HUYOHW LQLWLDOL]DWLRQ

LQLW

&OLHQW UHTXHVW

&OLHQW UHTXHVW

GR*HW

GR*HW

VHUYLFH

VHUYLFH

7KUHDG $

7KUHDG %

4HE #ONTAINER CALLS INIT ON THE SERVLET INSTANCE AFTER THE SERVLET INSTANCE IS CREATED BUT BEFORE THE SERVLET CAN SERVICE ANY CLIENT REQUESTS

7HEN THE lRST CLIENT REQUEST COMES IN THE #ONTAINER STARTS OR lNDS A THREAD AND CAUSES THE SERVLET S SERVICE METHOD TO BE INVOKED

)F YOU HAVE INITIALIZATION CODE LIKE GETTING A DATABASE CONNECTION OR REGISTERING YOURSELF WITH OTHER OBJECTS THEN YOU LL OVERRIDE THE INIT METHOD IN YOUR SERVLET CLASS /THERWISE THE INIT METHOD FROM 'ENERIC3ERVLET RUNS

9OU NORMALLY WILL ./4 OVERRIDE THE SERVICE METHOD SO THE ONE FROM (TTP3ERVLET WILL RUN 4HE SERVICE METHOD lGURES OUT WHICH (440 METHOD '%4 0/34 ETC IS IN THE REQUEST AND INVOKES THE MATCHING DO'ET OR DO0OST METHOD 4HE DO'ET AND DO0OST INSIDE (TTP3ERVLET DON T DO ANYTHING SO YOU HAVE TO OVERRIDE ONE OR BOTH 4HIS THREAD DIES OR IS PUT BACK IN A #ONTAINER MANAGED POOL WHEN SERVICE COMPLETES

7KUHDG & 7HEN THE SECOND AND ALL OTHER CLIENT REQUESTS COME IN THE #ONTAINER AGAIN CREATES OR lNDS A ANOTHER THREAD AND CAUSES THE SERVLET S SERVICE METHOD TO BE INVOKED 3O THE SERVICE DO'ET METHOD SEQUENCE HAPPENS EACH TIME THERE S A CLIENT REQUEST !T ANY GIVEN TIME YOU LL HAVE AT LEAST AS MANY RUNNABLE THREADS AS THERE ARE CLIENT REQUESTS LIMITED BY THE RESOURCES OR POLICIES CONlGURATION OF THE #ONTAINER 9OU MIGHT FOR EXAMPLE HAVE A #ONTAINER THAT LETS YOU SPECIFY THE MAXIMUM NUMBER OF SIMULTANEOUS THREADS AND WHEN THE NUMBER OF CLIENT REQUESTS EXCEEDS THAT SOME CLIENTS WILL JUST HAVE TO WAIT

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

-IKP ZMY]M[\ Z]V[ QV I [MXIZI\M \ PZMIL 9OU MIGHT HEAR PEOPLE SAY THINGS LIKE h%ACH INSTANCE OF THE SERVLET v BUT THAT S JUST WRONG 4HERE AREN T MULTIPLE INSTANCES OF ANY SERVLET CLASS EXCEPT IN ONE SPECIAL CASE CALLED 3INGLE4HREAD-ODEL WHICH IS INHERENTLY EVIL BUT WE RE NOT TALKING ABOUT THAT SPECIAL CASE YET

4HE #ONTAINER RUNS MULTIPLE THREADS TO PROCESS MULTIPLE REQUESTS TO A SINGLE SERVLET !ND EVERY CLIENT REQUEST GENERATES A NEW PAIR OF REQUEST AND RESPONSE OBJECTS &RQWDLQHU &OLHQW $

+77 3 UHT XHVW

TXHVW +773 UH

&OLHQW % :HE EURZVHU

:HE EURZVHU

6HUYOHW

WKUHDG %

WKUHDG $

UHVSRQVH

UHTXHVW

UHTXHVW

UHVSRQVH

e Each client gets a separat and the thread for each request,req Container allocates new uest and response objects.

WKHUH DUH QR

'XPE 4XHVWLRQV

4

4HIS IS CONFUSING IN THE PICTURE ABOVE YOU SHOW TWO DIFFER ENT CLIENTS EACH WITH ITS OWN THREAD 7HAT HAPPENS IF THE SAME CLIENT MAKES MULTIPLE REQUESTS )S IT ONE THREAD PER CLIENT OR ONE THREAD PER REQUEST

$

/NE THREAD PER REQUEST 4HE #ONTAINER DOESN T CARE WHO MAKES THE REQUESTˆEVERY INCOMING REQUEST MEANS A NEW THREAD STACK

4

7HAT IF THE #ONTAINER USES CLUSTERING AND DISTRIBUTES THE APP ON MORE THAN ONE *6-

$

)MAGINE THE PICTURE ABOVE IS FOR A SINGLE *6- AND EACH *6- HAS THE SAME PICTURE 3O FOR A DISTRIBUTED WEB APP THERE WOULD BE ONE INSTANCE OF A PARTICULAR SERVLET PER *6- BUT EACH *6- WOULD STILL HAVE ONLY A SINGLE INSTANCE OF THAT SERVLET

4

) NOTICED THAT (TTP3ERVLET IS IN A DIFFERENT PACKAGE FROM 'ENERIC3ERVLET HOW MANY SERVLET PACKAGES ARE THERE

$

%VERYTHING RELATED TO SERVLETS BUT EXCLUDING *30 STUFF IS IN EITHER JAVAX SERVLET OR JAVAX SERVLET HTTP !ND IT S EASY TO TELL THE DIFFERENCE THINGS THAT HAVE TO DO WITH (440 IS IN THE JAVAX SERVLET HTTP PACKAGE AND THE REST GENERIC SERVLET CLASSES AND INTERFACES ARE IN JAVAX SERVLET 7E LL SEE *30 RELATED CHAPTERS LATER IN THE BOOK \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

VHUYOHW LQLWLDOL]DWLRQ

1V \PM JMOQVVQVO" TWILQVO IVL QVQ\QITQbQVO 4HE SERVLET STARTS LIFE WHEN THE #ONTAINER lNDS THE SERVLET CLASS lLE 4HIS VIRTUALLY ALWAYS HAPPENS WHEN THE #ONTAINER STARTS UP FOR EXAMPLE WHEN YOU RUN 4OMCAT 7HEN THE #ONTAINER STARTS IT LOOKS FOR DEPLOYED WEB APPS AND THEN STARTS SEARCHING FOR SERVLET CLASS lLES )N THE $EPLOYMENT CHAPTER WE LL GO INTO MORE DETAILS OF HOW WHY AND WHERE THE #ONTAINER LOOKS FOR SERVLETS &INDING THE CLASS IS THE lRST STEP ,OADING THE CLASS IS THE SECOND STEP AND IT HAPPENS EITHER ON #ONTAINER STARTUP OR lRST CLIENT USE 9OUR #ONTAINER MIGHT GIVE YOU A CHOICE ABOUT CLASS LOADING OR IT MIGHT LOAD THE CLASS WHENEVER IT WANTS 2EGARDLESS OF WHETHER YOUR #ONTAINER GETS THE SERVLET READY EARLY OR DOES IT JUST IN TIME WHEN THE lRST CLIENT NEEDS IT A SERVLET S SERVICE METHOD WILL NOT RUN UNTIL THE SERVLET IS FULLY INITIALIZED

<RXU VHUYOHW LV DOZD\V ORDGHG DQG LQLWLDOL]HG %()25( LW FDQ VHUYLFH LWV ILUVW FOLHQW UHTXHVW

LQLW DOZD\V FRPSOHWHV EHIRUH WKH ÀUVW FDOO WR VHUYLFH

# 4X3 X9 8 :K\ LV WKHUH DQ LQLW PHWKRG" ,Q RWKHU ZRUGV ZK\ LVQ¶W WKH FRQVWUXFWRU HQRXJK IRU LQLWLDOL]LQJ D VHUYOHW" :KDW NLQG RI FRGH ZRXOG \RX SXW LQ WKH LQLW PHWKRG" +LQW WKH LQLW PHWKRG WDNHV DQ REMHFW UHIHUHQFH DUJXPHQW :KDW GR \RX WKLQN WKH DUJXPHQW WR WKH LQLW PHWKRG PLJKW EH DQG KRZ RU ZK\ ZRXOG \RX XVH LW"

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

GRHV QRW H[LVW

FRQVWUXFWRU LQLW

;MZ ^TM\ 1VQ\QITQbI\QWV" _PMV IV WJRMK\ JMKWUM[ I [MZ ^TM\ 7KH SURXGHVW PRPHQW RI P\ OLIH LV ZKHQ WKH *UDQG 0DVWHU &RQWDLQHU PDNHV PH D VHUYOHW E\ PDNLQJ D 6HUYOHW&RQĂ€ J IRU PH DQG FDOOLQJ P\ LQLW %HIRUH WKDW ,¡P MXVW DQ RUGLQDU\ REMHFW %XW DV D VHUYOHW , KDYH VSHFLDO SULYLOHJHV EHVLGHV WKH VHFUHW KDQGVKDNH OLNH WKH DELOLW\ WR ORJ HYHQWV JHW UHIHUHQFHV WR RWKHU UHVRXUFHV DQG VWRUH DWWULEXWHV IRU RWKHU VHUYOHWV

GHVWUR\

LQLWLDOL]HG

VHUYLFH

e, a servletÕs lif only once inÕt try to do ns ru () it in The it! And donnstructor is too so donÕt blow ... the co . on so o to things o servlet-speciÞc things d early to ! SERVLET MOVES FROM DOES NOT EXIST TO INITIALIZED WHICH REALLY MEANS READY TO SERVICE CLIENT REQUESTS BEGINNING WITH A CONSTRUCTOR "UT THE CONSTRUCTOR MAKES ONLY AN OBJECT NOT A SERVLET 4O BE A SERVLET THE OBJECT NEEDS TO BE GRANTED SERVLETNESS 7HEN AN OBJECT BECOMES A SERVLET IT GETS ALL THE UNIQUE PRIVILEGES THAT COME WITH BEING A SERVLET LIKE THE ABILITY TO USE ITS 3ERVLET#ONTEXT REFERENCE TO GET INFORMATION FROM THE #ONTAINER

7HY DO WE CARE ABOUT INITIALIZATION DETAILS "ECAUSE SOMEWHERE BETWEEN THE CONSTRUCTOR AND THE INIT METHOD THE SERVLET IS IN A 3CHROEDINGER S SERVLET STATE 9OU MIGHT HAVE SERVLET INITIALIZATION CODE LIKE GETTING WEB APP CONl GURATION INFO OR LOOKING UP A REFERENCE TO ANOTHER PART OF THE APPLICATION THAT WILL FAIL IF YOU RUN IT TOO EARLY IN THE SERVLET S LIFE )T S PRETTY SIMPLE THOUGH IF YOU REMEMBER TO PUT NOTHING IN THE SERVLET S CONSTRUCTOR 4HERE S NOTHING THAT CAN T WAIT UNTIL INIT ,I \RXU TXDQWXP PHFKDQLFV LV D OLWWOH UXVW\³\RX PLJKW ZDQW WR GR D *RRJOH VHDUFK RQ ´6FKURHGLQJHU¡V &DW¾ :DUQLQJ SHW ORYHUV MXVW GRQ¡W JR WKHUH :KHQ ZH UHIHU WR D 6FKURHGLQJHU VWDWH ZH PHDQ VRPHWKLQJ WKDW LV QHLWKHU IXOO\ GHDG RU IXOO\ DOLYH EXW LQ VRPH UHDOO\ ZHLUG SODFH LQ EHWZHHQ \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

6HUYOHW&RQÂż J DQG 6HUYOHW&RQWH[W

:KDW GRHV œEHLQJ D VHUYOHW¡ EX\ \RX" :KDW KDSSHQV ZKHQ D VHUYOHW JRHV IURP WKLV

WR WKLV"

arrying servlet

object

ofĂžcial, card-c

:DWFK LW

ƒ 2QH 6HUYOHW&RQILJ REMHFW SHU VHUYOHW 'RQ¡W FRQIXVH 6HU YOHW&RQĂ€ J SDUDPHWHUV ZLWK 6HU YOHW&RQWH[W SDUDPHWHUV

KHVH XQWLO :H GRQœW UHDOO\ WDON DERXW W \ SHRSOH WKH QH[W FKDSWHU EXW VR PDQ ZDQW WR JHW WKHP FRQIXVHG WKDW ZH WWHQWLRQ WR SODQW WKH VHHG HDUO\ SD\ D WKH GLIIHUHQFHV HV 6WDUW E\ ORRNLQJ DW WKH QDP UG ³FRQ¿ J´ LQ 6HUYOHW&RQ¿ J KDV WKH ZR XW GHSOR\ LW IRU ³FRQ¿ JXUDWLRQ´ ,WœV DER G IRU WKH WLPH YDOXHV \RXœYH FRQ¿ JXUH LQJV \RXU VHUYOHW RQH SHU VHUYOHW 7K V WKDW \RX VHUYOHW PLJKW ZDQW WR DFFHV H PD\EH D GRQœW ZDQW WR KDUG FRGH OLN GDWDEDVH QDPH ZRQœW 6HUYOHW&RQ¿ J SDUDPHWHUV HUYOHW LV FKDQJH IRU DV ORQJ DV WKLV V KDQJH GHSOR\HG DQG UXQQLQJ 7R F WKH WKHP \RXœOO KDYH WR UHGHSOR\ VHUYOHW H EHHQ 6HUYOHW&RQWH[W VKRXOG KDY H\ GLGQœW QDPHG $SS&RQWH[W EXW WK œV RQO\ RQH OLVWHQ WR XV EHFDXVH WKHUH HU VHUYOHW SHU ZHE DSS 127 RQH S LQ WKH $Q\ZD\ ZHœOO JHW LQWR DOO WKLV KHDGV XS QH[W FKDSWHU²WKLV LV MXVW D

$ 6HUYOHW&RQĂ€ J REMHFW

ƒ 8VH LW WR SDVV GHSOR\ WLPH LQIRUPDWLRQ WR WKH VHUYOHW D GDWDEDVH RU HQWHUSULVH EHDQ ORRNXS QDPH IRU H[DPSOH WKDW \RX GRQ¡W ZDQW WR KDUG FRGH LQWR WKH VHUYOHW VHUYOHW LQLW SDUDPHWHUV

ƒ 8VH LW WR DFFHVV WKH 6HUYOHW&RQWH[W ƒ 3DUDPHWHUV DUH FRQILJXUHG LQ WKH 'HSOR\PHQW 'HVFULSWRU

$ 6HUYOHW&RQWH[W

ƒ 2QH 6HUYOHW&RQWH[W SHU ZHE DSS 7KH\ VKRXOG KDYH QDPHG LW $SS&RQWH[W

ƒ 8VH LW WR DFFHVV ZHE DSS SDUDPHWHUV DOVR FRQILJXUHG LQ WKH 'HSOR\PHQW 'HVFULSWRU

ƒ 8VH LW DV D NLQG RI DSSOLFDWLRQ EXOOHWLQ ERDUG ZKHUH \RX FDQ SXW XS PHVVDJHV FDOOHG DWWULEXWHV WKDW RWKHU SDUWV RI WKH DSSOLFDWLRQ FDQ DFFHVV ZD\ PRUH RQ WKLV LQ WKH QH[W FKDSWHU

ƒ 8VH LW WR JHW VHUYHU LQIR LQFOXGLQJ WKH QDPH DQG YHUVLRQ RI WKH &RQWDLQHU DQG WKH YHUVLRQ RI WKH $3, WKDW¡V VXSSRUWHG

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

*]\ I ;MZ ^TM\¼[ :-)4 RWJ Q[ \W PIVLTM ZMY]M[\[ <PI\¼[ _PMV I [MZ ^TM\¼[ TQNM PI[ UMIVQVO )N THE NEXT CHAPTER WE LL LOOK AT 3ERVLET#ONl G AND 3ERVLET#ONTEXT BUT FOR NOW WE RE DIGGING INTO DETAILS OF THE REQUEST AND RESPONSE "ECAUSE THE 3ERVLET#ONl G AND 3ERVLET#ONTEXT EXIST ONLY TO SUPPORT YOUR SERVLET S /NE 4RUE *OB TO HANDLE CLIENT REQUESTS 3O BEFORE WE LOOK AT HOW YOUR CONTEXT AND CONl G OBJECTS CAN HELP YOU DO YOUR JOB WE HAVE TO BACK UP A LITTLE AND LOOK AT THE FUNDAMENTALS OF THE REQUEST AND RESPONSE 9OU ALREADY KNOW THAT YOU RE HANDED A REQUEST AND RESPONSE AS ARGUMENTS TO THE DO'ET OR DO0OST METHOD BUT WHAT POWERS DO THOSE REQUEST AND RESPONSE OBJECTS GIVE YOU 7HAT CAN YOU DO WITH THEM AND WHY DO YOU CARE

6KDUSHQ \RXU SHQFLO /DEHO WKH PLVVLQJ SLHFHV WKH HPSW\ ER[HV RI WKLV OLIHF\FOH WLPHOLQH &KHFN \RXU DQVZHUV ZLWK WKH WLPHOLQH VKRZQ HDUOLHU LQ WKLV FKDSWHU

:HE &RQWDLQHU

6HUYOHW &ODVV

6HUYOHW 2EMHFW

&RQWDLQHU

/RDG FODVV

$GG \RXU RZQ DQQRWDWLRQV DV ZHOO WR KHOS \RX UHPHPEHU WKH GHWDLOV

$6HUYOHW FODVV

LQLW

VHUYLFH

GHVWUR\

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

5HTXHVW DQG 5HVSRQVH

MY]M[\ IVL : M[XWV[M" \PM SMa \W M^MZa\PQVO : IVL \PM IZO]UMV\[ \W [MZ ^QKM 6HUYOHW5HTXHVW LQWHUIDFH

6HUYOHW5HVSRQVH LQWHUIDFH

MDYD[ VHUYOHW 6HUYOHW5HTXHVW

MDYD[ VHUYOHW 6HUYOHW5HVSRQVH

LQWHUIDFH!!

LQWHUIDFH!!

6HUYOHW5HTXHVW

6HUYOHW5HVSRQVH

JHW$WWULEXWH 6WULQJ

JHW%XIIHU6L]H

JHW&RQWHQW/HQJWK

VHW&RQWHQW7\SH

JHW,QSXW6WUHDP

JHW2XWSXW6WUHDP

JHW/RFDO3RUW

JHW:ULWHU

JHW3DUDPHWHU

VHW&RQWHQW7\SH

JHW3DUDPHWHU1DPHV

0$1< PRUH PHWKRGV

0$1< PRUH PHWKRGV

+WWS6HUYOHW5HVSRQVH LQWHUIDFH +WWS6HUYOHW5HTXHVW LQWHUIDFH

MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HVSRQVH

MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HTXHVW

LQWHUIDFH!!

+WWS6HUYOHW5HVSRQVH

LQWHUIDFH!!

+WWS6HUYOHW5HTXHVW

DGG&RRNLH

JHW&RQWH[W3DWK

DGG+HDGHU

JHW&RRNLHV

HQFRGH5HGLUHFW85/

JHW+HDGHU 6WULQJ

VHQG(UURU

JHW4XHU\6WULQJ

VHW6WDWXV

JHW6HVVLRQ

0$1< PRUH PHWKRGV

JHW0HWKRG

0$1< PRUH PHWKRGV

est methods The HttpServletRequngs like cookies, are about HTTP thi s. sion ses and s, der hea ace adds HttpServletRequest intteerfto the HTTP rela t tha s hod the met vlet uses to protocol... what your ser nt/browser. clie the h wit e communicat

Same thing with e response... the HttpServletRespoth care about when yonseuÕradds methods you things like errors, coo e using HTTPÑ kies, and headers.

7KH UHTXHVW DQG UHVSRQVH REMHFWV DUH DOVR DUJXPHQWV WR WKH RWKHU +WWS6HUYOHW PHWKRGV WKDW \RX ZULWH² GR*HW GR3RVW HWF

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

WKHUH DUH QR

'XPE 4XHVWLRQV

4

7HO IMPLEMENTS THE INTERFACES FOR (TTP3ERVLET2EQUEST AND (TTP3ERVLET2ESPONSE !RE THOSE CLASSES IN THE !0)

$

4HE #ONTAINER AND .O 4HE CLASSES AREN T IN THE !0) BECAUSE THEY RE LEFT TO THE VENDOR TO IMPLEMENT 4HE GOOD NEWS IS YOU DON T HAVE TO WORRY ABOUT IT *UST TRUST THAT WHEN THE SERVICE METHOD IS CALLED IN YOUR SERVLET IT LL BE HANDED REFERENCES TO TWO PERFECTLY GOOD OBJECTS THAT IMPLEMENT (TTP3ERVLET2EQUEST AND (TTP3ERVLET2ESPONSE 9OU SHOULD NEVER CARE ABOUT THE ACTUAL IMPLEMENTATION CLASS NAME OR TYPE !LL YOU CARE ABOUT IS THAT YOU LL GET SOMETHING THAT HAS ALL THE FUNCTIONALITY FROM (TTP3ERVLET2EQUEST AND (TTP3ERVLET2ESPONSE )N OTHER WORDS ALL YOU NEED TO KNOW ARE THE METHODS YOU CAN CALL ON THE OBJECTS THE #ONTAINER GIVES YOU AS PART OF THE REQUEST 4HE ACTUAL CLASS IN WHICH THEY RE IMPLEMENTED DOESN T MATTER TO YOUˆYOU RE REFERRING TO THE REQUEST AND RESPONSE OBJECTS ONLY BY THE INTERFACE TYPE

4

!M ) READING THIS 5-, CORRECTLY !RE THOSE INTERFACES EXTENDING INTERFACES

$

9ES 2EMEMBER INTERFACES CAN HAVE THEIR OWN INHERITANCE TREE 7HEN ONE INTERFACE EXTENDS ANOTHER INTERFACE WHICH IS ALL THEY CAN DOˆINTERFACES CAN T IMPLEMENT INTERFACES IT MEANS THAT WHOEVER IMPLEMENTS AN INTERFACE MUST IMPLEMENT ALL THE METHODS DEFINED IN BOTH THE INTERFACE AND ITS SUPERINTERFACES 4HIS MEANS FOR EXAMPLE THAT WHOEVER IMPLEMENTS (TTP3ERVLET2EQUEST MUST PROVIDE IMPLEMENTATION METHODS FOR THE METHODS DECLARED IN THE (TTP3ERVLET2EQUEST INTERFACE AND THE METHODS IN THE 3ERVLET2EQUEST INTERFACE

7KH H[DP GRHVQ¶W H[SHFW \RX WR NQRZ KRZ WR GHYHORS ZLWK QRQ +773 VHUYOHWV <RX¶UH QRW H[SHFWHG WR NQRZ DQ\WKLQJ DERXW KRZ \RX PLJKW XVH VHUYOHWV ZLWK D SURWRFRO RWKHU WKDQ +773 <RX DUH KRZHYHU VWLOO VXSSRVHG WR NQRZ KRZ WKH FODVV KLHUDUFK\ ZRUNV 6R \RX '2 KDYH WR NQRZ WKDW +WWS6HUYOHW5HTXHVW DQG +WWS6HUYOHW5HVSRQVH H[WHQG IURP 6HUYOHW5HTXHVW DQG 6HUYOHW5HVSRQVH DQG WKDW PRVW RI DQ +WWS6HUYOHW¶V LPSOHPHQWDWLRQ DFWXDOO\ FRPHV IURP *HQHULF6HUYOHW %XW WKDW¶V LW 7KH H[DP DVVXPHV \RX¶UH DQ +WWS6HUYOHW GHYHORSHU

4

) M STILL CONFUSED ABOUT WHY THERE S A 'ENERIC3ERVLET AND 3ERVLET2EQUEST AND 3ERVLET2ESPONSE )F NOBODY S DOING ANYTHING EXCEPT (440 SERVLETS THEN WHAT S THE POINT

$

7E DIDN T SAY NOBODY 3OMEBODY SOMEWHERE ONE COULD IMAGINE IS USING THE SERVLET TECHNOLOGY MODEL WITHOUT THE (440 PROTOCOL *UST NOBODY WE VE MET PERSONALLY OR READ ABOUT %VER 3TILL THE FLEXIBILITY WAS DESIGNED INTO THE SERVLET MODEL FOR THOSE WHO MIGHT WANT TO USE SERVLETS WITH SAY 3-40 OR PERHAPS A PROPRIETARY CUSTOM PROTOCOL 4HE ONLY SUPPORT BUILT IN TO THE !0) THOUGH IS FOR (440 AND THAT S WHAT VIRTUALLY EVERYONE S USING \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

+773 0HWKRGV

<PM 0<<8 ZMY]M[\ 5M\PWL LM\MZUQVM[ _PM\PMZ LW/M\ WZ LW8W[\ Z]V[

HU7DVWH WH PDOW\

*(7 VHOHFW VHOHFW%H

4HE CLIENT S REQUEST REMEMBER ALWAYS INCLUDES A SPECIl C (440 -ETHOD )F THE (440 -ETHOD IS A '%4 THE SERVICE METHOD CALLS DO'ET )F THE (440 REQUEST -ETHOD IS A 0/34 THE SERVICE METHOD CALLS DO0OST

<RX NHHS VKRZLQJ GR*HW DQG GR3RVW OLNH WKH\·UH WKH RQO\ RQHV EXW , .12: WKHUH DUH HLJKW PHWKRGV LQ +773

GR"FRORU GDUN WDV +773

PDUW FRP +RVW ZZZ ZLFNHGO\V 0DFLQWRVK 8VHU $JHQW 0R]LOOD 32 FK 2 67 HQ ; 0D VH86 OHFW VHOHFW%HH 8 33& 0DF 26 U 1HWVFDSH 7DVWH GR +773 UY *HFNR Q OLFDWLR O DSS +RVW W [P ZZZ ZLFNHG $FFHSW WH[ O\VPDUW FRP O [PO WH[W 8VHU $JH QW 0R]LOOD [PO DSSOLFDWLRQ [KWP YLGHR [ 0DFLQWRVK DLQ T 8 33 & 0DF 26 ; 0 KWPO T WH[W SO DFK 2 HQ 86 UY *HFNR 1HWVFD SH $FFHSW WH[W [PO DSSOLFDWLRQ [PO DSSOLFDWLRQ [ KWPO [PO WH[ W KWPO T WH[W SODLQ T YLGHR [ PQJ LPDJH S QJ LPDJH MSHJ LPDJH JLI T T

HTTP requests

<RX SUREDEO\ ZRQ·W FDUH DERXW DQ\ +773 0HWKRGV H[FHSW *(7 DQG 3267 9ES THERE ARE OTHER (440 -ETHODS BESIDES '%4 AND 0/34 4HERE S ALSO (%!$ 42!#% /04)/.3 054 $%,%4% AND #/..%#4 !LL BUT ONE OF THE EIGHT HAS A MATCHING DO888 METHOD IN THE (TTP3ERVLET CLASS SO BESIDES DO'ET AND DO0OST YOU VE GOT DO/PTIONS DO(EAD DO4RACE DO0UT AND DO$ELETE 4HERE S NO MECHANISM IN THE SERVLET !0) FOR HANDLING DO#ONNECT SO IT S NOT PART OF (TTP3ERVLET "UT WHILE THE OTHER (440 -ETHODS MIGHT MATTER TO SAY A WEB SERVER DEVELOPER A SERVLET DEVELOPER RARELY USES ANYTHING BUT '%4 AND 0/34 &OR MOST OR PROBABLY ALL SERVLET DEVELOPMENT YOU LL USE EITHER DO'ET FOR SIMPLE REQUESTS OR DO0OST TO ACCEPT AND PROCESS FORM DATA AND YOU WON T HAVE TO THINK ABOUT THE OTHERS

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

6R LI WKH\·UH QRW LPSRUWDQW WR PH RI &2856( WKDW PHDQV WKH\·OO EH RQ WKH H[DP

)K\]ITTa WVM WZ UWZM WN \PM W\PMZ 0<<8 5M\PWL[ UQOP\ UISM I JZQMN IXXMIZIVKM WV \PM M`IU )F YOU RE PREPARING FOR THE EXAM YOU SHOULD BE ABLE TO RECOGNIZE ALL OF THEM FROM A LIST AND HAVE AT LEAST THE BRIEFEST IDEA OF WHAT THEY RE USED FOR "UT DON T SPEND MUCH TIME HERE ,Q WKH UHDO VHUYOHW ZRUOG \RX FDUH DERXW *(7 DQG 3267 ,Q WKH H[DP ZRUOG \RX FDUH MXVW D WLQ\ ELW DERXW WKH RWKHU +773 0HWKRGV DV ZHOO

Example of a response to an HTTP OPTIONS request: +773 2. 6HUYHU $SDFKH &R\RWH 'DWH 7KX $SU *07 $OORZ 237,216 75$&( *(7 +($' 3267 &RQWHQW /HQJWK

*(7

$VNV WR JHW WKH WKLQJ UHVRXUFH ILOH DW WKH UHTXHVWHG 85/

3267

$VNV WKH VHUYHU WR DFFHSW WKH ERG\ LQIR DWWDFKHG WR WKH UHTXHVW DQG JLYH LW WR WKH WKLQJ DW WKH UHTXHVWHG 85/ ,W·V OLNH D IDW *(7 D *(7 ZLWK H[WUD LQIR VHQW ZLWK WKH UHTXHVW

+($'

$VNV IRU RQO\ WKH KHDGHU SDUW RI ZKDWHYHU D *(7 ZRXOG UHWXUQ 6R LW·V MXVW OLNH *(7 EXW ZLWK QR ERG\ LQ WKH UHVSRQVH *LYHV \RX LQIR DERXW WKH UHTXHVWHG 85/ ZLWKRXW DFWXDOO\ JHWWLQJ EDFN WKH UHDO WKLQJ

75$&(

$VNV IRU D ORRSEDFN RI WKH UHTXHVW PHVVDJH VR WKDW WKH FOLHQW FDQ VHH ZKDW·V EHLQJ UHFHLYHG RQ WKH RWKHU HQG IRU WHVWLQJ RU WURXEOHVKRRWLQJ

387

6D\V WR SXW WKH HQFORVHG LQIR WKH ERG\ DW WKH UHTXHVWHG 85/

'(/(7(

6D\V WR GHOHWH WKH WKLQJ UHVRXUFH ILOH DW WKH UHTXHVWHG 85/

237,216

$VNV IRU D OLVW RI WKH +773 PHWKRGV WR ZKLFK WKH WKLQJ DW WKH UHTXHVWHG 85/ FDQ UHVSRQG

&211(&7 6D\V WR FRQQHFW IRU WKH SXUSRVHV RI WXQQHOLQJ

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

*(7 DQG 3267

<PM LQNNMZMVKM JM\ _MMV /-< IVL 87;< 0/34 HAS A BODY 4HAT S THE KEY "OTH '%4 AND 0/34 CAN SEND PARAMETERS BUT WITH '%4 THE PARAMETER DATA IS LIMITED TO WHAT YOU CAN STUFF INTO THE 2EQUEST LINE

The HTTP method.

e.

uest lin The Req

The pat resource h to the web serve on the r.

st, e T reque In a GEeters (if therded param y) are appen L are an request UR to the

The protoc that the w ol version is requestin eb browser g.

*(7 DGYLVRU VHOHFW%HHU7DVWH GR"FRORU GDUN WDVWH PDOW\ +773

The Request headers.

... just NO bodyder info. a e h the

+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

st line.

ue The Req

The Request headers.

The HTTP method.

The path .

The Protocol.

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

, age body The messes called sometim yloadÓ. the Òpa

NO request parameters up here.

FRORU GDUN WDVWH PDOW\

This time, the paramete in the body, so they arers are down here nÕt limited the way they are if you use to put them in the Req a GET and have uest line.

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH 6RXQGV OLNH WKH GLIIHUHQFH EHWZHHQ *(7 DQG 3267 LV WKH VL]H RI WKH SDUDPHWHU GDWD \RX FDQ VHQG"

6W Q\¼[ VW\ R][\ IJW]\ \PM [QbM 7E TALKED ABOUT OTHER ISSUES WITH '%4 IN CHAPTER ONE REMEMBER 7HEN YOU USE '%4 THE PARAMETER DATA SHOWS UP IN THE BROWSER S INPUT BAR RIGHT AFTER ACTUAL 52, AND SEPARATED WITH A h v )MAGINE A SCENARIO IN WHICH YOU WOULD NOT WANT THE PARAMETERS TO BE VISIBLE KWWS ZLFNHGO\VPDUW FRP WRS6HFUHW P\6HUYOHW GR"QDPH FRZJLUO SDVVZRUG KLGDOJR

3O SECURITY MIGHT BE ANOTHER ISSUE 3TILL ANOTHER ISSUE IS WHETHER YOU NEED OR WANT END USERS TO BE ABLE TO BOOKMARK THE REQUEST PAGE '%4 REQUESTS CAN BE BOOKMARKED 0/34 REQUESTS CANNOT 4HAT MIGHT BE REALLY IMPORTANT IF YOU HAVE SAY A PAGE THAT LETS USERS SPECIFY SEARCH CRITERIA 4HE USERS MIGHT WANT TO COME BACK A WEEK LATER AND TRY THE SAME SEARCH AGAIN NOW THAT THERE S NEW DATA ON THE SERVER "UT BESIDES SIZE SECURITY AND BOOKMARKING THERE S ANOTHER CRUCIAL DIFFERENCE BETWEEN '%4 AND 0/34ˆTHE WAY THEY RE SUPPOSED TO BE USED '%4 IS MEANT TO BE USED FOR GETTING THINGS 0ERIOD 3IMPLE RETRIEVAL 3URE YOU MIGHT USE THE PARAMETERS TO HELP lGURE OUT WHAT TO SEND BACK BUT THE POINT ISˆYOU RE NOT MAKING ANY CHANGES ON THE SERVER 0/34 IS MEANT TO BE USED FOR SENDING DATA TO BE PROCESSED 4HIS COULD BE AS SIMPLE AS QUERY PARAMETERS USED TO lGURE OUT WHAT TO SEND BACK JUST AS WITH A '%4 BUT WHEN YOU THINK OF 0/34 THINK UPDATE 4HINK USE THE DATA FROM THE 0/34 BODY TO CHANGE SOMETHING ON THE SERVER !ND THAT BRINGS UP ANOTHER ISSUE WHETHER THE REQUEST IS IDEMPOTENT )F IT S NOT YOU COULD GET INTO THE KIND OF TROUBLE A LITTLE BLUE PILL CAN T lX )F YOU RE NOT FAMILIAR WITH THE WAY THE TERM hIDEMPOTENTv IS USED IN THE WEB WORLD KEEP READING \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

WKH QRQ LGHPSRWHQW UHTXHVW

<PM [\WZa WN \PM VWV QLMUXW\MV\ ZMY]M[\ $IANE HAS A NEED 3HE S TRYING DESPERATELY TO PURCHASE (EAD &IRST +NITTING FROM ,GHPSRWHQF\ THE 7ICKEDLY 3MART ONLINE BOOK SHOP WHICH UNBEKNOWNST TO $IANE IS STILL IN BETA $IANE S LOW ON MONEYˆSHE HAS JUST ENOUGH IN HER DEBIT ACCOUNT TO COVER ONE BOOK 3HE CONSIDERED BUYING DIRECTLY FROM !MAZON OR THE / 2EILLY COM SITE BUT DECIDED SHE WANTED AN AUTOGRAPHED COPY AVAILABLE ONLY FROM THE 7ICKEDLY 3MART SITE ! CHOICE SHE WOULD LATER COME TO REGRET

'LDQH KLWV WKH &+(&.287 EXWWRQ 6KH VXEPLWWHG KHU EDQN DFFRXQW LQIR HDUOLHU

%URZVHU VHQGV DQ +773 UHTXHVW WR WKH VHUYHU ZLWK WKH ERRN SXUFKDVH LQIR DQG 'LDQH·V FXVWRPHU ,' QXPEHU

[`][cgml

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR WKH &KHFNRXW VHUYOHW IRU SURFHVVLQJ

UHTXHVW

FKHFNRXW

:LFNHGO\ 6PDUW·V :HE 6HUYHU &RQWDLQHU 6HUYOHW HOHFWURQLFDOO\ GHELWV 'LDQH·V EDQN DFFRXQW

FKHFNRXW

6HUYOHW XSGDWHV WKH GDWDEDVH WDNHV WKH ERRN RXW RI LQYHQWRU\ FUHDWHV D QHZ VKLSSLQJ RUGHU HWF

GHELW

XSGDWH

FKHFNRXW

'%

5HPRWH EDQN DFFRXQW VHUYHU

6HUYOHW GRHV 127 VHQG DQ REYLRXV UHVSRQVH VR 'LDQH VWLOO VHHV WKH VDPH VKRSSLQJ FDUW SDJH DQG WKLQNV

0D\EH , GLGQ·W FOLFN LW ULJKW , EHWWHU KLW WKH &+(&.287 EXWWRQ DJDLQ

[`][cgml

%URZVHU VHQGV DQ +773 UHTXHVW WR WKH VHUYHU ZLWK WKH ERRN SXUFKDVH LQIR DQG 'LDQH·V FXVWRPHU ,' QXPEHU UHTXHVW

:LFNHGO\ 6PDUW·V :HE 6HUYHU &RQWDLQHU

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

7]Z [\WZa KWV\QV]M[

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR WKH &KHFNRXW VHUYOHW IRU SURFHVVLQJ

7KH VHUYOHW GRHV QRW KDYH D SUREOHP ZLWK 'LDQH EX\LQJ WKH VDPH ERRN VKH ERXJKW EHIRUH , JXHVV VKH UHDOO\ OLNHV WKLV NQLWWLQJ ERRN D ORW VKH·V EX\LQJ LW WZLFH &RRO

FKHFNRXW

:LFNHGO\ 6PDUW·V :HE 6HUYHU &RQWDLQHU

FKHFNRXW

6HUYOHW HOHFWURQLFDOO\ GHELWV 'LDQH·V EDQN DFFRXQW IRU WKH VHFRQG WLPH

FKHFNRXW

'LDQH·V EDQN DFFHSWV WKH GHELW EXW FKDUJHV KHU D KHIW\ RYHUGUDIW IHH

:H·OO OHW KHU EX\ WKLV ERRN EXW ZH·OO FKDUJH KHU DQ H[WUD IRU EHLQJ RYHUGUDZQ %DG EDG 'LDQH

GHELW 5HPRWH EDQN DFFRXQW VHUYHU 5HPRWH EDQN DFFRXQW VHUYHU

(YHQWXDOO\ 'LDQH QDYLJDWHV WR WKH &KHFN 2UGHU 6WDWXV SDJH DQG VHHV WKDW VKH KDV 7:2 RUGHUV IRU WKH NQLWWLQJ ERRN

7KLV LV QRW ULJKW , PHDQW WR EX\ RQO\ 21( ERRN :KDW VWXSLG ZHE DSS GHYHORSHU PDGH 7+,6" ,W VKRXOG KDYH UHFRJQL]HG D GXSOLFDWH WUDQVDFWLRQ

+HOOR EDQN" 7KLV ZLFNHGO\ VWXSLG ZHE SURJUDPPHU PDGH D PLVWDNH

gj\]jk

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

+773 PHWKRGV

6KDUSHQ \RXU SHQFLO :KLFK RI WKH +773 PHWKRGV GR \RX WKLQN DUH RU VKRXOG EH LGHPSRWHQW" %DVHG RQ \RXU SUHYLRXV XQGHUVWDQGLQJ RI WKH ZRUG DQG RU WKH 'LDQH GRXEOH SXUFKDVH VWRU\ \RX MXVW UHDG $QVZHUV DUH DW WKH ERWWRP RI WKLV SDJH

Q *(7 Q 3267 Q 387 Q +($' :H OHIW RII &211(&7 GHOLEHUDWHO\ VLQFH LW¶V QRW SDUW RI +WWS6HUYOHW

# 4X3 X9 8 :KDW ZHQW ZURQJ ZLWK 'LDQH¶V WUDQVDFWLRQ" $QG LW¶V QRW MXVW 21( WKLQJ WKHUH DUH SUREDEO\ VHYHUDO SUREOHPV WKH GHYHORSHU PXVW ¿[

:KDW DUH VRPH RI WKH ZD\V LQ ZKLFK D GHYHORSHU FRXOG UHGXFH WKH ULVN RI WKLV" +LQW WKH\ PLJKW QRW DOO EH SURJUDPPDWLF VROXWLRQV

\RXUVHOI EXW VKRXOGQ¶W 3267 LV FRQVLGHUHG LGHPSRWHQW E\ WKH +773 VSHF 7KH +773 VSHF GHFODUHV *(7 +($' DQG 387 DV LGHPSRWHQW HYHQ WKRXJK \RX &$1 ZULWH D QRQ LGHPSWRWHQW GR*HW PHWKRG

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

,GHPSRWHQF\ LV QRWKLQJ WR EH DVKDPHG RI

%HLQJ LGHPSRWHQW LV *22' ,W PHDQV \RX FDQ GR WKH VDPH WKLQJ RYHU DQG RYHU DJDLQ ZLWK QR XQZDQWHG VLGH HIIHFWV

,GHPSRWHQW *(7

+773 2. KWPO! KHDG!

&OLHQW

KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

6HUYOHW

6HUYOHW VHQGV EDFN D UHVSRQVH ZLWK D JHQHUDWHG +70/ SDJH

6HUYOHW XVHV WKH 3267 GDWD WR XSGDWH WKH GDWDEDVH

127 ,GHPSRWHQW 3267

'% 6HUYOHW

+773 2. KWPO! KHDG!

&OLHQW

KHDG! ERG\! LPJ VUF ! ERG\! KWPO!

6HUYOHW VHQGV EDFN D UHVSRQVH ZLWK D JHQHUDWHG +70/ SDJH

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

LGHPSRWHQW UHTXHVWV

87;< Q[ VW\ QLMUXW\MV\ !N (440 '%4 IS JUST FOR GETTING THINGS AND IT S NOT SUPPOSED TO CHANGE ANYTHING ON THE SERVER 3O A '%4 IS BY DEl NITION AND ACCORDING TO THE (440 SPEC IDEMPOTENT )T CAN BE EXECUTED MORE THAN ONCE WITHOUT ANY BAD SIDE EFFECTS 0/34 IS NOT IDEMPOTENTˆTHE DATA SUBMITTED IN THE BODY OF A 0/34 MIGHT BE DESTINED FOR A TRANSACTION THAT CAN T BE REVERSED 3O YOU HAVE TO BE CAREFUL WITH YOUR DO0OST FUNCTIONALITY *(7 LV LGHPSRWHQW 3267 LV QRW ,W¡V XS WR \RX WR PDNH VXUH WKDW \RXU ZHE DSS ORJLF FDQ KDQGOH VFHQDULRV OLNH 'LDQH¡V ZKHUH WKH 3267 FRPHV LQ PRUH WKDQ RQFH

:KDW¡V WR VWRS PH IURP XVLQJ WKH SDUDPHWHUV LQ *(7 WR XSGDWH WKH VHUYHU"

*(7 LV DOZD\V FRQVLGHUHG LGHPSRWHQW LQ +773

HYHQ LI \RX VHH FRGH RQ WKH H[DP WKDW XVHV WKH *(7 XVHV DW FD SDUDPHWHUV LQ D ZD\ WK *(7 VLGH HIIHFWV ,Q RWKHU ZRUGV R WKH LV LGHPSRWHQW DFFRUGLQJ W LQJ +773 VSHF %XW WKHUHÂśV QRWK WLQJ D WR VWRS \RX IURP LPSOHPHQ WKRG QRQ LGHPSRWHQW GR*HW PH LQ \RXU VHUYOHW 7KH FOLHQWÂśV EH *(7 UHTXHVW LV VXSSRVHG WR 28 GR LGHPSRWHQW HYHQ LI ZKDW < HIIHFWV ZLWK WKH GDWD FDXVHV VLGH LIIHUHQFH $OZD\V NHHS LQ PLQG WKH G KRG EHWZHHQ WKH +773 *(7 PHW PHWKRG DQG \RXU VHUYOHWÂśV GR*HW

Note: there are several different uses of the word ÒidempotentÓ; weÕre using it in the HTTP/servlet way to mean that the same request can be made twice with no negative consequences on the server. We do *not* use ÒidempotentÓ to mean that the same request always returns the same response, and we do NOT mean that a request has NO side effects.

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

?PI\ LM\MZUQVM[ _PM\PMZ \PM JZW_[MZ [MVL[ I /-< WZ 87;< ZMY]M[\ ' ink a simple hyperla GET. ns ea m ys alwa

*(7

$ +5() ´KWWS ZZZ ZLFNHGO\VPDUW FRP LQGH[ KWPO ´!FOLFN KHUH $!

if you explicitly SAY method=ÓPOSTÓ, then, surprisingly, itÕs a POST.

3267

IRUP PHWKRG ´3267´ DFWLRQ ´6HOHFW%HHU GR´! 6HOHFW EHHU FKDUDFWHULVWLFV S! VHOHFW QDPH ´FRORU´ VL]H ´ ´! RSWLRQ!OLJKW RSWLRQ!DPEHU RSWLRQ!EURZQ RSWLRQ!GDUN the UBMITÓ button, VHOHFW! user clicks the ÒSe body of the POST request. e th n he W FHQWHU! d nt in th parameters are, sethereÕs just one parameter, nalormethe LQSXW W\SH ´68%0,7´! In this examplee value is the <option> beer co FHQWHU! dark). IRUP! ÒcolorÓ, and th amber, brown, or

t,

user selected (ligh

?PI\ PIXXMV[ QN aW] LW 67< [Ia UM\PWL%¹87;<º QV aW]Z $NWZU&'

This time, thereÕs no method=ÒP OSTÓ here. IRUP DFWLRQ ´6HOHFW%HHU GR´! 6HOHFW EHHU FKDUDFWHULVWLFV S! VHOHFW QDPH ´FRORU´ VL]H ´ ´! RSWLRQ!OLJKW RSWLRQ!DPEHU RSWLRQ!EURZQ RSWLRQ!GDUN s to the NOW what happenenthe user clicks VHOHFW! wh rs te parame FHQWHU! form doesnÕt SUBMIT, if th=e ÒPOSTÓ? LQSXW W\SH ´68%0,7´! od th FHQWHU! have a me IRUP! \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

IRUPV DQG +773

87;< Q[ 67< \PM LMNI]T\ )F YOU DON T PUT METHOD h0/34v INTO YOUR FORM THE DEFAULT IS AN (440 '%4 REQUEST 4HAT MEANS THE BROWSER SENDS THE PARAMETERS IN THE REQUEST HEADER BUT THAT S THE LEAST OF YOUR PROBLEMS "ECAUSE IF THE REQUEST COMES IN AS A '%4 THAT MEANS YOU LL RUN INTO BIG TROUBLE AT RUNTIME IF YOU HAVE ONLY A DO0OST AND NOT A DO'ET IN YOUR SERVLET

,I \RX GR WKLV

d=POSTÓ No ÒmethToML form. in the H

IRUP DFWLRQ ´6HOHFW%HHU GR´!

$QG WKHQ WKLV SXEOLF FODVV %HHU6HOHFW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ FRGH KHUH ` No doGet() ` metho

d in the serv

let.

<RX·OO JHW WKLV )$,/85( ,I \RXU +70/ IRUP XVHV *(7 LQVWHDG RI 3267 WKHQ \RX 0867 KDYH GR*HW LQ \RXU VHUYOHW FODVV 7KH GHIDXOW PHWKRG IRU IRUPV LV *(7

4

7HAT IF ) WANT TO SUPPORT BOTH '%4 AND 0/34 FROM A SINGLE SERVLET

$

$EVELOPERS WHO WANT TO SUPPORT BOTH METHODS USUALLY PUT LOGIC IN DO0OST THEN DELEGATE

TO A DO'ET IF THIS REQUEST DOESN T NEED TO DO POST THINGS SXEOLF YRLG GR3RVW WKURZV ^ GR*HW UHTXHVW UHVSRQVH `

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

;MVLQVO IVL ][QVO I [QVOTM XIZIUM\MZ +70/ IRUP IRUP PHWKRG ´3267´ DFWLRQ ´6HOHFW%HHU GR´! 6HOHFW EHHU FKDUDFWHULVWLFV S! VHOHFW QDPH ´FRORU´ VL]H ´ ´! RSWLRQ!OLJKW one of these four The browser will sendfor RSWLRQ!DPEHU the parameter y, in the request bod RSWLRQ!EURZQ amberĂ“. RSWLRQ!GDUN Ă’colorĂ“. For example, Ă’color= VHOHFW! FHQWHU! LQSXW W\SH ´68%0,7´! FHQWHU! IRUP!

options named

+773 3267 UHTXHVW 3267 DGYLVRU 6HOHFW%HHU 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

Remember, the browse you donÕt have to worryr generates this request, so what it looks like coming about creating it, but hereÕs over to the server...

FRORU GDUN

6HUYOHW FODVV SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ 6WULQJ FRORU3DUDP UHTXHVW JHW3DUDPHWHU ³FRORU´ PRUH HQOLJKWHQLQJ FRGH KHUH `

(In this example, the String colorParam has a value of ÒdarkÓ.)

This matches the name in the form.

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

IRUP SDUDPHWHUV

;MVLQVO IVL ][QVO <?7 XIZIUM\MZ[ +70/ IRUP IRUP PHWKRG ´3267´ DFWLRQ ´6HOHFW%HHU7DVWH GR´! 6HOHFW EHHU FKDUDFWHULVWLFV S! &2/25 VHOHFW QDPH ´FRORU´ VL]H ´ ´! RSWLRQ!OLJKW of these four options in The browser will send onewith the name Ă’colorĂ“. RSWLRQ!DPEHU the request, associated RSWLRQ!EURZQ RSWLRQ!GDUN VHOHFW! %2'< VHOHFW QDPH ´ERG\´ VL]H ´ ´! RSWLRQ!OLJKW The browser will send one of RSWLRQ!PHGLXP in the request, associated wit these three options RSWLRQ!KHDY\ h the name Ă’bodyĂ“. VHOHFW! FHQWHU! LQSXW W\SH ´68%0,7´! FHQWHU! IRUP! +773 3267 UHTXHVW 3267 DGYLVRU 6HOHFW%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 ERG\ KHDY\

t has both Now the POST reqatues by an ampersand. ed ar sep parameters,

6HUYOHW FODVV SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ 6WULQJ FRORU3DUDP UHTXHVW JHW3DUDPHWHU ÂłFRORU´ 6WULQJ ERG\3DUDP UHTXHVW JHW3DUDPHWHU ÂłERG\´ Now the String variable color Para PRUH FRGH KHUH has a value of Ă’darkĂ“ and bodyParamm ` has

a value of ÒheavyÓ.

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

DPHWHU 7KDW YDOXHV IRU D VLQJOH SDU <RX FDQ KDYH PXOWLSOH UHWXUQV DQ DUDPHWHU9DOXHV WKDW HW3 J HG O QH RX·O QV \ PHD 6WULQJ V D WXUQ W UH WKD HU

:DWFK LW DUUD\ LQVWHDG RI JHW3DUDPHW RUH WKDQ RQH YDOXH 7KDW YH P Q KD V FD ER[H KHFN D VHW RI F 6RPH IRUP LQSXW W\SHV OLNH H PXOWLSOH YDOXHV GHSHQGLQJ ³VL]HV´ IRU H[DPSOH ZLOO KDY EHHU PHDQV D VLQJOH SDUDPHWHU D XVHU FDQ VHOHFW PXOWLSOH KHUH P Z $ IRU RII FNHG U FKH RQ KRZ PDQ\ ER[HV WKH XVH V PLJKW ORRN OLNH WKLV UHVWHG LQ $// RI WKRVH VL]H VL]HV WR VD\ WKDW KH¶V LQWH IRUP PHWKRG 3267 GR´! DFWLRQ ´6HOHFW%HHU HULVWLFV S! 6HOHFW EHHU FKDUDFW ! S HV 6L] &DQ ´ R]´! R] EU! R[ QDPH VL]HV YDOXH LQSXW W\SH FKHFNE ´ R]´! R] EU! XH YDO HV VL] H QDP R[ LQSXW W\SH FKHFNE XH ´ R]´! R] EU! YDO HV VL] H QDP R[ LQSXW W\SH FKHFNE EU! EU! FHQWHU! %0,7´! LQSXW W\SH ´68 FHQWHU! IRUP! G WKDW UHWXUQV DQ DUUD\ JHW3DUDPHWHU9DOXHV PHWKR ,Q \RXU FRGH \RX¶OO XVH WKH ³VL]HV´ > @ W JHW3DUDPHWHU9DOXHV 6WULQJ RQH UHTXHV HV ³VL]HV´ XHVW JHW3DUDPHWHU9DOX 6WULQJ >@ VL]HV UHT U WHVWLQJ \RX FDQ XVH QJ LQ WKH DUUD\ MXVW IRU IXQ R ,I \RX ZDQW WR VHH HYHU\WKL XHV ³VL]HV´ TXHVW JHW3DUDPHWHU9DO 6WULQJ >@ VL]HV UH ^ [ K QJW OH ]HV IRU LQW [ [ VL L]HV ³ VL]HV>[@ RXW SULQWOQ ³ EU!V ` SRQVH

W:ULWHU \RX JRW IURP WKH UHV DVVXPH WKDW ³RXW´ LV D 3ULQ

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

WKH +WWS6HUYOHW5HTXHVW REMHFW

*M[QLM[ XIZIUM\MZ[ _PI\ MT[M KIV 1 OM\ NZWU I : MY]M[\ WJRMK\' 4HE 3ERVLET2EQUEST AND (TTP3ERVLET2EQUEST INTERFACES HAVE A TON OF METHODS YOU CAN CALL BUT YOU DON T NEED TO MEMORIZE THEM ALL /N YOUR OWN YOU REALLY SHOULD LOOK AT THE FULL !0) FOR JAVAX SERVLET 3ERVLET2EQUEST AND JAVAX SERVLET HTTP (TTP3ERVLET2EQUEST BUT HERE WE LL LOOK AT ONLY THE METHODS YOU RE MOST LIKELY TO USE IN YOUR WORK AND WHICH MIGHT ALSO SHOW UP ON THE EXAM )N THE REAL WORLD YOU LL BE LUCKY OR UNLUCKY DEPENDING ON YOUR PERSPECTIVE TO USE MORE THAN OF THE REQUEST !0) $ON T WORRY IF YOU AREN T CLEAR ABOUT HOW OR WHY YOU D USE EACH OF THESE WE LL SEE MORE DETAILS ON SOME OF THEM ESPECIALLY COOKIES LATER IN THE BOOK

6HUYOHW5HTXHVW LQWHUIDFH MDYD[ VHUYOHW 6HUYOHW5HTXHVW

LQWHUIDFH!!

6HUYOHW5HTXHVW JHW$WWULEXWH 6WULQJ

JHW&RQWHQW/HQJWK

JHW,QSXW6WUHDP

JHW/RFDO3RUW

JHW5HPRWH3RUW

JHW6HUYHU3RUW

7KH FOLHQW·V SODWIRUP DQG EURZVHU LQIR 6WULQJ FOLHQW UHTXHVW JHW+HDGHU ³8VHU $JHQW´

JHW3DUDPHWHU 6WULQJ

JHW3DUDPHWHU9DOXHV 6WULQJ

JHW3DUDPHWHU1DPHV

0$1< PRUH PHWKRGV

7KH FRRNLHV DVVRFLDWHG ZLWK WKLV UHTXHVW &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV

+WWS6HUYOHW5HTXHVW LQWHUIDFH MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HTXHVW

7KH VHVVLRQ DVVRFLDWHG ZLWK WKLV FOLHQW

LQWHUIDFH!!

+WWS6HUYOHW5HTXHVW +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ

JHW&RQWH[W3DWK

JHW&RRNLHV

7KH +773 0HWKRG RI WKH UHTXHVW 6WULQJ WKH0HWKRG UHTXHVW JHW0HWKRG

JHW+HDGHU 6WULQJ

JHW,QW+HDGHU 6WULQJ

JHW0HWKRG

JHW4XHU\6WULQJ

JHW6HVVLRQ

$Q LQSXW VWUHDP IURP WKH UHTXHVW

0$1< PRUH PHWKRGV

,QSXW6WUHDP LQSXW UHTXHVW JHW,QSXW6WUHDP

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

WKHUH DUH QR

'XPE 4XHVWLRQV

4 $

7HY WOULD ) EVER WANT TO GET AN )NPUT3TREAM FROM THE REQUEST

7ITH A '%4 REQUEST THERE S NOTHING BUT THE REQUEST HEADER INFO )N OTHER WORDS THERE S NO BODY TO CARE ABOUT "54 WITH AN (440 0/34 THERE S BODY INFO -OST OF THE TIME ALL YOU CARE ABOUT FROM THE BODY IS SUCKING OUT THE PARAMETER VALUES FOR EXAMPLE hCOLOR DARKv USING REQUEST GET0ARAMETER BUT THOSE VALUES MIGHT BE LARGE )F YOU WANT TO GET AT THE RAW BYTES OF EVERYTHING THAT COMES IN WITH THE REQUEST YOU CAN DO IT WITH THE GET)NPUT3TREAM METHOD 7ITH THE INPUT STREAM YOU COULD FOR EXAMPLE STRIP OUT ALL THE HEADER INFO AND PROCESS THE RAW BYTES OF THE PAYLOAD THE BODY OF THE REQUEST IMMEDIATELY WRITING IT TO A FILE ON THE SERVER PERHAPS

4

7HAT S THE DIFFERENCE BETWEEN GET(EADER AND GET)NT(EADER &AR AS ) CAN TELL HEADERS ARE ALWAYS 3TRINGS %VEN THE GET)NT(EADER METHOD TAKES A 3TRING REPRESENTING THE NAME OF THE HEADER SO WHAT S THE INT ABOUT

$

(EADERS HAVE BOTH A NAME LIKE h5SER !GENTv OR h(OSTv AND A VALUE LIKE h0R]LOOD 0DFLQWRVK 8 33& 0DF 26 ; 0DFK 2 HQ 86 UY *HFNR 1HWVFDSH µ RU ´ZZZ ZLFNHGO\VPDUW FRPµ 7KH YDOXHV WKDW FRPH EDFN IURP KHDGHUV DUH DOZD\V LQ D 6WULQJ IRUP EXW IRU D IHZ KHDGHUV WKH 6WULQJ UHSUHVHQWV D QXPEHU 7KH ´&RQWHQW /HQJWKµ KHDGHU UHWXUQV WKH QXPEHU RI E\WHV WKDW PDNH XS WKH PHVVDJH ERG\ 7KH ´0D[ )RUZDUGVµ +773 KHDGHU IRU H[DPSOH UHWXUQV DQ LQWHJHU LQGLFDWLQJ KRZ PDQ\ URXWHU KRSV WKH UHTXHVW LV DOORZHG WR PDNH <RX PLJKW ZDQW WR XVH WKLV KHDGHU LI \RX·UH WU\LQJ WR WUDFH D UHTXHVW WKDW \RX WKLQN LV JHWWLQJ VWXFN LQ D ORRS VRPHZKHUH <RX FRXOG JHW WKH YDOXH RI WKH ´0D[ )RUZDUGVµ KHDGHU E\ XVLQJ JHW+HDGHU

6WULQJ IRUZDUGV UHTXHVW JHW+HDGHU ³0D[ )RUZDUGV´ LQW IRUZDUGV1XP ,QWHJHU SDUVH,QW IRUZDUGV !ND THAT WORKS FINE "UT IF YOU KNOW THE VALUE OF THE HEADER IS SUPPOSED TO REPRESENT AN INT YOU CAN USE GET)NT(EADER AS A CONVENIENCE METHOD TO SAVE THE EXTRA STEP OF PARSING THE 3TRING TO AN INT LQW IRUZDUGV1XP UHTXHVW JHW,QW+HDGHU ³0D[ )RUZDUGV´

LQJ HPRWH3RUW DUH FRQIXV RFDO3RU W DQG JHW5 JHW6HUYHU3RUW JHW/ W/RFDO3RUW DQG 7KH GLIIHUHQFH EHWZHHQ JH W EH REYLRXV XQWLO \RX PRUH VXEWOH²JHW6HUYHU3RU W LV U3RU HUYH 7KH JHW6HUYHU3RUW VKRXOG JHW6 ULJLQDOO\ 6(17"´ HDQV 6R OHW¶V GR WKH HDV\ ZKLFK SRUW ZDV WKH UHTXHVW R ³WR DVN ZKDW JHW/RFDO3RUW P VD\V VN )LUVW \RX VKRXOG D RQ ZKLFK SRUW GLG WKH RQH ¿ UVW JHW5HPRWH3RUW ZKLOH JHW/RFDO3RUW VD\V ³ DVH VLQFH LW¶V WKH VHUYHU UH¶V D GLIIHUHQFH EHFDXVH ³UHPRWH WR ZKRP"´ ,Q WKLV F UHTXHVW (1' 83"´ <HV WKH 7KH KLQJ RWH W H UHP ¶V WK QW WR D VLQJOH SRUW ZKHUH WKDW UH VH ,(17 VWV D H &/ HTXH W¶V WK WKH U DVNLQJ L DOWKRXJK HU VR JHW5HPRWH3RUW VHUYHU WXUQV DURXQG DQG VHUY WKH WKH LQJ WH WR LVWHQ HPR U LV O W LV U HUYH FOLHQ WKH V UW´ ,Q RWKHU ZRUGV WKH SRUW U HDFK WKUHDG VR WKDW WKH PHDQV ³JHW WKH FOLHQW¶V SR ¿ QGV D GLIIHUHQW ORFDO SRUW IR LPH KLFK WKH UHTXHVW ZDV VHQW XOWLSOH FOLHQWV DW WKH VDPH W OH P KDQG FDQ QXPEHU RQ WKH FOLHQW IURP Z DSS OHW UHPRWH PHDQV FOLHQW 5HPHPEHU LI \RX¶UH D VHUY

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

OLIHF\FOH UHYLHZ

: M^QM_" [MZ ^TM\ TQNMKaKTM IVL )81 LQWHUIDFH!!

MDYD[ VHUYOHW 6HUYOHW

:MDD=L HGAFLK

ƒ 7KH &RQWDLQHU LQLWLDOL]HV D VHUYOHW E\ ORDGLQJ WKH FODVV LQYRNLQJ WKH VHUYOHW¡V QR DUJ FRQVWUXFWRU DQG FDOOLQJ WKH VHUYOHW¡V LQLW PHWKRG

ƒ 7KH LQLW PHWKRG ZKLFK WKH GHYHORSHU FDQ RYHUULGH LV FDOOHG RQO\ RQFH LQ D VHUYOHW¡V OLIH DQG DOZD\V EHIRUH WKH VHUYOHW FDQ VHUYLFH DQ\ FOLHQW UHTXHVWV

ƒ 7KH LQLW PHWKRG JLYHV WKH VHUYOHW DFFHVV WR WKH 6HUY OHW&RQILJ DQG 6HUYOHW&RQWH[W REMHFWV ZKLFK WKH VHUYOHW QHHGV WR JHW LQIRUPDWLRQ DERXW WKH VHUYOHW FRQILJXUDWLRQ DQG WKH ZHE DSS

ƒ 7KH &RQWDLQHU HQGV D VHUYOHW¡V OLIH E\ FDOOLQJ LWV GHVWUR\ PHWKRG

ƒ 0RVW RI D VHUYOHW¡V OLIH LV VSHQW UXQQLQJ D VHUYLFH PHWKRG IRU D FOLHQW UHTXHVW

VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

LQLW 6HUYOHW&RQILJ

GHVWUR\

JHW6HUYOHW&RQILJ

JHW6HUYOHW,QIR

MDYD[ VHUYOHW *HQHULF6HUYOHW VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

LQLW 6HUYOHW&RQILJ

LQLW

GHVWUR\

JHW6HUYOHW&RQILJ

JHW6HUYOHW,QIR

JHW,QLW3DUDPHWHU 6WULQJ

JHW,QLW3DUDPHWHU1DPHV

JHW6HUYOHW&RQWH[W

ORJ 6WULQJ

ORJ 6WULQJ 7KURZDEOH

ƒ (YHU\ UHTXHVW WR D VHUYOHW UXQV LQ D VHSDUDWH WKUHDG 7KHUH LV RQO\ RQH LQVWDQFH RI DQ\ SDUWLFXODU VHUYOHW FODVV

ƒ <RXU VHUYOHW ZLOO DOPRVW DOZD\V H[WHQG MDYD[ VHUYOHW KWWS +WWS6HUYOHW IURP ZKLFK LW LQKHULWV DQ LPSOHPHQWDWLRQ RI WKH VHUYLFH PHWKRG WKDW WDNHV DQ +WWS6HUYOHW5HTXHVW DQG DQ +WWS6HUYOHW5HVSRQVH

ƒ +WWS6HUYOHW H[WHQGV MDYD[ VHUYOHW *HQHULF6HUYOHWÂłDQ DEVWUDFW FODVV WKDW LPSOHPHQWV PRVW RI WKH EDVLF VHUYOHW PHWKRGV

ƒ *HQHULF6HUYOHW LPSOHPHQWV WKH 6HUYOHW LQWHUIDFH ƒ 6HUYOHW FODVVHV H[FHSW WKRVH UHODWHG WR -63V DUH LQ RQH RI WZR SDFNDJHV MDYD[ VHUYOHW RU MDYD[ VHUYOHW KWWS

MDYD[ VHUYOHW KWWS +WWS6HUYOHW VHUYLFH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

VHUYLFH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

GR*HW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR3RVW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR+HDG +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR2SWLRQV +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR3XW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR7UDFH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

GR'HOHWH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

JHW/DVW0RGLILHG +WWS6HUYOHW5HTXHVW

ƒ <RX FDQ RYHUULGH WKH LQLW PHWKRG DQG \RX PXVW RYHUULGH DW OHDVW RQH VHUYLFH PHWKRG GR*HW GR3RVW HWF FRP ZLFNHGO\VPDUW H[DPSOHV 0\6HUYOHW GR3RVW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

P\%L]0HWKRG

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

:M^QM_" 0<<8 IVL 0 \\X;MZ ^TM\:MY]M[\ :MDD=L HGAFLK

ƒ 7KH +WWS6HUYOHW¡V GR*HW DQG GR3RVW PHWKRGV WDNH DQ

6HUYOHW5HTXHVW LQWHUIDFH MDYD[ VHUYOHW 6HUYOHW5HTXHVW

+WWS6HUYOHW5HTXHVW DQG DQ +WWS6HUYOHW5HVSRQVH

LQWHUIDFH!!

6HUYOHW5HTXHVW

ƒ 7KH VHUYLFH PHWKRG GHWHUPLQHV ZKHWKHU GR*HW RU GR3RVW UXQV EDVHG RQ WKH +773 0HWKRG *(7 3267 HWF RI WKH +773 UHTXHVW

ƒ 3267 UHTXHVWV KDYH D ERG\ *(7 UHTXHVWV GR QRW DOWKRXJK *(7 UHTXHVWV FDQ KDYH UHTXHVW SDUDPHWHUV DSSHQGHG WR WKH UHTXHVW 85/ VRPHWLPHV FDOOHG ´WKH TXHU\ VWULQJÂľ

ƒ *(7 UHTXHVWV DUH LQKHUHQWO\ DFFRUGLQJ WR WKH +773 VSHF LGHPSRWHQW 7KH\ VKRXOG EH DEOH WR UXQ PXOWLSOH WLPHV ZLWKRXW FDXVLQJ DQ\ VLGH HIIHFWV RQ WKH VHUYHU *(7 UHTXHVWV VKRXOGQ¡W FKDQJH DQ\WKLQJ RQ WKH VHUYHU %XW \RX FRXOG ZULWH D EDG QRQ LGHPSRWHQW GR*HW PHWKRG

JHW$WWULEXWH 6WULQJ

JHW&RQWHQW/HQJWK

JHW,QSXW6WUHDP

JHW/RFDO3RUW

JHW5HPRWH3RUW

JHW6HUYHU3RUW

JHW3DUDPHWHU 6WULQJ

JHW3DUDPHWHU9DOXHV 6WULQJ

JHW3DUDPHWHU1DPHV

0$1< PRUH PHWKRGV

ƒ 3267 LV LQKHUHQWO\ QRW LGHPSRWHQW VR LW¡V XS WR \RX WR GHVLJQ DQG FRGH \RXU DSS LQ VXFK D ZD\ WKDW LI WKH FOLHQW VHQGV D UHTXHVW WZLFH E\ PLVWDNH \RX FDQ KDQGOH LW

+WWS6HUYOHW5HTXHVW LQWHUIDFH MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HTXHVW

ƒ ,I DQ +70/ IRUP GRHV QRW H[SOLFLWO\ VD\ ´PHWKRG 3267Âľ WKH UHTXHVW LV VHQW DV D *(7 QRW D 3267 ,I \RX GR QRW KDYH D GR*HW LQ \RXU VHUYOHW WKH UHTXHVW ZLOO IDLO

ƒ <RX FDQ JHW SDUDPHWHUV IURP WKH UHTXHVW ZLWK WKH JHW3DUDPHWHU ´SDUDPQDPHÂľ PHWKRG 7KH UHWXUQ YDOXH LV DOZD\V D 6WULQJ

ƒ ,I \RX KDYH PXOWLSOH SDUDPHWHU YDOXHV IRU D JLYHQ SDUDP HWHU QDPH XVH WKH JHW3DUDPHWHU9DOXHV ´SDUDPQDPHÂľ PHWKRG WKDW UHWXUQV D 6WULQJ DUUD\

ƒ <RX FDQ JHW RWKHU WKLQJV IURP WKH UHTXHVW REMHFW LQFOXGLQJ

LQWHUIDFH!!

+7736HUYOHW5HTXHVW JHW&RQWH[W3DWK

JHW&RRNLHV

JHW+HDGHU 6WULQJ

JHW,QW+HDGHU 6WULQJ

JHW0HWKRG

JHW4XHU\6WULQJ

JHW6HVVLRQ

0$1< PRUH PHWKRGV

KHDGHUV FRRNLHV D VHVVLRQ WKH TXHU\ VWULQJ DQG DQ LQSXW VWUHDP

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

WKH +WWS6HUYOHW5HVSRQVH REMHFW

;W \PI\Âź[ \PM :MY]M[\ VW_ TM\Âź[ [MM \PM : M[XWV[M 4HE RESPONSE IS WHAT GOES BACK TO THE CLIENT 4HE THING THE BROWSER GETS PARSES AND RENDERS FOR THE USER 4YPICALLY YOU USE THE RESPONSE OBJECT TO GET AN OUTPUT STREAM USUALLY A 7RITER AND YOU USE THAT STREAM TO WRITE THE (4-, OR SOME OTHER TYPE OF CONTENT THAT GOES BACK TO THE CLIENT 4HE RESPONSE OBJECT HAS OTHER METHODS BESIDES JUST THE ) / OUTPUT THOUGH AND WE LL LOOK AT SOME OF THEM IN A BIT MORE DETAIL 6HUYOHW5HVSRQVH LQWHUIDFH MDYD[ VHUYOHW 6HUYOHW5HVSRQVH

LQWHUIDFH!!

6HUYOHW5HVSRQVH JHW%XIIHU6L]H

VHW&RQWHQW7\SH

JHW2XWSXW6WUHDP

JHW:ULWHU

the These are some of d most commonly-use methods.

VHW&RQWHQW/HQJWK

0$1< PRUH PHWKRGV

+WWS6HUYOHW5HVSRQVH LQWHUIDFH

0RVW RI WKH WLPH \RX XVH WKH 5HVSRQVH MXVW WR VHQG GDWD EDFN WR WKH FOLHQW <RX FDOO WZR PHWKRGV RQ WKH UHVSRQVH VHW&RQWHQW7\SH DQG JHW:ULWHU $IWHU WKDW \RX¡UH VLPSO\ GRLQJ , 2 WR ZULWH +70/ RU VRPHWKLQJ HOVH WR WKH VWUHDP %XW \RX FDQ DOVR XVH WKH UHVSRQVH WR VHW RWKHU KHDGHUV VHQG HUURUV DQG DGG FRRNLHV

MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HVSRQVH

LQWHUIDFH!!

+WWS6HUYOHW5HVSRQVH DGG&RRNLH

DGG+HDGHU

HQFRGH85/

Sometimes yo these too... uÕll use

VHQG(UURU

VHW6WDWXV

VHQG5HGLUHFW

0$1< PRUH PHWKRGV

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

:DLW D PLQXWH , WKRXJKW ZH ZHUHQ·W JRLQJ WR VHQG +70/ IURP D VHUYOHW EHFDXVH LW·V VR XJO\ WR IRUPDW LW IRU WKH RXWSXW VWUHDP

=[QVO \PM ZM[XWV[M NWZ 1 7 /+ YES WE SHOULD BE USING *30S RATHER THAN SENDING (4-, BACK IN THE RESPONSE OUTPUT STREAM FROM A SERVLET &ORMATTING (4-, TO STICK IN AN OUTPUT STREAM S PRINTLN METHOD HURTS "UT THAT DOESN T MEAN YOU LL NEVER HAVE TO WORK WITH AN OUTPUT STREAM FROM YOUR SERVLET 7HY 9OUR HOSTING PROVIDER MIGHT NOT SUPPORT *30S 4HERE ARE PLENTY OF OLDER SERVERS AND CONTAINERS OUT THERE THAT SUPPORT SERVLETS BUT NOT *30S SO YOU RE STUCK WITH IT 9OU DON T HAVE THE OPTION OF USING *30S FOR SOME OTHER REASON LIKE YOU HAVE AN INCREDIBLY STUPID MANAGER WHO WON T LET YOU USE *30S BECAUSE IN HIS BROTHER IN LAW TOLD HIM THAT *30S WERE BAD 7HO SAID THAT (4-, WAS THE ONLY THING YOU COULD SEND BACK IN A RESPONSE 9OU MIGHT SEND SOMETHING OTHER THAN (4-, BACK TO THE CLIENT 3OMETHING FOR WHICH AN OUTPUT STREAM MAKES PERFECT SENSE 4URN THE PAGE FOR AN EXAMPLE

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

VHQGLQJ E\WHV LQ WKH 5HVSRQVH

1UIOQVM aW] _IV\ \W [MVL I 2): \W \PM KTQMV\ ,ET S SAY YOU VE CREATED A DOWNLOAD PAGE WHERE THE CLIENT CAN GET CODE FROM *!2 l LES )NSTEAD OF SENDING BACK AN (4-, PAGE THE RESPONSE CONTAINS THE BYTES REPRESENTING THE *!2 9OU READ THE BYTES OF THE *!2 l LE THEN WRITE THEM TO THE RESPONSE S OUTPUT STREAM

'LDQH LV GHVSHUDWH WR GRZQORDG WKH -$5 RI FRGH IRU WKH ERRN VKH·V XVLQJ WR OHDUQ VHUYOHWV DQG -63V 6KH QDYLJDWHV WR WKH ERRN·V ZHEVLWH DQG FOLFNV WKH ´FRGH MDUµ OLQN ZKLFK UHIHUV WR D VHUYOHW QDPHG ´&RGH GRµ

;G<= B9J

%URZVHU VHQGV DQ +773 UHTXHVW WR WKH VHUYHU ZLWK WKH QDPH RI WKH UHTXHVWHG VHUYOHW ´&RGH GRµ

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR WKH &RGH5HWXUQ VHUYOHW PDSSHG WR WKH QDPH ´&RGH GRµ LQ WKH '' IRU SURFHVVLQJ

UHTXHVW

&RGH5HWXUQ

7KH -$5 VWDUWV GRZQORDGLQJ RQWR WKH FOLHQW·V PDFKLQH 'LDQH LV SOHDVHG

\gof% dgY\af_&

7KH +773 UHVSRQVH QRZ KROGV WKH E\WHV UHSUHVHQWLQJ WKH -$5

&RGH -$5

read

7KH &RGH5HWXUQ VHUYOHW JHWV WKH E\WHV IRU WKH -$5 WKHQ JHWV DQ RXWSXW VWUHDP IURP WKH UHVSRQVH DQG ZULWHV RXW WKH E\WHV UHSUHVHQWLQJ WKH -$5

+773 2.

&RGH5HWXUQ

bytes from JAR

UHVSRQVH

e writ

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

;MZ ^TM\ KWLM \W LW_VTWIL \PM 2): D EXQFK RI LPSRUWV KHUH SXEOLF FODVV &RGH5HWXUQ H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³DSSOLFDWLRQ MDU´

We want the browse to recognize that this is a JAR, not HTr ML content type to Òapplicati, so we set the on/jarÓ.

6HUYOHW&RQWH[W FW[ JHW6HUYOHW&RQWH[W ,QSXW6WUHDP LV FW[ JHW5HVRXUFH$V6WUHDP ³ ERRN&RGH MDU´

LQW UHDG E\WH>@ E\WHV QHZ E\WH> @ 2XWSXW6WUHDP RV UHVSRQVH JHW2XWSXW6WUHDP ZKLOH UHDG LV UHDG E\WHV

^ RV ZULWH E\WHV UHDG ` RV À XVK RV FORVH ` `

, Ògive me an This just says for the resource am re st input e.jarÓ. named bookCod HereÕs the key part, but itÕs just plain old I/O!! Nothing special, just read the JAR bytes, then write the bytes to the output stream that we get from the response object.

WKHUH DUH QR

'XPE 4XHVWLRQV

ZHEDSSV

4

7HERE WAS THE hBOOK#ODE JARv *!2 l LE LOCATED )N OTHER WORDS WHERE DOES THE GET2ESOURCE!S3TREAM METHOD ,//+ TO l ND THE l LE (OW DO YOU DEAL WITH THE PATH

-DU'RZQORDG

$

4HE GET2ESOURCE!S3TREAM REQUIRES YOU TO START WITH A FORWARD SLASH h v WHICH REPRESENTS THE ROOT OF YOUR WEB APP 3INCE THE WEB APP WAS NAMED *AR$OWNLOAD THEN THE DIRECTORY STRUCTURE LOOKS LIKE THE DIRECTORIES IN THE PICTURE 4HE *AR$OWNLOAD DIRECTORY IS INSIDE WEBAPPS AS A PEER DIRECTORY TO ALL THE OTHER WEB APP DIRECTORIES THEN INSIDE *AR$OWNLOAD WE PUT THE 7%" ).& DIRECTORY AND THE CODE *!2 ITSELF 3O THE FILE hBOOK#ODE JARv IS SITTING AT THE ROOT LEVEL OF THE *AR$OWNLOAD WEB APP $ON T WORRY WE LL GO INTO DEEP PENETRATING DETAILS ABOUT THE DEPLOYMENT DIRECTORY STRUCTURE WHEN WE GET TO THE DEPLOYMENT CHAPTER

&RGH -$5

:(% ,1)

ERRN&RGH MDU "[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"!

FODVVHV

'2&7<3(

ZHE [PO

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

FRQWHQW W\SH

?PWI ?PI\¼[ \PM LMIT _Q\P K WV\MV\ \aXM' 9OU MIGHT BE WONDERING ABOUT THIS LINE UHVSRQVH VHW&RQWHQW7\SH ³DSSOLFDWLRQ MDU´

/R AT LEAST YOU SHOULD BE 9OU HAVE TO TELL THE BROWSER WHAT YOU RE SENDING BACK SO THE BROWSER CAN DO THE RIGHT THING LAUNCH A hHELPERv APP LIKE A 0$& VIEWER OR VIDEO PLAYER RENDER THE (4-, SAVE THE BYTES OF THE RESPONSE AS A DOWNLOADED l LE ETC !ND SINCE YOU RE WONDERING YES WHEN WE SAY CONTENT TYPE WE MEAN THE SAME THING AS -)-% TYPE #ONTENT TYPE IS AN (440 HEADER THAT MUST BE INCLUDED IN THE (440 RESPONSE

*RVK WKDQNV 6HUYHU ,W·V QLFH RI \RX WR WHOO PH ZKDW W\SH \RX·UH VHQGLQJ EDFN ,·OO JHW WKH 4XLFNWLPH SOD\HU UHDG\ IRU WKH YLGHR

+HUH·V P\ UHVSRQVH WR \RXU UHTXHVW ,WV W\SH LV YLGHR TXLFNWLPH DQG LQ \RXU UHTXHVW \RX VDLG WKDW ZDV 2. $QG GHVSLWH P\ WUXVW LVVXHV , EHOLHYHG \RX

&RPPRQ 0,0( W\SHV WH[W KWPO DSSOLFDWLRQ SGI YLGHR TXLFNWLPH DSSOLFDWLRQ MDYD LPDJH MSHJ

+773 UHVSRQVH +773 2.

DSSOLFDWLRQ RFWHW VWUHDP

&OLHQW %URZVHU

DSSOLFDWLRQ MDU DSSOLFDWLRQ [ ]LS

6HUYHU &RQWDLQHU

<RX GRQ¶W QHHG WR PHPRUL]H D EXQFK RI FRQWHQW W\SHV <RX VKRXOG NQRZ ZKDW VHW&RQWHQW7\SH GRHV DQG KRZ \RX XVH LW EXW \RX GRQ¶W KDYH WR NQRZ HYHQ WKH PRVW FRPPRQ FRQWHQW W\SHV H[FHSW WH[W KWPO :KDW \RX QHHG WR NQRZ DERXW VHW&RQWHQW7\SH LV PRVWO\ FRPPRQ VHQVH IRU H[DPSOH LW ZRQ¶W GR \RX DQ\ JRRG WR FKDQJH WKH FRQWHQW W\SH $)7(5 \RX ZULWH WR WKH UHVSRQVH RXWSXW VWUHDP 'XK %XW WKDW GRHV PHDQ WKDW \RX FDQ¶W VHW D FRQWHQW W\SH ZULWH VRPH VWXII DQG WKHQ FKDQJH WKH FRQWHQW W\SH DQG ZULWH VRPHWKLQJ GLIIHUHQW %XW WKLQN DERXW LW²KRZ ZRXOG WKH EURZVHU GHDO ZLWK WKDW" ,W FDQ KDQGOH RQO\ RQH W\SH RI 7+,1* DW D WLPH IURP WKH UHVSRQVH 7R PDNH VXUH HYHU\WKLQJ ZRUNV FRUUHFWO\ \RXU EHVW SUDFWLFH DQG LQ VRPH FDVHV D UHTXLUHPHQW LV WR DOZD\V FDOO VHW&RQWHQW7\SH ¿ UVW %()25( \RX FDOO WKH PHWKRG WKDW JLYHV \RX \RXU RXWSXW VWUHDP JHW:ULWHU RU JHW2XWSXW6WUHDP

7KDW¶OO JXDUDQWHH \RX ZRQ¶W UXQ LQWR FRQÀ LFWV EHWZHHQ WKH FRQWHQW W\SH DQG WKH RXWSXW VWUHDP

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

WKHUH DUH QR

'XPE 4XHVWLRQV

4

7HY DO YOU HAVE TO SET THE CONTENT TYPE #AN T SERVERS lGURE IT OUT FROM THE EXTENSION OF THE lLE

$

-OST SERVERS CAN FOR STATIC CONTENT )N !PACHE FOR EXAMPLE YOU CAN SET UP -)-% TYPES BY MAPPING A SPECIFIC FILE EXTENSION TXT JAR ETC TO A SPECIFIC CONTENT TYPE AND !PACHE WILL USE THAT TO SET THE CONTENT TYPE IN THE (440 HEADER "UT WE RE TALKING ABOUT WHAT HAPPENS INSIDE A SERVLET WHERE THERE )3 NO FILE 9OU RE THE ONE WHO IS SENDING BACK THE RESPONSE THE #ONTAINER HAS NO IDEA WHAT YOU RE SENDING

4

"UT WHAT ABOUT THAT LAST EXAMPLE WHERE YOU READ A SPECIlC *!2 lLE #AN T THE #ONTAINER SEE THAT YOU RE READING A *!2

$

.O !LL WE DID FROM THE SERVLET WAS READ THE BYTES OF A FILE THAT JUST HAPPENED TO BE A *!2 FILE AND TURN AROUND AND WRITE THOSE BYTES TO THE OUTPUT STREAM 4HE #ONTAINER HAS NO IDEA WHAT WE WERE UP TO WHEN WE READ THOSE BYTES &OR ALL IT KNOWS WE RE READING FROM ONE TYPE OF THING AND WRITING SOMETHING COMPLETELY DIFFERENT IN THE RESPONSE

4

(OW CAN ) lND OUT WHAT THE COMMON CONTENT TYPES ARE

$

$O A 'OOGLE SEARCH 3ERIOUSLY .EW -)-% TYPES ARE BEING ADDED ALL THE TIME BUT YOU CAN EASILY FIND LISTS ON THE 7EB 9OU CAN ALSO LOOK IN YOUR BROWSER PREFERENCES FOR A LIST OF THOSE THAT HAVE BEEN CONFIGURED FOR YOUR BROWSER AND YOU CAN CHECK YOUR 7EB SERVER CONFIGURATION FILES AS WELL !GAIN YOU DON T HAVE TO WORRY ABOUT THIS FOR THE EXAM AND IT S NOT LIKELY TO CAUSE YOU MUCH STRESS IN THE REAL WORLD EITHER

4

7AIT A SECOND WHY WOULD YOU USE A SERVLET TO SEND BACK THAT *!2 lLE WHEN YOU CAN JUST HAVE THE WEB SERVER SEND IT BACK AS A RESOURCE )N OTHER WORDS WHY WOULDN T YOU HAVE THE USER CLICK A LINK THAT GOES TO THE *!2 INSTEAD OF TO A SERVLET #AN T THE SERVER BE CONlGURED TO SEND BACK THE *!2 DIRECTLY WITHOUT EVEN '/).' THROUGH A SERVLET

$

9ES 'OOD QUESTION 9OU #/5,$ CONFIGURE THE WEB SERVER SO THAT THE USER CLICKS AN (4-, LINK THAT GOES TO SAY THE *!2 FILE SITTING ON THE SERVER JUST LIKE ANY OTHER STATIC RESOURCE INCLUDING *0%'S AND TEXT FILES AND THE SERVER JUST SENDS IT BACK IN THE RESPONSE "UT WE RE ASSUMING THAT YOU MIGHT HAVE OTHER THINGS THAT YOU WANT TO DO IN THAT SERVLET "%&/2% SENDING BACK THE STREAM 9OU MIGHT FOR EXAMPLE NEED LOGIC IN THE SERVLET THAT DETERMINES WHICH *!2 FILE TO SEND /R YOU MIGHT BE SENDING BACK BYTES THAT YOU RE CREATING RIGHT THERE ON THE FLY )MAGINE A SYSTEM WHERE YOU TAKE INPUT PARAMETERS FROM THE USER AND THEN USE THEM TO DYNAMICALLY GENERATE A SOUND THAT YOU SEND BACK 3OUND THAT DIDN T PREVIOUSLY EXIST )N OTHER WORDS SOUND THAT S NOT SITTING ON THE SERVER AS A FILE SOMEWHERE 9OU JUST MADE IT UP AND NOW YOU RE SENDING IT BACK IN THE RESPONSE 3O YOU RE RIGHT PERHAPS OUR EXAMPLE OF JUST SENDING BACK A *!2 SITTING ON THE SERVER IS A LITTLE CONTRIVED BUT COME ON USE YOUR IMAGINATION HERE AND EMBELLISH IT WITH ALL SORTS OF THINGS YOU MIGHT ADD TO MAKE IT WORTH BEING A SERVLET -AYBE IT S SOMETHING AS SIMPLE AS PUTTING CODE IN YOUR SERVLET THATˆ ALONG WITH SENDING BACK THE *!2ˆWRITES SOME INFO TO A DATABASE ABOUT THIS PARTICULAR USER /R MAYBE YOU HAVE TO CHECK TO SEE IF HE S EVEN ALLOWED TO DOWNLOAD THIS *!2 BASED ON SOMETHING YOU FIRST READ FROM THE DATABASE

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

3ULQW:ULWHU DQG 2XWSXW6WUHDP

AW]Âź^M OW\ \ _W KPWQKM[ NWZ W]\X]\" KPIZIK\MZ[ WZ Ja\M[ 4HIS IS JUST PLAIN OLD JAVA IO EXCEPT THE 3ERVLET2ESPONSE INTERFACE GIVES YOU ONLY TWO STREAMS TO CHOOSE FROM 3ERVLET/UTPUT3TREAM FOR BYTES OR A 0RINT7RITER FOR CHARACTER DATA

3ULQW:ULWHU ([DPSOH 3ULQW:ULWHU ZULWHU UHVSRQVH JHW:ULWHU ZULWHU SULQWOQ ³VRPH WH[W DQG +70/´ 8VH LW IRU

0RINTING TEXT DATA TO A CHARACTER STREAM !LTHOUGH YOU CAN STILL WRITE CHARACTER DATA TO AN /UTPUT3TREAM THIS IS THE STREAM THAT S DESIGNED TO HANDLE CHARACTER DATA

2XWSXW6WUHDP ([DPSOH 6HUYOHW2XWSXW6WUHDP RXW UHVSRQVH JHW2XWSXW6WUHDP RXW ZULWH D%\WH$UUD\ 8VH LW IRU

7RITING ANYTHING ELSE

<RX 0867 PHPRUL]H WKHVH PHWKRGV <RX KDYH WR NQRZ WKHVH IRU WK H H[DP $QG LWÂśV WULFN\ 1RWLFH WK DW WR ZULWH WR D 6HUYOHW2XWSXW6WUHDP \RX ZULWH EXW WR ZULWH WR D 3U LQW :ULWHU \RX SULQWOQ ,WÂśV QD WXUDO WR DVVXPH WKDW \RX ZULWH WR D ZU LWHU EXW \RX GRQÂśW ,I \RX DOUHDG\ XV H MDYD LR WKHQ \RXÂśYH EHHQ GRZ Q WKLV URDG %XW LI \RX KDYHQÂśW MX VW UHPHPEHU SULQWOQ WR D 3ULQW:ULWHU ZULWH WR DQ 6HUYOHW2XWSXW6 WUHDP 0DNH VXUH \RX UHPHPEHU WKDW WKH PHWKRG QDPHV IRU JHWWLQJ W KH VWUHDP RU WKH ZULWHU ERWK GURS WKH Âż UVW ZRUG LQ WKH UHWXUQHG W\SH 6HUYOHW2XWSXW6WUHDP UHVSRQVH JHW2XWSXW6WUHDP

3ULQW:ULWHU UHVSRQVH JHW:ULWHU

<RX QHHG WR UHFRJQL]H :521 * PHWKRG QDPHV OLNH JHW3 3ULQW ULQW: :ULWH ULWHU

JHW5 W5HV HVSR SRQV QVH6 H WUHDP

JHW6 W6WUH WUHDP DP

JHW2 2XWS 2XW XWSXW XW:U :ULWHU

these are NOT real!

FIY: The PrintWriter actually ÒwrapsÓ the ServletOutputStream. In other words, the PrintWriter has a reference to the ServletOutputStream and delegates calls to it. ThereÕs just ONE output stream back to the client, but the PrintWriter ÒdecoratesÓ the stream by adding higher-level character-friendly methods.

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

AW] KIV [M\ ZM[XWV[M P MILMZ[ aW] KIV ILL ZM[XWV[M PMILMZ[ !ND YOU CAN WONDER WHAT THE DIFFERENCE IS "UT THINK ABOUT IT FOR A SECOND THEN DO THIS EXERCISE

0DWFK WKH PHWKRG FDOO ZLWK LWV EHKDYLRU

'UDZ D OLQH IURP WKH +WWS5HVSRQVH PHWKRG WR WKH PHWKRG¡V EHKDYLRU :H GLG WKH PRVW REYLRXV RQH IRU \RX

UHVSRQVH VHW+HDGHU ³IRR´ ³EDU´

$GGV D QHZ KHDGHU DQG YDOXH WR WKH UHVSRQVH RU DGGV DQ DGGLWLRQDO YDOXH WR DQ H[LVWLQJ KHDGHU

UHVSRQVH DGG+HDGHU ³IRR´ ³EDU´

$ FRQYHQLHQFH PHWKRG WKDW UHSODFHV WKH YDOXH RI DQ H[LVWLQJ KHDGHU ZLWK WKLV LQWHJHU YDOXH RU DGGV D QHZ KHDGHU DQG YDOXH WR WKH UHVSRQVH

UHVSRQVH VHW,QW+HDGHU ³IRR´

,I D KHDGHU ZLWK WKLV QDPH LV DOUHDG\ LQ WKH UHVSRQVH WKH YDOXH LV UHSODFHG ZLWK WKLV YDOXH 2WKHUZLVH DGGV D QHZ KHDGHU DQG YDOXH WR WKH UHVSRQVH

0RETTY OBVIOUS WHEN YOU SEE THEM ALL TOGETHER "UT FOR THE EXAM YOU SHOULD HAVE THEM MEMORIZED SO THAT IF NEXT 4UESDAY THE GUY DOWN THE HALL ASKS h7HAT S THAT RESPONSE METHOD THAT LETS ME ADD A VALUE TO AN EXISTING HEADER v YOU CAN WITHOUT THE SLIGHTEST PAUSE SAY h)T S ADD(EADER AND IT TAKES TWO 3TRINGS FOR THE NAME AND VALUE v *UST LIKE THAT "OTH SET(EADER AND ADD(EADER WILL ADD A HEADER AND VALUE TO THE RESPONSE IF THE HEADER THE l RST ARGUMENT TO THE METHOD IS NOT ALREADY IN THE RESPONSE 4HE DIFFERENCE BETWEEN SET AND ADD SHOWS UP WHEN THE HEADER IS THERE )N THAT CASE

SET(EADER OVERWRITES THE EXISTING VALUE ADD(EADER ADDS AN ADDITIONAL VALUE

DE NSE FROM THE NO 4HERE WAS A RESPO D OA AYL A P ITE QU WITH HEADERS AND ) TELL YOU .OT ONE HEADER E VALUE HAD MORE THAN ON E AS USED IN THE COD W ER AD T(E SE FOR

7HEN YOU CALL SET#ONTENT4YPE hTEXT HTMLv YOU RE SETTING A D(EADER GET IT HEADER JUST AS IF YOU SAID AS OPPOSED TO AD SET(EADER hCONTENT TYPEv hTEXT HTMLv 3O WHAT S THE DIFFERENCE .O DIFFERENCE ASSUMING YOU TYPE THE hCONTENT TYPEv HEADER CORRECTLY 4HE SET(EADER METHOD WON T COMPLAIN IF YOU MISSPELL THE HEADER NAMESˆIT JUST THINKS YOU RE ADDING A NEW KIND OF HEADER "UT SOMETHING ELSE WILL FAIL LATER BECAUSE NOW YOU HAVEN T PROPERLY SET THE CONTENT TYPE OF THE RESPONSE

(The Ăžrst person to send us an mp3 Ăžle of them actually reciting this poem, with the right timing and everything, gets a special edition t-shirt.) \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW UHGLUHFW

*]\ [WUM\QUM[ aW] R][\ LWV¼\ _IV\ \W LMIT _Q\P \PM ZM[XWV[M aW]Z[MTN 9OU CAN CHOOSE TO HAVE SOMETHING ELSE HANDLE THE RESPONSE FOR YOUR REQUEST 9OU CAN EITHER REDIRECT THE REQUEST TO A COMPLETELY DIFFERENT 52, OR YOU CAN DISPATCH THE REQUEST TO SOME OTHER COMPONENT IN YOUR WEB APP TYPICALLY A *30

5HGLUHFW

&OLHQW W\SHV D 85/ LQWR WKH EURZVHU EDU

7KH UHTXHVW JRHV WR WKH VHUYHU &RQWDLQHU

7KH VHUYOHW GHFLGHV WKDW

WKH UHTXHVW VKRXOG JR WR D FRPSOHWHO\ GLIIHUHQW 85/

UHTXHVW

&RGH5HWXUQ

7KH EURZVHU JHWV WKH UHVSRQVH VHHV WKH ´ µ VWDWXV FRGH DQG ORRNV IRU D ´/RFDWLRQµ KHDGHU

7KH +773 UHVSRQVH KDV D VWDWXV FRGH ´ µ DQG D ´/RFDWLRQµ KHDGHU ZLWK D 85/ DV WKH YDOXH

7KH VHUYOHW FDOOV VHQG5HGLUHFW D6WULQJ RQ WKH UHVSRQVH DQG WKDW·V LW

&RGH5HWXUQ

+773 /RFDWLRQ ZZZ 2. HGO\VPDUW FRP ZLFN 'DWH :HG 1RY *0 6HUYHU $SDF 7 &R\RWH KH &RQQHFWLRQ FORVH

UHVSRQVH

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

7KH EURZVHU PDNHV D QHZ UHTXHVW XVLQJ WKH 85/ WKDW ZDV WKH YDOXH RI WKH ´/RFDWLRQµ KHDGHU LQ WKH SUHYLRXV UHVSRQVH 7KH XVHU PLJKW QRWLFH WKDW WKH 85/ LQ WKH EURZVHU EDU FKDQJHG

7KHUH·V QRWKLQJ XQLTXH DERXW WKH UHTXHVW HYHQ WKRXJK LW KDSSHQHG WR EH WULJJHUHG E\ D UHGLUHFW UHTXHVW

7KH VHUYHU JHWV WKH WKLQJ DW WKH UHTXHVWHG 85/ 1RWKLQJ VSHFLDO KHUH KWPO! KHDG! KHDG! ERG\! ERG\! KWPO!

+RZ·G , HQG XS KHUH"

7KH EURZVHU UHQGHUV WKH QHZ SDJH 7KH XVHU LV 7KH +773 UHVSRQVH LV MXVW VXUSULVHG OLNH DQ\ RWKHU UHVSRQVH H[FHSW LW LVQ·W FRPLQJ IURP WKH ORFDWLRQ WKH FOLHQW W\SHG LQ Kmjhjak]

+773 /RFDWLRQ ZZZ 2. HGO\VPDUW FRP ZLFN 'DWH :HG 1RY *0 6HUYHU $SDF 7 &R\RWH KH &RQQHFWLRQ FORVH

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

VHUYOHW UHGLUHFW

;MZ ^TM\ ZMLQZMK\ UISM[ \PM JZW_[MZ LW \PM _WZS ! REDIRECT LETS THE SERVLET OFF THE HOOK COMPLETELY !FTER DECIDING THAT IT CAN T DO THE WORK THE SERVLET SIMPLY CALLS THE SEND2EDIRECT METHOD LI ZRUNV)RU0H ^ KDQGOH WKH UHTXHVW ` HOVH ^ UHVSRQVH VHQG5HGLUHFW ³KWWS ZZZ RUHLOO\ FRP´ `

ser want the brow The URL yothu e request. This is to use for ient will see. what the cl

=[QVO ZMTI\Q^M =:4[ QV [ MVL:MLQZMK\ 9OU CAN USE A RELATIVE 52, AS THE ARGUMENT TO SEND2EDIRECT INSTEAD OF SPECIFYING THE WHOLE hHTTP WWW v THING 2ELATIVE 52,S COME IN TWO m AVORS WITH OR WITHOUT A STARTING FORWARD SLASH h v )MAGINE THE CLIENT ORIGINALLY TYPED IN KWWS ZZZ ZLFNHGO\VPDUW FRP P\$SS FRRO EDU GR

knows the d The Container URL starte t es qu original re yApp/cool path, so m from the use a forward VHQG5HGLUHFW ÂłIRR VWXII KWPO´ O´ if you donĂ•tpart of the path slash, that to the front of 4HE #ONTAINER BUILDS THE FULL 52, IT NEEDS THIS FOR THE h,OCATIONv HEADER IT OR , is prepended lĂ“. PUTS IN THE (440 RESPONSE RELATIVE TO THE ORIGINAL REQUEST 52, L ST Ă’foo/stuff.htm 7HEN THE REQUEST COMES INTO THE SERVLET NAMED hBAR DOv THE SERVLET CALLS AR TH SEND2EDIRECT WITH A RELATIVE 52, THAT DOES ./4 START WITH A FORWARD SLASH ST IT

KWWS ZZZ ZLFNHGO\VPDUW FRP P\$SS FRRO IRR VWXII KWPO R

"UT IF THE ARGUMENT TO SEND2EDIRECT $/%3 START WITH A FORWARD SLASH VHQG5HGLUHFW ³ IRR VWXII KWPO´ 4HE #ONTAINER BUILDS THE COMPLETE 52, RELATIVE TO THE WEB APP ITSELF INSTEAD OF RELATIVE TO THE ORIGINAL 52, OF THE REQUEST 3O THE NEW 52, WILL BE

The forward slash at the beginning means Òrelative to the root of this web appÓ (in this case, the web app is ÒmyAppÓ).

KWWS ZZZ ZLFNHGO\VPDUW FRP P\$SS IRR VWXII KWPO

t of the See... the ÒcoolÓ par e. path isnÕt here this tim

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

<RX FDQ¡W GR D VHQG5HGLUHFW DIWHU ZULWLQJ WR WKH UHVSRQVH YLRXV EXW LWÂśV WKH /$: VR O\ RE REDE ÂśV SU 7KDW ZHÂśUH MXVW PDNLQJ VXUH W LQ ,I \RX ORRN XS VHQG5HGLUHF URZV DQ WKH $3, \RXÂśOO VHH WKDW LW WK WU\ WR LQYRNH ,OOHJDO6WDWH([FHSWLRQ LI \RX OUHDG\ EHHQ LW DIWHU ÂłWKH UHVSRQVH KDV D FRPPLWWHG ´ WKDW WKH %\ ÂłFRPPLWWHG´ WKH\ PHDQ KDW MXVW PHDQV UHVSRQVH KDV EHHQ VHQW 7 WKH VWUHDP WKH GDWD KDV EHHQ Ă€ XVKHG WR HDQV \RX FDQÂśW )RU SUDFWLFDO SXUSRVHV LW P KHQ FDOO ZULWH WR WKH UHVSRQVH DQG W VHQG5HGLUHFW LOO WHOO \RX WKDW %XW VRPH SLFN\ SURIHVVRU Z H WR WKH VWUHDP WHFKQLFDOO\ \RX FRXOG ZULW VHQG5HGLUHFW ZLWKRXW Ă€ XVKLQJ DQG WKHQ LRQ %XW LW ZRXOG FHSW Q H[ VH D FDX OGQÂśW ZRX J WR GR VR ZH EH D FRPSOHWHO\ VWXSLG WKLQ W WKDW ZH MXVW GLG ZRQÂśW WDON DERXW LW ([FHS WDON DERXW LW

HV PDNH D ,Q \RXU VHUYOHW IRU JRVK VDN UHTXHVW RU GR GHFLVLRQ (LWKHU KDQGOH WKH RPHRQH (/6( D VHQG5HGLUHFW WR KDYH V KDQGOH WKH UHTXHVW ³RQFH LWœV %\ WKH ZD\ WKLV LGHD WKDW DSSOLHV WR FRPPLWWHG LWœV WRR ODWH´ DOVR WDWXV FRGHV WKH VHWWLQJ KHDGHUV FRRNLHV V FRQWHQW W\SH DQG VR RQ

:DWFK LW

VHQG5HGLUH FW WDNHV D 6WULQJ 127 D 85/ REMHFW

:HOO LW WDNHV D 6WULQJ WKDW ,6 D 85/ 7KH S VHQG5HGLUHFW

RLQW LV GRHV 127 WD NH DQ <RX SDVV LW D 6 WULQJ WKDWœV HLWKH REMHFW RI W\SH 85/ U D UHODWLYH RQH ,I W FRPSOHWH 85/ KH &RQWDLQHU F RU D DQœW EXLOG D UH LQWR D IXOO RQH ODWLYH 85/ LWœOO WKURZ DQ ,OO HJDO6WDWH([F 7KH WULFN\ SDUW L HSWLRQ V WR UHPHPEH U WKDW 7+,6 LV VHQG5HGLUHFW Q ZURQJ HZ 85/ ³KWWS ZZZ RUHLOO\ F RP´

No ! It look sendRedirecst(so right, but itÕs SO w ro ) takes a St ring. Period.ng.

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW GLVSDWFK

) ZMY]M[\ LQ[XI\KP LWM[ \PM _WZS WV \PM [MZ ^MZ [QLM !ND THAT S THE BIG DIFFERENCE BETWEEN A REDIRECT AND A REQUEST DISPATCHˆREDIRECT MAKES THE CLIENT DO THE WORK WHILE REQUEST DISPATCH MAKES SOMETHING ELSE ON THE SERVER DO THE WORK 3O REMEMBER REDIRECT CLIENT REQUEST DISPATCH SERVER 7E LL SAY MORE ABOUT REQUEST DISPATCH IN A LATER CHAPTER BUT THESE TWO PAGES SHOULD GIVE YOU A QUICK LOOK AT THE HIGHLIGHTS

5HTXHVW 'LVSDWFK

8VHU W\SHV D VHUYOHW·V 85/ LQWR WKH EURZVHU EDU

7KH UHTXHVW JRHV WR WKH VHUYHU &RQWDLQHU

7KH VHUYOHW GHFLGHV WKDW WKH UHTXHVW VKRXOG JR WR DQRWKHU SDUW RI WKH ZHE DSS LQ WKLV FDVH D -63

UHTXHVW

&RGH5HWXUQ

7KH VHUYOHW FDOOV

7KH EURZVHU JHWV WKH UHVSRQVH LQ WKH XVXDO ZD\ DQG UHQGHUV LW IRU WKH XVHU 6LQFH WKH EURZVHU ORFDWLRQ EDU GLGQ·W FKDQJH WKH XVHU GRHV QRW NQRZ WKDW WKH -63 JHQHUDWHG WKH UHVSRQVH

F]o

5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH DQG WKH -63 WDNHV RYHU WKH UHVSRQVH

&RGH5HWXUQ

+773 /RFDWLRQ ZZZ 2. HGO\VPDUW FRP ZLFN 'DWH :HG 1RY *0 6HUYHU $SDF 7 &R\RWH KH &RQQHFWLRQ FORVH

UHVSRQVH

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

UHVXOW MVS

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

:MLQZMK\ ^[ :MY]M[\ ,Q[XI\KP , GRQ·W KDYH WLPH IRU WKLV 7HOO \RX ZKDW³ZK\ GRQ·W \RX FDOO %DUQH\ 0D\EH +( KDV WLPH IRU WKLV FUDS

+H\ .DUL WKLV LV 'DQ , ZDQW \RXU KHOS ZLWK D FOLHQW ,·OO IRUZDUG \RX WKH GHWDLOV RQ KRZ WR JHW EDFN WR KLP EXW , QHHG \RX WR WDNH RYHU QRZ <HV , .12: \RX KDYH QHHGV WRR \HV , .12: KRZ LPSRUWDQW WKH 9LHZ LV LQ 0RGHO 9LHZ &RQWUROOHU QR , GRQ·W WKLQN , FDQ ÀQG DQRWKHU -63 MXVW OLNH WKDW ZKDW" , GLGQ·W FDWFK WKDW" <RX·UH EUHDNLQJ XS VRUU\ FDQ·W KHDU D WKLQJ ORVLQJ SDFNHWV

5HGLUHFW

:KHQ D VHUYOHW GRHV D UHTXHVW GLVSDWFK LW·V OLNH DVNLQJ D FR ZRUNHU WR WDNH RYHU ZRUNLQJ ZLWK D FOLHQW 7KH FR ZRUNHU HQGV XS UHVSRQGLQJ WR WKH FOLHQW EXW WKH FOLHQW GRHVQ·W FDUH DV ORQJ DV VRPHRQH UHVSRQGV

5HTXHVW 'LVSDWFK

7KH XVHU QHYHU NQRZV VRPHRQH HOVH WRRN RYHU EHFDXVH WKH 85/ LQ WKH EURZVHU EDU GRHVQ·W FKDQJH

:KHQ D VHUYOHW GRHV D UHGLUHFW LW·V OLNH DVNLQJ WKH FOLHQW WR FDOO VRPHRQH HOVH LQVWHDG ,Q WKLV FDVH WKH FOLHQW LV WKH EURZVHU QRW WKH XVHU 7KH EURZVHU PDNHV WKH QHZ FDOO RQ WKH XVHU·V EHKDOI DIWHU WKH RULJLQDOO\ UHTXHVWHG VHUYOHW VD\V ´6RUU\ FDOO WKLV JX\ LQVWHDG µ 7KH XVHU VHHV WKH QHZ 85/ LQ WKH EURZVHU

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHYLHZ RI +WWS6HUYOHW5HVSRQVH

:M^QM_" 0 \\X;MZ ^TM\:M[XWV[M :MDD=L HGAFLK

ƒ <RX XVH WKH 5HVSRQVH WR VHQG GDWD EDFN WR WKH FOLHQW ƒ 7KH PRVW FRPPRQ PHWKRGV \RX¡OO FDOO RQ WKH UHVSRQVH REMHFW +WWS6HUYOHW5HVSRQVH DUH VHW&RQWHQW7\SH DQG JHW:ULWHU

ƒ %H FDUHIXOÂłPDQ\ GHYHORSHUV DVVXPH WKH PHWKRG LV JHW3ULQW:ULWHU EXW LW¡V JHW:ULWHU

ƒ 7KH JHW:ULWHU PHWKRG OHWV \RX GR FKDUDFWHU , 2 WR ZULWH +70/ RU VRPHWKLQJ HOVH WR WKH VWUHDP

ƒ <RX FDQ DOVR XVH WKH UHVSRQVH WR VHW KHDGHUV VHQG HUURUV DQG DGG FRRNLHV

ƒ ,Q WKH UHDO ZRUOG \RX¡OO SUREDEO\ XVH D -63 WR VHQG PRVW +70/ UHVSRQVHV EXW \RX PD\ VWLOO XVH D UHVSRQVH VWUHDP WR VHQG ELQDU\ GDWD OLNH D -$5 ILOH SHUKDSV WR WKH FOLHQW

ƒ 7KH PHWKRG \RX FDOO RQ \RXU UHVSRQVH IRU JHWWLQJ D ELQDU\ VWUHDP LV JHW2XWSXW6WUHDP

6HUYOHW5HVSRQVH LQWHUIDFH MDYD[ VHUYOHW 6HUYOHW5HVSRQVH

LQWHUIDFH!!

6HUYOHW5HVSRQVH JHW%XIIHU6L]H

VHW&RQWHQW7\SH

JHW2XWSXW6WUHDP

JHW:ULWHU

VHW&RQWHQW/HQJWK

0$1< PRUH PHWKRGV

ƒ 7KH VHW&RQWHQW7\SH PHWKRG WHOOV WKH EURZVHU KRZ WR KDQGOH WKH GDWD FRPLQJ LQ ZLWK WKH UHVSRQVH 7\SLFDO FRQWHQW W\SHV DUH ´WH[W KWPOÂľ ´DSSOLFDWLRQ SGIÂľ DQG ´LPDJH MSHJÂľ

ƒ <RX GRQ¡W KDYH WR PHPRUL]H FRQWHQW W\SHV DOVR NQRZQ DV 0,0( W\SHV ƒ <RX FDQ VHW UHVSRQVH KHDGHUV XVLQJ DGG+HDGHU RU VHW+HDGHU 7KH GLIIHUHQFH GHSHQGV RQ ZKHWKHU WKH KHDGHU LV DOUHDG\ SDUW RI WKH UHVSRQVH ,I LW LV VHW+HDGHU ZLOO UHSODFH WKH YDOXH EXW DGG+HDGHU ZLOO DGG DQ DGGLWLRQDO YDOXH WR WKH H[LVWLQJ UHVSRQVH ,I WKH KHDGHU LV QRW DOUHDG\ SDUW RI WKH UHVSRQVH WKHQ VHW+HDGHU DQG DGG+HDGHU EHKDYH LQ H[DFWO\ WKH VDPH ZD\

ƒ ,I \RX GRQ¡W ZDQW WR UHVSRQG WR D UHTXHVW \RX FDQ UHGLUHFW WKH UHTXHVW WR D GLIIHUHQW 85/ 7KH EURZVHU WDNHV FDUH RI VHQGLQJ WKH QHZ UHTXHVW WR WKH 85/ \RX SURYLGH

+WWS6HUYOHW5HVSRQVH LQWHUIDFH MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HVSRQVH

LQWHUIDFH!!

+WWS6HUYOHW5HVSRQVH DGG&RRNLH

DGG+HDGHU

HQFRGH85/

VHQG(UURU

VHW6WDWXV

VHQG5HGLUHFW

0$1< PRUH PHWKRGV

ƒ 7R UHGLUHFW D UHTXHVW FDOO VHQG5HGLUHFW D6WULQJ85/ RQ WKH UHVSRQVH ƒ <RX FDQQRW FDOO VHQG5HGLUHFW DIWHU WKH UHVSRQVH LV FRPPLWWHG ,Q RWKHU ZRUGV LI \RX¡YH DOUHDG\ ZULWWHQ VRPHWKLQJ WR WKH VWUHDP LW¡V WRR ODWH WR GR D UHGLUHFW

ƒ $ UHTXHVW UHGLUHFW LV GLIIHUHQW IURP D UHTXHVW GLVSDWFK $ UHTXHVW GLVSDWFK FRYHUHG PRUH LQ DQRWKHU FKDSWHU KDSSHQV RQ WKH VHUYHU ZKLOH D UHGLUHFW KDSSHQV RQ WKH FOLHQW $ UHTXHVW GLVSDWFK KDQGV WKH UHTXHVW WR DQRWKHU FRPSRQHQW RQ WKH VHUYHU XVXDOO\ ZLWKLQ WKH VDPH ZHE DSS $ UHTXHVW UHGLUHFW VLPSO\ WHOOV WKH EURZVHU WR JR D GLIIHUHQW 85/

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

0RFN ([DP &KDSWHU

(OW WOULD SERVLET CODE FROM A SERVICE METHOD E G GR3RVW RETRIEVE THE VALUE OF THE h5SER !GENTv HEADER FROM THE REQUEST #HOOSE ALL THAT APPLY

R ! 6WULQJ XVHU$JHQW UHTXHVW JHW3DUDPHWHU ³8VHU $JHQW´

R " 6WULQJ XVHU$JHQW UHTXHVW JHW+HDGHU ³8VHU $JHQW´ R # 6WULQJ XVHU$JHQW UHTXHVW JHW5HTXHVW+HDGHU ³0R]LOOD´

R $ 6WULQJ XVHU$JHQW JHW6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU ³8VHU $JHQW´

7HICH (440 METHODS ARE USED TO SHOW THE CLIENT WHAT THE SERVER IS RECEIVING #HOOSE ALL THAT APPLY

R ! '%4 R " 054 R # 42!#% R $ 2%452. R % /04)/.3

7HICH METHOD OF +WWS6HUYOHW5HVSRQVH IS USED TO REDIRECT AN (440 REQUEST TO ANOTHER 52,

R ! VHQG85/

R " UHGLUHFW85/

R # UHGLUHFW+WWS

R $ VHQG5HGLUHFW

R % JHW5HTXHVW'LVSDWFKHU

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

PRFN H[DP

7HICH (440 METHODS ARE ./4 CONSIDERED IDEMPOTENT #HOOSE ALL THAT APPLY

R ! '%4 R " 0/34 R # (%!$ R $ 054

'IVEN UHT IS A +WWS6HUYOHW5HTXHVW WHICH GETS A BINARY INPUT STREAM #HOOSE ALL THAT APPLY

R ! %LQDU\,QSXW6WUHDP V UHT JHW,QSXW6WUHDP R " 6HUYOHW,QSXW6WUHDP V UHT JHW,QSXW6WUHDP R # %LQDU\,QSXW6WUHDP V UHT JHW%LQDU\6WUHDP R $ 6HUYOHW,QSXW6WUHDP V UHT JHW%LQDU\6WUHDP

(OW WOULD YOU SET A HEADER NAMED h#/.4%.4 ,%.'4(v IN THE +WWS6HUYOHW5HVSRQVH OBJECT #HOOSE ALL THAT APPLY

R ! UHVSRQVH VHW+HDGHU &217(17 /(1*7+ ´QXP%\WHV´ R " UHVSRQVH VHW+HDGHU ³&217(17 /(1*7+´ ´QXP%\WHV´ R # UHVSRQVH VHW6WDWXV R $ UHVSRQVH VHW+HDGHU ³&217(17 /(1*7+´

#HOOSE THE SERVLET CODE FRAGMENT THAT GETS A BINARY STREAM FOR WRITING AN IMAGE OR OTHER BINARY TYPE TO THE +WWS6HUYOHW5HVSRQVH

R ! MDYD LR 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU R " 6HUYOHW2XWSXW6WUHDP RXW UHVSRQVH JHW2XWSXW6WUHDP R # MDYD LR 3ULQW:ULWHU RXW QHZ 3ULQW:ULWHU UHVSRQVH JHW:ULWHU

R $ 6HUYOHW2XWSXW6WUHDP RXW UHVSRQVH JHW%LQDU\6WUHDP

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

7HICH METHODS ARE USED BY A SERVLET TO HANDLE FORM DATA FROM A CLIENT #HOOSE ALL THAT APPLY

R ! +WWS6HUYOHW GR+HDG

R " +WWS6HUYOHW GR3RVW

R # +WWS6HUYOHW GR)RUP

R $ 6HUYOHW5HTXHVW GR*HW

R % 6HUYOHW5HTXHVW GR3RVW

R & 6HUYOHW5HTXHVW GR)RUP

7HICH OF THE FOLLOWING METHODS ARE DECLARED IN +WWS6HUYOHW5HTXHVW AS OPPOSED TO IN 6HUYOHW5HTXHVW #HOOSE ALL THAT APPLY

R ! JHW0HWKRG

R " JHW+HDGHU

R # JHW&RRNLHV

R $ JHW,QSXW6WUHDP

R % JHW3DUDPHWHU1DPHV

(OW SHOULD SERVLET DEVELOPERS HANDLE THE +WWS6HUYOHW S VHUYLFH METHOD WHEN EXTENDING +WWS6HUYOHW #HOOSE ALL THAT APPLY

R ! 4HEY SHOULD OVERRIDE THE VHUYLFH METHOD IN MOST CASES R " 4HEY SHOULD CALL THE VHUYLFH METHOD FROM GR*HW OR GR3RVW

R # 4HEY SHOULD CALL THE VHUYLFH METHOD FROM THE LQLW METHOD R $ 4HEY SHOULD OVERRIDE AT LEAST ONE GR;;; METHOD SUCH AS GR3RVW

\RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

PRFN DQVZHUV

&KDSWHU $QVZHUV (API)

(OW WOULD SERVLET CODE FROM A SERVICE METHOD E G GR3RVW RETRIEVE THE VALUE OF THE h5SER !GENTv HEADER FROM THE REQUEST #HOOSE ALL THAT APPLY

R ! 6WULQJ XVHU$JHQW UHTXHVW JHW3DUDPHWHU ³8VHU $JHQW´

-Option B shows the correct method call passing in the header name as a String parameter.

R " 6WULQJ XVHU$JHQW UHTXHVW JHW+HDGHU ³8VHU $JHQW´ R # 6WULQJ XVHU$JHQW UHTXHVW JHW5HTXHVW+HDGHU ³0R]LOOD´

R $ 6WULQJ XVHU$JHQW JHW6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU ³8VHU $JHQW´

7HICH (440 METHODS ARE USED TO SHOW THE CLIENT WHAT THE SERVER IS RECEIVING #HOOSE ALL THAT APPLY

R ! '%4 R " 054 R # 42!#% R $ 2%452. R % /04)/.3

-This method is typically used for troubleshooting, not for production.

7HICH METHOD OF +WWS6HUYOHW5HVSRQVH IS USED TO REDIRECT AN (440 REQUEST TO ANOTHER 52,

R ! VHQG85/

R " UHGLUHFW85/

R # UHGLUHFW+WWS

R $ VHQG5HGLUHFW

R % JHW5HTXHVW'LVSDWFKHU

thods) (HF 4, HTTP me

(API)

- Option D is correct, and of the methods listed, itÕs the only one that exists in HttpServletResponse

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

UHTXHVW DQG UHVSRQVH

7HICH (440 METHODS ARE ./4 CONSIDERED IDEMPOTENT #HOOSE ALL THAT APPLY

R ! '%4 R " 0/34 R # (%!$ R $ 054

nt (HF 4, idempote requests)

meant to convey -By design, POST isthe state of the requests to updatel the sam update server. In genera lied multipe le times. should not be app

'IVEN UHT IS A +WWS6HUYOHW5HTXHVW WHICH GETS A BINARY INPUT STREAM #HOOSE ALL THAT APPLY

(API)

R ! %LQDU\,QSXW6WUHDP V UHT JHW,QSXW6WUHDP s the R " 6HUYOHW,QSXW6WUHDP V UHT JHW,QSXW6WUHDP -Option B speciĂžeand correct method e. the R # %LQDU\,QSXW6WUHDP V UHT JHW%LQDU\6WUHDP correct return typ R $ 6HUYOHW,QSXW6WUHDP V UHT JHW%LQDU\6WUHDP

(OW WOULD YOU SET A HEADER NAMED h#/.4%.4 ,%.'4(v IN THE +WWS6HUYOHW5HVSRQVH OBJECT #HOOSE ALL THAT APPLY

( API)

R ! UHVSRQVH VHW+HDGHU &217(17 /(1*7+ ´QXP%\WHV´ t B shows the correc R " UHVSRQVH VHW+HDGHU ³&217(17 /(1*7+´ ´QXP%\WHV´ -Option set header way to an HTTPame ters, R # UHVSRQVH VHW6WDWXV with two String par representing the header one e. R $ UHVSRQVH VHW+HDGHU ³&217(17 /(1*7+´ name and the other the valu

#HOOSE THE SERVLET CODE FRAGMENT THAT GETS A BINARY STREAM FOR WRITING AN IMAGE OR OTHER BINARY TYPE TO THE +WWS6HUYOHW5HVSRQVH

R ! MDYD LR 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU R " 6HUYOHW2XWSXW6WUHDP RXW UHVSRQVH JHW2XWSXW6WUHDP R # MDYD LR 3ULQW:ULWHU RXW

( API) -Option A is incorrect because it uses a character-oriented PrintWriter

QHZ 3ULQW:ULWHU UHVSRQVH JHW:ULWHU

R $ 6HUYOHW2XWSXW6WUHDP RXW UHVSRQVH JHW%LQDU\6WUHDP \RX DUH KHUH

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 4. Being a Servlet

PRFN DQVZHUV

7HICH METHODS ARE USED BY A SERVLET TO HANDLE FORM DATA FROM A CLIENT #HOOSE ALL THAT APPLY

(API)

R ! +WWS6HUYOHW GR+HDG

R " +WWS6HUYOHW GR3RVW

wrong R # +WWS6HUYOHW GR)RUP

-Options C-F are hod se met s donÕt the e aus bec R $ 6HUYOHW5HTXHVW GR*HW

exist. R % 6HUYOHW5HTXHVW GR3RVW

R & 6HUYOHW5HTXHVW GR)RUP

7HICH OF THE FOLLOWING METHODS ARE DECLARED IN +WWS6HUYOHW5HTXHVW AS OPPOSED TO IN 6HUYOHW5HTXHVW #HOOSE ALL THAT APPLY

(API)

R ! JHW0HWKRG

-Options A, B, and C all R " JHW+HDGHU

relate to components of an HTTP request. R # JHW&RRNLHV

R $ JHW,QSXW6WUHDP

R % JHW3DUDPHWHU1DPHV

(OW SHOULD SERVLET DEVELOPERS HANDLE THE +WWS6HUYOHW S VHUYLFH METHOD WHEN EXTENDING +WWS6HUYOHW #HOOSE ALL THAT APPLY

( API)

R ! 4HEY SHOULD OVERRIDE THE VHUYLFH METHOD IN MOST CASES R " 4HEY SHOULD CALL THE VHUYLFH METHOD FROM GR*HW OR GR3RVW

R # 4HEY SHOULD CALL THE VHUYLFH METHOD FROM THE LQLW METHOD R $ 4HEY SHOULD OVERRIDE AT LEAST ONE GR;;; METHOD SUCH AS GR3RVW

-Option D is correct, developers typically focus on the doGet(), and doPost() methods

FKDSWHU

Chapter 4. Being a Servlet Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

%HLQJ D :HE $SS %XW PDVWHU KH XVHG D FRQWH[W DWWULEXWH ZKHQ KH VKRXOG KDYH XVHG D UHTXHVW DWWULEXWH +H PXVW EH NLOOHG

<RX PXVW XQGHUVWDQG KRZ WKH SLHFHV RI WKH ZHE DSS LQWHUDFW DQG \RX PXVW UHVSHFW WKH WKUHDGV ,I \RX VFRUH ZHOO RQ WKLV FKDSWHU·V PRFN H[DP , ZLOO OHW \RX OLYH

1R VHUYOHW VWDQGV DORQH ,Q WRGD\¶V PRGHUQ ZHE DSS PDQ\ FRPSRQHQWV ZRUN WRJHWKHU WR DFFRPSOLVK D JRDO <RX KDYH PRGHOV FRQWUROOHUV DQG YLHZV <RX KDYH SDUDPHWHUV DQG DWWULEXWHV <RX KDYH KHOSHU FODVVHV %XW KRZ GR \RX WLH WKH SLHFHV WRJHWKHU" +RZ GR \RX OHW FRPSRQHQWV VKDUH LQIRUPDWLRQ" +RZ GR \RX KLGH LQIRUPDWLRQ" +RZ GR \RX PDNH LQIRUPDWLRQ WKUHDG VDIH" <RXU OLIH PD\ GHSHQG RQ WKH DQVZHUV VR EH VXUH \RX KDYH SOHQW\ RI WHD ZKHQ \RX JR WKURXJK WKLV FKDSWHU $QG QRW WKDW IRRI\ KHUEDO GHFDI FUDS

WKLV LV D QHZ FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

RI¿FDO 6XQ H[DP REMHFWLYHV

7KH :HE &RQWDLQHU 0RGHO

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 )RU WKH VHUYOHW DQG 6HUYOHW&RQWH[W LQLWLDOL]DWLRQ SDUDPHWHUV ZULWH VHUYOHW FRGH WR DFFHVV LQLWLDOL]DWLRQ SDUDPHWHUV DQG FUHDWH GHSOR\PHQW GHVFULSWRU HOHPHQWV IRU GHFODULQJ LQLWLDOL]DWLRQ SDUDPHWHUV

)RU WKH IXQGDPHQWDO VHUYOHW DWWULEXWH VFRSHV UHTXHVW VHVVLRQ DQG FRQWH[W ZULWH VHUYOHW FRGH WR DGG UHWULHYH DQG UHPRYH DWWULEXWHV JLYHQ D XVDJH VFHQDULR LGHQWLI\ WKH SURSHU VFRSH IRU DQ DWWULEXWH DQG LGHQWLI\ PXOWL WKUHDGLQJ LVVXHV DVVRFLDWHG ZLWK HDFK VFRSH

'HVFULEH WKH HOHPHQWV RI WKH :HE FRQWDLQHU UHTXHVW SURFHVVLQJ PRGHO )LOWHU )LOWHU FKDLQ 5HTXHVW DQG UHVSRQVH ZUDSSHUV DQG :HE UHVRXUFH VHUYOHW RU -63 SDJH

!LL OF THE OBJECTIVES IN THIS SECTION ARE COVERED COMPLETELY IN THIS CHAPTER WITH THE EXCEPTION OF WHICH IS COVERED IN THE &ILTERS CHAPTER -OST OF WHAT S IN THIS CHAPTER WILL COME UP IN OTHER PARTS OF THE BOOK BUT IF YOU RE TAKING THE EXAM 4()3 IS THE CHAPTER WHERE WE EXPECT YOU TO LEARN AND MEMORIZE THE OBJECTIVE TOPICS

in the Covered chapter. s r e Filt

'HVFULEH WKH :HE &RQWDLQHU OLIHF\FOH HYHQW PRGHO IRU UHTXHVWV VHVVLRQV DQG ZHE DSSOLFDWLRQV FUHDWH DQG FRQ¿JXUH OLVWHQHU FODVVHV IRU HDFK VFRSH OLIH F\FOH FUHDWH DQG FRQ¿JXUH VFRSH DWWULEXWH OLVWHQHU FODVVHV DQG JLYHQ D VFHQDULR LGHQWLI\ WKH SURSHU DWWULEXWH OLVWHQHU WR XVH

'HVFULEH WKH 5HTXHVW'LVSDWFKHU PHFKDQLVP ZULWH VHUYOHW FRGH WR FUHDWH D UHTXHVW GLVSDWFKHU ZULWH VHUYOHW FRGH WR IRUZDUG RU LQFOXGH WKH WDUJHW UHVRXUFH DQG LGHQWLI\ WKH DGGLWLRQDO UHTXHVW VFRSHG DWWULEXWHV SURYLGHG E\ WKH FRQWDLQHU WR WKH WDUJHW UHVRXUFH

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

, ZDQW P\ HPDLO DGGUHVV WR VKRZ XS RQ WKH EHHU ZHE SDJH P\ VHUYOHW PDNHV EXW , WKLQN P\ HPDLO LV JRQQD FKDQJH DQG , GRQ¡W ZDQW WR KDYH WR UHFRPSLOH P\ VHUYOHW FRGH MXVW WR FKDQJH LW

3QU _IV\[ \W KWVNQO]ZM PQ[ MUIQT ILLZM[[ QV \PM ,, VW\ PIZL KWLM Q\ QV[QLM \PM [MZ ^TM\ KTI[[ (ERE S WHAT +IM DOES NOT WANT IN HIS SERVLET 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³EORRSHU#ZLFNHGO\VPDUW FRP´

dress is BAD ! Hard-coding the ad his email changes? What happens whcoenmpile... re to HeÕll have (E D MUCH RATHER PUT HIS EMAIL ADDRESS IN THE $EPLOYMENT $ESCRIPTOR WEB XML lLE SO THAT WHEN HE DEPLOYS HIS WEB APP HIS SERVLET CAN SOMEHOW hREADv HIS EMAIL ADDRESS FROM THE $$ 4HAT WAY HE WON T HAVE TO HARD CODE HIS ADDRESS IN THE SERVLET CLASS AND TO CHANGE HIS EMAIL HE MODIlES ONLY THE WEB XML lLE WITHOUT HAVING TO TOUCH HIS SERVLET SOURCE CODE

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

LQLW SDUDPHWHUV

1VQ\ 8IZIUM\MZ[ \W \PM ZM[K]M 9OU VE ALREADY SEEN THE REQUEST PARAMETERS THAT CAN COME OVER IN A DO'ET OR DO0OST BUT SERVLETS CAN HAVE INITIALIZATION PARAMETERS AS WELL

,Q WKH '' ZHE [PO ÀOH VHUYOHW! VHUYOHW QDPH!%HHU3DUDP7HVWV VHUYOHW QDPH! VHUYOHW FODVV!7HVW,QLW3DUDPV VHUYOHW FODVV! LQLW SDUDP!

m-name and a You give it a para le. Just make mp Si . lue va parame <servlet> th E D SI IN s sure itÕ . D D e th in element

SDUDP QDPH!DGPLQ(PDLO SDUDP QDPH! SDUDP YDOXH!OLNHZHFDUH#ZLFNHGO\VPDUW FRP SDUDP YDOXH! LQLW SDUDP! VHUYOHW!

,Q WKH VHUYOHW FRGH RXW SULQWOQ JHW6HUYOHW&RQ¿J JHW,QLW3DUDPHWHU ³DGPLQ(PDLO´

Every servlet inher its

a getServletConĂžg

() method.

onĂžg() method The getServletC for it... it returns a... wa And one of its ServletConĂžg. nitParameter(). methods is getI

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

AW] KIVÂź\ ][M [MZ ^TM\ QVQ\ XIZIUM\MZ[ ]V\QT \PM [MZ ^TM\ Q[ QVQ\QITQbML 9OU ALREADY SAW THAT YOUR SERVLET INHERITS GET3ERVLET#ONl G SO YOU CAN CALL THAT FROM ANY METHOD IN YOUR SERVLET TO GET A REFERENCE TO A 3ERVLET#ONl G /NCE YOU HAVE A 3ERVLET#ONl G REFERENCE YOU CAN CALL GET)NIT0ARAMETER "UT REMEMBER YOU CAN T CALL IT FROM YOUR CONSTRUCTOR 4HAT S TOO EARLY IN THE SERVLET S LIFE IT WON T HAVE ITS FULL SERVLETNESS UNTIL THE #ONTAINER CALLS INIT

NO Servlet at this poinConĂžg Too soon... t.

GRHV QRW H[LVW

FRQVWUXFWRU

LQLW 6HUYOHW&RQĂ€ J

the This is when its servlet getsĂžg object. ServletCon

By the time the ser vlet is running service me (doGet(), doPost(), thods got a ServletConÞg.etc.) itÕs

GHVWUR\

LQLWLDOL]HG

VHUYLFH

WKHUH DUH QR

:KHQ WKH &RQWDLQHU LQLWLDOL]HV D VHUYOHW LW PDNHV D XQLTXH 6HUYOHW&RQILJ IRU WKH VHUYOHW 7KH &RQWDLQHU ´UHDGV¾ WKH VHUYOHW LQLW SDUDPHWHUV IURP WKH '' DQG JLYHV WKHP WR WKH 6HUYOHW&RQILJ WKHQ SDVVHV WKH 6HUYOHW&RQILJ WR WKH VHUYOHW¡V LQLW PHWKRG

'XPE 4XHVWLRQV

4

7AY BACK IN THE LAST CHAPTER YOU SAID IT TAKES 47/ THINGS FOR THE SERVLET TO BECOME A CARD CARRYING FEZ WEARING SERVLET 9OU MENTIONED BOTH 3ERVLET#ONl G AND SOMETHING CALLED 3ERVLET#ONTEXT

$

/+ YES WE LL LOOK AT THE 3ERVLET#ONTEXT IN JUST A FEW PAGES &OR NOW WE CARE ONLY ABOUT 3ERVLET#ONFIG BECAUSE THAT S WHERE YOU GET YOUR SERVLET INIT PARAMETERS

4

7AIT A MINUTE )N THE LAST CHAPTER YOU SAID THAT WE COULD OVERRIDE THE INIT METHOD AND NOBODY SAID A WORD ABOUT THE 3ERVLET#ONl G ARGUMENT

$

7E DIDN T MENTION THAT THE INIT METHOD TAKES A 3ERVLET#ONFIG BECAUSE THE ONE YOU OVERRIDE DOESN T TAKE ONE 9OUR SUPERCLASS INCLUDES TWO VERSIONS OF INIT ONE THAT TAKES A 3ERVLET#ONFIG AND A CONVENIENCE VERSION THAT S A NO ARG 4HE INHERITED INIT 3ERVLET#ONFIG METHOD CALLS THE NO ARG INIT METHOD SO THE ONLY ONE YOU NEED TO OVERRIDE IS THE NO ARG VERSION 4HERE S NO LAW THAT STOPS YOU FROM OVERRIDING THE ONE THAT TAKES A 3ERVLET#ONFIG BUT IF YOU $/ THEN YOU BETTER CALL SUPER INIT 3ERVLET#ONFIG "UT THERE S REALLY ./ REASON WHY YOU NEED TO OVERRIDE THE INIT 3ERVLET#ONFIG METHOD SINCE YOU CAN ALWAYS GET YOUR 3ERVLET#ONFIG BY CALLING YOUR INHERITED GET3ERVLET#ONFIG METHOD \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

VHUYOHW LQLW SDUDPHWHUV

<PM [MZ ^TM\ QVQ\ XIZIUM\MZ[ IZM ZMIL WVTa 76+- ¸ _PMV \PM +WV\IQVMZ QVQ\QITQbM[ \PM [MZ ^TM\ 7HEN THE #ONTAINER MAKES A SERVLET IT READS THE $$ AND CREATES THE NAME VALUE PAIRS FOR THE 3ERVLET#ONl G 4HE #ONTAINER NEVER READS THE INIT PARAMETERS AGAIN /NCE THE PARAMETERS ARE IN THE 3ERVLET#ONl G THEY WON T BE READ AGAIN UNTIL UNLESS YOU REDEPLOY THE SERVLET 4HINK ABOUT THAT

&RQWDLQHU UHDGV WKH 'HSOR\PHQW 'HVFULSWRU IRU WKLV VHUYOHW LQFOXGLQJ WKH VHUYOHW LQLW SDUDPHWHUV LQLW SDUDP! &RQWDLQHU

UHDG

&RQWDLQHU FUHDWHV D QHZ 6HUYOHW&RQÀ J LQVWDQFH IRU WKLV VHUYOHW QHZ

VHUYOHW! LQLW SDUDP! SDUDP QDPH! IRR SDUDP QDPH! SDUDP YDOXH! EDU SDUDP YDOXH!

&RQWDLQHU 6HUYOHW&RQÀ J

ZHE [PO

&RQWDLQHU FUHDWHV D QDPH YDOXH SDLU RI 6WULQJV IRU HDFK VHUYOHW LQLW SDUDPHWHU $VVXPH ZH KDYH RQO\ RQH

QHZ

&RQWDLQHU JLYHV WKH 6HUYOHW&RQÀ J UHIHUHQFHV WR WKH QDPH YDOXH LQLW SDUDPHWHUV

QDPH

QDPH 6WULQJ

&RQWDLQHU

init parameter from DD

6WULQJ

QHZ

6HUYOHW&RQÀ J

YDOXH

6WULQJ

6WULQJ

&RQWDLQHU FUHDWHV D QHZ LQVWDQFH RI WKH VHUYOHW FODVV

YDOXH

&RQWDLQHU FDOOV WKH VHUYOHW·V LQLW PHWKRG SDVVLQJ LQ WKH UHIHUHQFH WR WKH 6HUYOHW&RQÀ J

QHZ 6WULQJ

&RQWDLQHU 6HUYOHW &RQÀ J

LQVWDQFH RI 0\6HUYOHW FODVV

6WULQJ

LQLW 6HUYOHW&RQÀ J

&RQWDLQHU LQVWDQFH RI 0\6HUYOHW FODVV

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

6LQFH WKH &RQWDLQHU UHDGV WKH VHUYOHW LQLW SDUDPHWHUV RQO\ RQFH \RX VWLOO FDQ·W FKDQJH \RXU HPDLO DGGUHVV GXULQJ WKH OLIH RI WKH VHUYOHW 6R WKLV LV D GXPE VROXWLRQ

,W·V VWLOO ZD\ EHWWHU WKDQ SXWWLQJ LW LQ P\ VHUYOHW VRXUFH FRGH $OO , KDYH WR GR LV FKDQJH WKH [PO DQG KLW WKH ´UHGHSOR\µ EXWWRQ DQG WKH QHZ DGGUHVV ZLOO EH LQ WKH 6HUYOHW&RQÀJ

WKHUH DUH QR

'XPE 4XHVWLRQV

4

3O UM WHERE S THAT hREDEPLOYv BUTTON ON 4OMCAT

$

7ITH 4OMCAT THERE ISN T A ONE BUTTON REALLY SIMPLE ADMIN TOOL FOR DEPLOYMENT AND REDEPLOYMENT ALTHOUGH THERE IS AN ADMIN TOOL THAT SHIPS WITH 4OMCAT "UT THINK ABOUT ITˆWHAT S THE WORST YOU HAVE TO DO TO CHANGE THE SERVLET S INIT PARAMETERS 9OU MAKE A QUICK CHANGE TO THE WEB XML FILE SHUT DOWN 4OMCAT BIN SHUTDOWN SH THEN RESTART 4OMCAT BIN STARTUP SH /N RESTART 4OMCAT LOOKS IN ITS WEBAPPS DIRECTORY AND DEPLOYS EVERYTHING IT FINDS THERE

4

3URE IT S EASY TO TELL 4OMCAT TO SHUTDOWN AND STARTUP BUT WHAT ABOUT THE WEB APPS THAT ARE RUNNING 4HEY ALL HAVE TO GO DOWN

$

4ECHNICALLY YES 4AKING YOUR WEB APPS DOWN SO THAT YOU CAN REDEPLOY ONE SERVLET IS A LITTLE HARSH ESPECIALLY IF YOU HAVE A LOT OF TRAFFIC ON YOUR WEB SITE "UT THAT S WHY MOST OF THE PRODUCTION QUALITY 7EB #ONTAINERS LET YOU DO A HOT REDEPLOY WHICH MEANS THAT YOU DON T HAVE TO RESTART YOUR SERVER OR TAKE ANY OTHER WEB APPS DOWN )N FACT 4OMCAT DOES INCLUDE A MANAGER TOOL THAT WILL LET YOU DEPLOY UNDEPLOY AND REDEPLOY ENTIRE WEB APPS WITHOUT RESTARTING

4OMCAT )N A PRODUCTION ENVIRONMENT THAT S WHAT YOU D USE "UT FOR TESTING IT S EASIER TO JUST RESTART 4OMCAT )NFO ON THE MANAGEMENT TOOL IS AT HTTP JAKARTA APACHE ORG TOMCAT TOMCAT DOC MANAGER HOWTO HTML "UT IN THE REAL WORLD EVEN A HOT REDEPLOY IS A "IG $EAL AND TAKING EVEN A SINGLE APP DOWN JUST BECAUSE THE INIT PARAMETER VALUE CHANGED CAN BE A BAD IDEA )F THE VALUES OF YOUR INIT PARAMETERS ARE GOING TO CHANGE FREQUENTLY YOU RE BETTER OFF HAVING YOUR SERVLET METHODS GET THE VALUES FROM A FILE OR DATABASE BUT THIS APPROACH WILL MEAN A LOT MORE OVERHEAD EACH TIME YOUR SERVLET CODE RUNS INSTEAD OF ONLY ONCE DURING INITIALIZATION \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

XVLQJ 6HUYOHW&RQ¿ J

<M[\QVO aW]Z ;MZ ^TM\+WVNQO

MDYD[ VHUYOHW 6HUYOHW&RQILJ LQWHUIDFH!!

3ERVLET#ONl G S MAIN JOB IS TO GIVE YOU INIT PARAMETERS )T CAN ALSO GIVE YOU A 3ERVLET#ONTEXT BUT WE LL USUALLY GET A CONTEXT IN A DIFFERENT WAY AND THE GET3ERVLET.AME METHOD IS RARELY USEFUL

JHW,QLW3DUDPHWHU 6WULQJ

,Q WKH '' ZHE [PO À OH

JHW6HUYOHW&RQWH[W

6HUYOHW&RQILJ (QXPHUDWLRQ JHW,QLW3DUDPHWHU1DPHV

JHW6HUYOHW1DPH

ZHE DSS [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE DSSB B [VG´ YHUVLRQ ´ ´! VHUYOHW! VHUYOHW QDPH!%HHU3DUDP7HVWV VHUYOHW QDPH! VHUYOHW FODVV!FRP H[DPSOH 7HVW,QLW3DUDPV VHUYOHW FODVV! LQLW SDUDP! SDUDP QDPH!DGPLQ(PDLO SDUDP QDPH! SDUDP YDOXH!OLNHZHFDUH#ZLFNHGO\VPDUW FRP SDUDP YDOXH! LQLW SDUDP! LQLW SDUDP! SDUDP QDPH!PDLQ(PDLO SDUDP QDPH! SDUDP YDOXH!EORRSHU#ZLFNHGO\VPDUW FRP SDUDP YDOXH! LQLW SDUDP! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!%HHU3DUDP7HVWV VHUYOHW QDPH! XUO SDWWHUQ! 7HVWHU GR XUO SDWWHUQ! VHUYOHW PDSSLQJ! ZHE DSS!

r Most people neodve. th me is th use

,Q D VHUYOHW FODVV SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV 7HVW,QLW3DUDPV H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW LQLW SDUDPHWHUV EU!´ (QXPHUDWLRQ H JHW6HUYOHW&RQ¿ J JHW,QLW3DUDPHWHU1DPHV ZKLOH H KDV0RUH(OHPHQWV

^ RXW SULQWOQ ³ EU!SDUDP QDPH ³ H QH[W(OHPHQW ³ EU!´ ` RXW SULQWOQ ³PDLQ HPDLO LV ³ JHW6HUYOHW&RQ¿ J JHW,QLW3DUDPHWHU ³PDLQ(PDLO´

RXW SULQWOQ ³ EU!´ RXW SULQWOQ ³DGPLQ HPDLO LV ³ JHW6HUYOHW&RQ¿ J JHW,QLW3DUDPHWHU ³DGPLQ(PDLO´

` `

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

8K RK , MXVW UHDOL]HG WKDW LQ P\ UHDO DSS ,¡P XVLQJ -63 WR UHQGHU WKH SDJH 6R FDQ D -63 ´VHH¾ D VHUYOHW¡V LQLW SDUDPHWHUV"

0W_ KIV I 2;8 OM\ [MZ ^TM\ QVQ\ XIZIUM\MZ[' ! 3ERVLET#ONlG IS FOR SERVLET CONlGURATION IT DOESN T SAY *30#ONlG 3O IF YOU WANT OTHER PARTS OF YOUR APPLICATION TO USE THE SAME INFO YOU PUT IN THE SERVLET S INIT PARAMETERS IN THE $$ YOU NEED SOMETHING MORE

?PI\ IJW]\ \PM _Ia _M LQL Q\ _Q\P \PM JMMZ IXX' ?M XI[[ML \PM UWLMT QVNW \W \PM 2;8 ][QVO I ZMY]M[\ I\\ZQJ]\M

clientÕs We got the Remember?ce from the request. color choi

LQVLGH WKH GR3RVW PHWKRG 6WULQJ FRORU UHTXHVW JHW3DUDPHWHU ³FRORU´ %HHU([SHUW EH QHZ %HHU([SHUW /LVW UHVXOW EH JHW%UDQGV FRORU UHTXHVW VHW$WWULEXWH ³VW\OHV´ UHVXOW

ÒattributeÓ Then we set an and the JSP t, es qu re e th in e request to we forward th it. t ge to le ab was

Then we in used the Mstantiated and info we needODEL to get the for the VIE W.

7E COULD DO IT THIS WAY 4HE REQUEST OBJECT LETS YOU SET ATTRIBUTES THINK OF THEM AS A NAME VALUE PAIR WHERE THE VALUE CAN BE ANY OBJECT THAT ANY OTHER SERVLET OR *30 THAT GETS THE REQUEST CAN USE 4HAT MEANS ANY SERVLET OR *30 TO WHICH THE REQUEST IS FORWARDED USING A 2EQUEST$ISPATCHER 7E LL LOOK AT 2EQUEST$ISPATCHER IN DETAIL AT THE END OF THIS CHAPTER BUT FOR NOW ALL WE CARE ABOUT IS GETTING THE DATA IN THIS CASE THE EMAIL ADDRESS TO THE PIECES OF THE WEB APP THAT NEED IT RATHER THAN JUST ONE SERVLET \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

LQLW SDUDPHWHU OLPLWDWLRQV

;M\\QVO I ZMY]M[\ I\\ZQJ]\M _WZS[ J]\ WVTa NWZ \PM 2;8 \W _PQKP aW] NWZ _IZLML \PM ZMY]M[\ 7ITH THE BEER APP IT MADE SENSE TO STORE THE MODEL INFO FOR THE CLIENT S REQUEST IN THE REQUEST OBJECT BECAUSE THE NEXT STEP WAS TO FORWARD THE REQUEST TO THE *30 RESPONSIBLE FOR CREATING THE VIEW 3INCE THAT *30 NEEDED THE MODEL DATA AND THE DATA WAS RELEVANT TO ONLY THAT PARTICULAR REQUEST EVERYTHING WAS lNE

%XW , UHDOO\ ZDQW $// WKH SDUWV RI P\ ZHE DSS WR KDYH DFFHVV WR WKH HPDLO DGGUHVV :LWK LQLW SDUDPHWHUV , KDYH WR FRQÀJXUH WKHP LQ WKH '' IRU HYHU\ VHUYOHW DQG WKHQ KDYH DOO WKH VHUYOHWV PDNH WKHP DYDLODEOH IRU WKH -63V +RZ ERULQJ LV WKDW" 1RW PDLQWDLQDEOH HLWKHU , QHHG VRPHWKLQJ PRUH JOREDO

"UT THAT DOESN T HELP US WITH THE EMAIL ADDRESS BECAUSE WE MIGHT NEED TO USE IT FROM ALL OVER THE APPLICATION 4HERE IS A WAY TO HAVE A SERVLET READ THE INIT PARAMETERS AND THEN STORE THEM IN A PLACE OTHER PARTS OF THE APP COULD USE BUT THEN WE D HAVE TO KNOW WHICH SERVLET WOULD ALWAYS RUN lRST WHEN THE APP IS DEPLOYED AND ANY CHANGES TO THE WEB APP COULD BREAK THE WHOLE THING .O THAT WON T DO EITHER , ZRQGHU LI WKHUH·V VRPHWKLQJ OLNH LQLW SDUDPHWHUV IRU WKH DSSOLFDWLRQ "

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

+WV\M`\ QVQ\ XIZIUM\MZ[ \W \PM ZM[K]M #ONTEXT INIT PARAMETERS WORK JUST LIKE SERVLET INIT PARAMETERS EXCEPT CONTEXT PARAMETERS ARE AVAILABLE TO THE ENTIRE WEBAPP NOT JUST A SINGLE SERVLET 3O THAT MEANS ANY SERVLET AND *30 IN THE APP AUTOMATICALLY HAS ACCESS TO THE CONTEXT INIT PARAMETERS SO WE DON T HAVE TO WORRY ABOUT CONlGURING THE $$ FOR EVERY SERVLET AND WHEN THE VALUE CHANGES YOU ONLY HAVE TO CHANGE IT ONE PLACE ,Q WKH '' ZHE [PO ÀOH

ent nit-param> elem We took the <iervlet> element. out of the <s

VHUYOHW! VHUYOHW QDPH!%HHU3DUDP7HVWV VHUYOHW QDPH! VHUYOHW FODVV!7HVW,QLW3DUDPV VHUYOHW FODVV! VHUYOHW!

FRQWH[W SDUDP! SDUDP QDPH!DGPLQ(PDLO SDUDP QDPH! SDUDP YDOXH!FOLHQWKHDGHUURU#ZLFNHGO\VPDUW FRP SDUDP YDOXH! FRQWH[W SDUDP!

You give it a para servlet init paramem-name and param-value just like wit ters, except this tim IMPORTAN h <context-param> ele for the WH T!! The <context-pa ment instead of <ine itĂ•s in the O ram> is it-param>. LE app, so inside an its no <context-inpadividual <servlet> eltemnested but OUTSIDram> inside the <web ent!! Put E any <servl et> declaraapp> tion. ,Q WKH VHUYOHW FRGH RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU ÂłDGPLQ(PDLO´

Every servlet inh (and JSPs have speerits a getServletContext() meth od cial access to a co ntext as well).

ontext() method The getServletCingly, a ServletContext returns, surprise of its methods is object. And onter(). getInitParame

25 6HUYOHW&RQWH[W FRQWH[W JHW6HUYOHW&RQWH[W RXW SULQWOQ FRQWH[W JHW,QLW3DUDPHWHU ³DGPLQ(PDLO´

Here we broke out the code into TWO stepsÑ getting the ServletContext reference, and calling its getInitParameter() method. \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRQWH[W YV VHUYOHW LQLW SDUDPHWHUV

:MUMUJMZ \PM LQNNMZMVKM JM\ _MMV [MZ ^TM\ QVQ\ XIZIUM\MZ[ IVL KWV\M`\ QVQ\ XIZIUM\MZ[ (ERE S A REVIEW OF THE KEY DIFFERENCES BETWEEN CONTEXT INIT PARAMETERS AND SERVLET INIT PARAMETERS 0AY SPECIAL ATTENTION TO THE FACT THAT THEY RE BOTH REFERRED TO AS INIT PARAMETERS EVEN THOUGH ONLY SERVLET INIT PARAMETERS HAVE THE WORD hINITv IN THE $$ CONlGURATION

&RQWH[W LQLW SDUDPHWHUV

6HUYOHW LQLW SDUDPHWHUV 'HSOR\PHQW 'HVFULSWRU

:LWKLQ WKH ZHE DSS! HOHPHQW EXW 127 ZLWKLQ D VSHFLILF VHUYOHW! HOHPHQW

:LWKLQ WKH VHUYOHW! HOHPHQW IRU HDFK VSHFLILF VHUYOHW

ZHE DSS ! FRQWH[W SDUDP! SDUDP QDPH!IRR SDUDP QDPH! SDUDP YDOXH!EDU SDUDP YDOXH! FRQWH[W SDUDP!

VHUYOHW! VHUYOHW QDPH! %HHU3DUDP7HVWV VHUYOHW QDPH! VHUYOHW FODVV! 7HVW,QLW3DUDPV VHUYOHW FODVV! LQLW SDUDP! SDUDP QDPH!IRR SDUDP QDPH! SDUDP YDOXH!EDU SDUDP YDOXH! LQLW SDUDP!

RWKHU VWXII LQFOXGLQJ VHUYOHW GHFODUDWLRQV ! ZHE DSS!

Notice it doesnÕt say ÒinitÓ text anywhere in the DD for con does init parameters, the way its. for servlet init parameter

RWKHU VWXII ! VHUYOHW!

6HUYOHW &RGH

JHW6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU ³IRR´

JHW6HUYOHW&RQ¿J JHW,QLW3DUDPHWHU ³IRR´

ItÕs the same method name! $YDLODELOLW\

7R DQ\ VHUYOHWV DQG -63V WKDW DUH SDUW RI WKLV ZHE DSS

7R RQO\ WKH VHUYOHW IRU ZKLFK WKH LQLW SDUDP! ZDV FRQILJXUHG $OWKRXJK WKH VHUYOHW FDQ FKRRVH WR PDNH LW PRUH ZLGHO\ DYDLODEOH E\ VWRULQJ LW LQ DQ DWWULEXWH

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

; MZ ^TM\+WVNQO Q[ WVM XMZ [MZ ^TM\ ; MZ ^TM\+WV\M`\ Q[ WVM XMZ _MJ IXX

U YO

HW&RQWH[

W

6H

4HERE S ONLY ONE 3ERVLET#ONTEXT FOR AN ENTIRE WEB APP AND ALL THE PARTS OF THE WEB APP SHARE IT "UT EACH SERVLET IN THE APP HAS ITS OWN 3ERVLET#ONl G 4HE #ONTAINER MAKES A 3ERVLET#ONTEXT WHEN A WEB APP IS DEPLOYED AND MAKES THE CONTEXT AVAILABLE TO EACH 3ERVLET AND *30 WHICH BECOMES A SERVLET IN THE WEB APP

t app-wide contex e id ins ms ra pa t ini

:HE DSS LQLWLDOL]DWLRQ

ƒ &RQWDLQHU UHDGV WKH '' DQG FUHDWHV D QDPH YDOXH 6WULQJ SDLU IRU HDFK FRQWH[W SDUDP!

ƒ &RQWDLQHU FUHDWHV D QHZ LQVWDQFH RI 6HUYOHW&RQWH[W

ƒ &RQWDLQHU JLYHV WKH 6HUYOHW&RQWH[W D UHIHUHQFH WR HDFK QDPH YDOXH SDLU RI WKH FRQWH[W LQLW SDUDPHWHUV

6HUYOHW $

6HUYOHW %

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

6HUYOHW &

-63

6H UYOHW&RQIL J

init params for Servlet A inside

:DWFK LW

6H UYOHW&RQIL J

6H UYOHW&RQIL J

init params for Servlet B inside

ƒ (YHU\ VHUYOHW DQG -63 GHSOR\HG DV SDUW RI D VLQJOH ZHE DSS KDV DFFHVV WR WKDW VDPH 6HUYOHW&RQWH[W

init params foidre 6HU J YOHW&RQIL Servlet C ins

rned into Yes, JSPs arervtulets, so se ss Ăžrst-cla eir own they also getg.th ServletConĂž

'RQ¡W FRQIXVH 6HU YOHW&RQĂ€ J SDUDPHWHUV ZLWK 6HU YOHW&RQWH[W SDUDPHWHUV

VH VWUDLJKW RQ <RX UHDOO\ KDYH WR NHHS WKH 0867 NQRZ WKH H[DP DQG LWœV WULFN\ <RX QG 6HUYOHW&RQWH[W WKDW ERWK 6HUYOHW&RQ¿ J D RWK KDYH WKH KDYH LQLW SDUDPHWHUV DQG E W3DUDPHWHU VDPH JHWWHU PHWKRG²JHW,QL Z WKDW FRQWH[W %87 \RX DOVR KDYH WR NQR FRQWH[W SDUDP! LQLW SDUDPHWHUV DUH VHW ZLWK HQW ZKLOH VHUYOHW QRW LQVLGH D VHUYOHW! HOHP DP! LQVLGH WKH LQLW SDUDPHWHUV XVH LQLW SDU WLRQV LQ WKH '' LQGLYLGXDO VHUYOHW! GHFODUD

,I WKH DSS LV GLVWULEXWHG WKHUH¡V :DWFK LW RQH 6HUYOHW&RQWH[W SHU -90 ,I \RXU DSSOLFDWLRQ LV GLVWULEXWHG DFURVV PXOWLSOH VHUYHUV SUREDEO\ LQ D FOXVWHUHG HQYLURQPHQW \RXU ZHE DSS UHDOO\ &28/' KDYH PRUH WKDQ RQH 6HUYOHW&RQWH[W $ 6HUYOHW&RQWH[W LV RQH SHU DSS EXW RQO\ LI WKH DSS LV LQ D VLQJOH -90 ,Q D GLVWULEXWHG HQYLURQPHQW \RXœOO KDYH RQH 6HUYOHW&RQWH[W SHU -90 1RZ FKDQFHV DUH WKLV ZRQœW FUHDWH SUREOHPV EXW LI \RX KDYH D GLVWULEXWHG ZHE DSS \RX EHWWHU FRQVLGHU WKH FRQVHTXHQFHV RI KDYLQJ GLIIHUHQW FRQWH[WV IRU HDFK -90 \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

VHUYOHW DQG FRQWH[W LQLW SDUDPHWHUV

WKHUH DUH QR

'XPE 4XHVWLRQV

4

/R MAYBE NOTHING BECAUSE THERE S NO NAME SPACE CONmICT SINCE YOU GET THE PARAMETERS THROUGH TWO DIFFERENT OBJECTS 3ERVLET#ONTEXT OR 3ERVLET#ONlG

$

)F YOU MODIFY THE 8-, TO CHANGE THE VALUE OF AN INIT PARAMETER EITHER SERVLET OR CONTEXT WHEN DOES THE SERVLET OR THE REST OF THE WEB APP SEE THE CHANGE

7HAT S WITH THE INCONSISTENT NAMING SCHEME (OW COME THE $$ ELEMENTS ARE CONTEXT PARAM AND INIT PARAM BUT IN THE SERVLET CODE "/4( USE THE GET)NIT0ARAMETER METHOD

4HEY DIDN T ASK US TO HELP THEM COME UP WITH THE NAMES )F THEY HAD OF COURSE WE D HAVE SAID IT SHOULD BE GET)NIT0ARAMETER AND GET#ONTEXT0ARAMETER TO MATCH THE 8-, ELEMENTS IN THE $$ /R THEY COULD HAVE USED DIFFERENT 8-, ELEMENTSˆPERHAPS SERVLET INIT PARAM AND CONTEXT INIT PARAM "UT NO THAT WOULD HAVE SUCKED ALL THE FUN OUT OF TRYING TO KEEP THEM STRAIGHT

4

7HY WOULD ) EVER USE INIT PARAM ANYWAY 7OULDN T ) ALWAYS WANT TO USE CONTEXT PARAM SO THAT OTHER PARTS OF MY APP COULD REUSE THE VALUES AND ) WON T HAVE TO DUPLICATE 8-, CODE FOR EVERY SERVLET DECLARATION

$

)T ALL DEPENDS ON WHICH PART OF YOUR APP IS SUPPOSED TO SEE THE VALUE 9OUR APPLICATION LOGIC MIGHT REQUIRE YOU TO USE A VALUE THAT YOU WANT TO RESTRICT TO ONLY AN INDIVIDUAL SERVLET "UT TYPICALLY DEVELOPERS FIND APP WIDE CONTEXT INIT PARAMETERS A LOT MORE HELPFUL THAN SERVLET SPECIFIC SERVLET INIT PARAMETERS 0ERHAPS THE MOST COMMON USE OF A CONTEXT PARAMETER IS STORING DATABASE LOOKUP NAMES 9OU D WANT ALL PARTS OF YOUR APP TO HAVE ACCESS TO THE CORRECT NAME AND WHEN IT CHANGES YOU WANT TO CHANGE IT IN ONLY ONE PLACE

4

7HAT HAPPENS IF ) GIVE A CONTEXT INIT PARAMETER THE SAME NAME AS A SERVLET INIT PARAMETER IN THE SAME WEB APP

$

4HE MOLECULAR SIZED BLACK HOLE MIRACULOUSLY CREATED IN A RESEARCH FACILITY IN .EW *ERSEY WILL SLIP FROM ITS CONTAINMENT lELD PLUMMET TO THE EARTH S CORE AND DESTROY THE PLANET

4 $

/.,9 WHEN THE WEB APP IS REDEPLOYED 2EMEMBERˆWE TALKED ABOUT THIS BEFOREˆTHE SERVLET IS INITIALIZED ONLY ONCE AT THE BEGINNING OF ITS LIFE AND THAT S WHEN IT S GIVEN ITS 3ERVLET#ONFIG AND 3ERVLET#ONTEXT 4HE #ONTAINER READS THE VALUES FROM THE $$ WHEN IT CREATES THOSE TWO OBJECTS AND SETS THE VALUES

4

#AN T ) GET AROUND THIS BY SETTING THE VALUES AT RUNTIME 3URELY THERE S AN !0) THAT LL LET ME CHANGE THOSE VALUES DYNAMICALLY

$

.O THERE S NOT ,OOK IN 3ERVLET#ONTEXT OR 3ERVLET#ONFIG AND YOU LL FIND A GETTER GET)NIT0ARAMETER BUT YOU WON T FIND A SETTER 4HERE S NO SET)NIT0ARAMETER

4 $

4HAT S LAME

4HESE ARE INIT PARAMETERS )NIT FROM THE ,ATIN WORD INITIALIZATION )F YOU THINK OF THEM PURELY AS DEPLOY TIME CONSTANTS YOU LL HAVE THE RIGHT PERSPECTIVE )N FACT THAT S SO IMPORTANT WE RE GOING TO SAY IT AGAIN IN A BOLDER WAY

7KLQN RI LQLW SDUDPHWHUV DV GHSOR\ WLPH FRQVWDQWV <RX FDQ JHW WKHP DW UXQWLPH EXW \RX FDQ·W VHW WKHP 7KHUH·V QR VHW,QLW3DUDPHWHU

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

&RGH 0DJQHWV 2EARRANGE THE MAGNETS TO FORM A $$ THAT DECLARES A PARAMETER THAT MATCHES THE SERVLET CODE

([HUFLVH

JHW6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU ³IRR´ 9OU WON T USE ALL OF THE MAGNETS .OTE WHEN YOU SEE WEB APP REMEMBER THAT THIS IS OUR SHORT CUT TO SAVE SPACE ON THE PAGE 9OU CAN T DEPLOY A WEB XML FILE UNLESS THE WEB APP TAG HAS ALL THE ATTRIBUTES IT NEEDS

FRP ZLFNHG O\VPDUW %H HU7HVWHU VHUYOHW FODVV!

LQLW SDUDP !

! YDOXH SDUDP FRQWH[W S DUDP!

HW! VHUYO

SDUDP QDPH!

ZHE DSS! VHUYOHW!

YDOXH! SDUDP EDU

WHUµ ZLWKRXW ,I \RX VHH ´LQLW SDUDPH VHU YOHW RU FRQWH[W NQRZLQJ LI LW PHDQV VHU YOHW LQLW SDUDPHWHU DVVXPH WR PHDQ ³VHUYOHW HWHU´ DUDP QLW S VH ³L SKUD 6RPH SHRSOH XVH WKH PHWHU´ RU HYHQ ³DS SDUD WH[W ³FRQ XVH WKH\ LQLW SDUDPHWHU´ DQG R HYHQ Q ³FRQWH[W LQLW SDUDPHWHU´ 6 SOLFDWLRQ SDUDPHWHU´ WR PHD H IURP DUDPHWHUV DQG ERWK FRP LRQ S OL]DW LQLWLD DUH 27+ WKRXJK % 59/(7 LQLW KRG UHPHPEHU WKDW RQO\ 6( PHW WHU

DPH W3DU HW,QL WKH J KH SKUDVH '' DV LQLW SDUDPHWHUV VR W SDUDPHWHUV DUH OLVWHG LQ WKH GHIDXOW ´ E\ HWHU UDP LW SD OHW LQ VHUY ³LQLW SDUDPHWHU´ PHDQV ³ DQG HU \RX¶OO EH NLQGHU WR RWKHUV :H NQRZ WKDW DV D GHYHORS U LV D VHUYOHW LQLW PHWH SDUD LQLW HU DQ KHWK DOZD\V VD\ H[SOLFLWO\ Z LW SDUDPHWHU SDUDPHWHU RU D FRQWH[W LQ

ODVV! OHW F VHUY

IRR ZHE DSS !

UDP! QDPH! OHW SD SDUDP VHUY FRQWH[W SD UDP! VHUYOHW QDPH!

HW QDPH! VHUYO %HHU7HVW

SDUDP! LQLW VHUYOHW SDUDP!

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

WKH 6HUYOHW&RQWH[W

;W _PI\ MT[M KIV aW] LW _Q\P aW]Z ; MZ ^TM\+WV\M`\'

6H

<RX FRPSOHWH PH

HW UY O

&RQ W H [

W

! 3ERVLET#ONTEXT IS A *30 OR SERVLET S CONNECTION TO BOTH THE #ONTAINER AND THE OTHER PARTS OF THE WEB APP (ERE ARE SOME OF THE 3ERVLET#ONTEXT METHODS 7E PUT THE ONES YOU SHOULD KNOW FOR THE EXAM IN BOLD

%HIRUH , FDPH LQWR \RXU OLIH \RX ZHUH MXVW DQRWKHU ORVHU REMHFW LQVWHDG RI D UHDO VHUYOHW

6HUYOHW

LQWHUIDFH!!

6HUYOHW&RQWH[W

ters and Get init parame es. ut ib tr at get/set

JHW,QLW3DUDPHWHU 6WULQJ

JHW,QLW3DUDPHWHU1DPHV

JHW$WWULEXWH 6WULQJ

JHW$WWULEXWH1DPHV

WeÕll talk about parameters vs. attributes in a few pages.

VHW$WWULEXWH 6WULQJ

UHPRYH$WWULEXWH 6WULQJ

Get info about the server/container.

JHW0DMRU9HUVLRQ

JHW6HUYHU,QIR

JHW5HDO3DWK 6WULQJ

JHW5HVRXUFH$V6WUHDP 6WULQJ

Write to the serverÕs log Þle (vendor-speciÞc) or System.out.

JHW5HTXHVW'LVSDWFKHU 6WULQJ

WeÕll talk about later in RequestDispatcher the chapter.

ORJ 6WULQJ

PRUH PHWKRGV

MDYD[ VHUYOHW 6HUYOHW&RQWH[W

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

RQWH[W LQ WZR <RX FDQ JHW D 6HUYOHW& D\V QW Z GLIIHUH FH WR WKH MHFW DOZD\V KROGV D UHIHUHQ H $ VHUYOHW¶V 6HUYOHW&RQ¿ J RE IRROHG LI \RX VHH VHUYOHW FRG W EH GRQ¶ 6R YOHW W VHU 6HUYOHW&RQWH[W IRU WKD D\V KDW V RQ WKH H[DP W JHW,QLW3DUDPHWHU

JHW6HUYOHW&RQWH[W JHW6HUYOHW&RQ¿ J W GRHV WKH VDPH WKLQJ DV 1RW RQO\ LV WKDW OHJDO EXW L U

[W JHW,QLW3DUDPHWH WKLV JHW6HUYOHW&RQWH RXJK \RXU \RX ZRXOG 1((' WR JR WKU ,Q D VHUYOHW WKH RQO\ WLPH LI \RX¶UH LQ D 6HUYOHW [W LV RQWH YOHW& 6HU \RXU 6HUYOHW&RQ¿ J WR JHW WKH WWS6HUYOHW RU *HQHULF6HUYOHW W FODVV WKDW GRHVQ¶W H[WHQG + ULW FRPHV IURP *HQHULF6HUYOH LQKH \RX WKRG

PH WH[W JHW6HUYOHW&RQ J D QRQ +773 VHUYOHW LV XVLQ 21( $1< H RI KDQF %XW WKH F U RZQ FKLQJ ]HUR 6R MXVW FDOO \RX ZHOO DV\PSWRWLFDOO\ DSSURD IXVHG LI \RX VHH EXW GRQ¶W EH GD]HG RU FRQ JHW6HUYOHW&RQWH[W PHWKRG [W RQWH WKH F JHW J WR RQ¿ FRGH WKDW XVHV WKH 6HUYOHW& VHUYOHW D KHOSHU GH VRPH FODVV WKDW LV 127 D HUYOHW&RQ¿ J %XW ZKDW LI WKH FRGH LV LQVL HRQH PLJKW KDYH SDVVHG D 6 6RP OH " DPS RU H[ VV I XWLOLW\ FOD HUYOHW&RQWH[W JHW6 XVH H WR KDY RXOG V FRGH Z WR WKDW FODVV DQG WKH FODV OHW&RQWH[W REMHFW WR JHW D UHIHUHQFH WR WKH 6HUY

4

(OW DO ALL THE PARTS OF A WEB APP GET ACCESS TO THEIR OWN 3ERVLET#ONTEXT

$

&OR SERVLETS YOU ALREADY KNOW CALL YOUR INHERITED GET3ERVLET#ONTEXT METHOD &OR *30S IT S A LITTLE DIFFERENTˆ*30S HAVE SOMETHING CALLED hIMPLICIT OBJECTSv AND 3ERVLET#ONTEXT IS ONE OF THEM 9OU LL SEE EXACTLY HOW A *30 USES A 3ERVLET#ONTEXT WHEN WE GET TO THE *30 CHAPTERS

4

3O YOU GET BUILT IN LOGGING THROUGH YOUR CONTEXT 4HAT SOUNDS 6%29 HELPFUL

$

5M NO .OT UNLESS YOU HAVE A REALLY SMALL SIMPLE WEB APP 4HERE ARE MUCH BETTER WAYS TO DO LOGGING 4HE MOST POPULAR ROBUST LOGGING MECHANISM IS ,OG J YOU CAN FIND IT ON THE !PACHE SITE AT KWWS MDNDUWD DSDFKH RUJ ORJ M 9OU CAN ALSO USE THE LOGGING !0) FROM JAVA UTIL LOGGING ADDED TO * 3% IN VERSION )T S FINE TO USE THE 3ERVLET#ONTEXT LOG METHOD FOR SIMPLE EXPERIMENTS BUT IN A REAL PRODUCTION ENVIRONMENT YOU WILL ALMOST CERTAINLY WANT TO CHOOSE SOMETHING ELSE 4HERE S A GOOD REFERENCE ON WEB APP LOGGING WITH AND WITHOUT ,OG J IN THE *AVA 3ERVLET *30 #OOKBOOK FROM / 2EILLY ,OGGING IS NOT PART OF THE EXAM OBJECTIVES BUT IT S IMPORTANT &ORTUNATELY YOU LL FIND THE !0)S EASY TO USE

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRQWH[W SDUDPHWHU OLPLWDWLRQV

+DWH WR VSRLO \RXU 6HUYOHW&RQWH[W SDUW\ EXW XP WKRVH LQLW SDUDPHWHUV FDQ·W EH DQ\WKLQJ H[FHSW 675,1*6 7KDW·V LW :KDW LI , ZDQW WR LQLWLDOL]H P\ DSS ZLWK D GDWDEDVH 'DWD6RXUFH WKDW DOO WKH VHUYOHWV FDQ XVH"

?PI\ QN aW] _IV\ IV IXX QVQ\ XIZIUM\MZ \PI\¼[ I LI\IJI[M ,I\I;W]ZKM' #ONTEXT PARAMETERS CAN T BE ANYTHING EXCEPT 3TRINGS !FTER ALL YOU CAN T VERY WELL STUFF A $OG OBJECT INTO AN 8-, DEPLOYMENT DESCRIPTOR !CTUALLY YOU COULD REPRESENT A SERIALIZED OBJECT IN 8-, BUT THERE S NO FACILITY FOR THIS IN THE 3ERVLET SPEC TODAY MAYBE IN THE FUTURE 7HAT IF YOU REALLY WANT ALL THE PARTS OF YOUR WEB APP TO HAVE ACCESS TO A SHARED DATABASE CONNECTION 9OU CAN CERTAINLY PUT THE $ATA3OURCE LOOKUP NAME IN A CONTEXT INIT PARAMETER AND THAT S PROBABLY THE MOST COMMON USE OF CONTEXT PARAMETERS TODAY "UT THEN WHO DOES THE WORK OF TURNING THE 3TRING PARAMETER INTO AN ACTUAL $ATA3OURCE REFERENCE THAT ALL PARTS OF THE WEB APP CAN SHARE 9OU CAN T REALLY PUT THAT CODE IN A SERVLET BECAUSE WHICH SERVLET WOULD YOU CHOOSE TO BE 4HE /NE 4O ,OOKUP 4HE $ATA3OURCE !ND 3TORE )T )N !N !TTRIBUTE $O YOU REALLY WANT TO TRY TO GUARANTEE THAT ONE SERVLET IN PARTICULAR WILL ALWAYS RUN lRST 4HINK ABOUT IT

# 4X3 X9 8 +RZ FRXOG \RX VROYH WKLV SUREOHP" +RZ FRXOG \RX LQLWLDOL]H D ZHE DSS ZLWK DQ REMHFW" $VVXPH WKDW \RX QHHG WKH 6WULQJ FRQWH[W LQLW SDUDPHWHU LQ RUGHU WR FUHDWH WKDW REMHFW WKLQN DERXW WKH GDWDEDVH H[DPSOH

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

2K LI RQO\ WKHUH ZHUH D ZD\ WR KDYH VRPHWKLQJ OLNH D PDLQ PHWKRG IRU P\ ZKROH ZHE DSS 6RPH FRGH WKDW DOZD\V UXQV EHIRUH $1< VHUYOHWV RU -63V

?PI\ [PM ZMITTa _IV\[ Q[ I TQ[\MVMZ 3HE WANTS TO LISTEN FOR A CONTEXT INITIALIZATION EVENT SO THAT SHE CAN GET THE CONTEXT INIT PARAMETERS AND RUN SOME CODE BEFORE THE REST OF THE APP CAN SERVICE A CLIENT 3HE NEEDS SOMETHING THAT CAN BE SITTING THERE WAITING TO BE NOTIlED THAT THE APP IS STARTING UP "UT WHICH PART OF THE APP COULD DO THE WORK 9OU DON T WANT TO PICK A SERVLETˆTHAT S NOT A SERVLET S JOB 4HERE S NO PROBLEM IN A PLAIN OLD STANDALONE *AVA APP BECAUSE YOU VE GOT MAIN "UT WITH A SERVLET WHAT DO YOU DO 9OU NEED SOMETHING ELSE .OT A SERVLET OR *30 BUT SOME OTHER KIND OF *AVA OBJECT WHOSE SOLE PURPOSE IN LIFE IS TO INITIALIZE THE APP AND POSSIBLY TO UNINITIALIZE IT TOO CLEANING UP RESOURCES WHEN IT LEARNS OF THE APP S DEMISE

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRQWH[W OLVWHQHUV

;PM _IV\[ I ; MZ ^TM\+WV\M`\4Q[\MVMZ 7E CAN MAKE A SEPARATE CLASS NOT A SERVLET OR *30 THAT CAN LISTEN FOR THE TWO KEY EVENTS IN A 3ERVLET#ONTEXT S LIFEˆ INITIALIZATION CREATION AND DESTRUCTION 4HAT SEPARATE CLASS IMPLEMENTS JAVAX SERVLET 3ERVLET#ONTEXT,ISTENER

:H QHHG D VHSDUDWH REMHFW WKDW FDQ *HW QRWLILHG ZKHQ WKH FRQWH[W LV LQLWLDOL]HG DSS LV EHLQJ ƒ GHSOR\HG

ƒ

*HW WKH FRQWH[W LQLW SDUDPHWHUV IURP WKH 6HUYOHW&RQWH[W

ƒ

8VH WKH LQLW SDUDPHWHU ORRNXS QDPH WR PDNH D GDWDEDVH FRQQHFWLRQ

ƒ

LQWHUIDFH! 6HUYOHW&RQWH[ ! W/LVWHQHU

FRQWH[W,QLWLDOL]H

G 6HUYOHW&RQWH [W(YHQW

\HG 6HUYOHW&R QWH[W(YHQW

FRQWH[W'HVWUR

MDYD[ VHUYOHW 6H

UYOHW&RQWH[W/L

VWHQHU

6WRUH WKH GDWDEDVH FRQQHFWLRQ DV DQ DWWULEXWH VR WKDW DOO SDUWV RI WKH ZHE DSS FDQ DFFHVV LW

*HW QRWLILHG ZKHQ WKH FRQWH[W LV GHVWUR\HG WKH DSS LV ƒ XQGHSOR\HG RU JRHV GRZQ

ƒ

&ORVH WKH GDWDEDVH FRQQHFWLRQ

$ 6HUYOHW&RQWH[W/LVWHQHU FODVV LPSRUW MDYD[ VHUYOHW

ServletContextListener is in javax.servlet package.

A context listener is simple: implement ServletContextListener.

SXEOLF FODVV 0\6HUYOHW&RQWH[W/LVWHQHU LPSOHPHQWV 6HUYOHW&RQWH[W/LVWHQHU ^ SXEOLF YRLG FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW HYHQW ^ FRGH WR LQLWLDOL]H WKH GDWDEDVH FRQQHFWLRQ DQG VWRUH LW DV D FRQWH[W DWWULEXWH ` SXEOLF YRLG FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW HYHQW ^ FRGH WR FORVH WKH GDWDEDVH FRQQHFWLRQ ` `

These are t he two not you get. Bot iĂžcations ServletCon h give you a textEvent.

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

2. , KDYH D OLVWHQHU FODVV 1RZ ZKDW GR , GR" :KHUH GR , SXW WKH FODVV" :KR LQVWDQWLDWHV LW" +RZ GR , UHJLVWHU IRU WKH HYHQWV" +RZ GRHV WKH OLVWHQHU VHW WKH DWWULEXWH LQ WKH ULJKW 6HUYOHW&RQWH[W"

# 4X3 X9 8 :KDW GR \RX WKLQN WKH PHFKDQLVP PLJKW EH IRU PDNLQJ D OLVWHQHU EH SDUW RI D VSHFLÂżF ZHE DSS"

+LQW KRZ GR \RX WHOO WKH &RQWDLQHU DERXW WKH RWKHU SDUWV RI \RXU ZHE DSS" :KHUH PLJKW WKH &RQWDLQHU GLVFRYHU \RXU OLVWHQHU"

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

XVLQJ D 6HUYOHW&RQWH[W/LVWHQHU

<]\WZQIT" I [QUXTM ;MZ ^TM\+WV\M`\4Q[\MVMZ .OW WE LL WALK THROUGH THE STEPS OF MAKING AND RUNNING A 3ERVLET#ONTEXT,ISTENER 4HIS IS JUST A SIMPLE TEST CLASS SO THAT YOU CAN SEE HOW ALL THE PIECES WORK TOGETHER WE RE NOT USING THE DATABASE CONNECTION EXAMPLE BECAUSE YOU D HAVE TO SET UP A DATABASE TO MAKE IT WORK "UT THE STEPS ARE THE SAME REGARDLESS OF THE CODE YOU PUT IN YOUR LISTENER CALLBACK METHODS )N THIS EXAMPLE WE LL TURN A 3TRING INIT PARAMETER INTO AN ACTUAL OBJECTˆA $OG 4HE LISTENER S JOB IS TO GET THE CONTEXT INIT PARAMETER FOR THE DOG S BREED "EAGLE 0OODLE ETC THEN USE THAT 3TRING TO CONSTRUCT A $OG OBJECT 4HE LISTENER THEN STICKS THE $OG OBJECT INTO A 3ERVLET#ONTEXT ATTRIBUTE SO THAT THE SERVLET CAN RETRIEVE IT 4HE POINT IS THAT THE SERVLET NOW HAS ACCESS TO A SHARED APPLICATION OBJECT IN THIS CASE A $OG AND DOESN T HAVE TO READ THE CONTEXT PARAMETERS 7HETHER THE SHARED OBJECT IS A $OG OR A DATABASE CONNECTION DOESN T MATTER 4HE KEY IS TO USE THE INIT PARAMETERS TO CREATE A SINGLE OBJECT THAT ALL PARTS OF THE APP WILL SHARE

In this example, weÕll put a Dog into a ServletContext.

2XU 'RJ H[DPSOH

ƒ 7KH OLVWHQHU REMHFW DVNV WKH 6HUYOHW&RQWH[W(YHQW REMHFW IRU D UHIHUHQFH WR WKH DSS¡V 6HUYOHW&RQWH[W ƒ 7KH OLVWHQHU XVHV WKH UHIHUHQFH WR WKH 6HUYOHW&RQWH[W WR JHW WKH FRQWH[W LQLW SDUDPHWHU IRU ´EUHHGÂľ ZKLFK LV D 6WULQJ UHSUHVHQWLQJ D GRJ EUHHG

ƒ 7KH OLVWHQHU XVHV WKDW GRJ EUHHG 6WULQJ WR FRQVWUXFW D 'RJ REMHFW ƒ 7KH OLVWHQHU XVHV WKH UHIHUHQFH WR WKH 6HUYOHW&RQWH[W WR VHW WKH 'RJ DWWULEXWH LQ WKH 6HUYOHW&RQWH[W ƒ 7KH WHVWHU VHUYOHW LQ WKLV ZHE DSS JHWV WKH 'RJ REMHFW IURP WKH 6HUYOHW&RQWH[W DQG FDOOV WKH 'RJ¡V JHW%UHHG PHWKRG

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

5ISQVO IVL ][QVO I KWV\M`\ TQ[\MVMZ -AYBE YOU RE STILL WONDERING HOW THE #ONTAINER DISCOVERS AND USES THE LISTENER 9OU CONl GURE A LISTENER THE SAME WAY YOU TELL THE #ONTAINER ABOUT THE REST OF YOUR WEB APPˆTHROUGH THE WEB XML $EPLOYMENT $ESCRIPTOR

&UHDWH D OLVWHQHU FODVV LQWHUIDFH!!

6HUYOHW&RQWH[W/LVWHQHU FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW

FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW

0\6HUYOHW&RQWH[W/LVWHQHU FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW

FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW

3XW WKH FODVV LQ :(% ,1) FODVVHV :(% ,1)

FODVVHV

7R OLVWHQ IRU 6HUYOHW&RQWH[W HYHQWV ZULWH D OLVWHQHU FODVV WKDW LPSOHPHQWV 6HUYOHW&RQWH[W/LVWHQHU SXW LW LQ \RXU :(% ,1) FODVVHV GLUHFWRU\ DQG WHOO WKH &RQWDLQHU E\ SXWWLQJ D OLVWHQHU! HOHPHQW LQ WKH 'HSOR\PHQW 'HVFULSWRU

go.... (This isnÕt the ONLY place itsevecan ral WEB-INF/classes is one of for places the Container can lookrs in the classes. WeÕll cover the othe Deployment chapter.)

3XW D OLVWHQHU! HOHPHQW LQ WKH ZHE [PO 'HSOR\PHQW 'HVFULSWRU OLVWHQHU! OLVWHQHU FODVV! FRP H[DPSOH 0\6HUYOHW&RQWH[W/LVWHQHU OLVWHQHU FODVV! OLVWHQHU!

Question for yo the DD does thu--which part of go into? Does e <listener> element element, or justit go into a <servlet> under <web-app>? Think about it. \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

6HUYOHW&RQWH[W/LVWHQHU WXWRULDO

?M VMML \PZMM KTI[[M[ IVL WVM ,, &OR OUR CONTEXT LISTENER TEST EXAMPLE WE NEED TO WRITE THE CLASSES AND THE WEB XML l LE

&OR EASE OF TESTING WE LL PUT ALL OF THE CLASSES IN THE SAME PACKAGE COM EXAMPLE

LQWHUIDFH!! 6HUYOHW&RQWH[W/LVWHQHU

7KH 6HUYOHW&RQWH[W/LVWHQHU 0\6HUYOHW&RQWH[W/LVWHQHU MDYD

4HIS CLASS IMPLEMENTS 3ERVLET#ONTEXT,ISTENER GETS THE CONTEXT INIT PARAMETERS CREATES THE $OG AND SETS THE $OG AS CONTEXT ATTRIBUTE

0\6HUYOHW&RQWH[W/LVWHQHU FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW

FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW

7KH DWWULEXWH FODVV

'RJ

'RJ MDYD

4HE $OG CLASS IS JUST A PLAIN OLD *AVA CLASS )TS JOB IS TO BE THE ATTRIBUTE VALUE THAT THE 3ERVLET#ONTEXT,ISTENER INSTANTIATES AND SETS IN THE 3ERVLET#ONTEXT FOR THE SERVLET TO RETRIEVE

7KH 6HUYOHW /LVWHQHU7HVWHU MDYD

4HIS CLASS EXTENDS (TTP3ERVLET )TS JOB IS TO VERIFY THAT THE LISTENER WORKED BY GETTING THE $OG ATTRIBUTE FROM THE CONTEXT INVOKING GET"REED ON THE $OG AND PRINTING THE RESULT TO THE RESPONSE SO WE LL SEE IT IN THE BROWSER

'RJ 6WULQJ

JHW%UHHG

LQWHUIDFH!! 6HUYOHW

*HQHULF6HUYOHW +WWS6HUYOHW

/LVWHQHU7HVWHU GR*HW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

?ZQ\QVO \PM TQ[\MVMZ KTI[[ )T WORKS JUST LIKE OTHER TYPES OF LISTENERS YOU MIGHT BE FAMILIAR WITH SUCH AS 3WING '5) EVENT HANDLERS 2EMEMBER ALL WE NEED TO DO IS GET THE CONTEXT INIT PARAMETERS TO l ND OUT THE DOG BREED MAKE THE $OG OBJECT AND PUT THE $OG INTO THE CONTEXT AS AN ATTRIBUTE

LQWHUIDFH!! 6HUYOHW&RQWH[W/LVWHQHU

0\6HUYOHW&RQWH[W/LVWHQHU FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW

FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW

ntextListener.

Implement javax.servlet.ServletCo

SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW

SXEOLF FODVV 0\6HUYOHW&RQWH[W/LVWHQHU LPSOHPHQWV 6HUYOHW&RQWH[W/LVWHQHU ^

SXEOLF YRLG FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW HYHQW ^ 6HUYOHW&RQWH[W VF HYHQW JHW6HUYOHW&RQWH[W 6WULQJ GRJ%UHHG VF JHW,QLW3DUDPHWHU ³EUHHG´ 'RJ G QHZ 'RJ GRJ%UHHG VF VHW$WWULEXWH ³GRJ´ G `

Make a new Dog.

xt. Ask the event for the ServletConte Use the context to get the init parameter.

Use the context to set an attribute (a name/object pair) that is the Dog. Now other parts of the app will be able to get the value of the attribute (the Dog).

SXEOLF YRLG FRQWH[W'HVWUR\HG 6HUYOHW&RQWH[W(YHQW HYHQW ^ QRWKLQJ WR GR KHUH ` We donÕt need anything here. `

The Dog doesnÕt need to be cleaned up... when the context goes away, it means the whole app is going down, including the Dog.

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

WKH DWWULEXWH FODVV

?ZQ\QVO \PM I\\ZQJ]\M KTI[[ ,WO /H YEAH WE NEED A $OG CLASSˆTHE CLASS REPRESENTING THE OBJECT WE RE GOING TO STORE IN THE 3ERVLET#ONTEXT AFTER READING THE CONTEXT INIT PARAMETERS

'RJ 'RJ 6WULQJ

JHW%UHHG

SDFNDJH FRP H[DPSOH SXEOLF FODVV 'RJ ^ SULYDWH 6WULQJ EUHHG

Nothing special here. Just a plain old Java class.

SXEOLF 'RJ 6WULQJ EUHHG ^ WKLV EUHHG EUHHG `

(WeÕll use the co parameter as thentext init the Dog constructargument for or.)

SXEOLF 6WULQJ JHW%UHHG ^ UHWXUQ EUHHG ` Our servlet will get the ` context (the Dog that

Dog from the the listener sets Ă•s getBreed() as an attribute), call the Dog ed in the method, and print out thein bre the browser. response so we can see it

4

) THOUGHT ) READ SOMEWHERE THAT SERVLET ATTRIBUTES HAD TO BE 3ERIALIZABLE

$

)NTERESTING QUESTION 4HERE ARE SEVERAL DIFFERENT ATTRIBUTE TYPES AND WHETHER THE ATTRIBUTE SHOULD BE 3ERIALIZABLE ONLY MATTERS WITH 3ESSION ATTRIBUTES !ND THE SCENARIO IN WHICH IT MATTERS IS ONLY IF THE APPLICATION IS DISTRIBUTED ACROSS MORE THAN ONE *6- 7E LL TALK ALL ABOUT THAT IN THE 3ESSIONS CHAPTER 4HERE S NO TECHNICAL NEED TO HAVE ANY ATTRIBUTES INCLUDING 3ESSION ATTRIBUTES BE 3ERIALIZABLE ALTHOUGH YOU MIGHT CONSIDER MAKING ALL OF YOUR ATTRIBUTES 3ERIALIZABLE BY DEFAULT UNLESS YOU HAVE A REALLY GOOD REASON ./4 TO 4HINK ABOUT ITˆARE YOU REALLY CERTAIN THAT NOBODY WILL EVER WANT TO USE OBJECTS OF THAT TYPE AS ARGUMENTS OR RETURN VALUES AS PART OF A REMOTE METHOD CALL #AN YOU REALLY GUARANTEE THAT ANYONE WHO USES THIS CLASS $OG IN THIS CASE WILL NEVER RUN IN A DISTRIBUTED ENVIRONMENT 3O ALTHOUGH YOU AREN T REQUIRED TO MAKE ANY ATTRIBUTES 3ERIALIZABLE YOU PROBABLY SHOULD IF YOU CAN

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

?ZQ\QVO \PM [MZ ^TM\ KTI[[ 4HIS IS THE CLASS THAT TESTS THE 3ERVLET#ONTEXT,ISTENER )F EVERYTHING IS WORKING RIGHT BY THE TIME THE 3ERVLET S DO'ET METHOD RUNS FOR THE l RST TIME THE $OG WILL BE WAITING AS AN ATTRIBUTE IN THE 3ERVLET#ONTEXT

+WWS6HUYOHW

/LVWHQHU7HVWHU GR*HW +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR

ial so far... Nothing speclar servlet. just a regu

SXEOLF FODVV /LVWHQHU7HVWHU H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW FRQWH[W DWWULEXWHV VHW E\ OLVWHQHU EU!´ m the Dog fro RXW SULQWOQ ³ EU!´ Now we geett Context. If the Servl er worked, the 'RJ GRJ 'RJ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³GRJ´ the listen BEFORE

donÕt forget the cast!!

RXW SULQWOQ Âł'RJÂśV EUHHG LV Âł GRJ JHW%UHHG

` `

JHW$WWULEXWH UHWXUQV W\SH 2EMHFW <RX QHHG WR FDVW WKH UHWXUQ X WHU UHWXUQV D 6WULQJ 6R \R DPH LW3DU %XW JHW,Q WWULEXWH EXW WKH JHW$ UQ RI UHWX W WKH W FDV PXV FDQ EH DVVLJQHG UHWXUQ RI JHW,QLW3DUDPHWHU

QœW EH IRROHG E\ EDG GLUHFWO\ WR D 6WULQJ 6R GR D FDVW H[DP FRGH WKDW GRHVQœW XVH Bad!! ³GRJ´ 'RJ G FW[ JHW$WWULEXWH RQWH[W

$VVXPH FW[ LV D 6HUYOHW&

there Dog will bee method is ic rv this se r the Ăžrst time. called fo

If things didnÕt work, THI weÕll Þnd out... weÕll get a Sbigis where NullPointerException if we tryfat getBreed() and thereÕs no Dog to call .

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRQÂż JXULQJ D OLVWHQHU LQ WKH ''

?ZQ\QVO \PM ,MXTWaUMV\ ,M[KZQX\WZ .OW WE TELL THE #ONTAINER THAT WE HAVE A LISTENER FOR THIS APP USING THE LISTENER ELEMENT 4HIS ELEMENT IS SIMPLEˆIT NEEDS ONLY THE CLASS NAME 4HAT S IT

"[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"! '2&7<3(

ZHE [PO

b.xml Ăžle This is the weEB -INF W e th e sid in is web app. th r fo y or ct re di

ZHE DSS [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE DSSB B [VG´ YHUVLRQ ´ ´! VHUYOHW! VHUYOHW QDPH!/LVWHQHU7HVWHU VHUYOHW QDPH! VHUYOHW FODVV!FRP H[DPSOH /LVWHQHU7HVWHU VHUYOHW FODVV! VHUYOHW! VHUYOHW PDSSLQJ! VHUYOHW QDPH!/LVWHQHU7HVWHU VHUYOHW QDPH! XUO SDWWHUQ! /LVWHQ7HVW GR XUO SDWWHUQ! VHUYOHW PDSSLQJ!

FRQWH[W SDUDP! SDUDP QDPH!EUHHG SDUDP QDPH! SDUDP YDOXH!*UHDW 'DQH SDUDP YDOXH! FRQWH[W SDUDP! OLVWHQHU! OLVWHQHU FODVV! FRP H[DPSOH 0\6HUYOHW&RQWH[W/LVWHQHU OLVWHQHU FODVV! OLVWHQHU! ZHE DSS!

meter ext init paraed We need a cont ne s this r ne te lis he T for the app. the Dog. to construct

Register this cla the <listener> elessmeas a listener. IMPORTANT: inside a <servlet> nt does NOT go ment. That wo nÕt work because a conele text listener is fould ServletContext (whic ra wide) event. The wh h means applicationole poi nt is to initialize the app BEFORE any servlets are initialize d.

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

WKHUH DUH QR

'XPE 4XHVWLRQV

4

(OLD ON HOW ARE YOU TELLING THE #ONTAINER THAT THIS IS A LISTENER FOR 3ERVLET#ONTEXT EVENTS 4HERE DOESN T SEEM TO BE AN 8-, ELEMENT FOR LISTENER TYPE OR ANYTHING THAT SAYS WHAT TYPE OF EVENTS THIS LISTENER IS FOR "UT ) NOTICED YOU HAVE h3ERVLET#ONTEXT,ISTENERv AS PART OF THE CLASS NAMEˆIS THAT HOW THE #ONTAINER KNOWS "Y THE NAMING CONVENTION

$

.O 4HERE S NO NAMING CONVENTION 7E JUST DID IT THAT WAY TO MAKE IT PAINFULLY CLEAR WHAT KIND OF A CLASS WE WROTE 4HE #ONTAINER FIGURES IT OUT SIMPLY BY INSPECTING THE CLASS AND NOTICING THE LISTENER INTERFACE OR INTERFACES A LISTENER CAN IMPLEMENT MORE THAN ONE LISTENER INTER FACE

4

$OES THAT MEAN THERE ARE OTHER TYPES OF LISTEN ERS IN THE SERVLET !0)

$

9ES THERE ARE SEVERAL OTHER TYPES OF LISTENERS THAT WE LL TALK ABOUT IN A MINUTE

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRPSLOLQJ DQG GHSOR\LQJ WKH OLVWHQHU WHVW

+WUXQTM IVL LMXTWa

ps LL your webowapn Remember: Aac s it th wi h go in here (eructure). directory st is web app. e The root of th st be insid mu y or This direct appsÓ directory. eb TomcatÕs Òw

WRPFDW

,ET S GET IT ALL WORKING 4HE STEPS ARE ZHEDSSV

&RPSLOH WKH WKUHH FODVVHV 7KH\ÂśUH DOO LQ WKH VDPH SDFNDJH

OLVWHQHU7HVW

&UHDWH D QHZ ZHE DSS LQ 7RPFDW N &UHDWH D GLUHFWRU\ QDPHG OLVWHQHU7HVW DQG SODFH LW LQVLGH WKH 7RPFDW ZHEDSSV GLUHFWRU\ N &UHDWH D GLUHFWRU\ QDPHG :(% ,1) DQG SODFH LW LQVLGH WKH OLVWHQHU7HVW GLUHFWRU\

:(% ,1) "[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"! '2&7<3(

FODVVHV

ZHE [PO

FRP

N 3XW \RXU ZHE [PO Âż OH LQ WKH :(% ,1) GLUHFWRU\ N 0DNH D FODVVHV GLUHFWRU\ LQVLGH :(% ,1) N 0DNH D GLUHFWRU\ VWUXFWXUH LQVLGH FODVVHV WKDW PDWFKHV \RXU SDFNDJH VWUXFWXUH D GLUHFWRU\ FDOOHG FRP WKDW FRQWDLQV H[DPSOH

&RS\ \RXU WKUHH FRPSLOHG À OHV LQWR \RXU ZHE DSS GLUHFWRU\ VWUXFWXUH LQ 7RPFDW

H[DPSOH

/LVWHQHU7HVWHU FODVV

'RJ FODVV

0\6HUYOHW&RQWH[W/LVWHQHU FODVV

OLVWHQHU7HVW :(% ,1) FODVVHV FRP H[DPSOH 'RJ FODVV OLVWHQHU7HVW :(% ,1) FODVVHV FRP H[DPSOH /LVWHQHU7HVWHU FODVV OLVWHQHU7HVW :(% ,1) FODVVHV FRP H[DPSOH 0\6HUYOHW&RQWH[W/LVWHQHU FODVV

3XW \RXU ZHE [PO 'HSOR\PHQW 'HVFULSWRU LQWR WKH :(% ,1) GLUHFWRU\ IRU WKLV ZHE DSS OLVWHQHU7HVW :(% ,1) ZHE [PO

'HSOR\ WKH DSS E\ VKXWWLQJ GRZQ DQG UHVWDUWLQJ 7RPFDW FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<Za Q\ W]\ "RING UP YOUR BROWSER AND LET S HIT THE SERVLET DIRECTLY 7E DIDN T BOTHER MAKING AN (4-, PAGE SO WE LL ACCESS THE SERVLET BY TYPING IN THE 52, FROM THE SERVLET MAPPING IN THE $$ ,ISTEN4EST DO

KWWS ORFDOKRVW OLVWHQHU7HVW /LVWHQ7HVW GR

WHVW FRQWH[W DWWULEXWHV VHW E\ OLVWHQHU 'RJ¡V EUHHG LV *UHDW 'DQH

servlet e worked ! The It must have hod on the Dog attribut called a mett by the listener. that was se

<ZW]JTM[PWW\QVO )F YOU GET A .ULL0OINTER%XCEPTION YOU DIDN T GET A $OG BACK FROM GET!TTRIBUTE #HECK THE 3TRING NAME USED IN SET!TTRIBUTE AND MAKE SURE IT MATCHES THE 3TRING NAME YOU RE USING IN GET!TTRIBUTE 2ECHECK YOUR WEB XML AND MAKE SURE THE LISTENER IS REGISTERED 4RY LOOKING AT THE SERVER LOGS AND SEE IF YOU CAN l ND OUT IF THE LISTENER IS ACTUALLY BEING CALLED 4O MAKE IT AS CONFUSING AS POSSIBLE WE GAVE EVERYTHING A SUBTLY DIFFERENT NAME 7E WANT TO MAKE SURE YOU RE PAYING ATTENTION TO HOW THESE NAMES ARE USED AND WHEN YOU NAME EVERYTHING THE SAME IT S TOUGH TO TELL HOW THE NAMES AFFECT YOUR APP 3ERVLET CLASS NAME ,ISTENER4ESTER CLASS 7EB APP DIRECTORY NAME LISTENER4EST 52, PATTERN MAPPED TO THIS THE SERVLET ,ISTEN4EST DO

itÕs out whether Be careful abListen, Tester or Test. Listener or

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

KRZ RXU FRQWH[W OLVWHQHU ZRUNV

<PM N]TT [\WZa (ERE S THE SCENARIO FROM START APP INITIALIZATION TO l NISH SERVLET RUNS 9OU LL SEE IN STEP WE CONDENSED THE 3ERVLET INITIALIZATION INTO ONE BIG STEP &RQWDLQHU UHDGV WKH 'HSOR\PHQW 'HVFULS WRU IRU WKLV DSS LQFOXGLQJ WKH OLVWHQHU! DQG FRQWH[W SDUDP! HOHPHQWV

UHDG

&RQWDLQHU

VHUYOHW! LQLW SDUDP! SDUDP QDPH! IRR SDUDP QDPH! SDUDP YDOXH! EDU SDUDP YDOXH!

&RQWDLQHU FUHDWHV D QHZ 6HUYOHW&RQWH[W IRU WKLV DSSOLFDWLRQ WKDW DOO SDUWV RI WKH DSS ZLOO VKDUH QHZ &RQWDLQHU 6HUYOHW&RQWH[W

ZHE [PO

&RQWDLQHU FUHDWHV D QDPH YDOXH SDLU RI 6WULQJV IRU HDFK FRQWH[W LQLW SDUDPHWHU $VVXPH ZH KDYH RQO\ RQH

QHZ

´EUHHGµ

´EUHHGµ 6WULQJ

&RQWDLQHU

QHZ

context init parameter from DD

´*UHDW 'DQHµ

&RQWDLQHU FUHDWHV D QHZ LQVWDQFH RI WKH 0\6HUYOHW&RQWH[W/LVWHQHU FODVV

6WULQJ

6HUYOHW&RQWH[W

´*UHDW 'DQHµ 6WULQJ

6WULQJ

&RQWDLQHU JLYHV WKH 6HUYOHW&RQWH[W UHIHU HQFHV WR WKH QDPH YDOXH SDUDPHWHUV

QHZ &RQWDLQHU LQVWDQFH RI 0\6HUYOHW&RQWH[W/LVWHQHU FODVV

&RQWDLQHU FDOOV WKH OLVWHQHU·V FRQWH[W,QLWLDOL]HG PHWKRG SDVVLQJ LQ D QHZ 6HUYOHW&RQWH[W(YHQW 7KH HYHQW REMHFW KDV D UHIHUHQFH WR WKH 6HUYOHW&RQWH[W VR WKH HYHQW KDQGOLQJ FRGH FDQ JHW WKH FRQWH[W IURP WKH HYHQW DQG JHW WKH FRQWH[W 6HUYOHW &RQWH[W LQLW SDUDPHWHU IURP WKH FRQWH[W

´EUHHGµ

6WULQJ ´*UHDW 'DQHµ

6WULQJ

6HUYOHW&RQWH[W(YHQW

&RQWDLQHU

FRQWH[W,QLWLDOL]HG 6HUYOHW&RQWH[W(YHQW

OLVWHQHU

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<PM [\WZa KWV\QV]M[

/LVWHQHU DVNV 6HUYOHW&RQWH[W(YHQW IRU D UHIHUHQFH WR WKH 6HUYOHW&RQWH[W

/LVWHQHU DVNV 6HUYOHW&RQWH[W IRU WKH FRQWH[W LQLW SDUDPHWHU ´EUHHGµ ´EUHHGµ

JHW6HUYOHW&RQWH[W

6WULQJ

JHW,QLW3DUDPHWHU ´EUHHGµ

6HUYOHW&RQWH[W(YHQW

OLVWHQHU

6HUYOHW&RQWH[W

OLVWHQHU

/LVWHQHU XVHV WKH LQLW SDUDPHWHU WR FRQVWUXFW D QHZ 'RJ REMHFW

´*UHDW 'DQHµ 6WULQJ

/LVWHQHU VHWV WKH 'RJ DV DQ DWWULEXWH LQ WKH 6HUYOHW&RQWH[W

QHZ VHW$WWULEXWH ´GRJµ G

6HUYOHW&RQWH[W

LQVWDQFH RI 'RJ FODVV

OLVWHQHU

OLVWHQHU

&RQWDLQHU PDNHV D QHZ 6HUYOHW L H PDNHV D QHZ 6HUYOHW&RQÀJ ZLWK LQLW SDUDPHWHUV JLYHV WKH 6HUYOHW&RQÀJ D UHIHUHQFH WR WKH 6HUYOHW&RQWH[W WKHQ FDOOV WKH 6HUYOHW·V LQLW PHWKRG

6HUYOHW JHWV D UHTXHVW DQG DVNV WKH 6HUYOHW&RQWH[W IRU WKH DWWULEXWH ´GRJµ JHW$WWULEXWH ´GRJµ

6WULQJ

6HUYOHW 6HUYOHW &RQÀJ

&RQWDLQHU

6HUYOHW&RQWH[W

6HUYOHW &RQWH[W 6WULQJ

LQLW 6HUYOHW&RQÀJ

LQVWDQFH RI /LVWHQHU7HVWHU FODVV

6HUYOHW FDOOV JHW%UHHG RQ WKH 'RJ DQG SULQWV WKDW WR WKH +WWS5HVSRQVH

JHW%UHHG

6HUYOHW

'RJ

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

RWKHU OLVWHQHUV

, MXVW WKRXJKW RI VRPHWKLQJ VLQFH DWWULEXWHV FDQ EH VHW SURJUDPPDWLFDOO\ XQOLNH LQLW SDUDPHWHUV FDQ , OLVWHQ IRU DWWULEXWH HYHQWV" /LNH LI VRPHRQH DGGV RU UHSODFHV D 'RJ"

4Q[\MVMZ[" VW\ R][\ NWZ KWV\M`\ M^MV\[ 7HERE THERE S A LIFECYCLE MOMENT THERE S USUALLY A LISTENER TO HEAR ABOUT IT "ESIDES CONTEXT EVENTS YOU CAN LISTEN FOR EVENTS RELATED TO CONTEXT ATTRIBUTES SERVLET REQUESTS AND ATTRIBUTES AND (440 SESSIONS AND SESSION ATTRIBUTES

<RX GRQÂśW KDYH WR NQRZ DOO RI WKH OLVWHQHU $3, 2WKHU WKDQ 6HUYOHW&RQWH[W/LVWHQHU \RX UHDOO\ GRQÂśW QHHG WR PHPRUL]H WKH PHWKRGV RI HDFK RI WKH OLVWHQHU LQWHUIDFHV %XW \RX '2 QHHG WR NQRZ WKH NLQGV RI HYHQWV WKDW \RX FDQ OLVWHQ IRU 7KH H[DP REMHFWLYHV DUH FOHDU \RXÂśOO EH JLYHQ D VFHQDULR D GHYHORSHUÂśV JRDO IRU DQ DSSOLFDWLRQ DQG \RXÂśOO QHHG WR GHFLGH ZKLFK LV WKH ULJKW W\SH RI OLVWHQHU RU ZKHWKHU LWÂśV HYHQ 3266,%/( WR EH QRWLÂżHG RI WKDW OLIHF\FOH HYHQW

Note: we donÕt talk about sessions until the next chapter, so donÕt worry about it if you donÕt yet know what an HTTP session is or why you care...

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

0ICK THE ,ISTENER ([HUFLVH

-ATCH THE SCENARIO ON THE LEFT WITH THE LISTENER INTERFACE AT THE BOTTOM OF THE PAGE THAT SUPPORTS THAT GOAL 5SE EACH INTERFACE ONLY ONCE 9ES WE +./7 WE HAVEN T LOOKED AT THESE YET 3EE WHAT YOU CAN COME UP WITH JUST BY LOOKING AT THE NAMES !NSWERS ARE ON THE NEXT PAGE SO DON T PEEK

3CENARIO

,ISTENER INTERFACE

<RX ZDQW WR NQRZ LI DQ DWWULEXWH LQ D ZHE DSS FRQWH[W KDV EHHQ DGGHG UHPRYHG RU UHSODFHG <RX ZDQW WR NQRZ KRZ PDQ\ FRQFXUUHQW XVHUV WKHUH DUH ,Q RWKHU ZRUGV \RX ZDQW WR WUDFN WKH DFWLYH VHVVLRQV <RX ZDQW WR NQRZ HDFK WLPH D UHTXHVW FRPHV LQ VR WKDW \RX FDQ ORJ LW <RX ZDQW WR NQRZ ZKHQ D UHTXHVW DWWULEXWH KDV EHHQ DGGHG UHPRYHG RU UHSODFHG <RX KDYH DQ DWWULEXWH FODVV D FODVV IRU DQ REMHFW WKDW ZLOO EH SXW LQ DQ DWWULEXWH DQG \RX ZDQW REMHFWV RI WKLV W\SH WR EH QRWLILHG ZKHQ WKH\ DUH ERXQG WR RU UHPRYHG IURP D VHVVLRQ <RX ZDQW WR NQRZ ZKHQ D VHVVLRQ DWWULEXWH KDV EHHQ DGGHG UHPRYHG RU UHSODFHG

#HOOSE FROM THESE LISTENER INTERFACES 5SE EACH LISTENER ONLY ONCE

+WWS6HVVLRQ$WWULEXWH/LVWHQHU

6HUYOHW5HTXHVW/LVWHQHU

+WWS6HVVLRQ%LQGLQJ/LVWHQHU 6HUYOHW&RQWH[W$WWULEXWH/LVWHQHU

+WWS6HVVLRQ/LVWHQHU 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRPPRQ OLVWHQHUV

<PM MQOP\ TQ[\MVMZ[ 6FHQDULR

/LVWHQHU LQWHUIDFH

(YHQW W\SH

<RX ZDQW WR NQRZ LI DQ DWWULEXWH LQ D ZHE DSS FRQWH[W KDV EHHQ DGGHG UHPRYHG RU UHSODFHG

MDYD[ VHUYOHW 6HUYOHW&RQWH[W$WWULEXWH/LVWHQHU DWWULEXWH$GGHG DWWULEXWH5HPRYHG DWWULEXWH5HSODFHG

6HUYOHW&RQWH[W$WWULEXWH(YHQW

<RX ZDQW WR NQRZ KRZ PDQ\ FRQFXUUHQW XVHUV WKHUH DUH ,Q RWKHU ZRUGV \RX ZDQW WR WUDFN WKH DFWLYH VHVVLRQV :H FRYHU VHVVLRQV LQ GHWDLO LQ WKH QH[W FKDSWHU

MDYD[ VHUYOHW KWWS +WWS6HVVLRQ/LVWHQHU VHVVLRQ&UHDWHG VHVVLRQ'HVWUR\HG

+WWS6HVVLRQ(YHQW

<RX ZDQW WR NQRZ HDFK WLPH D UHTXHVW FRPHV LQ VR WKDW \RX FDQ ORJ LW

MDYD[ VHUYOHW 6HUYOHW5HTXHVW/LVWHQHU UHTXHVW,QLWLDOL]HG UHTXHVW'HVWUR\HG

6HUYOHW5HTXHVW(YHQW

<RX ZDQW WR NQRZ ZKHQ D UHTXHVW DWWULEXWH KDV EHHQ DGGHG UHPRYHG RU UHSODFHG

MDYD[ VHUYOHW 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU DWWULEXWH$GGHG DWWULEXWH5HPRYHG DWWULEXWH5HSODFHG

6HUYOHW5HTXHVW$WWULEXWH(YHQW

<RX KDYH DQ DWWULEXWH FODVV D FODVV IRU DQ REMHFW WKDW ZLOO VWRUHG DV DQ DWWULEXWH DQG \RX ZDQW REMHFWV RI WKLV W\SH WR EH QRWLILHG ZKHQ WKH\ DUH ERXQG WR RU UHPRYHG IURP D VHVVLRQ

MDYD[ VHUYOHW KWWS +WWS6HVVLRQ%LQGLQJ/LVWHQHU YDOXH%RXQG YDOXH8QERXQG

+WWS6HVVLRQ%LQGLQJ(YHQW

<RX ZDQW WR NQRZ ZKHQ D VHVVLRQ DWWULEXWH KDV EHHQ DGGHG UHPRYHG RU UHSODFHG

+WWS6HVVLRQ%LQGLQJ(YHQW MDYD[ VHUYOHW KWWS +WWS6HVVLRQ$WWULEXWH/LVWHQHU t DWWULEXWH$GGHG ing inconsistency ! The Even nam this for out Watch DWWULEXWH5HPRYHG is NOT what you ener List bute ttri sionA pSes Htt for t). DWWULEXWH5HSODFHG expect HttpSessionAttributeEven

expect (you

<RX ZDQW WR NQRZ LI D FRQWH[W KDV EHHQ FUHDWHG RU GHVWUR\HG

MDYD[ VHUYOHW 6HUYOHW&RQWH[W/LVWHQHU FRQWH[W,QLWLDOL]HG FRQWH[W'HVWUR\HG

6HUYOHW&RQWH[W(YHQW

<RX KDYH DQ DWWULEXWH FODVV DQG \RX ZDQW REMHFWV RI WKLV W\SH WR EH QRWLILHG ZKHQ WKH VHVVLRQ WR ZKLFK WKH\¡UH ERXQG LV PLJUDWLQJ WR DQG IURP DQRWKHU -90

MDYD[ VHUYOHW KWWS +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU VHVVLRQ'LG$FWLYDWH VHVVLRQ:LOO3DVVLYDWH

+WWS6HVVLRQ(YHQW

ItÕs NOT ÒHttpSessionActivatio

nEventÓ

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<PM 0\\X;M[[QWV*QVLQVO4Q[\MVMZ 9OU MIGHT BE CONFUSED ABOUT THE DIFFERENCE BETWEEN AN (TTP3ESSION"INDING,ISTENER AND AN (TTP3ESSION!TTRIBUTE,ISTENER 7ELL NOT YOU BUT SOMEONE YOU WORK WITH ! PLAIN OLD (TTP3ESSION!TTRIBUTE,ISTENER IS JUST A CLASS THAT WANTS TO KNOW WHEN ANY TYPE OF ATTRIBUTE HAS BEEN ADDED REMOVED OR REPLACED IN A 3ESSION "UT THE (TTP3ESSION"INDING,ISTENER EXISTS SO THAT THE ATTRIBUTE ITSELF CAN lND OUT WHEN IT HAS BEEN ADDED TO OR REMOVED FROM A 3ESSION

:LWK WKLV OLVWHQHU ,¡P PRUH DZDUH RI P\ UROH LQ WKH DSSOLFDWLRQ 7KH\ WHOO PH ZKHQ ,¡P SXW LQWR D VHVVLRQ RU WDNHQ RXW

SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW KWWS SXEOLF FODVV 'RJ LPSOHPHQWV +WWS6HVVLRQ%LQGLQJ/LVWHQHU ^ SULYDWH 6WULQJ EUHHG SXEOLF 'RJ 6WULQJ EUHHG ^ WKLV EUHHG EUHHG ` SXEOLF 6WULQJ JHW%UHHG ^ UHWXUQ EUHHG `

This time the Listener... listeDninog attribute is ALSO a is added or re g for when the Dog it binding listenersmoved from a Session. (Notsee:lf DD... it just ha are NOT registered in the ppens automatica lly.)

SXEOLF YRLG YDOXH%RXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ ,ÂśP LQ D VHVVLRQ ` SXEOLF YRLG YDOXH8QERXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ , DP QR ORQJHU SDUW RI D VHVVLRQ ` `

4

/+ ) GET HOW IT WORKS ) GET THAT THE $OG AN ATTRIBUTE THAT LL BE ADDED TO A SESSION WANTS TO KNOW WHEN IT S IN OR OUT OF A SESSION 7HAT ) DON T GET IS 7(9

$

)F YOU KNOW ANYTHING ABOUT %NTITY BEANS THEN YOU CAN PICTURE THIS CAPABILITY AS A KIND OF hPOOR MAN S ENTITY BEANv )F YOU DON T KNOW ABOUT ENTITY BEANS YOU SHOULD RUN TO YOUR NEAREST BOOK STORE AND BUY TWO COPIES OF (EAD &IRST %*" ONE FOR YOU ONE FOR YOUR SIGNIFICANT OTHER SO YOU CAN SHARE SPECIAL MOMENTS DISCUSSING IT )N THE MEANTIME HERE S A WAY TO THINK ABOUT ITˆIMAGINE

They use the rd ÒboundÓ and Òuwo mean Òadded tonboundÓ to Òremoved fromÓ. Ó and

THE $OG IS A #USTOMER CLASS WITH EACH ACTIVE INSTANCE REPRESENTING A SINGLE CUSTOMER S INFO FOR NAME ADDRESS ORDER INFO ETC 4HE REAL DATA IS STORED IN AN UNDERLYING DATABASE 9OU USE THE DATABASE INFO TO POPULATE THE FIELDS OF THE #USTOMER OBJECT BUT THE ISSUE IS HOW AND WHEN DO YOU KEEP THE DATABASE RECORD AND THE #USTOMER INFO SYNCHRONIZED 9OU KNOW THAT WHENEVER A #USTOMER OBJECT IS ADDED TO A SESSION IT S TIME TO REFRESH THE FIELDS OF THE #USTOMER WITH THIS CUSTOMER S DATA FROM HIS RECORD IN THE DATABASE 3O THE VALUE"OUND METHOD IS LIKE A KICK THAT SAYS h'O LOAD ME UP WITH FRESH DATA FROM THE DATABASE JUST IN CASE IT CHANGED SINCE THE LAST TIME ) WAS USED v 4HEN VALUE5NBOUND IS A KICK THAT SAYS h5PDATE THE DATABASE WITH THE VALUE OF THE #USTOMER OBJECT FIELDS v \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

OLVWHQHU FKDUW

2EMEMBERING THE ,ISTENERS ([HUFLVH

$O YOUR BEST TO FILL IN THE SLOTS IN THIS TABLE +EEP IN MIND THAT THE LISTENER INTERFACES AND METHODS FOLLOW A CONSISTENT NAMING PATTERN MOSTLY !NSWERS ARE AT THE END OF THE CHAPTER

$WWULEXWH OLVWHQHUV

2WKHU OLIHF\FOH OLVWHQHUV

0HWKRGV LQ DOO DWWULEXWH OLVWHQHUV H[FHSW ELQGLQJ OLVWHQHU

/LIHF\FOH HYHQWV UHODWHG WR VHVVLRQV H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

/LIHF\FOH HYHQWV UHODWHG WR UHTXHVWV H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

/LIHF\FOH HYHQWV UHODWHG WR VHUYOHW FRQWH[W H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

?PI\ M`IK\Ta Q[ IV I\\ZQJ]\M' 7E SAW HOW THE 3ERVLET#ONTEXT LISTENER CREATED A $OG OBJECT AFTER GETTING THE CONTEXT INIT PARAMETER AND WAS ABLE TO STICK SET THE $OG INTO THE 3ERVLET#ONTEXT AS AN ATTRIBUTE SO THAT OTHER PARTS OF THE APP COULD GET IT %ARLIER WITH THE BEER TUTORIAL WE SAW HOW THE SERVLET WAS ABLE TO STICK THE RESULTS OF THE CALL TO THE MODEL INTO THE 2EQUEST USUALLY (TTP3ERVLET2EQUEST OBJECT AS AN ATTRIBUTE SO THAT THE *30 VIEW COULD GET THE VALUE !N ATTRIBUTE IS AN OBJECT SET REFERRED TO AS BOUND INTO ONE OF THREE OTHER SERVLET !0) OBJECTSˆ3ERVLET#ONTEXT (TTP3ERVLET2EQUEST OR 3ERVLET2EQUEST OR (TTP3ESSION 9OU CAN THINK OF IT AS SIMPLY A NAME VALUE PAIR WHERE THE NAME IS A 3TRING AND THE VALUE IS AN /BJECT IN A MAP INSTANCE VARIABLE )N REALITY WE DON T KNOW OR CARE HOW IT S ACTUALLY IMPLEMENTEDˆALL WE REALLY CARE ABOUT IS THE SCOPE IN WHICH THE ATTRIBUTE EXISTS )N OTHER WORDS WHO CAN SEE IT AND HOW LONG DOES IT LIVE

Who can see this bulletin board? Who can get and set the attributes?

$Q DWWULEXWH LV OLNH DQ REMHFW SLQQHG WR D EXOOHWLQ ERDUG 6RPHERG\ VWXFN LW RQ WKH ERDUG VR WKDW RWKHUV FDQ JHW LW 7KH ELJ TXHVWLRQV DUH ZKR KDV DFFHVV WR WKH EXOOHWLQ ERDUG DQG KRZ ORQJ GRHV LW OLYH" ,Q RWKHU ZRUGV ZKDW LV WKH VFRSH RI WKH DWWULEXWH"

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV YV SDUDPHWHUV

)\\ZQJ]\M[ IZM VW\ XIZIUM\MZ[ )F YOU RE NEW TO SERVLETS YOU MIGHT NEED TO SPEND SOME TIME REINFORCING THE DIFFERENCE BETWEEN ATTRIBUTES AND PARAMETERS 2EST ASSURED THAT WHEN WE CREATED THE EXAM WE SPENT JUST THAT LITTLE BIT OF EXTRA TIME TRYING TO MAKE SURE WE MADE ATTRIBUTE AND PARAMETER QUESTIONS AS CONFUSING AS POSSIBLE

$WWULEXWHV

7\SHV

$SSOLFDWLRQ FRQWH[W 5HTXHVW 6HVVLRQ

0HWKRG WR VHW

There is no servlet- t speciĂžc attribute (jus e). use an instance variabl

VHW$WWULEXWH 6WULQJ QDPH 2EMHFW YDOXH

3DUDPHWHUV

$SSOLFDWLRQ FRQWH[W LQLW SDUDPHWHUV 5HTXHVW SDUDPHWHUV 6HUYOHW LQLW SDUDPHWHUV

as No such thing ter session parame s!

<RX &$1127 VHW $SSOLFDWLRQ DQG 6HUYOHW LQLW SDUDPHWHUV³WKH\¡UH VHW LQ WKH '' UHPHPEHU" :LWK 5HTXHVW SDUDPHWHUV \RX FDQ DGMXVW WKH TXHU\ 6WULQJ EXW WKDW¡V GLIIHUHQW

nce !

Big differe

5HWXUQ W\SH

2EMHFW

6WULQJ

0HWKRG WR JHW

JHW$WWULEXWH 6WULQJ QDPH

JHW,QLW3DUDPHWHU 6WULQJ QDPH

DonÕt forget that attribu must be cast , since the return typetes is Object.

,W¡V WUXH ,I ZH¡G PDGH WKH H[DP VLPSOH DQG VWUDLJKWIRUZDUG DQG HDV\ \RX ZRXOGQ¡W IHHO WKDW VHQVH RI SULGH DQG DFFRP SOLVKPHQW IURP SDVVLQJ WKH H[DP 0DNLQJ WKH H[DP GLIILFXOW HQRXJK WR HQVXUH WKDW \RX¡G QHHG WR EX\ D VWXG\ JXLGH LQ RUGHU WR SDVV LW ZDV QHYHU (9(5 D SDUW RI RXU WKLQNLQJ 1R VHULRXVO\ :H ZHUH MXVW WKLQNLQJ RI \RX

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<PM <PZMM ; KWXM[" +WV\M`\ :MY]M[\ IVL ;M[[QWV ;gfl]pl 9lljaZml]k

VHUYOHW

VHW

VHW ´$GPLQ (PDLOµ

'%

JHW

IRR#ZLFNHGO\VPDUW FRP

'DWDEDVH &RQQHFWLRQ

&

HU RQ WH[ LVWHQ W/

W

JH

JHW JHW

VHW VHUYOHW

VHUYOHW

´&RQFXUUHQW 8VHUVµ

(YHU\RQH LQ WKH DSSOLFDWLRQ KDV DFFHVV

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

-63

K]kkagf 9lljaZml]k VHUYOHW

JHW

VHW

6HUYOHW $

W

6K RSS &D U LQ J

VHUYOHW

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

-63 9LHZ

JHW VHW

6HUYOHW % $FFHVVLEOH WR RQO\ WKRVH ZLWK DFFHVV WR D VSHFLÀ F +WWS6HVVLRQ

J=IM=KL 9lljaZml]k VHW VHUYOHW

JHW ´%HHU5HFRPPHQGDWLRQµ ´0RRVH 'URROµ

&RQWUROOHU

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

-63 9LHZ $FFHVVLEOH WR RQO\ WKRVH ZLWK DFFHVV WR D VSHFLÀ F 6HUYOHW5HTXHVW \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWH VFRSH H[HUFLVH

!TTRIBUTE 3COPE ([HUFLVH

$O YOUR BEST TO FILL IN THE SLOTS IN THIS TABLE 9OU 2%!,,9 NEED TO UNDERSTAND ATTRIBUTE SCOPE FOR THE EXAM AND THE REAL WORLD BECAUSE YOU HAVE TO KNOW WHICH SCOPE IS THE BEST TO USE FOR A GIVEN SCENARIO 9OU LL SEE THE ANSWER IN A FEW PAGES BUT DON T LOOK AHEAD )F YOU RE GOING TO TAKE THE EXAM TRUST US YOU NEED TO FILL THIS OUT YOURSELF BY TAKING THE TIME TO THINK IT THROUGH

$FFHVVLELOLW\

6FRSH

ZKR FDQ VHH LW

HOW LONG DOES IT LIVE

:KDW LW¡V JRRG IRU

&RQWH[W

+WWS6HVVLRQ

5HTXHVW

(Note: you sh collection whenould think about the impl wonÕt be GC you think about scop ications of garbage ThereÕs nothinÕd until the application ise... some attributes undeployed or management ing on the exam about desig dies. mind, but itÕs ni something to ng with memory be aware of).

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

)\\ZQJ]\M )81 4HE THREE ATTRIBUTE SCOPESˆCONTEXT REQUEST AND SESSIONˆARE HANDLED BY THE 3ERVLET#ONTEXT 3ERVLET2EQUEST AND (TTP3ESSION INTERFACES 4HE !0) METHODS FOR ATTRIBUTES ARE EXACTLY THE SAME IN EVERY INTERFACE

&RQWH[W

2EMHFW JHW$WWULEXWH 6WULQJ QDPH

VHW$WWULEXWH 6WULQJ QDPH 2EMHFW YDOXH

UHPRYH$WWULEXWH 6WULQJ QDPH

HQXPHUDWLRQ JHW$WWULEXWH1DPHV

5HTXHVW

6HVVLRQ

LQWHUIDFH!!

LQWHUIDFH!!

LQWHUIDFH!!

6HUYOHW&RQWH[W

6HUYOHW5HTXHVW

+WWS6HVVLRQ

JHW,QLW3DUDPHWHU 6WULQJ

JHW&RQWH[W7\SH

JHW$WWULEXWH 6WULQJ

JHW,QLW3DUDPHWHU1DPHV

JHW3DUDPHWHU

VHW$WWULEXWH 6WULQJ 2EMHFW

JHW$WWULEXWH 6WULQJ

UHPRYH$WWULEXWH 6WULQJ

JHW$WWULEXWH 6WULQJ

VHW$WWULEXWH 6WULQJ 2EMHFW

JHW$WWULEXWH1DPHV

VHW$WWULEXWH 6WULQJ 2EMHFW

UHPRYH$WWULEXWH 6WULQJ

UHPRYH$WWULEXWH 6WULQJ

JHW$WWULEXWH1DPHV

JHW$WWULEXWH1DPHV

0$1< PRUH PHWKRGV

JHW0DMRU9HUVLRQ

VHW0D[,QDFWLYH,QWHUYDO

JHW,'

JHW/DVW$FFHVVHG7LPH

0$1< PRUH PHWKRGV

JHW6HUYHU,QIR

JHW5HDO3DWK 6WULQJ

JHW5HVRXUFH$V6WUHDP 6WULQJ

LQWHUIDFH!!

+WWS6HUYOHW5HTXHVW

JHW5HTXHVW'LVSDWFKHU 6WULQJ

JHW&RQWH[W3DWK

ORJ 6WULQJ

JHW&RRNLHV

0$1< PRUH PHWKRGV

JHW+HDGHU 6WULQJ

JHW4XHU\6WULQJ

JHW6HVVLRQ

nothing related to attributes here

0$1< PRUH PHWKRGV

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWH VWUDQJHQHVV

<PM LIZS [QLM WN I\\ZQJ]\M[ +IM DECIDES TO TEST OUT ATTRIBUTES (E SETS AN ATTRIBUTE AND THEN IMMEDIATELY GETS THE VALUE OF THE ATTRIBUTE AND DISPLAYS IT IN THE RESPONSE (IS DO'ET LOOKS LIKE THIS

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW FRQWH[W DWWULEXWHV EU!´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³IRR´ ³ ´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³EDU´ ³ ´ RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³IRR´

RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³EDU´

`

(ERE S WHAT HE SEES THE l RST TIME HE RUNS IT )T S EXACTLY WHAT HE EXPECTED

KWWS ORFDOKRVW OLVWHQHU7HVW /LVWHQ7HVW GR

WHVW FRQWH[W DWWULEXWHV

pect...

ex Just what we

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

*]\ \PMV [WUM\PQVO OWM[ PWZZQJTa _ZWVO 4HE SECOND TIME HE RUNS IT HE S SHOCKED TO SEE

KWWS ORFDOKRVW OLVWHQHU7HVW /LVWHQ7HVW GR

WHVW FRQWH[W DWWULEXWHV

ppen??? * did THIS ha? WhereÕs 42 ? om How the #!@ fr 16 come WhereÕd the

# 4X3 X9 8 /RRN FORVHO\ DW WKH FRGH DQG WKLQN DERXW ZKDWÂśV KDSSHQLQJ 'R \RX VHH DQ\WKLQJ WKDW FRXOG H[SODLQ WKH SUREOHP" <RX PLJKW QRW KDYH HQRXJK LQIR WR VROYH WKH P\VWHU\ VR KHUHÂśV DQRWKHU FOXH .LP SXW WKLV FRGH LQ D WHVW VHUYOHW WKDWÂśV SDUW RI D ODUJHU WHVW ZHE DSS ,Q RWKHU ZRUGV WKH VHUYOHW WKDW KROGV WKLV GR*HW PHWKRG ZDV GHSOR\HG DV SDUW RI D ODUJHU DSS 1RZ FDQ \RX Âż JXUH LW RXW" &DQ \RX WKLQN RI KRZ KH PLJKW Âż [ LW"

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRQWH[W VFRSH DQG WKUHDG VDIHW\

7KHUH PXVW EH DQRWKHU VHUYOHW KLWWLQJ WKH VDPH FRQWH[W DWWULEXWH

+WV\M`\ [ KWXM Q[VÂź\ \PZMIL [INM 4HAT S THE PROBLEM 2EMEMBER EVERYONE IN THE APP HAS ACCESS TO CONTEXT ATTRIBUTES AND THAT MEANS MULTIPLE SERVLETS !ND MULTIPLE SERVLETS MEANS YOU MIGHT HAVE MULTIPLE THREADS SINCE REQUESTS ARE CONCURRENTLY HANDLED EACH IN A SEPARATE THREAD 4HIS HAPPENS REGARDLESS OF WHETHER THE REQUESTS ARE COMING IN FOR THE SAME OR DIFFERENT SERVLETS

;gfl]pl 9lljaZml]k

Thread B

´IRR¾

VHW

&OLHQW %

´EDU¾

W

Thread A

JH

VHW

VHUYOHW %

VHW W JH

VH W

VHUYOHW $

&OLHQW $

Thread C VHUYOHW $

Yikes! Another servlet that is part of the same web app, running in a separate thread can set the ÒbarÓ attribute. And thatÕs not all... the Container might launch another thread for Servlet A to handle a third client...

&OLHQW &

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<PM XZWJTMU QV [TW_ UW\QWV (ERE S WHAT HAPPENED TO +IM S TEST SERVLET

6HUYOHW $ VHWV WKH FRQWH[W DWWULEXWH ´IRR¾ ZLWK D YDOXH RI ´ ¾

6HUYOHW $ VHWV WKH FRQWH[W DWWULEXWH ´EDU¾ ZLWK D YDOXH RI ´ ¾

´IRR¾

´EDU¾

6WULQJ

6WULQJ

VHW

VHW

VHUYOHW $

VHUYOHW $ ´ ¾

6HUYOHW&RQWH[W

Thread A

6WULQJ

7KUHDG % EHFRPHV WKH UXQQLQJ WKUHDG WKUHDG $ JRHV EDFN WR 5XQQDEOH EXW QRW 5XQQLQJ DQG VHWV WKH FRQWH[W DWWULEXWH ´EDU¾ ZLWK D YDOXH RI ´ ¾ 7KH LV QRZ JRQH

´ ¾

6HUYOHW&RQWH[W

Thread A

6WULQJ

7KUHDG $ EHFRPHV WKH UXQQLQJ WKUHDG DJDLQ DQG JHWV WKH YDOXH RI ´EDU¾ DQG SULQWV LW WR WKH UHVSRQVH

´EDU¾ 6WULQJ

´EDU¾

JHW 6WULQJ

VHUYOHW $ ´ ¾

VHW

Thread A

VHUYOHW %

6HUYOHW&RQWH[W 6WULQJ

´ ¾

Thread B

6HUYOHW&RQWH[W 6WULQJ

JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³IRR´ ³ ´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³EDU´ ³ ´ RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³IRR´

RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³EDU´

vlet A set In between when serand then rÓ Òba of ue the val another rÓ, Òba of ue val got the and set in ck snu d rea servlet th ue. val ent fer dif ÒbarÓ to a vlet A So by the time serof ÒbarÓ, it printed the value to Ò16Ó. had been changed

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

WKUHDGV DQG FRQWH[W DWWULEXWHV

0W_ LW _M UISM KWV\M`\ I\\ZQJ]\M[ \PZMIL [INM' ,ET S HEAR WHAT SOME OF THE OTHER DEVELOPERS HAVE TO SAY

2I &2856( WKH\ GLGQ·W V\QFKURQL]H WKH PHWKRGV LQ 6HUYOHW&RQWH[W *HH] WKDW ZRXOG KDYH PHDQW WKDW HYHU\ FDOO WR JHW DQG VHW DQ DWWULEXWH ZRXOG KDYH DOO WKH V\QFKURQL]DWLRQ RYHUKHDG :KDW D KXJH ZDVWH LI \RX GRQ·W QHHG LW 1R SURWHFWLQJ DWWULEXWHV LV \RXU 2:1 SUREOHP GRQ·W H[SHFW WKH $3, WR VDYH \RX , FDQ·W HYHQ EHOLHYH VKH VXJJHVWHG WKDW

,·P WKLQNLQJ , FRXOG V\QFKURQL]H WKH GR*HW PHWKRG EXW WKDW GRHVQ·W UHDOO\ IHHO ULJKW %XW , GRQ·W NQRZ ZKDW HOVH WR GR

6\QFKURQL]LQJ RQ WKH GR*HW PHDQV NLVVLQJ \RXU FRQFXUUHQF\ JRRGE\H ,I \RX V\QFKURQL]H GR*HW LW PHDQV WKDW VHUYOHW FDQ KDQGOH RQO\ 21( FOLHQW DW D WLPH

:K\ GLGQ·W WKH 6HUYOHW VSHF GHYHORSHUV MXVW V\QFKURQL]H WKH JHW DQG VHW DWWULEXWH PHWKRGV LQ 6HUYOHW&RQWH[W WR PDNH WKH DWWULEXWHV WKUHDG VDIH"

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

;aVKPZWVQbQVO \PM [MZ ^QKM UM\PWL Q[ I [XMK\IK]TIZTa *), QLMI /+ SO WE KNOW THAT SYNCHRONIZING THE SERVICE METHOD WILL KILL OUR CONCURRENCY BUT IT DOES GIVE YOU THE THREAD PROTECTION RIGHT 4AKE A LOOK AT THIS LEGAL CODE AND DECIDE WHETHER IT WOULD PREVENT THE PROBLEM +IM HAD WITH THE CONTEXT ATTRIBUTE BEING CHANGED BY ANOTHER SERVLET SXEOLF V\QFKURQL]HG YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW FRQWH[W DWWULEXWHV EU!´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³IRR´ ³ ´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³EDU´ ³ ´ RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³IRR´

RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³EDU´

` 7KLV FDQ¡W ZRUN :HOO LW¡V OHJDO DV D VHUYOHW EXW , GRQ¡W VHH KRZ WKLV ZLOO Ă€[ WKH SUREOHP

What do you think? Will it Þx the problem Kim had? Look back at the code and the diagrams if youÕre not sure.

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

GRQÂśW V\QFKURQL]H WKH VHUYLFH PHWKRG

;aVKPZWVQbQVO \PM [MZ ^QKM UM\PWL _WVÂź\ XZW\MK\ I KWV\M`\ I\\ZQJ]\M 3YNCHRONIZING THE SERVICE METHOD MEANS THAT ONLY ONE THREAD IN A SERVLET CAN BE RUNNING AT A TIME BUT IT DOESN T STOP OTHER SERVLETS OR *30S FROM ACCESSING THE ATTRIBUTE 3YNCHRONIZING THE SERVICE METHOD WOULD STOP OTHER THREADS FROM THE SAME SERVLET FROM ACCESSING THE CONTEXT ATTRIBUTES BUT IT WON T DO ANYTHING TO STOP A COMPLETELY DIFFERENT SERVLET

;gfl]pl 9lljaZml]k

Thread B

´IRR¾

VHW

Thread A

&OLHQW %

´EDU¾

HW

J

VHW

VHUYOHW %

VHUYOHW $

VHW W JH

&OLHQW $

Thread C VHUYOHW $

&OLHQW &

If you synchronize the service method, you WILL stop the Container from starting any other methods for new requests coming into servlet A. So this WILL protect the context attributes from being accessed by more than one thread running a service method of Servlet A.

But you wonÕt do anything to stop OTHER servlets! Regardless of whether the service methods in other servlets are synchronized or not... it still means other parts of the app have access to the context attributes.

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

AW] LWVÂź\ VMML I TWKS WV \PM [MZ ^TM\ aW] VMML \PM TWKS WV \PM KWV\M`\ 4HE TYPICAL WAY TO PROTECT THE CONTEXT ATTRIBUTE IS TO SYNCHRONIZE /. THE CONTEXT OBJECT ITSELF )F EVERYONE ACCESSING THE CONTEXT HAS TO l RST GET THE LOCK ON THE CONTEXT OBJECT THEN YOU RE GUARANTEED THAT ONLY ONE THREAD AT A TIME CAN BE GETTING OR SETTING THE CONTEXT ATTRIBUTE "UT THERE S STILL AN IF THERE )T ONLY WORKS IF ALL OF THE OTHER CODE THAT MANIPULATES THE SAME CONTEXT ATTRIBUTES !,3/ SYNCHRONIZES ON THE 3ERVLET#ONTEXT )F CODE DOESN T ASK FOR THE LOCK THEN THAT CODE IS STILL FREE TO HIT THE CONTEXT ATTRIBUTES "UT IF YOU RE DESIGNING THE WEB APP THEN YOU CAN DECIDE TO MAKE EVERYONE ASK FOR THE LOCK BEFORE ACCESSING THE ATTRIBUTES

6HUYOHW

For context attributetos, it wonÕt do any good vlet, synchronize on the Ser of the because other parts to ess app will still be able acc the context!

6HUYOHW&RQWH[W

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW FRQWH[W DWWULEXWHV EU!´ V \QFKURQL]HG JHW6HUYOHW&RQWH[W

^ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³IRR´ ³ ´ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³EDU´ ³ ´

k on the Now weÕre getting theis loc way to context itself!! This ribthe state. (You protect context att zedute (this).) donÕt want synchroni

RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³IRR´

RXW SULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³EDU´

` `

Since we have e context loc once we get insth ide the synchronk, weÕre assuming that attributes are sa ized block, the block... sort fe from other threads untithe context of code that ALSO . Safe means Òsafe from l we exit any other synchronizes on the ServletCont But this is the ext.Ó context attributbest youÕve got for making the es as thread-saf e as you can.

([SHFW WR VHH ORWV RI HW\ FRGH DERXW WKUHDG VDI

QW\ RI FRGH VKRZ 2Q WKH H[DP \RXÂśOO VHH SOH DNLQJ DWWULEXWHV RU P LHV I UDWHJ QW VW LIIHUH LQJ G GHFLGH LI WKH FRGH WKUHDG VDIH <RXÂśOO KDYH WR RDO -XVW EHFDXVH ZRUNV JLYHQ D SDUWLFXODU J DQG UXQV GRHVQÂśW WKH FRGH LV OHJDO FRPSLOHV PHDQ LWÂśOO VROYH WKH SUREOHP

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

VHVVLRQ DWWULEXWHV DQG WKUHDG VDIHW\

)ZM ;M[[QWV I\\ZQJ]\M[ \PZMIL [INM' 4HINK ABOUT IT 7E HAVEN T TALKED ABOUT (440 SESSIONS IN DETAIL YET WE WILL IN THE 3ESSIONS CHAPTER BUT YOU ALREADY KNOW THAT A SESSION IS AN OBJECT USED TO MAINTAIN CONVERSATIONAL STATE WITH A CLIENT 4HE SESSION PERSISTS ACROSS MULTIPLE REQUESTS FROM THE SAME CLIENT "UT IT S STILL JUST ONE CLIENT WE RE TALKING ABOUT !ND IF IT S ONE CLIENT AND A SINGLE CLIENT CAN BE IN ONLY ONE REQUEST AT A TIME DOESN T THAT AUTOMATICALLY MEAN THAT SESSIONS ARE THREAD SAFE )N OTHER WORDS EVEN IF MULTIPLE SERVLETS ARE INVOLVED AT ANY GIVEN MOMENT THERE S ONLY ONE REQUEST FROM THAT PARTICULAR CLIENT SO THERE S ONLY ONE THREAD OPERATING ON THAT SESSION AT A TIME 2IGHT

Request A / Thread A K]kkagf 9lljaZml]k VHUYOHW

6HUYOHW $

6K RSS &D U LQ J

W

&OLHQW $

JHW

VHW

VHUYOHW

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

-63 9LHZ

JHW VHW

6HUYOHW %

Request B / Thread B %VEN THOUGH BOTH SERVLETS CAN ACCESS THE 3ESSION ATTRIBUTES IN SEPARATE THREADS EACH THREAD IS A SEPARATE REQUEST 3O IT LOOKS SAFE 5NLESS #AN YOU THINK OF A SCENARIO IN WHICH THERE COULD BE MORE THAN ONE REQUEST AT THE SAME TIME FROM THE SAME CLIENT

7HAT DO YOU THINK !RE SESSION ATTRIBUTES GUARANTEED THREAD SAFE

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

?PI\¼[ :-)44A \Z]M IJW]\ I\\ZQJ]\M[ IVL \PZMIL [INM\a'

7E KNOW THAT CONTEXT ATTRIBUTES ARE INHERENTLY ./4 SAFE BECAUSE ALL PIECES OF THE APP CAN ACCESS CONTEXT ATTRIBUTES FROM ANY REQUEST WHICH MEANS ANY THREAD

6ERY GOOD .OW WHAT ABOUT 3ESSION ATTRIBUTES !RE THEY ARE SAFE

9OU HAVE MUCH TO LEARN GRASSHOPPER 9OU DO NOT KNOW THE TRUTH ABOUT SESSION ATTRIBUTES -EDITATE ON THIS BEFORE SPEAKING AGAIN

,ISTEN IN AS OUR TWO BLACK BELTS DISCUSS THE ISSUES AROUND PROTECTING THE STATE OF ATTRIBUTES FROM MULTITHREADING PROBLEMS

9ES MASTER !ND ) KNOW THAT SYNCHRONIZING THE SERVICE METHOD IS NOT A SOLUTION BECAUSE ALTHOUGH IT WILL STOP THAT SERVLET FROM SERVICING MORE THAN ONE REQUEST AT A TIME IT WILL ./4 STOP OTHER SERVLETS AND *30S IN THE SAME WEB APP FROM ACCESSING THE CONTEXT 9ES MASTER 4HEY ARE FOR ONLY ONE CLIENT AND THE LAWS OF PHYSICS PREVENT A CLIENT FROM MAKING MORE THAN ONE REQUEST AT A TIME

"UT MASTER ) HAVE MEDITATED AND STILL ) DO NOT KNOW HOW ONE CLIENT COULD HAVE MORE THAN ONE REQUEST

9OU MUST THINK OUTSIDE THE #ONTAINER #OLOR OUTSIDE THE LINES 2UN WITH SCISSORS 6ERY WISE ADVICE MASTER ) HAVE IT 4HE CLIENT COULD OPEN A NEW BROWSER WINDOW 3O THE #ONTAINER CAN STILL USE THE SAME SESSION FOR A CLIENT EVEN THOUGH IT S COMING FROM A DIFFERENT INSTANCE OF THE BROWSER 9ES 4HE #ONTAINER CAN SEE THE REQUEST FROM THE SECOND WINDOW AS COMING FROM THE SAME SESSION !ND HOW WOULD YOU PROTECT THESE SESSION ATTRIBUTES FROM THE HAVOC OF MULTIPLE THREADS

3O 3ESSION ATTRIBUTES ARE NOT THREAD SAFE AND THEY TOO MUST BE PROTECTED ) WILL MEDITATE ON THIS !H ) MUST SYNCHRONIZE THE PART OF MY CODE THAT ACCESSES THE SESSION ATTRIBUTES *UST THE WAY WE DID FOR THE CONTEXT ATTRIBUTES

4HAT IS GOOD YES BUT SYNCHRONIZE ON WHAT ) MUST SYNCHRONIZE ON THE (TTP3ESSION \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

V\QFKURQL]H RQ WKH VHVVLRQ

8ZW\MK\ [M[[QWV I\\ZQJ]\M[ Ja [aVKPZWVQbQVO WV \PM 0\\X;M[[QWV ,OOK AT THE TECHNIQUE WE USED TO PROTECT THE CONTEXT ATTRIBUTES 7HAT DID WE DO 9OU CAN DO THE SAME THING WITH SESSION ATTRIBUTES BY SYNCHRONIZING ON THE (TTP3ESSION OBJECT SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW FRQWH[W DWWULEXWHV EU!´ +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ ze on the This time, we synchr, oni V\QFKURQL]HG VHVVLRQ ^ protect the to ect obj ion ess VHVVLRQ VHW$WWULEXWH ³IRR´ ³ ´ tpS Ht s. ute rib VHVVLRQ VHW$WWULEXWH ³EDU´ ³ ´ att n sessio RXW SULQWOQ VHVVLRQ JHW$WWULEXWH ³IRR´

RXW SULQWOQ VHVVLRQ JHW$WWULEXWH ³EDU´

` `

WKHUH DUH QR

'XPE 4XHVWLRQV

4

)SN T THIS OVERKILL )S THIS REALLY A POSSIBILITY THAT A CLIENT WILL OPEN ANOTHER BROWSER WINDOW

$

/F COURSE IT IS 3URELY YOU VE DONE THIS YOURSELF WITHOUT A SECOND THOUGHTˆOPENED A SECOND WINDOW BECAUSE YOU WERE TIRED OF WAITING FOR THE OTHER ONE TO RESPOND OR BECAUSE YOU MINIMIZED ONE OR MISPLACED THE WINDOW WITHOUT REALIZING IT ETC 4HE POINT IS YOU CAN T TAKE THE CHANCE IF YOU NEED THREAD SAFETY FOR YOUR SESSION VARIABLES 9OU HAVE TO KNOW THAT IT S QUITE POSSIBLE FOR A SESSION SCOPED ATTRIBUTE TO BE USED BY MORE THAN ONE THREAD AT A TIME

4

)SN T IT A BAD IDEA TO SYNCHRONIZE CODE BECAUSE IT CAUSES A LOT OF OVERHEAD AND HURTS CONCURRENCY

$

9OU SHOULD !,7!93 THINK CAREFULLY BEFORE SYNCHRONIZING ANY CODE BECAUSE YOU RE RIGHTˆIT DOES ADD SOME EXPENSE IN CHECKING ACQUIRING AND RELEASING LOCKS )F YOU NEED PROTECTION THEN USE SYNCHRONIZATION BUT REMEMBER THE STANDARD RULE OF ALL FORMS OF LOCKINGˆKEEP THE LOCK FOR THE SHORTEST AMOUNT OF TIME TO ACCOMPLISH YOUR GOAL )N OTHER WORDS DON T SYNCHRONIZE THE CODE THAT DOESN T ACCESS THE PROTECTED STATE -AKE YOUR SYNCHRONIZED BLOCK AS SMALL AS POSSIBLE 'ET THE LOCK GET IN GET WHAT YOU NEED AND GET THE HECK OUT SO THE LOCK CAN RELEASE AND OTHER THREADS CAN RUN THAT CODE

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

<PM M^QT[ WN ; QVOTM<PZMIL5WLMT !LTHOUGH IT S NOT ON THE EXAM BECAUSE IT S EVIL YOU VE PROBABLY HEARD OF OR EVEN USED THE NOW DEPRECATED 3INGLE4HREAD-ODEL INTERFACE SO WE FEEL COMPELLED TO MENTION IT WHILE SIMULTANEOUSLY REINFORCING ITS EVILNESS 4HE 3INGLE4HREAD-ODEL SOUNDS GOOD TO NEWBIES AT l RST GLANCE AS A WAY TO SOLVE MULTI THREADING PROBLEMS 9OU SIMPLY HAVE YOUR SERVLET IMPLEMENT THE 3INGLE4HREAD-ODEL INTERFACE AND "//-ˆAS IF BY MAGIC YOUR SERVLET WILL NEVER HAVE MORE THAN ONE THREAD AT A TIME RUNNING )N OTHER WORDS YOU VE REDUCED YOUR SERVLET TO A SINGLE THREAD 'OODBYE MULTI THREADING PROBLEMS RIGHT .O OF COURSE NOT 9OU ALREADY KNOW WHY IMPLEMENTING 3INGLE4HREAD-ODEL IS NO DIFFERENT THAN SYNCHRONIZING THE SERVICE METHODˆALL YOU VE DONE IS DEMOLISHED YOUR CONCURRENCY WITHOUT PROTECTING ATTRIBUTE STATE "ECAUSE AGAIN EVEN IF ALL YOUR SERVLETS IMPLEMENT 3INGLE4HREAD-ODEL YOU CAN STILL HAVE TWO SERVLETS EACH ONE DUTIFULLY RUNNING ONLY ONE CLIENT REQUEST THREAD AT A TIME IN THE SAME WEB APP ACCESSING CONTEXT ATTRIBUTES AT THE SAME TIME

6KDUSHQ \RXU SHQFLO 3ODFH D FKHFNPDUN QH[W WR WKH WKLQJV WKDW DUH 127 WKUHDG VDIH :H GLG WKH ILUVW RQH

‰ &RQWH[W VFRSHG DWWULEXWHV ‰ 6HVVLRQ VFRSHG DWWULEXWHV ‰ 5HTXHVW VFRSHG DWWULEXWHV ‰ ,QVWDQFH YDULDEOHV LQ WKH VHUYOHW ‰ /RFDO YDULDEOHV LQ VHUYLFH PHWKRGV ‰ 6WDWLF YDULDEOHV LQ WKH VHUYOHW

3INGLE4HREAD-ODEL OFFERS NOTHING BUT POOR PERFORMANCE IF YOUR APPLICATION HAS MORE THAN ONE COMPONENT 3INCE IT MISLED THOUSANDS OF NEW DEVELOPERS INTO THINKING THAT 3INGLE4HREAD-ODEL GAVE THEM THREAD SAFETY IT S BEEN DEPRECATED AND THE DEVELOPERS WHO USE IT NOW ARE SUBJECT TO RIDICULE AND HUMILIATION "UT NOW YOU KNOW AND YOU WOULD NEVER HAVE USED IT ANYWAY SINCE YOU ALREADY KNOW THAT RESTRICTING A SINGLE SERVLET TO A SINGLE THREAD PROTECTS YOU ONLY IF YOUR ENTIRE APPLICATION CONSISTS OF A SINGLE SERVLET 3O IF YOU CATCH ANYBODY USING 3INGLE4HREAD-ODEL REVOKE THEIR 3ERVLET LICENSE AND INSIST NO DEMAND THAT THEY BUY A COPY OF THIS BOOK

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

UHTXHVW DWWULEXWHV DUH WKUHDG VDIH

7VTa :MY]M[\ I\\ZQJ]\M[ IVL TWKIT ^IZQIJTM[ IZM \PZMIL [INM 4HAT S IT 7E INCLUDE METHOD PARAMETERS WHEN WE SAY hLOCAL VARIABLESv %VERYTHING ELSE IS SUBJECT TO MANIPULATION BY MULTIPLE THREADS UNLESS YOU DO SOMETHING TO STOP IT

WKHUH DUH QR

'XPE 4XHVWLRQV

4 $

3O INSTANCE VARIABLES AREN T THREAD SAFE

4HAT S RIGHT )F YOU HAVE MULTIPLE CLIENTS MAKING REQUESTS ON THAT SERVLET THAT MEANS MULTIPLE THREADS RUNNING THAT SERVLET CODE !ND ALL THREADS HAVE ACCESS TO THE SERVLET S INSTANCE VARIABLES SO INSTANCE VARI ABLES AREN T THREAD SAFE

4

"UT THEY 7/5,$ BE THREAD SAFE IF YOU IMPLE MENTED THE 3INGLE4HREAD-ODEL RIGHT

$

9ES BECAUSE YOU D NEVER HAVE MORE THAN ONE THREAD FOR THAT SERVLET SO THE INSTANCE VARIABLES WOULD BE THREAD SAFE "UT OF COURSE NOBODY WOULD EVER AL LOW YOU INTO THE SERVLETS CLUB EVER AGAIN

4

) WAS JUST TALKING HYPOTHETICALLY !S IN hIF SOMEONE 7%2% STUPID ENOUGH TO IMPLEMENT 3INGLE 4HREAD-ODEL v .OT THAT ) WOULD EVER DO IT "UT WHILE WE RE BEING HYPOTHETICAL IF ) HAVE A FRIEND WHO SAY SYNCHRONIZES THE SERVICE METHOD WOULDN T THAT !,3/ MAKE THE INSTANCE VARIABLES THREAD SAFE

$

9ES "UT YOUR FRIEND WOULD BE AN IDIOT 4HE EFFECT OF IMPLEMENTING 3INGLE4HREAD-ODEL IS VIRTUALLY THE SAME AS SYNCHRONIZING THE SERVICE METHOD "OTH CAN BRING A WEB APP TO ITS KNEES WITHOUT PROTECTING THE SESSION AND ATTRIBUTE STATE

4

"UT IF YOU RE NOT SUPPOSED TO USE 3INGLE 4HREAD-ODEL OR SYNCHRONIZE THE SERVICE METHOD THEN HOW $/ YOU MAKE INSTANCE VARIABLES THREAD SAFE

$

9OU DON T ,OOK AT A WELL WRITTEN SERVLET AND CHANCES ARE YOU WON T FIND ANY INSTANCE VARIABLES /R AT LEAST ANY THAT ARE NON FINAL !ND SINCE YOU RE A *AVA PROGRAMMER YOU KNOW THAT EVEN A FINAL VARIABLE CAN STILL BE MANIPULATED UNLESS IT S IMMUTABLE 3O JUST DON T USE INSTANCE VARIABLES IF YOU NEED THREAD SAFE STATE BECAUSE ALL THREADS FOR THAT SERVLET CAN STEP ON INSTANCE VARIABLES

4

4HEN WHAT 3(/5,$ YOU USE IF YOU NEED MUL TIPLE INSTANCES OF THE SERVLET TO SHARE SOMETHING

$

3TOP RIGHT THERE 9OU SAID hMULTIPLE INSTANCES OF THE SERVLETv 7E KNOW YOU DIDN T MEAN THAT BE CAUSE THERE IS ALWAYS ONLY /.% INSTANCE OF THE SERVLET /NE INSTANCE MANY THREADS )F YOU WANT ALL THE THREADS TO ACCESS A VALUE DECIDE WHICH ATTRIBUTE STATE MAKES THE MOST SENSE AND STORE THE VALUE IN AN ATTRIBUTE #HANCES ARE YOU CAN SOLVE YOUR PROBLEMS IN ONE OF TWO WAYS $ECLARE THE VARIABLE AS A LOCAL VARIABLE WITHIN THE SERVICE METHOD RATHER THAN AS AN INSTANCE VARIABLE /2 5SE AN ATTRIBUTE IN THE MOST APPROPRIATE SCOPE

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

:MY]M[\ I\\ZQJ]\M[ IVL :MY]M[\ LQ[XI\KPQVO 2EQUEST ATTRIBUTES MAKE SENSE WHEN YOU WANT SOME OTHER COMPONENT OF THE APP TO TAKE OVER ALL OR PART OF THE REQUEST /UR TYPICAL SIMPLE EXAMPLE IS AN -6# APP THAT STARTS WITH A SERVLET CONTROLLER BUT ENDS WITH A *30 VIEW 4HE CONTROLLER COMMUNICATES WITH THE MODEL AND GETS BACK DATA THAT THE VIEW NEEDS IN ORDER TO BUILD THE RESPONSE 4HERE S NO REASON TO PUT THE DATA IN A CONTEXT OR SESSION ATTRIBUTE SINCE IT APPLIES ONLY TO THIS REQUEST SO WE PUT IT IN THE REQUEST SCOPE 3O HOW DO WE MAKE ANOTHER PART OF THE COMPONENT TAKE OVER THE REQUEST 7ITH A 2EQUEST$ISPATCHER

FRGH LQ D GR*HW

%HHU([SHUW EH QHZ %HHU([SHUW $UUD\/LVW UHVXOW EH JHW%UDQGV F

Put model data into Request scope.

UHTXHVW VHW$WWULEXWH ³VW\OHV´ UHVXOW

Get a dispatcher the view JSP.

for 5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH

7KH %HHU VHUYOHW FDOOV WKH JHW%UDQGV PHWKRG RQ WKH PRGHO WKDW UHWXUQV VRPH GDWD WKDW WKH YLHZ QHHGV

Tell JSP to take over the req here are the Request and Res uest, and, oh yeah, ponse objects.

7KH VHUYOHW VHWV D 5HTXHVW DWWULEXWH QDPHG ´VW\OHVµ )LUVW LW SXWV ´0RRVH 'URROµ LQWR DQ $UUD\/LVW

VHW$WWULEXWH ´VW\OHVµ UHVXOWV

JHW%UDQGV

6HUYOHW

6HUYOHW

´0RRVH 'URROµ

&RQWUROOHU

0RGHO REMHFW

7KH VHUYOHW DVNV WKH +WWS5HTXHVW IRU D 5HTXHVW'LVSDWFKHU SDVVLQJ LQ D UHODWLYH SDWK WR WKH YLHZ -63 JHW5HTXHVW'LVSDWFKHU XUL7R9LHZ

+WWS5HTXHVW

&RQWUROOHU

7KH VHUYOHW FDOOV IRUZDUG RQ WKH 5HTXHVW 'LVSDWFKHU WR WHOO WKH -63 WR WDNH RYHU WKH UHTXHVW 1RW VKRZQ WKH -63 JHWV WKH IRUZDUGHG UHTXHVW DQG JHWV WKH ´VW\OHVµ DW WULEXWH IURP WKH 5HTXHVW VFRSH

IRUZDUG UHTXHVW UHVSRQVH

6HUYOHW

&RQWUROOHU

6HUYOHW

+WWS5HTXHVW &RQWUROOHU

5HTXHVW'LVSDWFKHU

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

WKH 5HTXHVW'LVSDWFKHU

: MY]M[\,Q[XI\KPMZ ZM^MITML 2EQUEST$ISPATCHERS HAVE ONLY TWO METHODSˆ FORWARD AND INCLUDE "OTH TAKE THE REQUEST AND RESPONSE OBJECTS WHICH THE COMPONENT YOU RE FORWARDING TO WILL NEED TO l NISH THE JOB /F THE TWO METHODS FORWARD IS BY FAR THE MOST POPULAR )T S VERY UNLIKELY YOU LL USE THE INCLUDE METHOD FROM A CONTROLLER SERVLET HOWEVER BEHIND THE SCENES THE INCLUDE METHOD IS BEING USED BY *30S IN THE JSP INCLUDE STANDARD ACTION WHICH WE LL REVIEW IN CHAPTER 9OU CAN GET A 2EQUEST$ISPATCHER IN TWO WAYS FROM THE REQUEST OR FROM THE CONTEXT 2EGARDLESS OF WHERE YOU GET IT YOU HAVE TO TELL IT THE WEB COMPONENT TO WHICH YOU RE FORWARDING THE REQUEST )N OTHER WORDS THE SERVLET OR *30 THAT LL TAKE OVER

LQWHUIDFH!!

5HTXHVW'LVSDWFKHU IRUZDUG 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

LQFOXGH 6HUYOHW5HTXHVW 6HUYOHW5HVSRQVH

MDYD[ VHUYOHW 5HTXHVW'LVSDWFKHU

*HWWLQJ D 5HTXHVW'LVSDWFKHU IURP D 6HUYOHW5HTXHVW 5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ 7KH JHW5HTXHVW'LVSDWFKHU PHWKRG LQ 6HUYOHW5HTXHVW WDNHV D 6WULQJ SDWK IRU WKH UHVRXUFH WR ZKLFK \RX¡UH IRUZDUGLQJ WKH UHTXHVW ,I WKH SDWK VWDUWV ZLWK D IRUZDUG VODVK ´ ¾ WKH &RQWDLQHU VHHV WKDW DV ´VWDUWLQJ IURP WKH URRW RI WKLV ZHE DSS¾ ,I WKH SDWK GRHV 127 VWDUW ZLWK D IRUZDUG VODVK LW¡V FRQVLGHUHG UHODWLYH WR WKH RULJLQDO UHTXHVW %XW \RX FDQ¡W WU\ WR WULFN WKH &RQWDLQHU LQWR ORRNLQJ RXWVLGH WKH FXUUHQW ZHE DSS ,Q RWKHU ZRUGV MXVW EHFDXVH \RX KDYH ORWV RI ´ ¾ GRHVQ¡W PHDQ LW¡OO ZRUN LI LW WDNHV \RX SDVW WKH URRW RI \RXU FXUUHQW ZHE DSS

cause thereÕs This is a relative path h(be(Ò/Ó)). So in this slas d war for ial init no for Òresult. case, the Container lool kslocation the ica log e sam the in jspÓ er the details request is ÒinÓ. (WeÕll cov ical locations in log and hs pat e of relativ r.) pte cha ent ym the Deplo

*HWWLQJ D 5HTXHVW'LVSDWFKHU IURP D 6HUYOHW&RQWH[W 5HTXHVW'LVSDWFKHU YLHZ JHW6HUYOHW&RQWH[W JHW5HTXHVW'LVSDWFKHU ³ UHVXOW MVS´ /LNH WKH HTXLYDOHQW PHWKRG LQ 6HUYOHW5HTXHVW WKLV JHW5HTXHVW'LVSDWFKHU PHWKRG WDNHV D 6WULQJ SDWK IRU WKH UHVRXUFH WR ZKLFK \RX¡UH IRUZDUGLQJ WKH UHTXHVW (;&(37 \RX FDQQRW VSHFLI\ D SDWK UHODWLYH WR WKH FXUUHQW UHVRXUFH WKH RQH WKDW UHFHLYHG WKLV UHTXHVW 7KDW PHDQV \RX PXVW VWDUW WKH SDWK ZLWK D IRUZDUG VODVK

You MUST use the forward slash with the getRequestDispatcher() method of ServletContext.

&DOOLQJ IRUZDUG RQ D 5HTXHVW'LVSDWFKHU YLHZ IRUZDUG UHTXHVW UHVSRQVH 6LPSOH 7KH 5HTXHVW'LVSDWFKHU \RX JRW IURP \RXU FRQWH[W RU UHTXHVW NQRZV WKH UHVRXUFH \RX¡UH IRUZDUGLQJ WR³WKH UHVRXUFH VHUYOHW -63 \RX SDVVHG DV WKH DUJXPHQW WR JHW5HTXHVW'LVSDWFKHU 6R \RX¡UH VD\LQJ ´+H\ 5HTXHVW'LVSDWFKHU SOHDVH IRUZDUG WKLV UHTXHVW WR WKH WKLQJ , WROG \RX DERXW HDUOLHU LQ WKLV FDVH D -63 ZKHQ , ILUVW JRW \RX $QG KHUH¡V WKH UHTXHVW DQG UHVSRQVH EHFDXVH WKDW QHZ WKLQJ LV JRLQJ WR QHHG WKHP LQ RUGHU WR ILQLVK KDQGOLQJ WKH UHTXHVW ¾

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

?PI\¼[ _ZWVO _Q\P \PQ[ KWLM' 7HAT DO YOU THINK $OES THIS 2EQUEST$ISPATCHER CODE LOOK LIKE IT WILL WORK THE WAY YOU D EXPECT SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³DSSOLFDWLRQ MDU´ 6HUYOHW&RQWH[W FW[ JHW6HUYOHW&RQWH[W ,QSXW6WUHDP LV FW[ JHW5HVRXUFH$V6WUHDP ³ERRN&RGH MDU´ LQW UHDG Assume that all E\WH>@ E\WHV QHZ E\WH> @ this works. 2XWSXW6WUHDP RV UHVSRQVH JHW2XWSXW6WUHDP ZKLOH UHDG LV UHDG E\WHV

^ RV ZULWH E\WHV UHDG ` RV À XVK 5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH RV FORVH `

AW]¼TT OM\ I JQO NI\ 1TTMOIT;\I\M-`KMX\QWV

HTXHVW <RX FDQ·W IRUZDUG WKH U LWWHG LI \RX·YH DOUHDG\ FRPP D UHVSRQVH QVH´ ZH PHDQ ³VHQW WKH $QG E\ ³FRPPLWWHG D UHVSR N DW WKH FRGH DJDLQ 7KH ELJ /RR HQW´ H FOL UHVSRQVH WR WK LV SUREOHP

RV À XVK

WKH UHVSRQVH WR EH VHQW WR 7KDW¶V WKH OLQH WKDW FDXVHV KLV UHVSRQVH LV '21( WKH FOLHQW DQG DW WKDW SRLQW W ¶W SRVVLEO\ IRUZDUG WKH ),1,6+(' 29(5 <RX FDQ H WKH UHTXHVW LV KLVWRU\ UHTXHVW DW WKLV SRLQW EHFDXV QG \RX JHW RQO\ RQH VKRW HG D SRQG \ UHV OUHDG <RX¶YH D LV DW WK HH TXHVWLRQV RQ WKH H[DP 6R GRQ¶W EH IRROHG LI \RX V 5 D UHVSRQVH LV VHQW 7KH WKDW IRUZDUG D UHTXHVW $)7( DO6WDWH([FHSWLRQ &RQWDLQHU ZLOO WKURZ DQ ,OOHJ

4

(OW COME YOU DIDN T TALK ABOUT THE 2EQUEST$ISPATCHER INCLUDE METHOD

$

)T S NOT ON THE EXAM FOR ONE THING &OR ANOTHER WE ALREADY MENTIONED THAT IT S NOT USED MUCH IN THE REAL WORLD "UT TO SATISFY YOUR CURIOSITY THE INCLUDE METHOD SENDS THE REQUEST TO SOMETHING ELSE TYPICALLY ANOTHER SERVLET TO DO SOME WORK AND THEN COMES BACK TO THE SENDER )N OTHER WORDS INCLUDE MEANS ASKING FOR HELP IN HANDLING THE REQUEST BUT IT S NOT A COMPLETE HAND OFF )T S A TEMPORARY RATHER THAN PERMANENT TRANSFER OF CONTROL 7ITH FORWARD YOU RE SAYING h4HAT S IT ) M NOT DO ING ANYTHING ELSE TO PROCESS THIS REQUEST AND RESPONSE v "UT WITH INCLUDE YOU RE SAYING h) WANT SOMEONE ELSE TO DO SOME THINGS WITH THE REQUEST AND OR RESPONSE BUT WHEN THEY RE DONE ) WANT TO FINISH HANDLING THE REQUEST AND RESPONSE MYSELF ALTHOUGH ) MIGHT DECIDE TO DO ANOTHER INCLUDE OR FORWARD AFTER THAT v

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

OLVWHQHU H[HUFLVH DQVZHUV

2EMEMBERING THE ,ISTENERS !.37%23

([HUFLVH

$WWULEXWH OLVWHQHUV

2WKHU OLIHF\FOH OLVWHQHUV

0HWKRGV LQ DOO DWWULEXWH OLVWHQHUV H[FHSW ELQGLQJ OLVWHQHU

/LIHF\FOH HYHQWV UHODWHG WR VHVVLRQV H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

/LIHF\FOH HYHQWV UHODWHG WR UHTXHVWV H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

/LIHF\FOH HYHQWV UHODWHG WR VHUYOHW FRQWH[W H[FOXGLQJ DWWULEXWH UHODWHG HYHQWV

ServletRequestAttributeListener ServletContextAttributeListener HttpSessionAttributeListener ServletRequestListener erence between (Notice that the only diffliste ServletContextListener ners is the word these and the attribute interface name.) HttpSessionListener ÒAttributeÓ inserted in the HttpSessionBindingListener HttpSessionActivationListener attributeAdded() attributeRemoved() attributeReplaced() when the session is created, and when its destroyed sessionCreated() ll are others weÕ sessionDestroyed() (Note: theree Sessions chapter.) cover in th when the request is initialized or destroyed n the session requestInitialized() (Notice the difference betwee sessionCreated(), is ion sess tsÑ even est requ and sessionDestroyed() .) request is requestInitialized() when the context is initialized or destroyed contextInitialized() contextDestroyed()

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

!TTRIBUTE 3COPE !.37%23

([HUFLVH

&RQWH[W

(NOT thread-safe!)

+WWS6HVVLRQ

(NOT thread-safe!)

5HTXHVW

(Thread-safe)

$FFHVVLELOLW\

6FRSH

ZKR FDQ VHH LW

HOW LONG DOES IT LIVE

Any part of the web app including servlets, JSPs, ServletContextListeners, ServletContextAttributeListeners.

Lifetime of the ServletContext, which means life of the deployed app. If server or app goes down, the context is destroyed (along with its attributes).

Any servlet or JSP with The life of the session. A access to this particular session can be destroyed session. Remember, a session programmatically or can extends beyond a single client simply time-out. (WeÕll request to span multiple go into the details in requests by the same client, the Session Management which could go to different chapter.) servlets. Any part of the application that has direct access to the Request object. That mostly means only the Servlets and JSPs to which the request is forwarded using a RequestDispateher. Also Request-related listeners.

The life of the Request, which means until the ServletÕs service() method completes. In other words, for the life of the thread (stack) handling this request.

:KDW LW·V JRRG IRU

Resources you want the entire application to share, including database connections, JNDI lookup names, email addresses., etc.

Data and resources related to this clientÕs session, not just a single request. Something that requires an ongoing conversation with the client. A shopping cart is a typical example.

Passing model info from the controller to the view... or any other data speciÞc to a single client request.

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

FRGH PDJQHWV DQVZHUV

&RGH 0DJQHWV !.37%23 CONFIGURING A CONTEXT PARAMETER IN THE $$

([HUFLVH

This part is NOT required.

ZHE DSS !

VHUYOHW! VHUYOHW QDPH! VHUYOHW FODVV!

%HHU7HVW VHUYOHW QDPH! FRP ZLFNHGO\VPDUW %HHU7HVWHU

VHUYOHW FODVV!

VHUYOHW!

FRQWH[W SDUDP! SDUDP QDPH!

IRR

SDUDP QDPH!

SDUDP YDOXH!

EDU

SDUDP YDOXH!

FRQWH[W SDUDP! ZHE DSS!

1RW XVHG UDP! OHW SD VHUY

<init-param> is used for servlet init parameters, not context init parameters. You Þnd <init-param> ONLY inside a <servlet> element. ThereÕs no such thing as <servlet-param>.

LQLW SDUDP !

SDUDP! LQLW VHUYOHW SDUDP!

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

0RFN ([DP &KDSWHU

7HEN USING A 5HTXHVW'LVSDWFKHU THE USE OF WHICH METHODS CAN OFTEN LEAD TO AN ,OOHJDO6WDWH([FHSWLRQ #HOOSE ALL THAT APPLY

R ! UHDG R " ÀXVK R # ZULWH R $ JHW2XWSXW6WUHDP R % JHW5HVRXUFH$V6WUHDP

7HICH STATEMENTS ABOUT 6HUYOHW&RQWH[W INITIALIZATION PARAMETERS ARE TRUE #HOOSE ALL THAT APPLY

R ! 4HEY SHOULD BE USED FOR DATA THAT CHANGES RARELY

R " 4HEY SHOULD BE USED FOR DATA THAT CHANGES FREQUENTLY

R # 4HEY CAN BE ACCESSED USING 6HUYOHW&RQWH[W JHW3DUDPHWHU

R $ 4HEY CAN BE ACCESSED USING 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU

R % 4HEY SHOULD BE USED FOR DATA THAT IS SPECIlC TO A PARTICULAR SERVLET

R & 4HEY SHOULD BE USED FOR DATA THAT IS APPLICABLE TO AN ENTIRE WEB APPLICATION

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN H[DP

7HICH TYPES DElNE THE METHODS JHW$WWULEXWH AND VHW$WWULEXWH #HOOSE ALL THAT APPLY

R ! +WWS6HVVLRQ R " 6HUYOHW5HTXHVW R # 6HUYOHW5HVSRQVH R $ 6HUYOHW&RQWH[W R % 6HUYOHW&RQ¿J R & 6HVVLRQ&RQ¿J

)F A SERVLET IS INVOKED USING THE IRUZDUG OR LQFOXGH METHOD OF 5HTXHVW'LVSDWFKHU WHICH METHODS OF THE SERVLET S REQUEST OBJECT CAN ACCESS THE REQUEST ATTRIBUTES SET BY THE CONTAINER #HOOSE ALL THAT APPLY

R ! JHW&RRNLHV R " JHW$WWULEXWH

R # JHW5HTXHVW3DWK

R $ JHW5HTXHVW$WWULEXWH

R % JHW5HTXHVW'LVSDWFKHU

7HICH CALLS PROVIDE INFORMATION ABOUT INITIALIZATION PARAMETERS THAT ARE APPLICABLE TO AN ENTIRE WEB APPLICATION #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQ¿J JHW,QLW3DUDPHWHUV R " 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHUV

R # 6HUYOHW&RQ¿J JHW,QLW3DUDPHWHU1DPHV

R $ 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU1DPHV

R % 6HUYOHW&RQ¿J JHW,QLW3DUDPHWHU 6WULQJ

R & 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU 6WULQJ

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

7HICH STATEMENTS ABOUT LISTENERS DElNED IN THE MDYD[ VHUYOHW PACKAGE ARE TRUE #HOOSE ALL THAT APPLY

R ! ! 6HUYOHW5HVSRQVH/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN A SERVLET RESPONSE HAS BEEN SENT

R " !N +WWS6HVVLRQ/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN AN +WWS6HVVLRQ HAS TIMED OUT

R # ! 6HUYOHW&RQWH[W/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN THE SERVLET CONTEXT IS ABOUT TO BE SHUT DOWN

R $ ! 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN AN ATTRIBUTE HAS BEEN REMOVED FROM A 6HUYOHW5HTXHVW

R % ! 6HUYOHW&RQWH[W$WWULEXWH/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN THE SERVLET CONTEXT HAS JUST BEEN CREATED AND IS AVAILABLE TO SERVICE ITS lRST REQUEST

7HICH IS MOST LOGICALLY STORED AS AN ATTRIBUTE IN SESSION SCOPE

R ! ! COPY OF A QUERY PARAMETER ENTERED BY A USER R " 4HE RESULT OF A DATABASE QUERY TO BE RETURNED IMMEDIATELY TO A USER

R # ! DATABASE CONNECTION OBJECT USED BY ALL WEB COMPONENTS OF THE SYSTEM

R $ !N OBJECT REPRESENTING A USER WHO HAS JUST LOGGED INTO THE SYSTEM

R % ! COPY OF AN INITIALIZATION PARAMETER RETRIEVED FROM A 6HUYOHW&RQWH[W OBJECT

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN H[DP

'IVEN THIS CODE FROM AN OTHERWISE VALID +WWS6HUYOHW THAT HAS ALSO BEEN REGISTERED AS A 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHT +WWS6HUYOHW5HVSRQVH UHV WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHT VHW$WWULEXWH ³D´ ³E´ UHT VHW$WWULEXWH ³D´ ³F´ UHT UHPRYH$WWULEXWH ³D´ ` SXEOLF YRLG DWWULEXWH$GGHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ $ ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` SXEOLF YRLG DWWULEXWH5HPRYHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ 0 ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` SXEOLF YRLG DWWULEXWH5HSODFHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ 3 ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` 7HAT LOGGING OUTPUT IS GENERATED

R ! $ D !E 3 D !E R " $ D !E 0 D !F R # $ D !E 3 D !E 0 D !F R $ $ D !E 3 D !E 3 D !QXOO R % $ D !E 0 D !E $ D !F 0 D !F R & $ D !E 0 D !E $ D !F 3 D !QXOO

7HEN DECLARING A LISTENER IN THE $$ WHICH SUB ELEMENTS OF THE OLVWHQHU! ELEMENT ARE REQUIRED #HOOSE ALL THAT APPLY

R ! GHVFULSWLRQ! R " OLVWHQHU QDPH! R # OLVWHQHU W\SH! R $ OLVWHQHU FODVV! R % VHUYOHW PDSSLQJ!

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

7HICH TYPES OF OBJECTS CAN STORE ATTRIBUTES #HOOSE ALL THAT APPLY

7HICH ARE TRUE #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQ¿J R " 6HUYOHW5HVSRQVH R # 5HTXHVW'LVSDWFKHU R $ +WWS6HUYOHW5HTXHVW R % +WWS6HVVLRQ&RQWH[W

R ! 7HEN A WEB APPLICATION IS PREPARING TO SHUTDOWN THE ORDER OF LISTENER NOTIlCATION IS NOT GUARANTEED

R " 7HEN LISTENER FRIENDLY EVENTS OCCUR LISTENER INVOCATION ORDER IS NOT PREDICTABLE

R # 4HE CONTAINER REGISTERS LISTENERS BASED ON DECLARATIONS IN THE DEPLOYMENT DESCRIPTOR

R $ /NLY THE CONTAINER CAN INVALIDATE A SESSION

7HICH STATEMENTS ABOUT 5HTXHVW'LVSDWFKHU ARE TRUE WHERE APPLICABLE ASSUME THE 5HTXHVW'LVSDWFKHU WAS NOT OBTAINED VIA A CALL TO JHW1DPHG'LVSDWFKHU #HOOSE ALL THAT APPLY

R ! ! 5HTXHVW'LVSDWFKHU CAN BE USED TO FORWARD A REQUEST TO ANOTHER SERVLET

R " 4HE ONLY METHOD IN THE 5HTXHVW'LVSDWFKHU INTERFACE IS IRUZDUG

R # 0ARAMETERS SPECIlED IN THE QUERY STRING USED TO CREATE A 5HTXHVW'LVSDWFKHU ARE NOT FORWARDED BY THE IRUZDUG METHOD

R $ 4HE SERVLET TO WHICH A REQUEST IS FORWARDED MAY ACCESS THE ORIGINAL QUERY STRING BY CALLING JHW4XHU\6WULQJ ON THE 6HUYOHW5HTXHVW

R % 4HE SERVLET TO WHICH A REQUEST IS FORWARDED MAY ACCESS THE ORIGINAL QUERY STRING BY CALLING JHW$WWULEXWH ³MDYD[ VHUYOHW IRUZDUG TXHU\BVWULQJ´ ON THE \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN H[DP

7HICH STATEMENTS ACCURATELY DESCRIBE HOW MANY INSTANCES OF A SERVLET THE SERVLET CONTAINER INSTANTIATES FOR EACH WEB APPLICATION #HOOSE ALL THAT APPLY

R ! )F THE SERVLET IMPLEMENTS MDYD[ VHUYOHW 6LQJOH7KUHDG0RGHO THE CONTAINER MAY CREATE ONE INSTANCE FOR EACH REQUEST

R " )F THE SERVLET DOES NOT IMPLEMENT 6LQJOH7KUHDG0RGHO THE CONTAINER MAY CREATE MULTIPLE INSTANCES OF THE SERVLET IN THE SAME *6-

R # 4HE ORDG RQ VWDUWXS! ZHE [PO ELEMENT CAN DETERMINE HOW MANY INSTANCES ARE CREATED

R $ )F THE SERVLET DOES NOT IMPLEMENT 6LQJOH7KUHDG0RGHO THE CONTAINER WILL CREATE NO MORE THAN ONE INSTANCE PER *6-

7HAT IS THE RECOMMENDED WAY TO DEAL WITH SERVLETS AND THREAD SAFETY

R ! 7RITE THE SERVLET CODE TO EXTEND 7KUHDG6DIH6HUYOHW R " (AVE THE SERVLET IMPLEMENT 6LQJOH7KUHDG0RGHO R # ,OG ALL SERVLET METHOD CALLS R $ 5SE LOCAL VARIABLES EXCLUSIVELY AND IF YOU HAVE TO USE INSTANCE VARIABLES SYNCHRONIZE ACCESS TO THEM

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

&KDSWHU $QVZHUV

7HEN USING A 5HTXHVW'LVSDWFKHU THE USE OF WHICH METHODS CAN OFTEN LEAD TO AN ,OOHJDO6WDWH([FHSWLRQ #HOOSE ALL THAT APPLY

R ! UHDG R " ÀXVK R # ZULWH R $ JHW2XWSXW6WUHDP R % JHW5HVRXUFH$V6WUHDP

. 167)

(Servlet v2.4 pg

-An IllegalStateException is caused when a response has already been Ă”committedĂ• to the client (the Ă&#x;ush method does that), and then you attempt a forward.

7HICH STATEMENTS ABOUT 6HUYOHW&RQWH[W INITIALIZATION PARAMETERS ARE TRUE #HOOSE ALL THAT APPLY

R ! 4HEY SHOULD BE USED FOR DATA THAT CHANGES RARELY

R " 4HEY SHOULD BE USED FOR DATA THAT CHANGES FREQUENTLY

. 31)

(Servlet v2.4 pg

-Option B is incorrect because ServletContext init parameters are only read at Container start-up time.

R # 4HEY CAN BE ACCESSED USING 6HUYOHW&RQWH[W JHW3DUDPHWHU

R $ 4HEY CAN BE ACCESSED USING

-Option C is incorrect because this method does not exist.

6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU

R % 4HEY SHOULD BE USED FOR DATA THAT IS SPECIlC TO A PARTICULAR SERVLET

R & 4HEY SHOULD BE USED FOR DATA THAT IS APPLICABLE

-Option E is incorrect because there is only one ServletContext object per web application.

TO AN ENTIRE WEB APPLICATION

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN DQVZHUV

7HICH TYPES DElNE THE METHODS JHW$WWULEXWH AND VHW$WWULEXWH #HOOSE ALL THAT APPLY

)F A SERVLET IS INVOKED USING THE IRUZDUG OR LQFOXGH METHOD OF 5HTXHVW'LVSDWFKHU WHICH METHODS OF THE SERVLET S REQUEST OBJECT CAN ACCESS THE REQUEST ATTRIBUTES SET BY THE CONTAINER #HOOSE ALL THAT APPLY

R ! JHW&RRNLHV R " JHW$WWULEXWH

R # JHW5HTXHVW3DWK

R $ JHW5HTXHVW$WWULEXWH

R % JHW5HTXHVW'LVSDWFKHU

-66) (Servlet v2.4 65

-Option B is the correct method. With it you can access the container populated javax.servlet.forward.Xxx and javax.servlet.include.Xxxx attributes.

-Options C and D refer to methods that donÕt exist.

7HICH CALLS PROVIDE INFORMATION ABOUT INITIALIZATION PARAMETERS THAT ARE APPLICABLE TO AN ENTIRE WEB APPLICATION #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQÂżJ JHW,QLW3DUDPHWHUV R " 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHUV

R # 6HUYOHW&RQÂżJ JHW,QLW3DUDPHWHU1DPHV

R $ 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU1DPHV

R % 6HUYOHW&RQÂżJ JHW,QLW3DUDPHWHU 6WULQJ

R & 6HUYOHW&RQWH[W JHW,QLW3DUDPHWHU 6WULQJ

s. 32, 36, 59)

(Servlet v2.4 pg

R ! +WWS6HVVLRQ R " 6HUYOHW5HTXHVW R # 6HUYOHW5HVSRQVH R $ 6HUYOHW&RQWH[W R % 6HUYOHW&RQÂżJ R & 6HVVLRQ&RQÂżJ

. 32)

(Servlet v2.4 pg

-Options A and B are incorrect because these methods do not exist.

-Options C and E are incorrect because they provide access to servlet-speciĂžc initialization parameters.

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

7HICH STATEMENTS ABOUT LISTENERS DElNED IN THE MDYD[ VHUYOHW PACKAGE ARE TRUE #HOOSE ALL THAT APPLY

R ! ! 6HUYOHW5HVSRQVH/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN A SERVLET RESPONSE HAS BEEN SENT

R " !N +WWS6HVVLRQ/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN

. 80)

(Servlet v2.4 pg

-Option A is incorrect because these is no ServletResponseListener interface.

AN +WWS6HVVLRQ HAS TIMED OUT

R # ! 6HUYOHW&RQWH[W/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN THE SERVLET CONTEXT IS ABOUT TO BE SHUT DOWN

R $ ! 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU CAN BE USED TO PERFORM AN ACTION WHEN AN ATTRIBUTE HAS BEEN REMOVED FROM A 6HUYOHW5HTXHVW

-Option E is

R % ! 6HUYOHW&RQWH[W$WWULEXWH/LVWHQHU CAN BE USED TO PERFORM AN incorrect because a ACTION WHEN THE SERVLET CONTEXT HAS JUST BEEN CREATED AND IS AVAILABLE TO SERVICE ITS lRST REQUEST

7HICH IS MOST LOGICALLY STORED AS AN ATTRIBUTE IN SESSION SCOPE

R ! ! COPY OF A QUERY PARAMETER ENTERED BY A USER

such -Option B is incorrect becauseiately data is typically either immed scope. st returned or stored in reque

IMMEDIATELY TO A USER

COMPONENTS OF THE SYSTEM

)

. 58 (Servlet v2.4 pg

ect because a query -Option A is incorr typically used . re mo is r ete am par operation immediately to perform an

R " 4HE RESULT OF A DATABASE QUERY TO BE RETURNED

R # ! DATABASE CONNECTION OBJECT USED BY ALL WEB

ServletContextListener would be used for this purpose.

-Option C is incorrect because (since it is not speciĂžc to a particular session) it should be stored in context scope.

R $ !N OBJECT REPRESENTING A USER WHO HAS JUST LOGGED INTO THE SYSTEM

R % ! COPY OF AN INITIALIZATION PARAMETER RETRIEVED FROM A 6HUYOHW&RQWH[W OBJECT

-Option E is incorrect because servlet context parameters should stay with the ServletContext object.

\RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN DQVZHUV

'IVEN THIS CODE FROM AN OTHERWISE VALID +WWS6HUYOHW THAT HAS ALSO BEEN REGISTERED AS A 6HUYOHW5HTXHVW$WWULEXWH/LVWHQHU

. 199-200)

(Servlet v2.4 pg

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHT +WWS6HUYOHW5HVSRQVH UHV WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHT VHW$WWULEXWH ³D´ ³E´ UHT VHW$WWULEXWH ³D´ ³F´ UHT UHPRYH$WWULEXWH ³D´ ` SXEOLF YRLG DWWULEXWH$GGHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ $ ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` SXEOLF YRLG DWWULEXWH5HPRYHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ 0 ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` SXEOLF YRLG DWWULEXWH5HSODFHG 6HUYOHW5HTXHVW$WWULEXWH(YHQW HY ^ 6\VWHP RXW SULQW ³ 3 ´ HY JHW1DPH ³ !´ HY JHW9DOXH

` 7HAT LOGGING OUTPUT IS GENERATED

R ! $ D !E 3 D !E R " $ D !E 0 D !F R # $ D !E 3 D !E 0 D !F R $ $ D !E 3 D !E 3 D !QXOO R % $ D !E 0 D !E $ D !F 0 D !F R & $ D !E 0 D !E $ D !F 3 D !QXOO

-Tricky! The getValue method returns the OLD value of the attribute if the attribute was replaced.

7HEN DECLARING A LISTENER IN THE $$ WHICH SUB ELEMENTS OF THE OLVWHQHU! ELEMENT ARE REQUIRED #HOOSE ALL THAT APPLY

R ! GHVFULSWLRQ! R " OLVWHQHU QDPH! R # OLVWHQHU W\SH! R $ OLVWHQHU FODVV! -The <listener-class> sub-element is the ONLY required sub-element of R % VHUYOHW PDSSLQJ! the <listener> element.

(Servlet v2.4.4, section 10 & 13.4.9 )

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

DWWULEXWHV DQG OLVWHQHUV

7HICH TYPES OF OBJECTS CAN STORE ATTRIBUTES #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQÂżJ R " 6HUYOHW5HVSRQVH R # 5HTXHVW'LVSDWFKHU R $ +WWS6HUYOHW5HTXHVW R % +WWS6HVVLRQ&RQWH[W

-Options A, B, and C are s do invalid because these type not store attributes. -Option E is invalid because there is no such type.

(API) Note: The other two types related to servlets, that can store attributes are HttpSession and ServletContext.

s. 81-84)

(Servlet v2.4 pg

7HICH ARE TRUE #HOOSE ALL THAT APPLY

R ! 7HEN A WEB APPLICATION IS PREPARING TO SHUTDOWN THE ORDER OF LISTENER NOTIlCATION IS NOT GUARANTEED

R " 7HEN LISTENER FRIENDLY EVENTS OCCUR LISTENER INVOCATION ORDER IS NOT PREDICTABLE

-Options A and B are incorrect because the container uses the DD to determine the notiĂžcation order of registered listeners.

R # 4HE CONTAINER REGISTERS LISTENERS BASED ON DECLARATIONS IN THE DEPLOYMENT DESCRIPTOR

R $ /NLY THE CONTAINER CAN INVALIDATE A SESSION

-Option D is incorrect because a servlet can invalidate a session using the HttpSession.invalidate() method.

7HICH STATEMENTS ABOUT 5HTXHVW'LVSDWFKHU ARE TRUE WHERE APPLICABLE ASSUME THE 5HTXHVW'LVSDWFKHU WAS NOT OBTAINED VIA A CALL TO JHW1DPHG'LVSDWFKHU #HOOSE ALL THAT APPLY

. (Servlet v2.4 pg

65)

R ! ! 5HTXHVW'LVSDWFKHU CAN BE USED TO FORWARD A REQUEST TO ANOTHER SERVLET

R " 4HE ONLY METHOD IN THE 5HTXHVW'LVSDWFKHU INTERFACE IS IRUZDUG

R # 0ARAMETERS SPECIlED IN THE QUERY STRING USED TO CREATE A 5HTXHVW'LVSDWFKHU ARE NOT FORWARDED BY THE IRUZDUG METHOD

R $ 4HE SERVLET TO WHICH A REQUEST IS FORWARDED MAY ACCESS THE

-Option B is incorrect because the interface also contains an include method. -Option C is incorrect because such parameters are forwarded in this case.

ORIGINAL QUERY STRING BY CALLING JHW4XHU\6WULQJ ON THE 6HUYOHW5HTXHVW

R % 4HE SERVLET TO WHICH A REQUEST IS FORWARDED MAY ACCESS THE ORIGINAL QUERY STRING BY CALLING JHW$WWULEXWH ³MDYD[ VHUYOHW IRUZDUG TXHU\BVWULQJ´ ON THE 6HUYOHW5HTXHVW \RX DUH KHUH

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 5. Being a Web App

PRFN DQVZHUV

7HICH STATEMENTS ACCURATELY DESCRIBE HOW MANY INSTANCES OF A SERVLET THE SERVLET CONTAINER INSTANTIATES FOR EACH WEB APPLICATION #HOOSE ALL THAT APPLY

)

(Servlet spec p 24

R ! )F THE SERVLET IMPLEMENTS MDYD[ VHUYOHW 6LQJOH7KUHDG0RGHO THE CONTAINER MAY CREATE ONE INSTANCE FOR EACH REQUEST

R " )F THE SERVLET DOES NOT IMPLEMENT 6LQJOH7KUHDG0RGHO THE CONTAINER MAY CREATE MULTIPLE INSTANCES OF THE SERVLET IN THE SAME *6-

R # 4HE ORDG RQ VWDUWXS! ZHE [PO ELEMENT CAN DETERMINE HOW -Option C is incorrect

because the <load-on-startup> deployment-descriptor element determines the order of instantiation, not the number of instances.

MANY INSTANCES ARE CREATED

R $ )F THE SERVLET DOES NOT IMPLEMENT 6LQJOH7KUHDG0RGHO THE CONTAINER WILL CREATE NO MORE THAN ONE INSTANCE PER *6-

rrect R ! 7RITE THE SERVLET CODE TO EXTEND 7KUHDG6DIH6HUYOHW -Option A and B areeSeinco rvlet does Saf ead Thr e aus bec R " (AVE THE SERVLET IMPLEMENT 6LQJOH7KUHDG0RGHO vlet API not exist in the SeradM odel is R # ,OG ALL SERVLET METHOD CALLS and the .SingleThre d in version 2.4 and ate rec dep R $ 5SE LOCAL VARIABLES EXCLUSIVELY AND IF YOU HAVE TO USE INSTANCE recommended.. VARIABLES SYNCHRONIZE ACCESS TO THEM

) (Servlet spec p 27

7HAT IS THE RECOMMENDED WAY TO DEAL WITH SERVLETS AND THREAD SAFETY

not

FKDSWHU

Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

&RQYHUVDWLRQDO VWDWH , KDYH IDLOHG RQH FXVWRPHU·V LWHPV HQGHG XS LQ D GLIIHUHQW FXVWRPHU·V VKRSSLQJ FDUW 0\ DSS FRXOG QRW UHFRJQL]H WKH FOLHQWV

<RX ZHUH OLVWHQLQJ WR \RXU L3RG ZKHQ , ZDV WHDFKLQJ WKH FODVV RQ VHVVLRQ PDQDJHPHQW <RX KDYH GLVKRQRUHG WKH &RQWDLQHU

:HE VHUYHUV KDYH QR VKRUW WHUP PHPRU\ $V VRRQ DV WKH\ VHQG \RX D UHVSRQVH WKH\ IRUJHW ZKR \RX DUH 7KH QH[W WLPH \RX PDNH D UHTXHVW WKH\ GRQ¶W UHFRJQL]H \RX ,Q RWKHU ZRUGV WKH\ GRQ¶W UHPHPEHU ZKDW \RX¶YH UHTXHVWHG LQ WKH SDVW DQG WKH\ GRQ¶W UHPHPEHU ZKDW WKH\¶YH VHQW \RX LQ UHVSRQVH 1RWKLQJ 6RPHWLPHV WKDW¶V ¿ QH %XW VRPHWLPHV \RX QHHG WR NHHS FRQYHUVDWLRQDO VWDWH ZLWK WKH FOLHQW DFURVV PXOWLSOH UHTXHVWV $ VKRSSLQJ FDUW ZRXOGQ¶W ZRUN LI WKH FOLHQW KDG WR PDNH DOO KLV FKRLFHV DQG WKHQ FKHFNRXW LQ D VLQJOH UHTXHVW <RX¶OO ¿ QG D VXUSULVLQJO\ VLPSOH VROXWLRQ LQ WKH 6HUYOHW $3,

WKLV LV D QHZ FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

RI¿FLDO 6XQ H[DP REMHFWLYHV

6HVVLRQ 0DQDJHPHQW

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 :ULWH VHUYOHW FRGH WR VWRUH REMHFWV LQWR D

VHVVLRQ REMHFW DQG UHWULHYH REMHFWV IURP D VHVVLRQ REMHFW

*LYHQ D VFHQDULR GHVFULEH WKH $3,V XVHG WR DFFHVV WKH VHVVLRQ REMHFW H[SODLQ ZKHQ WKH VHVVLRQ REMHFW ZDV FUHDWHG DQG GHVFULEH WKH PHFKDQLVPV XVHG WR GHVWUR\ WKH VHVVLRQ REMHFW DQG ZKHQ LW ZDV GHVWUR\HG

!LL FOUR OF THE EXAM OBJECTIVES ON SESSION MANAGEMENT ARE COVERED COMPLETELY IN THIS CHAPTER ALTHOUGH SOME OF THESE TOPICS WERE TOUCHED ON IN THE PREVIOUS CHAPTER 4HIS CHAPTER IS YOUR ONE CHANCE TO LEARN AND MEMORIZE THESE TOPICS SO TAKE YOUR TIME

8VLQJ VHVVLRQ OLVWHQHUV ZULWH FRGH WR UHVSRQG WR DQ HYHQW ZKHQ DQ REMHFW LV DGGHG WR D VHVVLRQ DQG ZULWH FRGH WR UHVSRQG WR DQ HYHQW ZKHQ D VHVVLRQ REMHFW PLJUDWHV IURP RQH 90 WR DQRWKHU

*LYHQ D VFHQDULR GHVFULEH ZKLFK VHVVLRQ PDQDJHPHQW PHFKDQLVP WKH :HE FRQWDLQHU FRXOG HPSOR\ KRZ FRRNLHV PLJKW EH XVHG WR PDQDJH VHVVLRQV KRZ 85/ UHZULWLQJ PLJKW EH XVHG WR PDQDJH VHVVLRQV DQG ZULWH VHUYOHW FRGH WR SHUIRUP 85/ UHZULWLQJ

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

, ZDQW WKH EHHU DSS WR KDYH D EDFN DQG IRUWK FRQYHUVDWLRQ ZLWK WKH FOLHQW ZRXOGQ¡W LW EH FRRO LI WKH XVHU DQVZHUV D TXHVWLRQ DQG WKHQ WKH ZHE DSS UHVSRQGV ZLWK D QHZ TXHVWLRQ EDVHG RQ WKH DQVZHU WR WKH SUHYLRXV RQHV"

3QU _IV\[ \W SMMX KTQMV\ [XMKQNQK [\I\M IKZW[[ U]T\QXTM ZMY]M[\[ 2IGHT NOW THE BUSINESS LOGIC IN THE MODEL SIMPLY CHECKS THE PARAMETER FROM THE REQUEST AND GIVES BACK A RESPONSE THE ADVICE .OBODY IN THE APP REMEMBERS ANYTHING THAT WENT ON WITH THIS CLIENT PRIOR TO THE CURRENT REQUEST :KDW KH KDV 12: SXEOLF FODVV %HHU([SHUW ^ SXEOLF $UUD\/LVW JHW%UDQGV 6WULQJ FRORU ^ $UUD\/LVW EUDQGV QHZ $UUD\/LVW LI FRORU HTXDOV ³DPEHU´

^ e EUDQGV DGG Âł-DFN $PEHU´ We check the onte r EUDQGV DGG Âł5HG 0RRVH´ incoming parame back ` HOVH ^ lor) and give (co EUDQGV DGG Âł-DLO 3DOH $OH´ the Ăžnal responseands EUDQGV DGG Âł*RXW 6WRXW´ (an array of br lor). ` that Ăžt that cosmart UHWXUQ EUDQGV This isnĂ•t very ` ` advice...

:KDW KH :$176 SXEOLF FODVV %HHU([SHUW ^

The model (the business logic) has to Þgure out whether it has enough information to make a recommendation (in other words, to give Þnal advice), and if it doesnÕt, it has to give back the next question to ask the user.

SXEOLF 1H[W5HVSRQVH JHW$GYLFH 6WULQJ DQVZHU ^ 3URFHVV FOLHQW DQVZHU E\ ORRNLQJ DW $// RI WKH FOLHQWÂśV SUHYLRXV DQVZHUV DV ZHOO DV WKH DQVZHU IURP WKH FXUUHQW UHTXHVW LI WKHUHÂśV HQRXJK LQIR UHWXUQ ÂżQDO DGYLFH HOVH UHWXUQ WKH QH[W TXHVWLRQ WR DVN ` ` apsulates

sponse class enc r, and Assume the NextRedis play for the use e Þnal to ing th t nex e th tes whether itÕs th on. ica ind at th g hin et som or another questi on ati nd me om rec e advic

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

FOLHQW FRQYHUVDWLRQ

1\¼[ []XXW[ML \W _WZS TQSM I :-)4 KWV^MZ[I\QWV :H QHHG VRPH EHWWHU GULQNV DW WKLV SDUW\ , JRWWD FDOO .LP

:HOO , OLNH GDUN EXW WKLV LV D ZLPS\ ORRNLQJ FURZG VR ,·OO VD\ DPEHU WR EH VDIH

'XGH ,·P DW -RH·V EHDFK SDUW\ DQG , DP KROGLQJ LQ P\ KDQG DV , VSHDN D IRRI\ UHG XPEUHOOD GULQN \RX JRWWD JHW VRPH EHHU RYHU KHUH 12:

+PPP , KDYH D ORW RI DPEHUV GR \RX FDUH DERXW SULFH"

8PEUHOOD GULQNV" 2RRRRK WKDW·V MXVW :521* *RRG WKLQJ \RX FDOOHG OHW PH DVN \RX VRPH TXHVWLRQV³Ã€UVW GR \RX ZDQW VRPHWKLQJ GDUN DPEHU RU SDOH"

'XGH ZRXOG , EH ZRUNLQJ DV D FRPSXWHU ERRN PRGHO LI , GLGQ·W QHHG WKH PRQH\" 2) &2856( , FDUH DERXW SULFH

1R SUREOHP , KDYH VRPH RXWVRXUFHG ELWWHU DOH , FDQ VHQG RYHU

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

0W_ KIV PM \ZIKS \PM KTQMV\¼[ IV[_MZ[' +IM S DESIGN WON T WORK UNLESS HE CAN KEEP TRACK OF EVERYTHING THE CLIENT HAS ALREADY SAID DURING THE CONVERSATION NOT JUST THE ANSWER IN THE CURRENT REQUEST (E NEEDS THE SERVLET TO GET THE REQUEST PARAMETERS REPRESENTING THE CLIENT S CHOICES AND SAVE IT SOMEWHERE %ACH TIME THE CLIENT ANSWERS A QUESTION THE ADVICE ENGINE USES ALL OF THAT CLIENT S PREVIOUS ANSWERS TO COME UP WITH EITHER ANOTHER QUESTION TO ASK OR A lNAL RECOMMENDATION 7HAT ARE SOME OPTIONS 8VH D VWDWHIXO VHVVLRQ HQWHUSULVH MDYDEHDQ 3URE HE COULD DO THAT (E COULD HAVE HIS SERVLET BECOME A CLIENT TO A STATEFUL SESSION BEAN AND EACH TIME A REQUEST COMES IN HE COULD LOCATE THAT CLIENT S STATEFUL BEAN 4HERE ARE A LOT OF LITTLE ISSUES TO WORK OUT BUT YES YOU CAN CERTAINLY USE A STATEFUL SESSION BEAN TO STORE CONVERSATIONAL STATE "UT THAT S WAY TOO MUCH OVERHEAD OVERKILL FOR THIS APP "ESIDES +IM S HOSTING PROVIDER DOESN T HAVE A FULL * %% SERVER WITH AN %*" #ONTAINER (E S GOT 4OMCAT A WEB #ONTAINER AND THAT S IT 8VH D GDWDEDVH

'%

4HIS WOULD WORK TOO (IS HOSTING PROVIDER DOES ALLOW ACCESS TO -Y31, SO HE COULD DO IT (E COULD WRITE THE CLIENT S DATA TO A DATABASE BUT THIS IS NEARLY AS MUCH OF A RUNTIME PERFORMANCE HIT AS AN ENTERPRISE BEAN WOULD BE POSSIBLY MORE !ND WAY MORE THAN HE NEEDS

+W

,Q RWKHU ZRUGV LW SHUVLVWV IRU DQ HQWLUH VHVVLRQ ZLWK D VSHFLILF FOLHQW :H FDQ XVH LW WR VWRUH HYHU\WKLQJ ZH JHW EDFN IURP WKH FOLHQW LQ DOO WKH UHTXHVWV WKH FOLHQW PDNHV GXULQJ D VHVVLRQ

WS6HVVLR

Q

8VH DQ +WWS6HVVLRQ

$Q +WWS6HVVLRQ REMHFW FDQ KROG FRQYHUVDWLRQDO VWDWH DFURVV PXOWLSOH UHTXHVWV IURP WKH VDPH FOLHQW

"UT YOU ALREADY KNEW THAT 7E CAN USE AN (TTP3ESSION OBJECT TO HOLD THE CONVERSATIONAL STATE ACROSS MULTIPLE REQUESTS )N OTHER WORDS FOR AN ENTIRE SESSION WITH THAT CLIENT !CTUALLY +IM WOULD STILL HAVE TO USE AN (TTP3ESSION EVEN IF HE DID CHOOSE ANOTHER OPTION SUCH AS A DATABASE OR SESSION BEAN BECAUSE IF THE CLIENT IS A WEB BROWSER +IM STILL NEEDS TO MATCH A SPECIlC CLIENT WITH A SPECIlC DATABASE KEY OR SESSION BEAN )$ AND AS YOU LL SEE IN THIS CHAPTER THE (TTP3ESSION TAKES CARE OF THAT IDENTIlCATION \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQV LQ DFWLRQ

0W_ [M[[QWV[ _WZS

'LDQH VHOHFWV ´'DUNµ DQG KLWV WKH VXEPLW EXWWRQ

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR D QHZ WKUHDG RI WKH %HHU$SS VHUYOHW

7KH %HHU$SS WKUHDG À QGV WKH VHVVLRQ DVVRFLDWHG ZLWK 'LDQH DQG VWRUHV KHU FKRLFH ´'DUNµ LQ WKH VHVVLRQ DV DQ DWWULEXWH

6HUYOHW$

:HE &RQWDLQHU

Q $

VHW$WWULEXWH

+W

WS6HVVLR

+W

WS6HVVLR

7KH VHUYOHW UXQV LWV EXVLQHVV ORJLF LQFOXGLQJ FDOOV WR WKH PRGHO DQG UHWXUQV D UHVSRQVH LQ WKLV FDVH DQRWKHU TXHVWLRQ ´:KDW SULFH UDQJH"µ

:HE &RQWDLQHU

'LDQH FRQVLGHUV WKH QHZ TXHVWLRQ RQ WKH SDJH VHOHFWV ´([SHQVLYHµ DQG KLWV WKH VXEPLW EXWWRQ

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR D QHZ WKUHDG RI WKH %HHU$SS VHUYOHW

Q $

6HUYOHW$

7KH %HHU$SS WKUHDG À QGV WKH VHVVLRQ DVVRFLDWHG ZLWK 'LDQH DQG VWRUHV KHU QHZ FKRLFH ´([SHQVLYHµ LQ WKH VHVVLRQ DV DQ DWWULEXWH

6HUYOHW$

:HE &RQWDLQHU

+W

Q $

VHW$WWULEXWH

6DPH FOLHQW 6DPH VHUYOHW 'LIIHUHQW UHTXHVW 'LIIHUHQW WKUHDG 6DPH VHVVLRQ

WS6HVVLR

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

7KH VHUYOHW UXQV LWV EXVLQHVV ORJLF LQFOXGLQJ FDOOV WR WKH PRGHO DQG UHWXUQV D UHVSRQVH LQ WKLV FDVH DQRWKHU TXHVWLRQ

+W

:HE &RQWDLQHU

Q $

6HUYOHW$

WS6HVVLR

0HDQZKLOH LPDJLQH $127+(5 FOLHQW JRHV WR WKH EHHU VLWH

'LDQH¡V VHVVLRQ LV VWLOO DFWLYH EXW PHDQZKLOH 7HUUL VHOHFWV ´3DOH¾ DQG KLWV WKH VXEPLW EXWWRQ

7KH &RQWDLQHU VHQGV 7HUUL¡V UHTXHVW WR D QHZ WKUHDG RI WKH %HHU$SS VHUYOHW

7KH %HHU$SS WKUHDG VWDUWV D QHZ 6HVVLRQ IRU 7HUUL DQG FDOOV VHW$WWULEXWH WR VWRUH KHU FKRLFH ´3DOH¾

+W

:HE &RQWDLQHU

We donÕt want Terri and DianeÕs answers mixed up... so they each need their own separate session object.

7HUUL

+W

WS6HVVLR

Q $

6HUYOHW$

'LDQH

WS6HVVLR

Q %

'LIIHUHQW FOLHQW 6DPH VHUYOHW 'LIIHUHQW UHTXHVW 'LIIHUHQW WKUHDG 'LIIHUHQW VHVVLRQ

\RX DUH KHUH

Terri

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

UHFRJQL]LQJ WKH FOLHQW

7VM XZWJTMU PW_ LWM[ \PM +WV\IQVMZ SVW_ _PW \PM KTQMV\ Q[' 4HE (440 PROTOCOL USES STATELESS CONNECTIONS 4HE CLIENT BROWSER MAKES A CONNECTION TO THE SERVER SENDS THE REQUEST GETS THE RESPONSE AND CLOSES THE CONNECTION )N OTHER WORDS THE CONNECTION EXISTS FOR ONLY A SINGLE REQUEST RESPONSE

+RZ ZLOO WKH &RQWDLQHU UHFRJQL]H LW·V 'LDQH DQG QRW 7HUUL" +773 LV VWDWHOHVV VR HDFK UHTXHVW LV D QHZ FRQQHFWLRQ

"ECAUSE THE CONNECTIONS DON T PERSIST THE #ONTAINER DOESN T RECOGNIZE THAT THE CLIENT MAKING A SECOND REQUEST IS THE SAME CLIENT FROM A PREVIOUS REQUEST !S FAR AS THE #ONTAINER S CONCERNED EACH REQUEST IS FROM A NEW CLIENT

%XW WKLQJV ZHUH JRLQJ VR ZHOO , WKRXJKW ZH KDG D UHODWLRQVKLS

,·P VRUU\ EXW , GRQ·W UHPHPEHU \RX ,·P VXUH ZH VKDUHG JRRG WLPHV WRJHWKHU EXW ZH·OO KDYH WR VWDUW RYHU

WKHUH DUH QR

4

'XPE 4XHVWLRQV

7HY CAN T THE #ONTAINER JUST USE THE )0 ADDRESS OF THE CLIENT )T S PART OF THE REQUEST RIGHT

$

/H THE #ONTAINER CAN GET THE )0 ADDRESS OF THE REQUEST BUT DOES THAT UNIQUELY IDENTIFY THE CLIENT )F YOU RE ON A LOCAL )0 NETWORK YOU HAVE A UNIQUE )0 ADDRESS BUT CHANCES ARE THAT S NOT THE )0 ADDRESS THE OUTSIDE WORLD SEES 4O THE SERVER YOUR )0 ADDRESS IS THE ADDRESS OF THE ROUTER SO YOU HAVE THE SAME )0 ADDRESS AS EVERYBODY ELSE ON THAT NETWORK 3O THAT WOULDN T HELP 9OU D HAVE THE SAME PROBLEMˆTHE STUFF *IM PUTS IN HIS SHOPPING CART MIGHT END UP IN 0RADEEP S CART AND VICE VERSA 3O NO )0 ADDRESS ISN T A SOLUTION FOR UNIQUELY IDENTIFYING A SPECIFIC CLIENT ON THE INTERNET

4

7ELL THEN HOW ABOUT SECURITY INFO )F THE USER IS LOGGED IN AND THE CONNECTION IS SECURE (4403 THE #ONTAINER KNOWS %8!#4,9 WHO THE CLIENT IS RIGHT

$

9ES IF THE USER IS LOGGED IN AND THE CONNECTION IS SECURE THE #ONTAINER CAN IDENTIFY THE CLIENT AND ASSOCIATE HIM WITH A SESSION "UT THAT S A BIG IF -OST GOOD WEB SITE DESIGN SAYS hDON T FORCE THE USER TO LOG IN UNTIL IT REALLY MATTERS AND DON T SWITCH ON SECURITY (4403 UNTIL IT REALLY MATTERS v )F YOUR USERS ARE JUST BROWSING EVEN IF THEY RE ADDING ITEMS TO A SHOPPING CART YOU PROBABLY DON T WANT THE OVERHEAD FOR YOU OR THE USER OF HAVING THEM AUTHENTICATE TO THE SYSTEM UNTIL THEY DECIDE TO CHECKOUT 3O WE NEED A MECHANISM TO LINK A CLIENT TO A SESSION THAT DOESN T REQUIRE A SECURELY AUTHENTICATED CLIENT 7E LL GO INTO SECURITY DETAILS IN THE WAIT FOR IT 3ECURITY CHAPTER

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

<PM KTQMV\ VMML[ I ]VQY]M [M[[QWV 1, 4HE IDEA IS SIMPLE ON THE CLIENT S lRST REQUEST THE #ONTAINER GENERATES A UNIQUE SESSION )$ AND GIVES IT BACK TO THE CLIENT WITH THE RESPONSE 4HE CLIENT SENDS BACK THE SESSION )$ WITH EACH SUBSEQUENT REQUEST 4HE #ONTAINER SEES THE )$ lNDS THE MATCHING SESSION AND ASSOCIATES THE SESSION WITH THE REQUEST

<HV EXW ,·P VWDWH FKDOOHQJHG DQG ZRQ·W UHPHPEHU \RX VR ,·P JLYLQJ \RX D XQLTXH VHVVLRQ ,' <RX 0867 JLYH WKDW EDFN WR PH HDFK WLPH \RX PDNH D UHTXHVW VR ,·OO NQRZ LW·V \RX

+H\ VHUYHU KHUH·V P\ ÀUVW UHTXHVW ZLWK WKH SDUDPHWHU ´GDUNµ &DQ ZH VWDUW D FRQYHUVDWLRQ"

UHTXHVW ´GDU Nµ

QHZ ,'

UHVSRQ VH ,'

+W

Q

´GDUNµ

WH ´GDUNµ

VHW$WWULEX

WS6HVVLR

&RQWDLQHU

+HUH·V P\ VHFRQG UHTXHVW ZLWK WKH SDUDPHWHU ´DOHµ 0\ ,' LV GR \RX UHPHPEHU PH"

/HW·V VHH RK WKHUH \RX DUH <HV , UHPHPEHU \RX QRZ /DVW WLPH \RX VDLG WKDW \RX OLNHG ´GDUNµ EHHU

,'

UHTXHVW ´DOHµ ,'

´DOHµ

H FLDW DVVR

´GDUNµ

+W

Q

WS6HVVLR

+W VW WS6 XH HUYOHW5HT

&RQWDLQHU

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

WKH MR\ RI &RRNLHV

0W_ LW \PM +TQMV\ IVL +WV\IQVMZ M`KPIVOM ;M[[QWV 1, QVNW' 3OMEHOW THE #ONTAINER HAS TO GET THE SESSION )$ TO THE CLIENT AS PART OF THE RESPONSE AND THE CLIENT HAS TO SEND BACK THE SESSION )$ AS PART OF THE REQUEST 4HE SIMPLEST AND MOST COMMON WAY TO EXCHANGE THE INFO IS THROUGH COOKIES

+WWSQM[ +HUH¡V \RXU FRRNLH ZLWK WKH VHVVLRQ ,' LQVLGH

ÒSet-CookieÓ is just another header sent in the response. . +773 2 % & '( 6(66,21,' $$ 6HW &RRNLH [W KWPO &RQWHQW 7\SH WH &RQWHQW /HQJWK 7 *0 'DWH :HG 1RY WH 6HUYHU $SDFKH &R\R VH &RQQHFWLRQ FOR KWPO! KWPO!

QVH

+773 5HVSR

2. KHUH¡V WKH FRRNLH ZLWK P\ UHTXHVW

ÒCookieÓ is another header sent in the request.

3267 VHOHFW VH OHFW

%HHU7DVWH GR

+RVW ZZZ ZLFNHG O\VPDUW FRP 8VHU $JHQW 0 R]LOOD

&RRNLH -6(66,

+773

21,' $$% & '(

$FFHSW WH[W [PO DSSOLFDWLRQ [PO D SSOLFDWLRQ [KW KWPO T WH[W PO [PO WH[W SODLQ T YLGHR [ PQJ LPDJH MSHJ LPDJH JLI T SQJ LPDJH T $FFHSW /DQJX DJH HQ XV HQ T $FFHSW (QFRG LQJ J]LS GHÓż DWH

+773 5HTXHVW

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

<PM JM[\ XIZ\" \PM +WV\IQVMZ LWM[ ^QZ\]ITTa ITT \PM KWWSQM _WZS 9OU DO HAVE TO TELL THE #ONTAINER THAT YOU WANT TO CREATE OR USE A SESSION BUT THE #ONTAINER TAKES CARE OF GENERATING THE SESSION )$ CREATING A NEW #OOKIE OBJECT STUFlNG THE SESSION )$ INTO THE COOKIE AND SETTING THE COOKIE AS PART OF THE RESPONSE !ND ON SUBSEQUENT REQUESTS THE #ONTAINER GETS THE SESSION )$ FROM A COOKIE IN THE REQUEST MATCHES THE SESSION )$ WITH AN EXISTING SESSION AND ASSOCIATES THAT SESSION WITH THE CURRENT REQUEST

6HQGLQJ D VHVVLRQ FRRNLH LQ WKH 5(63216( +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ 4HAT S IT 3OMEWHERE IN YOUR SERVICE METHOD YOU ASK FOR A SESSION AND EVERYTHING ELSE HAPPENS AUTOMATICALLY 9OU DON T MAKE THE NEW (TTP3ESSION OBJECT YOURSELF 9OU DON T GENERATE THE UNIQUE SESSION )$ 9OU DON T MAKE THE NEW #OOKIE OBJECT 9OU DON T ASSOCIATE THE SESSION )$ WITH THE COOKIE 9OU DON T SET THE #OOKIE INTO THE RESPONSE UNDER THE 3ET #OOKIE HEADER

est for a session, You ask the requer kicks everything and the Contain You donÕt have to else into action.e! do anything els es more than just (This method do, but the FIRST on ssi se create a on the request, it time you invokeokitie to be sent with co a will cause thereÕs still not the response. Nocliw,ent will ACCEPT e guarantee th t weÕre getting ahead the cookie... bu of ourselves.)

!LL THE COOKIE WORK HAPPENS BEHIND THE SCENES

*HWWLQJ WKH VHVVLRQ ,' IURP WKH 5(48(67 +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ ,OOK FAMILIAR 9ES IT S EXACTLY THE SAME METHOD USED TO GENERATE THE SESSION )$ AND COOKIE FOR THE RESPONSE )& THE REQUEST INCLUDES A SESSION )$ COOKIE lND THE SESSION MATCHING THAT )$ %,3% )& THERE S NO SESSION )$ COOKIE /2 THERE S NO CURRENT SESSION MATCHING THE SESSION )$

Whoa! The me od for GETTING a session ID cookth with an existingiese (and matching it as SENDING a ssion) is the same You never actuallsession ID cookie. ID yourself (althy SEE the session ough you can as the session to giv e it to you). k

CREATE A NEW SESSION !LL THE COOKIE WORK HAPPENS BEHIND THE SCENES \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

FKHFNLQJ IRU D QHZ VHVVLRQ

?PI\ QN 1 _IV\ \W SVW_ _PM\PMZ \PM [M[[QWV ITZMILa M`Q[\ML WZ _I[ R][\ KZMI\ML' 'OOD QUESTION 4HE NO ARG REQUEST METHOD GET3ESSION RETURNS A SESSION REGARDLESS OF WHETHER THERE S A PRE EXISTING SESSION 3INCE YOU ALWAYS GET AN (TTP3ESSION INSTANCE BACK FROM THAT METHOD THE ONLY WAY TO KNOW IF THE SESSION IS NEW IS TO ASK THE SESSION SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW VHVVLRQ DWWULEXWHV EU!´

s a session no matter getSession() returnnÕt tell if itÕs a new what.... but you caask the session. u yo ess unl session

+WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ

LI VHVVLRQ LV1HZ

^

if the isNew() returns trueresponded yet t no s client ha with this session ID.

RXW SULQWOQ ³7KLV LV D QHZ VHVVLRQ ´ ` HOVH ^ RXW SULQWOQ ³:HOFRPH EDFN ´ ` `

4

9OU GET A SESSION BY CALLING REQUEST GET3ESSION BUT IS THAT THE ONLY WAY TO GET THE SESSION #AN T YOU GET IT FROM THE 3ERVLET#ONTEXT

$

9OU GET A SESSION FROM THE REQUEST OBJECT BECAUSEˆ THINK ABOUT ITˆTHE SESSION IS IDENTIFIED BY THE REQUEST 7HEN YOU CALL GET3ESSION ON THE #ONTAINER YOU RE SAYING h) WANT A SESSION FOR 4()3 CLIENT EITHER THE SESSION THAT MATCHES THE SESSION )$ THIS CLIENT SENT OR A NEW ONE "UT IN EITHER CASE THE SESSION IS FOR THE CLIENT ASSOCIATED WITH THIS REQUEST v "UT THERE IS ANOTHER WAY THAT YOU CAN GET A SESSION FROM A SESSION EVENT OBJECT 2EMEMBER A LISTENER CLASS ISN T A SERVLET OR *30ˆIT S JUST A CLASS THAT WANTS TO KNOW ABOUT THE EVENTS &OR EXAMPLE THE LISTENER MIGHT BE AN ATTRIBUTE TRYING TO FIND OUT WHEN IT THE ATTRIBUTE OBJECT WAS ADDED TO OR REMOVED FROM A SESSION

4HE EVENT HANDLING METHODS DEFINED BY THE LISTENER INTERFACES RELATED TO SESSIONS TAKE AN ARGUMENT OF TYPE (TTP3ESSION%VENT OR ITS SUBCLASS (TTP3ESSION"INDING%VENT !ND (TTP3ESSION%VENT HAS A GET3ESSION METHOD 3O IF YOU IMPLEMENT ANY OF THE FOUR LISTENER INTERFACES RELATED TO SESSIONS WE LL GET TO THAT LATER IN THE CHAPTER YOU CAN ACCESS THE SESSION THROUGH THE EVENT HANDLING CALLBACK METHODS &OR EXAMPLE THIS CODE IS FROM A CLASS THAT IMPLEMENTS THE (TTP3ESSION,ISTENER INTERFACE

SXEOLF YRLG VHVVLRQ&UHDWHG +WWS6HVVLRQ(YHQW HYHQW ^ +WWS6HVVLRQ VHVVLRQ HYHQW JHW6HVVLRQ HYHQW KDQGOLQJ FRGH `

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

?PI\ QN 1 _IV\ 764A I XZM M`Q[\QVO [M[[QWV' 9OU MIGHT HAVE A SCENARIO IN WHICH A SERVLET WANTS TO USE ONLY A PREVIOUSLY CREATED SESSION )T MIGHT NOT MAKE SENSE FOR THE CHECKOUT SERVLET FOR EXAMPLE TO START A NEW SESSION 3O THERE S AN OVERLOADED GET3ESSION BOOLEAN METHOD JUST FOR THAT PURPOSE )F YOU DON T WANT TO CREATE A NEW SESSION CALL GET3ESSION FALSE AND YOU LL GET EITHER NULL OR A PRE EXISTING (TTP3ESSION 4HE CODE BELOW CALLS GET3ESSION FALSE THEN TESTS WHETHER THE RETURN VALUE WAS NULL )F IT WAS NULL THE CODE OUTPUTS A MESSAGE AND THEN CREATES A NEW SESSION SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU RXW SULQWOQ ³WHVW VHVVLRQV EU!´

+WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ IDOVH

LI VHVVLRQ

QXOO ^

Now we can test for whether there was already a session (the no-arg getSession() would NEVER return null).

RXW SULQWOQ ³QR VHVVLRQ ZDV DYDLODEOH´ RXW SULQWOQ ³PDNLQJ RQH ´ VHVVLRQ UHTXHVW JHW6HVVLRQ Here ` HOVH ^ RXW SULQWOQ ³WKHUH ZDV D VHVVLRQ ´ ` `

4

s the method Passing ÒfalseÓ meanting session, xis -e pre a s urn ret no session or null if there waiss client. th h wit ed at associ

we KNOW weÕre making a new session.

4

)SN T THE CODE ABOVE JUST A STUPID INEFlCIENT WAY TO DO THE SAME THING AS THE OPPOSITE PAGE )N THE END YOU STILL CREATED A NEW SESSION

3O IT LOOKS LIKE GET3ESSION TRUE IS EXACTLY THE SAME AS GET3ESSION

$

2IGHT AGAIN 4HE NO ARG VERSION IS A CONVENIENCE FOR THOSE TIMES WHEN YOU KNOW THAT YOU ALWAYS WANT A SESSION NEW OR EXISTING 4HE VERSION THAT TAKES A BOOLEAN IS USEFUL WHEN YOU KNOW THAT YOU DON T WANT A NEW SESSION OR WHEN THE DECISION OF WHETHER TO MAKE A NEW SESSION HAPPENS AT RUNTIME AND YOU RE PASSING A VARIABLE INTO THE GET3ESSION SOME"OOLEAN METHOD

9OU RE RIGHT 4HE CODE ABOVE IS JUST FOR TESTING HOW THE TWO DIFFERENT VERSIONS OF GET3ESSION WORK )N THE REAL WORLD THE ONLY TIME YOU D WANT TO USE GET3ESSION FALSE IS IF YOU DO ./4 WANT TO CREATE A NEW SESSION )F YOUR GOAL IS TO CREATE A NEW SESSION BUT STILL RESPOND DIFFERENTLY IF YOU KNOW THIS IS A NEW VERSUS PRE EXISTING SESSION THEN USE THE NO ARG GET3ESSION METHOD AND SIMPLY ASK THE SESSION IF IT S NEW USING THE (TTP3ESSION IS.EW METHOD

$

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

ZKHQ FRRNLHV IDLO

*HH WKLV DOO VRXQGV QLFH EXW XK 1(:6 )/$6+³DQ\ERG\ ZLWK KDOI D EUDLQ GLVDEOHV FRRNLHV +RZ GR \RX GR VHVVLRQV LI \RX FDQ·W XVH FRRNLHV"

AW] KIV LW [M[[QWV[ M^MV QN \PM KTQMV\ LWM[V¼\ IKKMX\ KWWSQM[ J]\ aW] PI^M \W LW I TQ\\TM UWZM _WZS 7E DON T AGREE THAT ANYBODY WITH HALF A BRAIN DISABLES COOKIES )N FACT MOST BROWSERS DO HAVE COOKIES ENABLED AND EVERYTHING S WONDERFUL "UT THERE S NO GUARANTEE )F YOUR APP DEPENDS ON SESSIONS YOU NEED A DIFFERENT WAY FOR THE CLIENT AND #ONTAINER TO EXCHANGE SESSION )$ INFO ,UCKY FOR YOU THE #ONTAINER CAN HANDLE A COOKIE REFUSING CLIENT BUT IT TAKES A LITTLE MORE EFFORT FROM YOU )F YOU USE THE SESSION CODE ON THE PREVIOUS PAGESˆCALLING GET3ESSION ON THE REQUESTˆTHE #ONTAINER TRIES TO USE COOKIES )F COOKIES AREN T ENABLED IT MEANS THE CLIENT WILL NEVER JOIN THE SESSION )N OTHER WORDS THE SESSION S IS.EW METHOD WILL ALWAYS RETURN TRUE

GLVDEOHG ZLOO LJQRUH $ FOLHQW ZLWK FRRNLHV KHDGHUV ´6HW &RRNLHµ UHVSRQVH HW DQ H[FHSWLRQ 1R Q¶W J RNLHV \RX ZR ,I D FOLHQW GRHVQ¶W DFFHSW FR WR KDYH D R WHOO \RX WKDW \RXU DWWHPSW RII W RLQJ QV J VLUH DQG EHOOV FOLHQW URQJ 1R LW MXVW PHDQV WKH HQW Z HQW Z LV FOL LWK WK LRQ Z VHVV ' ,Q \RXU D FRRNLH ZLWK WKH VHVVLRQ , LJQRUHV \RXU DWWHPSW WR VHW HVVLRQ / UHZULWLQJ LW PHDQV WKDW JHW6 FRGH LI \RX GR 127 XVH 85 DOZD\V UHWXUQV WKDW RQH L H VLRQ VHV ZLOO DOZD\V UHWXUQ D 1(: HYHU VHQGV RQ LW 7KH FOLHQW VLPSO\ Q ³WUXH´ ZKHQ \RX FDOO LV1HZ FRRNLH KHDGHU Q ,' VVLR D VH W KDV W WKD EDFN D UHTXHV

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

=:4 ZM_ZQ\QVO" [WUM\PQVO \W NITT JIKS WV )F THE CLIENT WON T TAKE COOKIES YOU CAN USE 52, REWRITING AS A BACK UP !SSUMING YOU DO YOUR PART CORRECTLY 52, REWRITING WILL ALWAYS WORKˆTHE CLIENT WON T CARE THAT IT S HAPPENING AND WON T DO ANYTHING TO PREVENT IT 2EMEMBER THE GOAL IS FOR THE CLIENT AND #ONTAINER TO EXCHANGE SESSION )$ INFO 0ASSING 85/ COOKIES BACK AND FORTH IS THE SIMPLEST WAY TO EXCHANGE SESSION )$S BUT IF YOU CAN T PUT THE )$ IN A COOKIE WHERE CAN YOU PUT IT 52, REWRITING TAKES THE SESSION )$ THAT S IN THE COOKIE AND STICKS IT RIGHT ONTO THE END OF EVERY 52, THAT COMES IN TO THIS APP

QLG

MVHVVLR

)MAGINE A WEB PAGE WHERE EVERY LINK HAS A LITTLE BIT OF EXTRA INFO THE SESSION )$ TACKED ONTO THE END OF THE 52, 7HEN THE USER CLICKS THAT hENHANCEDv LINK THE REQUEST GOES TO THE #ONTAINER WITH THAT EXTRA BIT ON THE END AND THE #ONTAINER SIMPLY STRIPS OFF THE EXTRA PART OF THE REQUEST 52, AND USES IT TO l ND THE MATCHING SESSION

We add the session ID to the end of all the URLs in the HTML we send back in the Response.

2. +773

&RQWHQW /HQJWK 7 *0 'DWH :HG 1RY WH 6HUYHU $SDFKH &R\R &RQQHFWLRQ FORVH

KWPO! ERG\!

% & '( ´ GR MVHVVLRQLG $$

HU7HVW GO\VPDUW FRP %H

WS ZZZ ZLFNH D KUHI ÂľKW FOLFN PH D! ERG\! KWPO!

QVH

+773 5HVSR

The session ID comes back as ÒextraÓ info stuck to the end of the Request URL. (The semicolon separator is vendor-speciÞc.)

*(7 %HHU7H

VW GR MVHVVLRQ

+773

LG $$% & ' (

+RVW ZZZ ZLFNHG

O\VPDUW FRP

8VHU $JHQW 0

R]LOOD

$FFHSW WH[W [PO

DSSOLFDWLRQ [PO D SSOLFDWLRQ [KW SODLQ T YLGHR PO [PO WH[W KWPO T WH[W [ PQJ LPDJH SQJ LPDJH MS $FFHSW /DQJX HJ LPDJH JLI T DJH HQ XV HQ T T $FFHSW (QFRG LQJ J]LS GHÓż DWH

+773 5HTXHVW \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

85/ UHZULWLQJ

=:4 ZM_ZQ\QVO SQKS[ QV 764A QN KWWSQM[ NIQT IVL 764A QN aW] \MTT \PM ZM[XWV[M \W MVKWLM \PM =:4 )F COOKIES DON T WORK THE #ONTAINER FALLS BACK TO 52, REWRITING BUT ONLY IF YOU VE DONE THE EXTRA WORK OF ENCODING ALL THE 52,S YOU SEND IN THE RESPONSE )F YOU WANT THE #ONTAINER TO ALWAYS DEFAULT TO USING COOKIES lRST WITH 52, REWRITING ONLY AS A LAST RESORT YOU CAN RELAX 4HAT S EXACTLY HOW IT WORKS EXCEPT FOR THE lRST TIME BUT WE LL GET TO THAT IN A MOMENT "UT IF YOU DON T EXPLICITLY ENCODE YOUR 52,S AND THE CLIENT WON T ACCEPT COOKIES YOU DON T GET TO USE SESSIONS )F YOU DO ENCODE YOUR 52,S THE #ONTAINER WILL lRST ATTEMPT TO USE COOKIES FOR SESSION MANAGEMENT AND FALL BACK TO 52, REWRITING ONLY IF THE COOKIE APPROACH FAILS SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU n get a sessio +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ RXW SULQWOQ ³ KWPO! ERG\!´ RXW SULQWOQ ³ D KUHI ?´´ UHVSRQVH HQFRGH85/ ³ %HHU7HVW GR´ ³?´!FOLFN PH D!´ RXW SULQWOQ ³ ERG\! KWPO!´ ` to this URL.

Add the extra session ID info

4

7AIT A MINUTE HOW $/%3 THE #ONTAINER KNOW THAT COOKIES AREN T WORKING !T WHAT POINT DOES THE #ONTAINER DECIDE TO USE 52, REWRITING

$

! REALLY DUMB #ONTAINER DOESN T CARE WHETHER COOK IES WORK OR NOTˆTHE DUMB #ONTAINER WILL ALWAYS ATTEMPT TO SEND THE COOKIE !.$ DO 52, REWRITING EACH TIME EVEN IF COOKIES ARE WORKING "UT HERE S HOW A DECENT #ONTAINER HANDLES IT 7HEN THE #ONTAINER SEES A CALL TO GET3ESSION AND THE #ONTAINER DIDN T GET A SESSION )$ WITH THE CLIENT S REQUEST THE #ONTAINER NOW KNOWS THAT IT MUST ATTEMPT TO START A NEW SESSION WITH THE CLIENT !T THIS POINT THE #ONTAINER DOESN T KNOW IF COOKIES WILL WORK SO WITH THIS FIRST RESPONSE BACK TO THE CLIENT IT TRIES "/4( COOKIES AND 52, REWRITING

4

7HY CAN T IT TRY COOKIES lRST AND DO 52, REWRIT ING ON THE NEXT RESPONSE IF IT DOESN T GET BACK A COOKIE

$

2EMEMBER IF THE #ONTAINER DOESN T GET A SESSION )$ FROM THE CLIENT THE #ONTAINER WON T EVEN +./7 THAT THIS IS THE NEXT REQUEST FROM THAT CLIENT 4HE #ONTAINER WON T HAVE ANY WAY TO KNOW THAT IT TRIED COOKIES THE LAST TIME AND THEY DIDN T WORK 2EMEMBER THE /.,9 WAY THE #ONTAINER CAN RECOGNIZE THAT IT HAS SEEN THIS CLIENT BEFORE IS IF THE CLIENT SENDS A SESSION )$ 3O WHEN THE #ONTAINER SEES YOU CALL REQUEST GET3ESSION AND REALIZES IT NEEDS TO START A NEW SESSION WITH THIS CLIENT THE #ONTAINER SENDS THE RESPONSE WITH BOTH A h3ET #OOKIEv HEADER FOR THE SESSION )$ AND THE SESSION )$ APPENDED TO THE 52,S ASSUMING YOU USED RESPONSE ENCODE52, .OW IMAGINE THE NEXT REQUEST FROM THIS CLIENTˆIT WILL HAVE THE SESSION )$ APPENDED TO THE REQUEST 52, BUT IF THE CLIENT ACCEPTS COOKIES THE REQUEST WILL !,3/ HAVE A SESSION )$ COOKIE 7HEN THE SERVLET CALLS REQUEST GET3ESSION THE #ONTAINER READS THE SESSION )$ FROM THE REQUEST FINDS THE SESSION AND THINKS TO ITSELF h4HIS CLIENT ACCEPTS COOKIES SO ) CAN IGNORE THE RESPONSE ENCODE52, CALLS )N THE RESPONSE ) LL SEND A COOKIE SINCE ) KNOW THAT WORKS AND THERE S NO NEED FOR ANY 52, REWRITING SO ) WON T BOTHER v

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

=:4 ZM_ZQ\QVO _WZS[ _Q\P [MVL:MLQZMK\ 9OU MIGHT HAVE A SCENARIO IN WHICH YOU WANT TO REDIRECT THE REQUEST TO A DIFFERENT 52, BUT YOU STILL WANT TO USE A SESSION 4HERE S A SPECIAL 52, ENCODING METHOD JUST FOR THAT UHVSRQVH HQFRGH5HGLUHFW85/ ³ %HHU7HVW GR´

4

7HAT ABOUT ALL MY STATIC (4-, PAGES THEY ARE FULL OF A HREF LINKS (OW DO ) DO 52, REWRITING ON THOSE STATIC PAGES

$

9OU CAN T 4HE ONLY WAY TO USE 52, REWRITING IS IF !,, THE PAGES THAT ARE PART OF A SESSION ARE DYNAMICALLY GENERATED 9OU CAN T HARD CODE SESSION )$ S OBVIOUSLY SINCE THE )$ DOESN T EXIST UNTIL RUNTIME 3O IF YOU DEPEND ON SESSIONS YOU NEED 52, REWRITING AS A FALL BACK STRATEGY !ND SINCE YOU NEED 52, REWRITING YOU HAVE TO DYNAMICALLY GENERATE THE 52,S IN THE RESPONSE (4-, !ND THAT MEANS YOU HAVE TO PROCESS THE (4-, AT RUNTIME 9ES THIS IS A PERFORMANCE ISSUE 3O YOU MUST THINK VERY CAREFULLY ABOUT THE PLACES WHERE SESSIONS MATTER TO YOUR APP AND WHETHER SESSIONS ARE CRITICAL TO HAVE OR MERELY GOOD TO HAVE

85/ UHZULWLQJ LV DXWRPDWLF EXW RQO\ LI \RX HQFRGH \RXU 85/V <28 KDYH WR UXQ DOO \RXU 85/V WKURXJK D PHWKRG RI WKH UHVSRQVH REMHFW³HQFRGH85/ RU HQFRGH5HGLUHFW85/ ³DQG WKH &RQWDLQHU GRHV HYHU\WKLQJ HOVH

4

9OU SAID THAT TO USE 52, REWRITING PAGES MUST BE DY NAMICALLY GENERATED SO DOES THIS MEAN ) CAN DO IT WITH *30S

$

9ES 9OU CAN DO 52, REWRITING IN A *30 AND THERE S EVEN A SIMPLE *34, TAG THAT MAKES IT EASY C 52, THAT YOU LL SEE WHEN YOU GET TO THE CHAPTER ON USING CUSTOM TAGS

4 $

)S 52, REWRITING HANDLED IN A VENDOR SPECIl C WAY

9ES 52, REWRITING IS HANDLED IN A VENDOR SPECIFIC WAY 4OM CAT USES A SEMICOLON h v TO APPEND THE EXTRA INFO TO THE 52, !NOTHER VENDOR MIGHT USE A COMMA OR SOMETHING ELSE !ND WHILE 4OMCAT ADDS hJSESSIONID v IN THE REWRITTEN 52, ANOTHER VENDOR MIGHT AP PEND ONLY THE SESSION )$ ITSELF 4HE POINT IS WHATEVER THE #ONTAINER USES AS THE SEPARATOR IS RECOGNIZED BY THE #ONTAINER WHEN A REQUEST COMES IN 3O WHEN THE #ONTAINER SEES THE SEPARATOR THAT IT USES IN OTHER WORDS THE SEPARATOR THAT IT ADDED DURING 52, REWRITING IT KNOWS THAT EVERYTHING AFTER THAT IS hEXTRA INFOv THAT THE #ONTAINER PUT THERE )N OTHER WORDS THE #ONTAINER KNOWS HOW TO RECOGNIZE AND PARSE THE EXTRA STUFF IT THE #ONTAINER APPENDED TO THE 52,

85/ HQFRGLQJ LV KDQGOHG E\ WKH 5HVSRQVH

H85/ 'RQ¶W IRUJHW WKDW WKH HQFRG OO RQ \RXU PHWKRG LV VRPHWKLQJ \RX FD FW <RX GRQ¶W FDOO +WWS6HUYOHW5HVSRQVH REMH U FRQWH[W RU \RXU LW RQ WKH UHTXHVW RU RQ \RX \RXUVHOI WKDW VHVVLRQ REMHFW -XVW UHPLQG WKH UHVSRQVH 85/ HQFRGLQJ LV DOO DERXW

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

XHVW SDUDPHWHU 'RQ¡W EH IRROHG E\ D UHT 6,21,'¾ KHDGHU ´MVHVVLRQLG¾ RU D ´-6(6 HWHU ³MVHVVLRQLG´ UHTXHVW SDUDP RQLG´ \RXUVHOI ,I \RX VHH D J OLNH WKLV HWKLQ <28 GRQœW HYHU XVH ³MVHVVL VRP VHH HYHU XOG Q J ZURQJ <RX VKR VRPHERG\œV GRLQJ VRPHWKLQ No!! Q ´ L QLG VLR HVV JHW3DUDPHWHU ³MVHV VW TXH UH ' RQ, VVL 6WULQJ VH Q D UHTXHVW RU UHVSRQVH XVWRP ³MVHVVLRQLG´ KHDGHU L $QG \RX VKRXOGQœW VHH D F HHU7DVWH GR +773 3267 VHOHFW VHOHFW% HU $JHQW 0R]LOOD 8VHU ,2 ,' $$% & '( 6,21 66, 6(66 -6(6

DonÕt do this! ItÕs supposed to be a header!

D FRRNLH KHDGHU HVVLRQLG´ EHORQJV LV LQVLGH ,Q IDFW WKH 21/< SODFH D ³MV

but you This is right,yourself. it donĂ•t do The resuwltriof URL renĂ•t tidongthis IR´ UD LQ ÂłH[W / DV D 85 G RI H HQ WR WK GHG (you do either). SSHQ RU D '( yourself GR MVHVVLRQLG $$% &

HHU7DVWH GR +773 3267 VHOHFW VHOHFW% 8VHU $JHQW 0R]LOOD % & '( &RRNLH -6(66,21,' $$

3267 VHOHFW VHOHFW%

HHU7DVWH

:MDD=L HGAFLK

ƒ 85/ UHZULWLQJ DGGV WKH VHVVLRQ ,' WR WKH HQG RI DOO WKH 85/V LQ WKH +70/ WKDW \RX ZULWH WR WKH UHVSRQVH

ƒ 7KH VHVVLRQ ,' WKHQ FRPHV EDFN ZLWK WKH UHTXHVW DV ´H[WUDÂľ LQIR DW WKH HQG RI WKH UHTXHVW 85/

ƒ 85/ UHZULWLQJ ZLOO KDSSHQ DXWRPDWLFDOO\ LI FRRNLHV GRQ¡W ZRUN ZLWK WKH FOLHQW EXW \RX KDYH WR H[SOLFLWO\ HQFRGH DOO RI WKH 85/V \RX ZULWH

ƒ 7R HQFRGH D 85/ FDOO UHVSRQVH HQFRGH85/ D6WULQJ RXW SULQWOQ Âł D KUHI ?´´ UHVSRQVH HQFRGH85/ Âł %HHU7HVW GR´

ƒ 7KHUH¡V QR ZD\ WR JHW DXWRPDWLF 85/ UHZULWLQJ ZLWK \RXU VWDWLF SDJHV VR LI \RX GHSHQG RQ VHVVLRQV \RX PXVW XVH G\QDPLFDOO\ JHQHUDWHG SDJHV

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

, 5($//< GRQ¡W ZDQW D EXQFK RI VWDOH VHVVLRQV VLWWLQJ DURXQG LQ P\ VHUYHU WDNLQJ XS YDOXDEOH VSDFH

/M\\QVO ZQL WN [M[[QWV[ 4HE CLIENT COMES IN STARTS A SESSION THEN CHANGES HER MIND AND LEAVES THE SITE /R THE CLIENT COMES IN STARTS A SESSION THEN HER BROWSER CRASHES /R THE CLIENT COMES IN STARTS A SESSION AND THEN COMPLETES THE SESSION BY MAKING A PURCHASE SHOPPING CART CHECK OUT /R HER COMPUTER CRASHES 7HATEVER 4HE POINT IS SESSION OBJECTS TAKE RESOURCES 9OU DON T WANT SESSIONS TO STICK AROUND LONGER THAN NECESSARY 2EMEMBER THE (440 PROTOCOL DOESN T HAVE ANY MECHANISM FOR THE SERVER TO KNOW THAT THE CLIENT IS GONE )N DISTRIBUTED APPLICATION TERMS FOR THOSE OF YOU FAMILIAR WITH THEMˆ THERE S NO LEASING

"UT HOW DOES THE #ONTAINER OR YOU KNOW WHEN THE CLIENT WALKED AWAY (OW DOES THE #ONTAINER KNOW WHEN THE CLIENT S BROWSER CRASHED (OW DOES THE #ONTAINER KNOW WHEN IT S SAFE TO DESTROY A SESSION

(He wants to conse on his machine for rve space ÒThe SimsÓ with th playing DateÓ expansion pae ÒHot ck.)

# 4X3 X9 8 :KDW DUH VWUDWHJLHV \RX DQG WKH &RQWDLQHU PLJKW XVH WR PDQDJH WKH QXPEHU RI VHVVLRQV DQG HOLPLQDWH XQQHHGHG VHVVLRQV" :KDW DUH VRPH SRVVLEOH ZD\V LQ ZKLFK WKH &RQWDLQHU FRXOG WHOO WKDW D VHVVLRQ LV QR ORQJHU QHHGHG" 7KLQN DERXW LW WKHQ ORRN DW WKH +WWS6HVVLRQ $3, D IHZ SDJHV IURP QRZ IRU FOXHV

6RPH GLVWULEXWHG DSSV XVH OHDVLQJ DV D ZD\ IRU WKH VHUYHU WR NQRZ ZKHQ D FOLHQW LV JRQH 7KH FOLHQW JHWV D OHDVH IURP WKH VHUYHU DQG WKHQ PXVW UHQHZ WKH OHDVH DW VSHFLILHG LQWHUYDOV WR WHOO WKH VHUYHU WKDW WKH FOLHQW LV VWLOO DOLYH ,I WKH FOLHQW¡V OHDVH H[SLUHV WKH VHUYHU NQRZV LW FDQ GHVWUR\ DQ\ UHVRXUFHV LW ZDV KROGLQJ IRU WKDW FOLHQW \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

DEDQGRQHG VHVVLRQV

0W_ _M _IV\ Q\ \W _WZS 7E D LIKE THE #ONTAINER TO RECOGNIZE WHEN A SESSION HAS BEEN INACTIVE FOR TOO LONG AND DESTROY THE SESSION /F COURSE WE MIGHT HAVE TO l GHT THE #ONTAINER OVER WHAT hTOO LONGv REALLY MEANS )S MINUTES TOO LONG !N HOUR ! DAY -AYBE THERE S A WAY FOR US TO TELL THE #ONTAINER WHAT hTOO LONGv IS

'LDQH VHOHFWV ´'DUNµ DQG KLWV WKH VXEPLW EXWWRQ

7KH &RQWDLQHU VHQGV WKH UHTXHVW WR D QHZ WKUHDG RI WKH %HHU$SS VHUYOHW

7KH &RQWDLQHU PDNHV D QHZ VHV VLRQ ,' 7KH ´-6(66,21,'µ FRRNLH LV VHQW EDFN WR 'LDQH LQ WKH UHVSRQVH QRW VKRZQ

6HUYOHW$

+W

:HE &RQWDLQHU

'LDQH YDQLVKHV P\VWHULRXVO\

7KH &RQWDLQHU GRHV ZKDWHYHU &RQWDLQHUV GR LQ WKHLU VSDUH WLPH DOWKRXJK WKHUH DUH SUREDEO\ SOHQW\ RI RWKHU FOLHQWV WR VHUYLFH

Q $

,'

WS6HVVLR

7KH VHVVLRQ VWDUWHG IRU 'LDQH LV VWLOO VLWWLQJ WKHUH ZDLWLQJ DEDQGRQHG

6HUYOHW$

:HE &RQWDLQHU

'LDQH GRHVQ·W UHWXUQ 7KH &RQWDLQHU FKHFNV WKH VWDWH RI 0LQXWHV JR E\ VHVVLRQ DQG À QGV WKDW QR UHTXHVWV KDYH FRPH LQ ZLWK WKDW VHVVLRQ ,' IRU PLQXWHV

Q $

,'

+W

WS6HVVLR

7KH &RQWDLQHU VD\V ´ PLQXWHV LV MXVW WRR ORQJ 6KH·V QRW FRPLQJ EDFN µ DQG GHVWUR\V WKH SRRU DEDQGRQHG VHVVLRQ

6HUYOHW$

$

+W

WS6HVVLR

Q

end the session :HE &RQWDLQHU

This is ssanion ex-Se

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

<PM 0\\X;M[[QWV QV\MZNIKM !LL YOU CARE ABOUT WHEN YOU CALL GET3ESSION IS THAT YOU GET AN INSTANCE OF A CLASS THAT IMPLEMENTS THE (TTP3ESSION INTERFACE )T S THE #ONTAINER S JOB TO CREATE THE IMPLEMENTATION /NCE YOU HAVE A SESSION WHAT CAN YOU DO WITH IT -OST OF THE TIME YOU LL USE SESSIONS TO GET AND SET SESSION SCOPED ATTRIBUTES "UT THERE S MORE OF COURSE 3EE IF YOU CAN l GURE OUT SOME OF THE KEY METHODS FOR YOURSELF ANSWERS ARE ON THE NEXT PAGE SO DON T TURN THE PAGE

LQWHUI

MDYD[ VHUYOHW KW DFH!! WS +WWS6HVVLRQ

2EMHFW JHW$WWUL

EXWH 6WULQJ

ORQJ JHW&UHDWLR Q7LPH

6WULQJ JHW,G

ORQJ JHW/DVW$F FHVVHG7LPH

LQW JHW0D[,QDFWLY H,QWHUYDO

6HUYOHW&RQWH[ W JHW6HUYOHW&R QWH[W

YRLG LQYDOLGDWH

ERROHDQ LV1HZ

YRLG UHPRYH$ WWULEXWH 6WULQJ

YRLG VHW$WWULEXWH 6WULQJ 2EMHF W

YRLG VHW0D[,QD FWLYH,QWHUYDO LQ W

D IHZ PRUH P HWKRGV

6KDUSHQ \RXU SHQFLO :KDW LW GRHV

:KDW \RX·G XVH LW IRU

JHW&UHDWLRQ7LPH

JHW/DVW$FFHVVHG7LPH

VHW0D[,QDFWLYH,QWHUYDO

JHW0D[,QDFWLYH,QWHUYDO

LQYDOLGDWH

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

+WWS6HVVLRQ PHWKRGV

3Ma 0\\X;M[[QWV UM\PWL[ 9OU ALREADY KNOW ABOUT THE METHODS FOR ATTRIBUTES GET!TTRIBUTE SET!TTRIBUTE REMOVE!TTRIBUTE BUT HERE ARE A FEW KEY ONES YOU MIGHT NEED IN YOUR APPLICATION AND THAT MIGHT BE ON THE EXAM

:KDW LW GRHV

:KDW \RX·G XVH LW IRU

5HWXUQV WKH WLPH WKH VHVVLRQ ZDV ILUVW FUHDWHG

7R ILQG RXW KRZ ROG WKH VHVVLRQ LV <RX PLJKW ZDQW WR UHVWULFW FHUWDLQ VHVVLRQV WR D IL[HG OHQJWK RI WLPH )RU H[DPSOH \RX PLJKW VD\ ´2QFH \RX·YH ORJJHG LQ \RX KDYH H[DFWO\ PLQXWHV WR FRPSOHWH WKLV IRUP µ

JHW/DVW$FFHVVHG7LPH

5HWXUQV WKH ODVW WLPH WKH &RQWDLQHU JRW D UHTXHVW ZLWK WKLV VHVVLRQ ,' LQ PLOOLVHFRQGV

7R ILQG RXW ZKHQ D FOLHQW ODVW DFFHVVHG WKLV VHVVLRQ <RX PLJKW XVH LW WR GHFLGH WKDW LI WKH FOLHQW·V EHHQ JRQH D ORQJ WLPH \RX·OO VHQG WKHP DQ HPDLO DVNLQJ LI WKH\·UH FRPLQJ EDFN 2U PD\EH \RX·OO LQYDOLGDWH WKH VHVVLRQ

VHW0D[,QDFWLYH,QWHUYDO

6SHFLILHV WKH PD[LPXP WLPH LQ VHFRQGV WKDW \RX ZDQW WR DOORZ EHWZHHQ FOLHQW UHTXHVWV IRU WKLV VHVVLRQ

7R FDXVH D VHVVLRQ WR EH GHVWUR\HG DIWHU D FHUWDLQ DPRXQW RI WLPH KDV SDVVHG ZLWKRXW WKH FOLHQW PDNLQJ DQ\ UHTXHVWV IRU WKLV VHVVLRQ 7KLV LV RQH ZD\ WR UHGXFH WKH DPRXQW RI VWDOH VHVVLRQV VLWWLQJ LQ \RXU VHUYHU

JHW0D[,QDFWLYH,QWHUYDO

5HWXUQV WKH PD[LPXP WLPH 7R ILQG RXW KRZ ORQJ WKLV VHVVLRQ FDQ EH LQDFWLYH DQG VWLOO EH DOLYH <RX FRXOG XVH WKLV WR MXGJH KRZ PXFK PRUH WLPH DQ LQ VHFRQGV WKDW LV DOORZHG EHWZHHQ FOLHQW UHTXHVWV IRU LQDFWLYH FOLHQW KDV EHIRUH WKH VHVVLRQ ZLOO EH LQYDOLGDWHG WKLV VHVVLRQ

LQYDOLGDWH

(QGV WKH VHVVLRQ 7KLV LQFOXGHV XQELQGLQJ DOO VHVVLRQ DWWULEXWHV FXUUHQWO\ VWRUHG LQ WKLV VHVVLRQ 0RUH RQ WKDW ODWHU LQ WKLV FKDSWHU

JHW&UHDWLRQ7LPH

7R NLOO D VHVVLRQ LI WKH FOLHQW KDV EHHQ LQDFWLYH RU LI \RX .12: WKH VHVVLRQ LV RYHU IRU H[DPSOH DIWHU WKH FOLHQW GRHV D VKRSSLQJ FKHFN RXW RU ORJV 7KH VHVVLRQ LQVWDQFH LWVHOI PLJKW EH UHF\FOHG E\ WKH &RQWDLQHU EXW ZH GRQ·W FDUH ,QYDOLGDWH PHDQV WKH VHVVLRQ ,' QR ORQJHU H[LVWV DQG WKH DWWULEXWHV DUH UHPRYHG IURP WKH VHVVLRQ REMHFW

# 4X3 X9 8 1RZ WKDW \RX¶YH VHHQ WKHVH PHWKRGV FDQ \RX SXW WRJHWKHU D VWUDWHJ\ IRU HOLPLQDWLQJ DEDQGRQHG VHVVLRQV"

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW <RX FDQ¡W EH VHULRXV GRHV WKLV PHDQ WKDW , KDYH WR NHHS WUDFN RI VHVVLRQ DFWLYLW\ DQG WKDW , KDYH WR GHVWUR\ WKH VWDOH VHVVLRQV" &DQ¡W WKH &RQWDLQHU GR WKDW"

;M\\QVO [M[[QWV \QUMW]\ 'OOD NEWS YOU DON T HAVE TO KEEP TRACK OF THIS YOURSELF 3EE THOSE METHODS ON THE OPPOSITE PAGE 9OU DON T HAVE TO USE THEM TO GET RID OF STALE INACTIVE SESSIONS 4HE #ONTAINER CAN DO IT FOR YOU 7KUHH ZD\V D VHVVLRQ FDQ GLH

)T TIMES OUT 9OU CALL INVALIDATE ON THE SESSION OBJECT 4HE APPLICATION GOES DOWN CRASHES OR IS UNDEPLOYED

&RQĂ€ JXULQJ VHVVLRQ WLPHRXW LQ WKH ''

#ONl GURING A TIMEOUT IN THE $$ HAS VIRTUALLY THE SAME EFFECT AS CALLING SET-AX)NACTIVE)NTERVAL ON EVERY SESSION THAT S CREATED

s says if

hi ZHE DSS ! ts in minutes. T The Ò15Ó is doesnÕt make any requesll it.* VHUYOHW! ki , nt es ie cl ut e in th n for 15 m VHUYOHW! on this sessio VHVVLRQ FRQ¿ J! VHVVLRQ WLPHRXW! VHVVLRQ WLPHRXW! VHVVLRQ FRQ¿ J! ZHE DSS! 6HWWLQJ VHVVLRQ WLPHRXW IRU D VSHFLÀ F VHVVLRQ

7LPHRXWV LQ WKH '' DUH LQ 0,187(6

WR +HUHÂśV D ELJ LQFRQVLVWHQF\ ZDWFK RXW IRU \RX VSHFLI\ WLPHRXWV LQ WKH '' XVLQJ 0,187(6 EXW LI \RX VHW D RX WLPHRXW SURJUDPPDWLFDOO\ \ VSHFLI\ 6(&21'6

)F YOU WANT TO CHANGE THE SESSION TIMEOUT VALUE FOR A PARTICULAR SESSION INSTANCE WITHOUT AFFECTING THE TIMEOUT LENGTH FOR ANY OTHER SESSIONS IN THE APP VHVVLRQ VHW0D[,QDFWLYH,QWHUYDO

Only the se which you cassllion on method is affethe cted

.

The argument to the method is in seconds, so this says if the client doesnÕt make any * requests on the session for 20 minutes, kill it. *The session, not the client. \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ WLPHRXW H[HUFLVH

&RGH 0DJQHWV 6SHFLI\ LQ ERWK WKH '' DQG SURJUDPPDWLFDOO\ WKDW LI D VHV VLRQ GRHV QRW UHFHLYH DQ\ UHTXHVWV IRU PLQXWHV LW VKRXOG EH GHVWUR\HG :H SXW RQH PDJQHW LQ WKH VHUYOHW IRU \RX WR JHW VWDUWHG DQG \RX PLJKW QRW XVH DOO PDJQHWV

'' ZHE DSS !

ZHE DSS!

FRQ¿ J! VHVVLRQ WLPHRXW! VHVVLRQ WLPHRXW!

¿ J! FRQ [W H W Q R WLPHRXW! F

! OHW HUY V

VHVVLRQ WLPHRXW!

VHUYOHW! ZHE DSS!

QWHUYDO! PD[ LQDFWLYH L VHVVLRQ FRQ¿ J! PD[ LQDFWLYH LQWHUYDO!

Q¿ J! W FR QWH[ FR VHUYOHW!

6HUYOHW SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ +WWS6HVVLRQ

VHVVLRQ

JHW6HUYOHW&RQWH[W JHW6HVVLRQ

`

VHW0D[,QDF

WLYH,QWHUY

VHW7LPHRXW UHTXHVW

DO

UHTXHVW JHW6HVVLRQ

VHW&UHDWLRQ7LPH

!

VHVVLRQ VHW6HVVLRQ7LPHRXW

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

%( WKH &RQWDLQHU (DFK RI WKH WZR OLVWLQJV UHSUHVHQWV FRGH IURP D FRPSLOHG +WWS6HUYOHW <RXU MRE LV WR WKLQN OLNH WKH &RQWDLQHU DQG GHWHUPLQH ZKDW ZLOO KDSSHQ ZKHQ HDFK RI WKHVH VHUYOHWV DUH LQYRNHG WZLFH E\ WKH VDPH FOLHQW 'HVFULEH ZKDW KDSSHQV WKH ILUVW DQG VHFRQG WLPH WKH VDPH FOLHQW DFFHVVHV WKH VHUYOHW

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ VHVVLRQ VHW$WWULEXWH ³IRR´ ³ ´ VHVVLRQ VHW$WWULEXWH ³EDU´ ³ ´ VHVVLRQ LQYDOLGDWH 6WULQJ IRR 6WULQJ VHVVLRQ JHW$WWULEXWH ³IRR´ RXW SULQWOQ ³)RR ³ IRR `

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ VHVVLRQ VHW$WWULEXWH ³IRR´ ³ ´ VHVVLRQ VHW0D[,QDFWLYH,QWHUYDO 6WULQJ IRR 6WULQJ VHVVLRQ JHW$WWULEXWH ³IRR´ LI VHVVLRQ LV1HZ

^ RXW SULQWOQ ³7KLV LV D QHZ VHVVLRQ ´ ` HOVH ^ RXW SULQWOQ ³:HOFRPH EDFN ´ ` RXW SULQWOQ ³)RR ³ IRR ` \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

H[HUFLVH DQVZHUV

&RGH 0DJQHWV $QVZHUV

6SHFLI\ LQ ERWK WKH '' DQG SURJUDPPDWLFDOO\ WKDW LI D VHVVLRQ GRHV QRW UHFHLYH DQ\ UHTXHVWV IRU PLQXWHV LW VKRXOG EH GHVWUR\HG

''

e timeout Remember, th speciĂžed in is D D in the S. E MINUT

ZHE DSS ! VHVVLRQ FRQÂż J! VHVVLRQ WLPHRXW!

VHVVLRQ WLPHRXW!

VHVVLRQ FRQÂż J! ZHE DSS! WLPHRXW! WLPHRXW!

VHUYOHW!

PD[ LQDFWLYH LQWHUYDO! PD[ LQDFWLYH LQWHUYDO!

VHUYOHW!

ZHE DSS! VHUYOHW!

FRQWH[W FRQÂż J!

FRQWH[W FRQÂż J!

6HUYOHW SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ +WWS6HVVLRQ

VHVVLRQ

VHVVLRQ

UHTXHVW JHW6HVVLRQ

VHW0D[,QDFWLYH,QWHUYDO

timeout is In code, the SECONDS. speciĂžed in

`

VHW7LPHRXW UHTXHVW

VHW6HVVLRQ7LPHRXW

!

VHW&UHDWLRQ7LPH

JHW6HUYOHW&RQWH[W JHW6HVVLRQ

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

%( WKH &RQWDLQHU $QVZHUV

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ VHVVLRQ VHW$WWULEXWH ³IRR´ ³ ´ VHVVLRQ VHW$WWULEXWH ³EDU´ ³ ´ VHVVLRQ LQYDOLGDWH here we invalidate the session 6WULQJ IRR 6WULQJ VHVVLRQ JHW$WWULEXWH ³IRR´

Uh-oh! ItÕs too late to call getAttribute() on the session because the session already IS invalid!

RXW SULQWOQ Âł)RR Âł IRR ` 5HVXOW D UXQWLPH H[FHSWLRQ ,OOHJDO6WDWH([FHSWLRQ LV WKURZQ EHFDXVH \RX FDQÂśW JHW DQ DWWULEXWH $)7(5 WKH VHVVLRQ EHFRPHV LQYDOLG

SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU +WWS6HVVLRQ VHVVLRQ UHTXHVW JHW6HVVLRQ VHVVLRQ VHW$WWULEXWH ³IRR´ ³ ´ VHVVLRQ VHW0D[,QDFWLYH,QWHUYDO

Here weÕre causing the session to timeout IMMEDIATELY, because weÕre saying, Òtimeout after 0 seconds of inactivityÓ.

LI VHVVLRQ LV1HZ

^ You canĂ•t call isNew() on a session that RXW SULQWOQ Âł7KLV LV D QHZ VHVVLRQ ´ already been invalidated. So itĂ•s reall Ă•s ` HOVH ^ y the same problem as the code RXW SULQWOQ Âł:HOFRPH EDFN ´ e... you canĂ•t call this method on an invalabov ` id session. RXW SULQWOQ Âł)RR Âł VHVVLRQ JHW$WWULEXWH ÂłIRR´

` 5HVXOW D UXQWLPH H[FHSWLRQ ,OOHJDO6WDWH([FHSWLRQ LV WKURZQ EHFDXVH \RX FDQÂśW FDOO LV1HZ RQ WKH VHVVLRQ $)7(5 WKH VHVVLRQ EHFRPHV LQYDOLG 6HWWLQJ WKH PD[LPXP LQDFWLYH LQWHUYDO WR PHDQV WKH VHVVLRQ WLPHV RXW DQG LV LQYDOLGDWHG LPPHGLDWHO\

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

FXVWRP FRRNLHV

+IV 1 ][M KWWSQM[ NWZ W\PMZ \PQVO[ WZ IZM \PMa WVTa NWZ [M[[QWV[' !LTHOUGH COOKIES WERE ORIGINALLY DESIGNED TO HELP SUPPORT SESSION STATE YOU CAN USE CUSTOM COOKIES FOR OTHER THINGS 2EMEMBER A COOKIE IS NOTHING MORE THAN A LITTLE PIECE OF DATA A NAME VALUE 3TRING PAIR EXCHANGED BETWEEN THE CLIENT AND SERVER 4HE SERVER SENDS THE COOKIE TO THE CLIENT AND THE CLIENT RETURNS THE COOKIE WHEN THE CLIENT MAKES ANOTHER REQUEST /NE COOL THING ABOUT COOKIES IS THAT THE USER DOESN T HAVE TO GET INVOLVEDˆTHE COOKIE EXCHANGE IS AUTOMATIC ASSUMING COOKIES ARE ENABLED ON THE CLIENT OF COURSE "Y DEFAULT A COOKIE LIVES ONLY AS LONG AS A SESSION ONCE THE CLIENT QUITS HIS BROWSER THE COOKIE DISAPPEARS 4HAT S HOW THE h*3%33)/.)$v COOKIE WORKS "UT YOU CAN TELL A COOKIE TO STAY ALIVE EVEN !&4%2 THE BROWSER SHUTS DOWN 4HAT WAY YOUR WEB APP CAN STILL GET THE COOKIE INFORMATION EVEN THOUGH THE SESSION WITH THAT CLIENT IS LONG GONE )MAGINE THAT +IM WANTS TO DISPLAY THE USER S NAME EACH TIME HE RETURNS TO THE BEER SITE 3O HE SETS THE COOKIE THE l RST TIME HE RECEIVES THE CLIENT S NAME AND IF HE GETS THE COOKIE BACK WITH A REQUEST HE KNOWS NOT TO ASK FOR THE NAME AGAIN !ND IT DOESN T MATTER IF THE USER RESTARTED HIS BROWSER AND HASN T BEEN ON THE SITE FOR A WEEK . +773 2 LUVFK VHUQDPH 7RPDV+ 6HW &RRNLH X [W KWPO &RQWHQW 7\SH WH &RQWHQW /HQJWK 7 *0 'DWH :HG 1RY WH 6HUYHU $SDFKH &R\R &RQQHFWLRQ FORVH

Server sends this Ăžrst.

KWPO! KWPO!

<RX FDQ XVH FRRNLHV WR H[FKDQJH QDPH YDOXH 6WULQJ SDLUV EHWZHHQ WKH VHUYHU DQG WKH FOLHQW 7KH VHUYHU VHQGV WKH FRRNLH WR WKH FOLHQW DQG WKH FOLHQW VHQGV LW EDFN ZLWK HDFK VXEVHTXHQW UHTXHVW 6HVVLRQ FRRNLHV YDQLVK ZKHQ WKH FOLHQW¡V EURZVHU TXLWV EXW \RX &$1 WHOO D FRRNLH WR SHUVLVW RQ WKH FOLHQW HYHQ DIWHU WKH EURZVHU VKXWV GRZQ

3267 VHOHFW VH

OHFW%HHU7DVWH

+RVW ZZZ ZLFNHG O\VPDUW FRP 8VHU $JHQW 0 R]LOOD

&RRNLH XVHUQ

GR +773

DPH 7RPDV+ LUVFK

$FFHSW WH[W [PO DSSOLFDWLRQ [PO D SSOLFDWLRQ [KW KWPO T WH[W PO [PO WH[W SODLQ T YLGHR [ PQJ LPDJH MSHJ LPDJH JLI T SQJ LPDJH T $FFHSW /DQJX DJH HQ XV HQ T $FFHSW (QFRG LQJ J]LS GHÓż DWH

Client sends this back.

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

=[QVO +WWSQM[ _Q\P \PM ;MZ ^TM\ )81 9OU CAN GET COOKIE RELATED HEADERS OUT OF THE (440 REQUEST AND RESPONSE BUT DON T %VERYTHING YOU NEED TO DO WITH COOKIES HAS BEEN ENCAPSULATED IN THE 3ERVLET !0) IN THREE CLASSES (TTP3ERVLET2EQUEST (TTP3ERVLET2ESPONSE AND #OOKIE

LQWHUIDFH!! HW5HTXHVW KWWS +WWS6HUYO MDYD[ VHUYOHW

JHW&RQWH[W3DWK

JHW&RRNLHV

LQJ

JHW+HDGHU 6WU JHW4XHU\6WULQJ

JHW6HVVLRQ

PHWKRGV 0$1< PRUH

LQWHUIDFH!! MDYD[ VHUYOHW KWWS +WWS6HUYOHW5HVSRQVH

DGG&RRNLH

DGG+HDGHU

HQFRGH5HGLUHFW85/

VHQG(UURU

VHW6WDWXV

MDYD[ VHUYOHW KW WS

&RRNLH

&RRNLH 6WULQJ 6

WULQJ

6WULQJ JHW'RPD LQ

LQW JHW0D[$JH

6WULQJ JHW1DPH

6WULQJ JHW3DWK

ERROHDQ JHW6HFX UH

6WULQJ JHW9DOXH

YRLG VHW'RPDLQ 6WULQJ

YRLG VHW0D[$JH LQW

YRLG VHW3DWK 6W ULQJ

YRLG VHW9DOXH 6 WULQJ

D IHZ PRUH PH WKRGV

0$1< PRUH PHWKRGV

&UHDWLQJ D QHZ &RRNLH &RRNLH FRRNLH QHZ &RRNLH ³XVHUQDPH´ QDPH

takes constructor The Cookielue String pair. a name/va

6HWWLQJ KRZ ORQJ D FRRNLH ZLOO OLYH RQ WKH FOLHQW FRRNLH VHW0D[$JH

6HQGLQJ WKH FRRNLH WR WKH FOLHQW UHVSRQVH DGG&RRNLH FRRNLH

setMaxAge is deÞ ned in SECONDS. alive on the clien This code Setting max age ttofor 30*60 secondsÓ (30 minusays Òstay the browser exits -1 makes the cookie disappeartes). . So, if you call ge when ÒJSESSIONIDÓ co tMaxAge() on th okie, what will yo e u get back?

*HWWLQJ WKH FRRNLH V IURP WKH FOLHQW UHTXHVW

&RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV IRU LQW L L FRRNLHV OHQJWK L ^ &RRNLH FRRNLH FRRNLHV>L@ LI FRRNLH JHW1DPH HTXDOV ³XVHUQDPH´

^ 6WULQJ XVHU1DPH FRRNLH JHW9DOXH RXW SULQWOQ Âł+HOOR Âł XVHU1DPH EUHDN ` `

d... ring) metho tCookie(Sts in a Cookie ge no Ă•s re ie The get cook op over you can ondlythen you have to lou want. yo an e y, on arra to Ăžnd the the array

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

FRRNLH H[DPSOH

;QUXTM K][\WU KWWSQM M`IUXTM 3O IMAGINE THAT +IM WANTS TO PUT UP A FORM THAT ASKS THE USER TO SUBMIT HIS NAME 4HE FORM CALLS A SERVLET THAT GETS THE USERNAME REQUEST PARAMETER AND USES THE NAME VALUE TO SET A COOKIE IN THE RESPONSE 4HE NEXT TIME THIS USER MAKES A REQUEST ON !.9 SERVLET IN THIS WEB APP THE COOKIE COMES BACK WITH THE REQUEST ASSUMING THE COOKIE IS STILL ALIVE BASED ON THE COOKIE S MAX!GE VALUE 7HEN A SERVLET IN THE WEB APP SEES THIS COOKIE IT CAN PUT THE USER S NAME INTO THE DYNAMICALLY GENERATED RESPONSE AND THE BUSINESS LOGIC KNOWS NOT TO ASK THE USER TO INPUT HIS NAME AGAIN 4HIS CODE IS A SIMPLIlED TEST VERSION OF THE SCENARIO WE JUST DESCRIBED 6HUYOHW WKDW FUHDWHV DQG 6(76 WKH FRRNLH LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV &RRNLH7HVW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 6WULQJ QDPH UHTXHVW JHW3DUDPHWHU ³XVHUQDPH´

name Get the userÕsthe form. in d te it subm

Make a new cook store the userÕs naie so me. Keep it alive on the client for 30 minutes. Add the cookie as a ÒSet-CookieÓ Let a JSP make response header. the response page.

&RRNLH FRRNLH QHZ &RRNLH ³XVHUQDPH´ QDPH FRRNLH VHW0D[$JH UHVSRQVH DGG&RRNLH FRRNLH

5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³FRRNLHUHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH ` ` -63 WR UHQGHU WKH YLHZ IURP WKLV VHUYOHW KWPO! ERG\! D KUHI ´FKHFNFRRNLH GR´!FOLFN KHUH D! ERG\! KWPO!

about nothing JSP-ish OK, sure, thereÕste outputting even THIS this, but we hafrom a servlet. The fact much HTML arding to a JSP doesnÕt ie is that weÕre forw setting. The cook change the cookreiesponse by the time the already in the arded to the JSP... request is forw

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

+][\WU KWWSQM M`IUXTM KWV\QV]ML 6HUYOHW WKDW *(76 WKH FRRNLH LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV &KHFN&RRNLH H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU

s Get the cookie t. es qu re e from th

&RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV IRU LQW L L FRRNLHV OHQJWK L ^ &RRNLH FRRNLH FRRNLHV>L@ LI FRRNLH JHW1DPH HTXDOV ³XVHUQDPH´

^ 6WULQJ XVHU1DPH FRRNLH JHW9DOXH RXW SULQWOQ Âł+HOOR Âł XVHU1DPH EUHDN ` ` ` `

<RX GRQÂś $// WKH FR W KDYH WR NQRZ RNLH PHW KRGV )RU WKH H[D P PHPRUL]H \RX GRQÂśW KDYH WR HYHU\ RQH RI WKH PHW LQ FODVV & KRGV RRNLH EXW \RX PXV NQRZ WKH W UHTX PHWKRGV WR HVW DQG UHVSRQVH JHW DQG D GG &RRNLHV <RX VKRX OG D FRQVWUXFWRU OVR NQRZ WKH &RRNL H DQG WKH JH DQG VHW0 D[$JH P W0D[$JH HWKRGV

Loop through the cookie array looking for a cookie named ÒusernameÓ. If there is one, get the value and print it.

'RQ¡W FRQIXVH &RRNLHV ZLWK KHDGHUV

PH D UHVSRQVH \RX SDVV WKH QD :KHQ \RX DGG D KHDGHU WR HQWV UJXP DV D ULQJV H 6W YDOX DQG RR´ ³EDU´ UHVSRQVH DGG+HDGHU ³I WKH UHVSRQVH \RX SDVV D LH WR RRN G D & %XW ZKHQ \RX DG NLH QDPH DQG YDOXH LQ WKH &RR W WKH X VH W <R EMHF &RRNLH R U UXFWR RQVW &RRNLH F RRNLH ³QDPH´ QDPH &RRNLH FRRNLH QHZ & RNLH UHVSRQVH DGG&RRNLH FR ERWK D VHW+HDGHU DQG HUHœV DW WK R WK HU WR $QG UHPHPE DGHU DGGV D QHZ YDOXH WR G+H G DG HWKR U P HDGH DQ DGG+ FHV V RQH EXW VHW+HDGHU UHSOD HUH L LI WK DGHU J KH [LVWLQ DQ H HWKRG HœV 127 D VHW&RRNLH P WKH H[LVWLQJ YDOXH %XW WKHU PHWKRG 7KHUHœV RQO\ DQ DGG&RRNLH

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ OLIHF\FOH PRPHQWV

3Ma UQTM[\WVM[ NWZ IV 0\\X;M[[QWV (IGHLIGHTS OF THE IMPORTANT MOMENTS IN AN (TTP3ESSION OBJECT S LIFE

art of or some p Timeout calls invalidate () the app session. on the

7KH VHVVLRQ LV FUHDWHG RU GHVWUR\HG

Q $

+W

+W

WS6HVVLR

Q $

H LQYDOLGDW

QHZ

WS6HVVLR

6HVVLRQ DWWULEXWHV DUH DGGHG UHPRYHG RU UHSODFHG E\ RWKHU SDUWV RI WKH DSS EXWH

VHW$WWUL

UHPRYH$WWULEXWH

+W

Q $

6HUYOHW% 6HUYOHW$

WS6HVVLR

7KH VHVVLRQ LV SDVVLYDWHG LQ RQH 90 DQG DFWLYDWHG LQ DQRWKHU ZLWKLQ D GLVWULEXWHG DSS

90

90

H \ WR PRY JHW UHDG

&RQWDLQHU $

WS6HVVLR

The session migrates from one VM to another.

+W

Q $

+W

Q $

1RZ , KDYH \RX

WS6HVVLR

&RQWDLQHU $

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

;M[[QWV TQNMKaKTM -^MV\[ 0LOHVWRQH /LIHF\FOH

(YHQW DQG /LVWHQHU W\SH +WWS6HVVLRQ(YHQW

7KH VHVVLRQ ZDV FUHDWHG 7HEN THE #ONTAINER lRST CREATES A SESSION !T THIS POINT THE SESSION IS STILL CONSIDERED NEW IN OTHER WORDS THE CLIENT HAS NOT YET SENT A REQUEST WITH THE SESSION )$ 7KH VHVVLRQ ZDV GHVWUR\HG 7HEN THE #ONTAINER INVALIDATES A SESSION BECAUSE THE SESSION TIMED OUT OR SOME PART OF THE APPLICATION CALLED THE SESSION S INVALIDATE METHOD

$WWULEXWHV

+WWS6HVVLRQ/LVWHQHU +WWS6HVVLRQ%LQGLQJ(YHQW

$Q DWWULEXWH ZDV DGGHG 7HEN SOME PART OF THE APP CALLS SET!TTRIBUTE ON THE SESSION $Q DWWULEXWH ZDV UHPRYHG 7HEN SOME PART OF THE APP CALLS REMOVE!TTRIBUTE ON THE SESSION $Q DWWULEXWH ZDV UHSODFHG 7HEN SOME PART OF THE APP CALLS SET!TTRIBUTE ON THE SESSION AND THE NAME OF THE ATTRIBUTE HAS ALREADY BEEN BOUND TO THE SESSION

+WWS6HVVLRQ$WWULEXWH/LVWHQHU

0LJUDWLRQ +WWS6HVVLRQ(YHQW

7KH VHVVLRQ LV DERXW WR EH SDVVLYDWHG 7HEN THE #ONTAINER IS ABOUT TO MIGRATE MOVE THE SESSION INTO A DIFFERENT 6- #ALLED BEFORE THE SESSION MOVES SO THAT ATTRIBUTES HAVE A CHANCE TO PREPARE THEMSELVES FOR MIGRATION 7KH VHVVLRQ KDV EHHQ DFWLYDWHG 7HEN THE #ONTAINER HAS JUST MIGRATED MOVED THE SESSION INTO A DIFFERENT 6- #ALLED BEFORE ANY OTHER PART OF THE APP CAN CALL GET!TTRIBUTE ON THE SESSION SO THE JUST MOVED ATTRIBUTES HAVE A CHANCE TO GET THEMSELVES READY FOR ACCESS

+WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

+WWS6HVVLRQ%LQGLQJ/LVWHQHU

,WVÂź\ NWZOM\ IJW]\ 0\\X;M[[QWV*QVLQVO4Q[\MVMZ 4HE EVENTS ON THE PREVIOUS PAGE ARE FOR KEY MOMENTS IN THE LIFE OF THE SESSION "UT THE (TTP3ESSION"INDING,ISTENER IS FOR KEY MOMENTS IN THE LIFE OF A SESSION ATTRIBUTE 2EMEMBER FROM CHAPTER WHERE WE LOOKED AT HOW YOU MIGHT USE THISˆIF FOR EXAMPLE YOUR ATTRIBUTE WANTS TO KNOW WHEN IT S ADDED TO A SESSION SO THAT IT CAN SYNCHRONIZE ITSELF WITH AN UNDERLYING DATABASE AND UPDATE THE DATABASE WHEN IT S REMOVED FROM A SESSION (ERE S A LITTLE REVIEW FROM THE PREVIOUS CHAPTER SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW KWWS

7KLV OLVWHQHU LV MXVW VR WKDW , FDQ Ă€ QG RXW ZKHQ ,¡P SXW LQWR D VHVVLRQ RU WDNHQ RXW ,W ZRQ¡W WHOO PH DQ\WKLQJ DERXW RWKHU VHVVLRQ HYHQWV

the e. ag ener is in This listervlet.http pack javax.s

SXEOLF FODVV 'RJ LPSOHPHQWV +WWS6HVVLRQ%LQGLQJ/LVWHQHU ^ SULYDWH 6WULQJ EUHHG SXEOLF 'RJ 6WULQJ EUHHG ^ WKLV EUHHG EUHHG ` SXEOLF 6WULQJ JHW%UHHG ^ UHWXUQ EUHHG `

This time the og attribute is an HttpSessionD ALSO listening for whBindingListener... en added or remove the Dog itself is d from a Session .

SXEOLF YRLG YDOXH%RXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ ,ÂśP LQ D VHVVLRQ `

The word ÒBou someone ADD ndÓ means attribute to aED this session.

SXEOLF YRLG YDOXH8QERXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ , DP QR ORQJHU SDUW RI D VHVVLRQ ` `

out You can ÞgurendÓ means. ou nb ÒU at wh

H VHVVLRQ <RX GR 127 FRQĂ€ JXU '' ELQGLQJ OLVWHQHUV LQ WKH SOHPHQWV WKH UH LP 'RJ FODVV KH ,I DQ DWWULEXWH FODVV OLNH WKH HQW U WKH &RQWDLQHU FDOOV WKH HY WHQH J/LV LQGLQ LRQ% 6HVV +WWS

ZKHQ RXQG DQG YDOXH8QERXQG

KDQGOLQJ FDOOEDFNV YDOXH% VHVVLRQ DGGHG WR RU UHPRYHG IURP D DQ LQVWDQFH RI WKLV FODVV LV RWKHU VHVVLRQ WKH H IRU 7 WUX LV 12 WKLV 7KDWÂśV LW ,W MXVW ZRUNV %XW VWHQHU LRXV SDJH +WWS6HVVLRQ/L UHODWHG OLVWHQHUV RQ WKH SUHY VVLRQ$FWLYDWLRQ/LVWHQHU WS6H G +W U DQ WHQH WH/LV +WWS6HVVLRQ$WWULEX VHVVLRQ VLQFH WKH\ÂśUH UHODWHG WR WKH PXVW EH UHJLVWHUHG LQ WKH '' Q WWULEXWH SODFHG LQ WKH VHVVLR XDO D GLYLG DQ LQ KDQ KHU W LWVHOI UDW

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

;M[[QWV UQOZI\QWV 2EMEMBER FROM THE PREVIOUS CHAPTER WE TALKED BRIEmY ABOUT DISTRIBUTED WEB APPS WHERE THE PIECES OF THE APP MIGHT BE REPLICATED ACROSS MULTIPLE NODES IN THE NETWORK )N A CLUSTERED ENVIRONMENT THE #ONTAINER MIGHT DO LOAD BALANCING BY TAKING CLIENT REQUESTS AND SENDING THEM OUT TO *6-S WHICH MAY OR MAY NOT BE ON DIFFERENT PHYSICAL BOXES BUT THAT DOESN T MATTER TO US 4HE POINT IS THE APP IS IN MULTIPLE PLACES 4HAT MEANS EACH TIME THE SAME CLIENT MAKES A REQUEST THE REQUEST COULD END UP GOING TO A DIFFERENT INSTANCE OF THE SAME SERVLET )N OTHER WORDS REQUEST ! FOR 3ERVLET ! COULD HAPPEN ON ONE 6- AND REQUEST " FOR 3ERVLET ! COULD END UP ON A DIFFERENT 6- 3O THE QUESTION IS WHAT HAPPENS TO THINGS LIKE 3ERVLET#ONTEXT 3ERVLET#ONlG AND (TTP3ESSION OBJECTS 3IMPLE ANSWER IMPORTANT IMPLICATIONS /NLY (TTP3ESSION OBJECTS AND THEIR ATTRIBUTES MOVE FROM ONE 6- TO ANOTHER 4HERE IS ONE 3ERVLET#ONTEXT PER 6- 4HERE IS ONE 3ERVLET#ONlG PER SERVLET PER 6- "UT THERE IS ONLY ONE (TTP3ESSION OBJECT FOR A GIVEN SESSION )$ PER WEB APP REGARDLESS OF HOW MANY 6- S THE APP IS DISTRIBUTED ACROSS

<PM *MMZ ?MJ )XX LQ[\ZQJ]\ML IKZW[[ \ _W >5[

ated. NOT duplic

90 6H

$

6HUYOHW $

%HHU :HE $SS

J UYO HW&RQĂ€

W&R

[W QWH

WS6HVVLRQ

6HUYOHW % 6H

%

6

HU YOH

$

+W J UYO HW&RQĂ€

90

[W W&RQWH

J UYO HW&RQĂ€

+W

6HUYOHW % 6H

%

$

6

HU YOH

6H

$

6HUYOHW $

%HHU :HE $SS

J UYO HW&RQĂ€

WS6HVVLRQ

Each servlet has its own ServletConĂžg, and both servlets in the web app share a ServletContext. Everything except the HttpSession is duplicated on the other VM.

Note: everything is duplicated in the second server EXCEPT the HttpSession objects! Sessions live in only ONE place at any given moment. The same session ID for a given web app will NEVER appear in two VMs at the same time. \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PLJUDWLRQ

;M[[QWV UQOZI\QWV QV IK\QWV (OW AN APP SERVER VENDOR HANDLES CLUSTERING AND WEB APP DISTRIBUTION VARIES WITH EACH VENDOR AND THERE S NO GUARANTEE IN THE * %% SPEC THAT A VENDOR HAS TO SUPPORT DISTRIBUTED APPS "UT THE PICTURE HERE GIVES YOU A HIGH LEVEL IDEA OF HOW IT WORKS 4HE KEY POINT IS THAT WHILE OTHER PARTS OF THE APP ARE REPLICATED ON EACH NODE 6- THE SESSION OBJECTS ARE MOVED !ND THAT IS GUARANTEED )N OTHER WORDS IF THE VENDOR DOES SUPPORT DISTRIBUTED APPS THEN THE #ONTAINER IS REQUIRED TO MIGRATE SESSIONS ACROSS 6-S !ND THAT INCLUDES MIGRATING SESSION ATTRIBUTES AS WELL

'LDQH VHOHFWV ´3DOHµ DQG KLWV WKH VXEPLW EXWWRQ

7KH /RDG %DODQFLQJ VHUYHU GHFLGHV WR VHQG WKH UHTXHVW WR &RQWDLQHU $ LQ 90 2QH

7KH &RQWDLQHU PDNHV D QHZ VHVVLRQ ,' 7KH ´-6(66,21,'µ FRRNLH LV VHQW EDFN WR 'LDQH LQ WKH UHVSRQVH QRW VKRZQ

90 6HUYOHW$

/RDG EDODQFLQJ 6HUYHU &RQWDLQHU

'LDQH VHOHFWV ´%LWWHUµ DQG KLWV WKH VXEPLW EXWWRQ +HU UHTXHVW DOVR LQFOXGHV WKH ´-6(66,21,'µ

&RQWDLQHU $

7KLV WLPH WKH /RDG %DODQFLQJ VHUYHU GHFLGHV WR VHQG WKH UHTXHVW WR &RQWDLQHU $ LQ 90 7ZR

+W

Q $

,'

WS6HVVLR

7KH &RQWDLQHU JHWV WKH UHTXHVW VHHV WKH VHVVLRQ ,' DQG UHDOL]HV WKDW WKH VHVVLRQ LV RQ D GLIIHUHQW 90 90 2QH

90 8K RK KHU VHVVLRQ LV RQ 90 2QH 7KDW VHVVLRQ PXVW PLJUDWH RYHU KHUH 6HUYOHW$

/RDG EDODQFLQJ 6HUYHU &RQWDLQHU

&RQWDLQHU $

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

7KH VHVVLRQ PLJUDWHV IURP 90 2QH WR 90 7ZR ,Q RWKHU ZRUGV LW QR ORQJHU H[LVWV RQ 90 2QH RQFH LW PRYHV WR 90 7ZR 7KLV PLJUDWLRQ PHDQV WKH VHVVLRQ ZDV SDVVLYDWHG RQ 90 2QH DQG DFWLYDWHG RQ 90 7ZR

passivates here

90 6HUYOHW$

&RQWDLQHU $

90 /RDG EDODQFLQJ 6HUYHU &RQWDLQHU

activates here ,' 6HUYOHW$

+W

Q $

WS6HVVLR

&RQWDLQHU $

7KH &RQWDLQHU PDNHV D QHZ WKUHDG IRU 6HUYOHW$ DQG DVVRFLDWHV WKH QHZ UHTXHVW ZLWK WKH UHFHQWO\ PRYHG VHVVLRQ

'LDQH·V QHZ UHTXHVW LV VHQW WR WKH WKUHDG DQG HYHU\ERG\ LV KDSS\ 'LDQH KDV QR LGHD ZKDW KDSSHQHG H[FHSW IRU WKH VOLJKW GHOD\ ODWHQF\ ZDLWLQJ IRU WKH VHVVLRQ WR PRYH

90 6HUYOHW$

/RDG EDODQFLQJ 6HUYHU &RQWDLQHU &RQWDLQHU $

Q $

,'

+W

WS6HVVLR

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

+WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU

0\\X;M[[QWV)K\Q^I\QWV4Q[\MVMZ TM\[ I\\ZQJ]\M[ XZMXIZM NWZ \PM JQO UW^M 3INCE IT S POSSIBLE THAT AN (TTP3ESSION CAN MIGRATE FROM ONE 6- TO ANOTHER THE SPEC DESIGNERS THOUGHT IT WOULD BE NICE IF SOMEONE BOTHERED TO TELL THE ATTRIBUTES WITHIN THE SESSION THAT THEY TOO WERE ABOUT TO MOVE 4HAT WAY THE ATTRIBUTES CAN MAKE SURE THEY LL SURVIVE THE TRIP

7KLV OLVWHQHU LV VR WKDW DV DQ DWWULEXWH , FDQ À QG RXW ZKHQ ,·P DERXW WR EH PRYHG WR D QHZ 90 DV SDUW RI D VHVVLRQ DQG , FDQ PDNH VXUH P\ LQVWDQFH YDULDEOHV DUH UHDG\

)F ALL YOUR ATTRIBUTES ARE STRAIGHTFORWARD 3ERIALIZABLE OBJECTS THAT DON T CARE WHERE THEY END UP YOU LL PROBABLY NEVER USE THIS LISTENER )N FACT WE RE GUESSING OF ALL WEB APPS NEVER USE THIS LISTENER "UT IT S THERE IF YOU NEED IT AND THE MOST LIKELY USE OF THIS LISTENER IS TO GIVE ATTRIBUTES A CHANCE TO MAKE THEIR INSTANCE VARIABLES READY FOR 3ERIALIZATION 6HVVLRQ PLJUDWLRQ DQG 6HULDOL]DWLRQ

LQWHUIDFH!! +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU

.OW IT GETS A LITTLE TRICKY

VHVVLRQ'LG$FWLYDWH +WWS6HVVLRQ(YHQW

! #ONTAINER IS REQUIRED TO MIGRATE 3ERIALIZABLE ATTRIBUTES WHICH ASSUMES THAT ALL INSTANCE VARIABLES WITHIN THE ATTRIBUTE ARE EITHER 3ERIALIZABLE OR NULL

VHVVLRQ:LOO3DVVLYDWH +WWS6HVVLRQ(YHQW

"UT A #ONTAINER IS NOT REQUIRED TO USE 3ERIALIZATION AS THE MEANS FOR MIGRATING THE (TTP3ESSION OBJECT

MDYD[ VHUYOHW KWWS +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU

7HAT DOES THIS MEAN TO YOU 3IMPLE MAKE SURE YOUR ATTRIBUTE CLASS TYPES ARE 3ERIALIZABLE AND YOU NEVER HAVE TO WORRY ABOUT IT "UT IF THEY RE NOT 3ERIALIZABLE WHICH COULD BE BECAUSE ONE OF THE ATTRIBUTE OBJECT S INSTANCE VARIABLES IS NOT 3ERIALIZABLE HAVE YOUR ATTRIBUTE OBJECT CLASS IMPLEMENT (TTP3ESSION!CTIVATION,ISTENER AND USE THE ACTIVATION PASSIVATION CALLBACKS TO WORK AROUND IT R LDOL]DWLRQ VR WKHUH·V Q (48,5(' WR XVH 6HU OOHG RQ D 7KH &RQWDLQHU LV QRW 5 ULWH2EMHFW ZLOO EH FD QG Z W D EMHF DG2 W UH JXDUDQWHH WKD ULDEOHV U RQH RI LWV LQVWDQFH YD 6HULDOL]DEOH DWWULEXWH R WR HULDOL]DEOH FDQ DOVR FKRRVH QWV 6 HPH LPSO WKDW DVV W L]DWLRQ \RX NQRZ WKDW D FO VHULDOL]HG DQG D ZULWH2EMHF FW LV REMH U DQ ,I \RX¶UH IDPLOLDU ZLWK 6HULDO QHYH ZKH IRU H[DPSOH HWKRG FDOOHG E\ WKH 90 FDQ XVH WKHVH PHWKRGV WR MHFW LPSOHPHQW D UHDG2EMHFW P OH RE L]DE HULDO $ 6 FW LV GHVHULDOL]HG WRUH WKH ¿ HOGV GXULQJ PHWKRG FDOOHG ZKHQ DQ REMH ZULWH2EMHFW

DQG WKHQ UHV RQ¶W ZRUU\ DERXW LW

WR QXOO GXULQJ 6HULDOL]DWLRQ H GHWDLOV RI 6HULDOL]DWLRQ G VHW QRQ 6HULDOL]DEOH ¿ HOGV LWK WK LDU Z DPLO 27 I ¶UH 1 YH DQG UHVWRUH

,I \RX WR VD MHFW HHG G2E RX Q UHD R LI \ DWLRQ RQ 6 ULDOL] GHVH VVLRQ PLJUDWL VVDULO\ EH FDOOHG GXULQJ VH H WZR HYHQW FDOO VH WK QG X HU D LVWHQ WLRQ/ %XW WKH PHWKRGV ZRQ¶W QHFH RQ$FWLYD XU DWWULEXWH XVH +WWS6HVVL EMHFW DQG ZULWH2EMHFW LQVWDQFH YDULDEOH VWDWH LQ \R

WKH ZD\ \RX¶G XVH UHDG2 DQG VHVVLRQ:LOO3DVVLYDWH EDFNV VHVVLRQ'LG$FWLYDWH

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

4Q[\MVMZ M`IUXTM[ /VER THE NEXT THREE PAGES PAY ATTENTION TO THE EVENT OBJECT TYPES AND TO WHETHER THE LISTENER IS ALSO AN ATTRIBUTE CLASS

6HVVLRQ FRXQWHU 4HIS LISTENER LETS YOU KEEP TRACK OF THE NUMBER OF ACTIVE SESSIONS IN THIS WEB APP 6ERY SIMPLE SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW KWWS SXEOLF FODVV %HHU6HVVLRQ&RXQWHU LPSOHPHQWV +WWS6HVVLRQ/LVWHQHU ^ VWDWLF SULYDWH LQW DFWLYH6HVVLRQV loyed

/classes in WEB-INFall servlets ep d so be s, ill This class we other web-app classecess this method. like all th helper classes can ac and other

SXEOLF VWDWLF LQW JHW$FWLYH6HVVLRQV ^ UHWXUQ DFWLYH6HVVLRQV ` SXEOLF YRLG VHVVLRQ&UHDWHG +WWS6HVVLRQ(YHQW HYHQW ^ DFWLYH6HVVLRQV `

These methods take an HttpSessionEvent.

SXEOLF YRLG VHVVLRQ'HVWUR\HG +WWS6HVVLRQ(YHQW HYHQW ^ DFWLYH6HVVLRQV ` `

&RQĂ€JXULQJ WKH OLVWHQHU LQ WKH '' ZHE DSS ! OLVWHQHU!

OLVWHQHU FODVV! FRP H[DPSOH %HHU6HVVLRQ&RXQWHU OLVWHQHU FODVV! OLVWHQHU! ZHE DSS!

if the appisisno correctly us k or w t nÕ ld ou ca e there s FYI- this w on multiple JVMs, bes in sync. If the clasve distributedep the static variableM, each class will ha way to ke on more than one JV unter variable. is loaded lue for the static co its own va \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ DWWULEXWH OLVWHQHU

4Q[\MVMZ M`IUXTM[ $WWULEXWH /LVWHQHU 4HIS LISTENER LETS YOU TRACK EACH TIME ANY ATTRIBUTE IS ADDED TO REMOVED FROM OR REPLACED IN A SESSION

nt uses inconsistelis This listener an tribute tener, At s itÕ Ñ ng mi na nding event. but it takes a Bi

SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW KWWS SXEOLF FODVV %HHU$WWULEXWH/LVWHQHU LPSOHPHQWV +WWS6HVVLRQ$WWULEXWH/LVWHQHU ^ SXEOLF YRLG DWWULEXWH$GGHG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ 6WULQJ QDPH HYHQW JHW1DPH 2EMHFW YDOXH HYHQW JHW9DOXH

ingEvent letths eyou HttpSessionBind d value of is event. an me na e th get iggered th attribute that tr

6\VWHP RXW SULQWOQ Âł$WWULEXWH DGGHG Âł QDPH Âł Âł YDOXH ` SXEOLF YRLG DWWULEXWH5HPRYHG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ 6WULQJ QDPH HYHQW JHW1DPH 2EMHFW YDOXH HYHQW JHW9DOXH 6\VWHP RXW SULQWOQ Âł$WWULEXWH UHPRYHG Âł QDPH Âł Âł YDOXH ` SXEOLF YRLG DWWULEXWH5HSODFHG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ 6WULQJ QDPH HYHQW JHW1DPH 2EMHFW YDOXH HYHQW JHW9DOXH 6\VWHP RXW SULQWOQ Âł$WWULEXWH UHSODFHG Âł QDPH Âł Âł YDOXH ` `

&RQĂ€JXULQJ WKH OLVWHQHU LQ WKH '' ZHE DSS ! OLVWHQHU!

OLVWHQHU FODVV! FRP H[DPSOH %HHU$WWULEXWH/LVWHQHU OLVWHQHU FODVV! OLVWHQHU! ZHE DSS!

4

(EY WHAT THE HECK ARE YOU PRINTING TO 7HERE DOES 3YSTEM OUT GO IN A WEB APP

$

7HEREVER THIS #ONTAINER CHOOSES TO SEND IT WHICH MAY OR MAY NOT BE CONFIGURABLE BY YOU )N OTHER WORDS IN A VENDOR SPECIFIC PLACE OFTEN A LOG FILE 4OMCAT PUTS THE OUTPUT IN TOMCAT LOGS CATALINA LOG 9OU LL HAVE TO READ YOUR SERVER DOCS TO FIND OUT WHAT YOUR #ON TAINER DOES WITH STANDARD OUTPUT

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

4Q[\MVMZ M`IUXTM[ $WWULEXWH FODVV OLVWHQLQJ IRU HYHQWV WKDW DIIHFW ,7

4HIS LISTENER LETS AN ATTRIBUTE KEEP TRACK OF EVENTS THAT MIGHT BE IMPORTANT TO THE ATTRIBUTE ITSELFˆWHEN IT S ADDED TO OR REMOVED FROM A SESSION AND WHEN THE SESSION MIGRATES FROM ONE 6- TO ANOTHER SDFNDJH FRP H[DPSOH LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD LR SXEOLF FODVV 'RJ LPSOHPHQWV +WWS6HVVLRQ%LQGLQJ/LVWHQHU +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU 6HULDOL]DEOH ^ SULYDWH 6WULQJ EUHHG LPDJLQH PRUH LQVWDQFH YDULDEOHV LQFOXGLQJ VRPH WKDW DUH QRW 6HULDOL]DEOH LPDJLQH FRQVWUXFWRU DQG RWKHU JHWWHU VHWWHU PHWKRGV SXEOLF YRLG YDOXH%RXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ ,ÂśP LQ D VHVVLRQ `

Session binding events.

SXEOLF YRLG YDOXH8QERXQG +WWS6HVVLRQ%LQGLQJ(YHQW HYHQW ^ FRGH WR UXQ QRZ WKDW , NQRZ , DP QR ORQJHU SDUW RI D VHVVLRQ ` SXEOLF YRLG VHVVLRQ:LOO3DVVLYDWH +WWS6HVVLRQ(YHQW HYHQW ^ FRGH WR JHW P\ QRQ 6HULDOL]DEOH Âż HOGV LQ D VWDWH WKDW FDQ VXUYLYH WKH PRYH WR D QHZ 90 ` SXEOLF YRLG VHVVLRQ'LG$FWLYDWH +WWS6HVVLRQ(YHQW HYHQW ^ FRGH WR UHVWRUH P\ Âż HOGV WR UHGR ZKDWHYHU , XQGLG LQ VHVVLRQ:LOO3DVVLYDWH

` `

&RQĂ€ JXULQJ WKH OLVWHQHU LQ WKH '' ZHE DSS ! OLVWHQHU!

OLVWHQHU FODVV! FRP H[DPSOH 'RJ OLVWHQHU FODVV! OLVWHQHU! ZHE DSS!

donÕt Remember, youtener lis e th y specif ainer type; the Cont t! ou it Þgures

Session activation events (but notice that the methods take an HttpSessionEvent).

LVWHQHU +WWS6HVVLRQ$FWLYDWLRQ/ Q WKH '' PXVW EH FRQĂ€ JXUHG L

Âż JXUHG LQ WKH '' EXW 7KLV 'RJ FODVV PXVW EH FRQ RQ DFWLYDWLRQ OLVWHQHU ,I LW 21/< EHFDXVH LW LV D VHVVL 6HVVLRQ%LQGLQJ/LVWHQHU LPSOHPHQWHG RQO\ WKH +WWS Âż JXUHG LQ WKH '' DQG FRQ 7 EH 12 RXOG LW Z LQWHUIDFH WDNH FDUH RI QRWLI\LQJ LW PSO\ OG VL ZRX LQHU RQWD WKH & WLRQ/LVWHQHU QHHG QRW EH DQ %XW DQ +WWS6HVVLRQ$FWLYD ÂśV LWV PRVW W\SLFDO XVH DWWULEXWH FODVV DOWKRXJK WKDW \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ OLVWHQHUV

;M[[QWV ZMTI\ML 4Q[\MVMZ[ 6FHQDULR

<RX ZDQW WR NQRZ KRZ PDQ\ FRQFXUUHQW XVHUV WKHUH DUH ,Q RWKHU ZRUGV \RX ZDQW WR WUDFN WKH DFWLYH VHVVLRQV

<RX ZDQW WR NQRZ ZKHQ D VHVVLRQ PRYHV IURP RQH 90 WR DQRWKHU

<RX KDYH DQ DWWULEXWH FODVV D FODVV IRU DQ REMHFW WKDW ZLOO EH XVHG DV DQ DWWULEXWH YDOXH DQG \RX ZDQW REMHFWV RI WKLV W\SH WR EH QRWLILHG ZKHQ WKH\ DUH ERXQG WR RU UHPRYHG IURP D VHVVLRQ <RX ZDQW WR NQRZ ZKHQ DQ\ VHVVLRQ DWWULEXWH LV DGGHG UHPRYHG RU UHSODFHG LQ D VHVVLRQ

/LVWHQHU LQWHUIDFH PHWKRGV +WWS6HVVLRQ/LVWHQHU MDYD[ VHUYOHW KWWS

(YHQW W\SH

+WWS6HVVLRQ(YHQW

VHVVLRQ'LG$FWLYDWH VHVVLRQ:LOO3DVVLYDWH

+WWS6HVVLRQ%LQGLQJ/LVWHQHU MDYD[ VHUYOHW KWWS

+WWS6HVVLRQ(YHQW

‰ $Q DWWULEXWH FODVV

s no speciÞc Note: therioeÕnActivationEvent. ss Se Http

‰ 6RPH RWKHU FODVV

+WWS6HVVLRQ%LQGLQJ(YHQW

‰ $Q DWWULEXWH FODVV ‰ 6RPH RWKHU FODVV

YDOXH%RXQG YDOXH8QERXQG

+WWS6HVVLRQ$WUULEXWH/LVWHQHU MDYD[ VHUYOHW KWWS

DWWULEXWH$GGHG DWWULEXWH5HPRYHG DWWULEXWH5HSODFHG

‰ $Q DWWULEXWH FODVV ‰ 6RPH RWKHU FODVV

VHVVLRQ&UHDWHG VHVVLRQ'HVWUR\HG

+WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU MDYD[ VHUYOHW KWWS

8VXDOO\ LPSOHPHQWHG E\

+WWS6HVVLRQ%LQGLQJ(YHQW

‰ $Q DWWULEXWH FODVV

s no speciÞc Note: therioeÕnAttributeEvent. HttpSess

‰ 6RPH RWKHU FODVV

UHODWHG HYHQWV GRQ¡W 6RPH RI WKH VHVVLRQ LQJ FRQYHQWLRQV IROORZ WKH HYHQW QDP RQ(YHQWV HVVL WWS6 NH + GV WD +WWS6HVVLRQ/LVWHQHU PHWKR RQ%LQGLQJ(YHQWV HU PHWKRGV WDNH +WWS6HVVL YHQWV +WWS6HVVLRQ%LQGLQJ/LVWHQ NH +WWS6HVVLRQ%LQGLQJ( GV WD HWKR HU P LVWHQ XWH/ V %XW +WWS6HVVLRQ$WWULE GV WDNH +WWS6HVVLRQ(YHQW HWKR QHU P /LVWH WLRQ ZHOO FWLYD $QG +WWS6HVVLRQ$ W FODVVHV ZRUNHG SHUIHFWO\ G +WWS6HVVLRQ%LQGLQJ(YHQ 6LQFH +WWS6HVVLRQ(YHQW DQ HV ODVV HQW F UH HY R PR , WR DGG WZ WKHUH ZDV QR QHHG IRU WKH $3

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

;M[[QWV ZMTI\ML -^MV\ 4Q[\MVMZ[ IVL -^MV\ 7JRMK\[ )81 W^MZ ^QM_ LQWHUIDFH!! +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU

VHVVLRQ'LG$FWLYDWH +WWS6HVVLRQ(YHQW

VHVVLRQ:LOO3DVVLYDWH +WWS6HVVLRQ(YHQW

LQWHUIDFH!! +WWS6HVVLRQ/LVWHQHU

VHVVLRQ&UHDWHG +WWS6HVVLRQ(YHQW

VHVVLRQ'HVWUR\HG +WWS6HVVLRQ(YHQW

+WWS6HVVLRQ(YHQW

JHW6HVVLRQ

+WWS6HVVLRQ%LQGLQJ(YHQW LQWHUIDFH!! +WWS6HVVLRQ%LQGLQJ/LVWHQHU

JHW6HVVLRQ

JHW1DPH

YDOXH%RXQG +WWS6HVVLRQ%LQGLQJ(YHQW

JHW9DOXH

YDOXH8QERXQG +WWS6HVVLRQ%LQGLQJ(YHQW

LQWHUIDFH!! +WWS6HVVLRQ$WWULEXWH/LVWHQHU

DWWULEXWH$GGHG +WWS6HVVLRQ%LQGLQJ(YHQW

DWWULEXWH5HPRYHG +WWS6HVVLRQ%LQGLQJ(YHQW

DWWULEXWH5HSODFHG +WWS6HVVLRQ%LQGLQJ(YHQW

The getName() method returns the String name of the attribute that triggered the event. The getValue() method returns the object value of the attribute that triggered the event. Watch out! It returns the old value, not the new one. In other words, it returns the value the attribute had BEFORE the change that triggered the event!

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ OLVWHQHUV

6KDUSHQ \RXU SHQFLO

;M[[QWV ZMTI\ML 4Q[\MVMZ[ <HV WKLV LV DOPRVW DQ H[DFW FRS\ RI WKH WDEOH IURP WZR SDJHV EDFN VR GRQ·W JR WKHUH 7U\ WR WKLQN WKURXJK WKHVH OLVWHQHUV DQG SXW GRZQ \RXU EHVW JXHVV <RX FDQ H[SHFW DW OHDVW WZR DQG DV PDQ\ DV IRXU TXHVWLRQV RQ WKH H[DP DERXW VHVVLRQ OLVWHQHUV 8VH ERWK \RXU PHPRU\ DQG FRPPRQ VHQVH WR ILOO WKLV RXW

6FHQDULR

<RX ZDQW WR NQRZ ZKHQ D VHVVLRQ LV FUHDWHG

/LVWHQHU LQWHUIDFH PHWKRGV

(YHQW W\SH

8VXDOO\ LPSOHPHQWHG E\

‰ $Q DWWULEXWH FODVV ‰ 6RPH RWKHU FODVV

$Q DWWULEXWH ZDQWV WR NQRZ ZKHQ LW KDV EHHQ PRYHG LQWR D QHZ 90

‰ $Q DWWULEXWH FODVV

$Q DWWULEXWH ZDQWV WR NQRZ ZKHQ LW KDV EHHQ UHSODFHG LQ D VHVVLRQ

‰ $Q DWWULEXWH FODVV

<RX ZDQW WR EH QRWLILHG ZKHQHYHU DQ\WKLQJ LV ERXQG WR D VHVVLRQ

‰ $Q DWWULEXWH FODVV

‰ 6RPH RWKHU FODVV

‰ 6RPH RWKHU FODVV

+LQW WKHUH DUH RQO\ WZR (YHQW REMHFW W\SHV

‰ 6RPH RWKHU FODVV

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

0RFN ([DP &KDSWHU

'IVEN SXEOLF FODVV 0\6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHT +WWS6HUYOHW5HVSRQVH UHV WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHTXHVW JHW6HVVLRQ VHW$WWULEXWH ³NH\´ ³YDOXH´ UHTXHVW JHW+WWS6HVVLRQ VHW$WWULEXWH ³NH\´ ³YDOXH´ +WWS6HVVLRQ UHTXHVW JHW6HVVLRQ

VHW$WWULEXWH ³NH\´ ³YDOXH´ +WWS6HVVLRQ UHTXHVW JHW+WWS6HVVLRQ

VHW$WWULEXWH ³NH\´ ³YDOXH´ ` ` 7HICH LINE S COULD BE UNCOMMENTED WITHOUT CAUSING A COMPILE OR RUNTIME ERROR #HOOSE ALL THAT APPLY

R ! ,INE ONLY R " ,INE ONLY R # ,INE ONLY R $ ,INE ONLY R % ,INE OR LINE R & ,INE OR LINE

)F A CLIENT WILL ./4 ACCEPT A COOKIE WHICH SESSION MANAGEMENT MECHANISM COULD THE WEB CONTAINER EMPLOY #HOOSE ONE

R ! #OOKIES BUT ./4 52, REWRITING R " 52, REWRITING BUT ./4 COOKIES R # %ITHER COOKIES OR 52, REWRITING CAN BE USED R $ .EITHER COOKIES NOR 52, REWRITING CAN BE USED R % #OOKIES AND 52, REWRITING MUST BE USED TOGETHER

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN H[DP

7HICH STATEMENTS ABOUT +WWS6HVVLRQ OBJECTS ARE TRUE #HOOSE ALL THAT APPLY

R ! ! SESSION WHOSE TIMEOUT PERIOD HAS BEEN SET TO WILL NEVER EXPIRE

R " ! SESSION WILL BECOME INVALID AS SOON AS THE USER CLOSES ALL BROWSER WINDOWS

R # ! SESSION WILL BECOME INVALID AFTER A TIMEOUT PERIOD DElNED BY THE SERVLET CONTAINER

R $ ! SESSION MAY BE EXPLICITLY INVALIDATED BY CALLING +WWS6HVVLRQ LQYDOLGDWH6HVVLRQ

7HICH OF THE FOLLOWING ARE ./4 LISTENER EVENT TYPES IN THE * %% !0) #HOOSE ALL THAT APPLY

R ! +WWS6HVVLRQ(YHQW R " 6HUYOHW5HTXHVW(YHQW R # +WWS6HVVLRQ%LQGLQJ(YHQW R $ +WWS6HVVLRQ$WWULEXWH(YHQW R % 6HUYOHW&RQWH[W$WWULEXWH(YHQW

7HICH STATEMENTS ABOUT SESSION TRACKING ARE TRUE #HOOSE ALL THAT APPLY

R ! 52, REWRITING MAY BE USED BY A SERVER AS THE BASIS FOR SESSION TRACKING

R " 33, HAS A BUILT IN MECHANISM THAT A SERVLET CONTAINER COULD USE TO OBTAIN DATA USED TO DElNE A SESSION

R # 7HEN USING COOKIES FOR SESSION TRACKING THERE IS NO RESTRICTION ON THE NAME OF THE SESSION TRACKING COOKIE

R $ 7HEN USING COOKIES FOR SESSION TRACKING THE NAME OF THE SESSION TRACKING COOKIE MUST BE -6(66,21,'

R % )F A USER HAS COOKIES DISABLED IN THEIR BROWSER THE CONTAINER MAY CHOOSE TO USE A MDYD[ VHUYOHW KWWS &RRNLHOHVV+WWS6HVVLRQ OBJECT TO TRACK THE USER S SESSION

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

'IVEN LPSRUW MDYD[ VHUYOHW KWWS SXEOLF FODVV 0\6HVVLRQ/LVWHQHU LPSOHPHQWV +WWS6HVVLRQ/LVWHQHU ^ SXEOLF YRLG VHVVLRQ&UHDWHG ^ 6\VWHP RXW SULQWOQ ³6HVVLRQ &UHDWHG´ ` SXEOLF YRLG VHVVLRQ'HVWUR\HG ^ 6\VWHP RXW SULQWOQ ³6HVVLRQ 'HVWUR\HG´ ` ` 7HAT IS WRONG WITH THIS CLASS #HOOSE ALL THAT APPLY

R ! 4HE METHOD SIGNATURE ON LINE IS ./4 CORRECT R " 4HE METHOD SIGNATURE ON LINE IS ./4 CORRECT R # 4HE IMPORT STATEMENT WILL ./4 IMPORT THE +WWS6HVVLRQ/LVWHQHU INTERFACE

R $ VHVVLRQ&UHDWHG AND VHVVLRQ'HVWUR\HG ARE ./4 THE ONLY METHODS DElNED BY THE +WWS6HVVLRQ/LVWHQHU INTERFACE

7HICH STATEMENTS ABOUT SESSION ATTRIBUTES ARE TRUE #HOOSE ALL THAT APPLY

R ! 4HE RETURN TYPE OF +WWS6HVVLRQ JHW$WWULEXWH 6WULQJ IS 2EMHFW

R " 4HE RETURN TYPE OF +WWS6HVVLRQ JHW$WWULEXWH 6WULQJ IS 6WULQJ

R # !TTRIBUTES BOUND INTO A SESSION ARE AVAILABLE TO ANY OTHER SERVLET THAT BELONGS TO THE SAME 6HUYOHW&RQWH[W

R $ #ALLING VHW$WWULEXWH ³NH\$´ ³YDOXH%´ ON AN +WWS6HVVLRQ WHICH ALREADY HOLDS A VALUE FOR THE KEY NH\$ WILL CAUSE AN EXCEPTION TO BE THROWN

R % #ALLING VHW$WWULEXWH ³NH\$´ ³YDOXH%´ ON AN +WWS6HVVLRQ WHICH ALREADY HOLDS A VALUE FOR THE KEY NH\$ WILL CAUSE THE PREVIOUS VALUE FOR THIS ATTRIBUTE TO BE REPLACED WITH THE 3TRING YDOXH%

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN H[DP

7HICH INTERFACES DElNE A JHW6HVVLRQ METHOD #HOOSE ALL THAT APPLY

R ! 6HUYOHW5HTXHVW R " 6HUYOHW5HVSRQVH R # +WWS6HUYOHW5HTXHVW R $ +WWS6HUYOHW5HVSRQVH

'IVEN A SESSION OBJECT V AND THE CODE V VHW$WWULEXWH ³NH\´ YDOXH 7HICH LISTENERS COULD BE NOTIlED #HOOSE ONE

R ! /NLY +WWS6HVVLRQ/LVWHQHU R " /NLY +WWS6HVVLRQ%LQGLQJ/LVWHQHU R # /NLY +WWS6HVVLRQ$WWULEXWH/LVWHQHU R $ +WWS6HVVLRQ/LVWHQHU AND +WWS6HVVLRQ%LQGLQJ/LVWHQHU

R % +WWS6HVVLRQ/LVWHQHU AND +WWS6HVVLRQ$WWULEXWH/LVWHQHU

R & +WWS6HVVLRQ%LQGLQJ/LVWHQHU AND +WWS6HVVLRQ$WWULEXWH/LVWHQHU

R ' !LL THREE

'IVEN THAT UHT IS AN +WWS6HUYOHW5HTXHVW WHICH SNIPPETS CREATE A SESSION IF ONE DOESN T EXIST #HOOSE ALL THAT APPLY

R ! UHT JHW6HVVLRQ R " UHT JHW6HVVLRQ WUXH R # UHT JHW6HVVLRQ IDOVH R $ UHT FUHDWH6HVVLRQ R % UHT JHW1HZ6HVVLRQ R & UHT FUHDWH6HVVLRQ WUXH R ' UHT FUHDWH6HVVLRQ IDOVH

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

'IVEN A SESSION OBJECT V WITH TWO ATTRIBUTES NAMED P\$WWU AND P\$WWU WHICH WILL REMOVE BOTH ATTRIBUTES FROM THIS SESSION #HOOSE ALL THAT APPLY

R ! V UHPRYH$OO9DOXHV R " V UHPRYH$WWULEXWH ³P\$WWU ´ V UHPRYH$WWULEXWH ³P\$WWU ´

R # V UHPRYH$OO$WWULEXWHV R $ V JHW$WWULEXWH ³P\$WWU ´ 81%,1' V JHW$WWULEXWH ³P\$WWU ´ 81%,1'

R % V JHW$WWULEXWH1DPHV 81%,1'

7HICH STATEMENTS ABOUT +WWS6HVVLRQ OBJECTS IN DISTRIBUTED ENVIRONMENTS ARE TRUE #HOOSE ALL THAT APPLY

R ! 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER ANY ATTRIBUTES STORED IN THE SESSION WILL BE LOST

R " 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER APPROPRIATELY REGISTERED +WWS6HVVLRQ%LQGLQJ/LVWHQHU OBJECTS WILL BE NOTIlED

R # 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER APPROPRIATELY REGISTERED +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU OBJECTS WILL BE NOTIlED

R $ 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER ATTRIBUTE VALUES THAT IMPLEMENT MDYD LR 6HULDOL]DEOH WILL BE TRANSFERRED TO THE NEW *6-

7HICH STATEMENTS ABOUT SESSION TIMEOUTS ARE TRUE #HOOSE ALL THAT APPLY

R ! 3ESSION TIMEOUT DECLARATIONS MADE IN THE $$ CAN SPECIFY TIME IN SECONDS

R " 3ESSION TIMEOUT DECLARATIONS MADE IN THE $$ CAN SPECIFY TIME IN MINUTES

R # 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN SPECIFY TIME ONLY IN SECONDS

R $ 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN SPECIFY TIME ONLY IN MINUTES

R % 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN SPECIFY TIME IN EITHER MINUTES OR SECONDS \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN H[DP

#HOOSE THE SERVLET CODE FRAGMENT THAT WOULD RETRIEVE FROM THE REQUEST THE VALUE OF A COOKIE NAMED h/2!?5)$v #HOOSE ALL THAT APPLY

R ! 6WULQJ YDOXH UHTXHVW JHW&RRNLH ³25$B8,'´ R " 6WULQJ YDOXH UHTXHVW JHW+HDGHU ³25$B8,'´ R # MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV 6WULQJ F1DPH QXOO 6WULQJ YDOXH QXOO LI FRRNLHV QXOO ^ IRU LQW L L FRRNLHV OHQJWK L ^ F1DPH FRRNLHV>L@ JHW1DPH LI F1DPH QXOO F1DPH HTXDOV,JQRUH&DVH ³25$B8,'´

^ YDOXH FRRNLHV>L@ JHW9DOXH ` ` `

R $ MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV LI FRRNLHV OHQJWK ! ^ 6WULQJ YDOXH FRRNLHV> @ JHW9DOXH `

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

(OW WOULD YOU USE THE +WWS6HUYOHW5HVSRQVH OBJECT IN A SERVLET TO ADD A COOKIE TO THE CLIENT R ! FRQWH[W SDUDP! SDUDP QDPH!P\&RRNLH SDUDP QDPH! SDUDP YDOXH!FRRNLH9DOXH SDUDP YDOXH! FRQWH[W SDUDP!

R " UHVSRQVH DGG&RRNLH ³P\&RRNLH´ ´FRRNLH9DOXH´ R # MDYD[ VHUYOHW KWWS &RRNLH QHZ&RRN QHZ MDYD[ VHUYOHW KWWS &RRNLH ³P\&RRNLH´ ´FRRNLH9DOXH´ VHW RWKHU &RRNLH SURSHUWLHV UHVSRQVH DGG&RRNLH QHZ&RRN

R $ MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV 6WULQJ FQDPH QXOO LI FRRNLHV QXOO ^ IRU LQW L L FRRNLHV OHQJWK L ^ F1DPH FRRNLHV>L@ JHW1DPH LI F1DPH QXOO F1DPH HTXDOV,JQRUH&DVH ³P\&RRNLH´

^ RXW SULQWOQ F1DPH ³ ³ FRRNLHV>L@ JHW9DOXH ` ` `

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN DQVZHUV

&KDSWHU $QVZHUV 59) (Servlet Spec p.

'IVEN

SXEOLF FODVV 0\6HUYOHW H[WHQGV +WWS6HUYOHW ^ SXEOLF YRLG GR*HW +WWS6HUYOHW5HTXHVW UHT +WWS6HUYOHW5HVSRQVH UHV WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHTXHVW JHW6HVVLRQ VHW$WWULEXWH ³NH\´ ³YDOXH´ UHTXHVW JHW+WWS6HVVLRQ VHW$WWULEXWH ³NH\´ ³YDOXH´ +WWS6HVVLRQ UHTXHVW JHW6HVVLRQ

VHW$WWULEXWH ³NH\´ ³YDOXH´ +WWS6HVVLRQ UHTXHVW JHW+WWS6HVVLRQ

VHW$WWULEXWH ³NH\´ ³YDOXH´ ` ` 7HICH LINE S COULD BE UNCOMMENTED WITHOUT CAUSING A COMPILE OR RUNTIME ERROR #HOOSE ALL THAT APPLY

R ! ,INE ONLY R " ,INE ONLY R # ,INE ONLY R $ ,INE ONLY lines 13 and R % ,INE OR LINE -Option E is correct because both cast to 15 make the correct method call. Thedoes reĂ&#x;ect R & ,INE OR LINE HttpSession is NOT necessary, but it

the correct type, so it is valid.

)F A CLIENT WILL ./4 ACCEPT A COOKIE WHICH SESSION MANAGEMENT MECHANISM COULD THE WEB CONTAINER EMPLOY #HOOSE ONE

(Servlet v2.4 pg. 57)

R ! #OOKIES BUT ./4 52, REWRITING -Option B is correct because R " 52, REWRITING BUT ./4 COOKIES cookies CANNOT be used, but R # %ITHER COOKIES OR 52, REWRITING CAN BE USED URL rewriting does NOT depend . R $ .EITHER COOKIES NOR 52, REWRITING CAN BE USED on cookies being enabled R % #OOKIES AND 52, REWRITING MUST BE USED TOGETHER

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

(Servlet v2.4 p.

7HICH STATEMENTS ABOUT +WWS6HVVLRQ OBJECTS ARE TRUE #HOOSE ALL THAT APPLY

59)

R ! ! SESSION WHOSE TIMEOUT PERIOD HAS BEEN SET TO WILL NEVER EXPIRE

R " ! SESSION WILL BECOME INVALID AS SOON AS THE USER CLOSES ALL BROWSER WINDOWS

use -Option B is incorrect beca there is no explicit termination signal in the HTTP protocol.

R # ! SESSION WILL BECOME INVALID AFTER A TIMEOUT PERIOD DElNED BY THE SERVLET CONTAINER

R $ ! SESSION MAY BE EXPLICITLY INVALIDATED BY CALLING +WWS6HVVLRQ LQYDOLGDWH6HVVLRQ

-Option D is incorrect because the method that should be used is called invalidate().

7HICH OF THE FOLLOWING ARE ./4 LISTENER EVENT TYPES IN THE * %% !0) #HOOSE ALL THAT APPLY

(API)

R ! +WWS6HVVLRQ(YHQW R " 6HUYOHW5HTXHVW(YHQW R # +WWS6HVVLRQ%LQGLQJ(YHQW -HttpSessionBindingEvents are used for both HttpSessionBindingListeners AND R $ +WWS6HVVLRQ$WWULEXWH(YHQW HttpSessionAttributeListeners. R % 6HUYOHW&RQWH[W$WWULEXWH(YHQW

7HICH STATEMENTS ABOUT SESSION TRACKING ARE TRUE #HOOSE ALL THAT APPLY

(Servlet v2.4 p.

57)

R ! 52, REWRITING MAY BE USED BY A SERVER AS THE BASIS FOR SESSION TRACKING

R " 33, HAS A BUILT IN MECHANISM THAT A SERVLET CONTAINER COULD USE TO OBTAIN DATA USED TO DElNE A SESSION

R # 7HEN USING COOKIES FOR SESSION TRACKING THERE IS NO RESTRICTION ON THE NAME OF THE SESSION TRACKING COOKIE

R $ 7HEN USING COOKIES FOR SESSION TRACKING THE NAME OF THE

-Option C is incorrect because the speciĂžcation dictates that the session tracking cookie must be JSESSIONID.

SESSION TRACKING COOKIE MUST BE -6(66,21,'

R % )F A USER HAS COOKIES DISABLED IN THEIR BROWSER THE CONTAINER MAY CHOOSE TO USE A MDYD[ VHUYOHW KWWS &RRNLHOHVV+WWS6HVVLRQ OBJECT TO TRACK THE USER S SESSION

-Option E is incorrect because there is no such class. \RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN DQVZHUV

(Servlet v2.4 p.

'IVEN LPSRUW MDYD[ VHUYOHW KWWS SXEOLF FODVV 0\6HVVLRQ/LVWHQHU LPSOHPHQWV +WWS6HVVLRQ/LVWHQHU ^ SXEOLF YRLG VHVVLRQ&UHDWHG ^ 6\VWHP RXW SULQWOQ ³6HVVLRQ &UHDWHG´ ` SXEOLF YRLG VHVVLRQ'HVWUR\HG ^ 6\VWHP RXW SULQWOQ ³6HVVLRQ 'HVWUR\HG´ ` ` 7HAT IS WRONG WITH THIS CLASS #HOOSE ALL THAT APPLY

R ! 4HE METHOD SIGNATURE ON LINE IS ./4 CORRECT R " 4HE METHOD SIGNATURE ON LINE IS ./4 CORRECT R # 4HE IMPORT STATEMENT WILL ./4 IMPORT THE

276)

-Options A and B are correct because these methods should have an HttpSessionEvent parameter.

+WWS6HVVLRQ/LVWHQHU INTERFACE

- Option C is incorrect because the listener is deĂžned in the imported package.

R $ VHVVLRQ&UHDWHG AND VHVVLRQ'HVWUR\HG ARE ./4 THE ONLY METHODS DElNED BY THE +WWS6HVVLRQ/LVWHQHU INTERFACE

-Option D is incorrect because these are the only two methods in this interface.

(Servlet v2.4 p.

7HICH STATEMENTS ABOUT SESSION ATTRIBUTES ARE TRUE #HOOSE ALL THAT APPLY

59)

R ! 4HE RETURN TYPE OF +WWS6HVVLRQ JHW$WWULEXWH 6WULQJ IS 2EMHFW

R " 4HE RETURN TYPE OF +WWS6HVVLRQ JHW$WWULEXWH 6WULQJ IS 6WULQJ

ect -Option B is incorr because the return type is Object.

R # !TTRIBUTES BOUND INTO A SESSION ARE AVAILABLE TO ANY OTHER SERVLET THAT BELONGS TO THE SAME 6HUYOHW&RQWH[W

R $ #ALLING VHW$WWULEXWH ³NH\$´ ³YDOXH%´ ON AN +WWS6HVVLRQ -Option D is incorrect WHICH ALREADY HOLDS A VALUE FOR THE KEY NH\$ WILL CAUSE AN EXCEPTION TO BE THROWN

because this call will simply replace the existing value.

R % #ALLING VHW$WWULEXWH ³NH\$´ ³YDOXH%´ ON AN +WWS6HVVLRQ WHICH ALREADY HOLDS A VALUE FOR THE KEY NH\$ WILL CAUSE THE PREVIOUS VALUE FOR THIS ATTRIBUTE TO BE REPLACED WITH THE 3TRING YDOXH%

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

. 243)

(Servlet v2.4 pg

7HICH INTERFACES DElNE A JHW6HVVLRQ METHOD #HOOSE ALL THAT APPLY

R ! 6HUYOHW5HTXHVW R " 6HUYOHW5HVSRQVH R # +WWS6HUYOHW5HTXHVW R $ +WWS6HUYOHW5HVSRQVH

(Servlet v2.4 pg

'IVEN A SESSION OBJECT V AND THE CODE V VHW$WWULEXWH ³NH\´ YDOXH

. 80)

7HICH LISTENERS COULD BE NOTIlED #HOOSE ONE

R ! /NLY +WWS6HVVLRQ/LVWHQHU R " /NLY +WWS6HVVLRQ%LQGLQJ/LVWHQHU R # /NLY +WWS6HVVLRQ$WWULEXWH/LVWHQHU R $ +WWS6HVVLRQ/LVWHQHU AND +WWS6HVVLRQ%LQGLQJ/LVWHQHU

R % +WWS6HVVLRQ/LVWHQHU AND +WWS6HVVLRQ$WWULEXWH/LVWHQHU

R & +WWS6HVVLRQ%LQGLQJ/LVWHQHU AND +WWS6HVVLRQ$WWULEXWH/LVWHQHU

R ' !LL THREE

-Option F is correct because an HttpSessionAttributeListener is notiĂžed any time an attribute is added and the value object will also be notiĂžed if it . implements an HttpSessionBindingListener

'IVEN THAT UHT IS AN +WWS6HUYOHW5HTXHVW WHICH SNIPPETS CREATE A SESSION IF ONE DOESN T EXIST #HOOSE ALL THAT APPLY

R ! UHT JHW6HVVLRQ R " UHT JHW6HVVLRQ WUXH R # UHT JHW6HVVLRQ IDOVH R $ UHT FUHDWH6HVVLRQ R % UHT JHW1HZ6HVVLRQ R & UHT FUHDWH6HVVLRQ WUXH R ' UHT FUHDWH6HVVLRQ IDOVH

(API)

-Options A and B will each create a new session if one doesnÕt exist. getSession(false) returns a null if the session doesnÕt exist.

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN DQVZHUV

'IVEN A SESSION OBJECT V WITH TWO ATTRIBUTES NAMED P\$WWU AND P\$WWU WHICH WILL REMOVE BOTH ATTRIBUTES FROM THIS SESSION #HOOSE ALL THAT APPLY

R ! V UHPRYH$OO9DOXHV R " V UHPRYH$WWULEXWH ³P\$WWU ´ V UHPRYH$WWULEXWH ³P\$WWU ´

R # V UHPRYH$OO$WWULEXWHV R $ V JHW$WWULEXWH ³P\$WWU ´ 81%,1'

(API)

-Option B is correct, removeAttribute() is the only way to remove attributes from a session object, and it removes only one attribute at a time.

V JHW$WWULEXWH ³P\$WWU ´ 81%,1'

R % V JHW$WWULEXWH1DPHV 81%,1'

. 60)

(Servlet v2.4 pg

7HICH STATEMENTS ABOUT +WWS6HVVLRQ OBJECTS IN DISTRIBUTED ENVIRONMENTS ARE TRUE #HOOSE ALL THAT APPLY

A is incorrect because R ! 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER ANY ATTRIBUTES -Optionable serializ attributes will be STORED IN THE SESSION WILL BE LOST

R " 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER APPROPRIATELY REGISTERED +WWS6HVVLRQ%LQGLQJ/LVWHQHU OBJECTS WILL BE NOTIlED

R # 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER APPROPRIATELY

transferred.

-Option B is incorrect since attributes remain bound to the session.

REGISTERED +WWS6HVVLRQ$FWLYDWLRQ/LVWHQHU OBJECTS WILL BE NOTIlED

R $ 7HEN A SESSION IS MOVED FROM ONE *6- TO ANOTHER ATTRIBUTE VALUES THAT IMPLEMENT MDYD LR 6HULDOL]DEOH WILL BE TRANSFERRED TO THE NEW *6-

7HICH STATEMENTS ABOUT SESSION TIMEOUTS ARE TRUE #HOOSE ALL THAT APPLY

(API)

R ! 3ESSION TIMEOUT DECLARATIONS MADE IN THE $$ CAN SPECIFY TIME IN SECONDS

R " 3ESSION TIMEOUT DECLARATIONS MADE IN THE $$ CAN SPECIFY TIME IN MINUTES

R # 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN SPECIFY TIME ONLY IN SECONDS

R $ 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN

-In the DD, using the <session-timeout> element, only minutes can be speciÞed, using HttpSessionÕs setMaxInactiveInterval() only seconds can be speciÞed.

SPECIFY TIME ONLY IN MINUTES

R % 3ESSION TIMEOUT DECLARATIONS MADE PROGRAMMATICALLY CAN SPECIFY TIME IN EITHER MINUTES OR SECONDS

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

VHVVLRQ PDQDJHPHQW

#HOOSE THE SERVLET CODE FRAGMENT THAT WOULD RETRIEVE FROM THE REQUEST THE VALUE OF A COOKIE NAMED h/2!?5)$v #HOOSE ALL THAT APPLY

(API)

R ! 6WULQJ YDOXH UHTXHVW JHW&RRNLH Âł25$B8,'´ - Option A refers to a that doesnĂ•t exist. R " 6WULQJ YDOXH UHTXHVW JHW+HDGHU Âł25$B8,'´ method R # MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV 6WULQJ F1DPH QXOO 6WULQJ YDOXH QXOO LI FRRNLHV QXOO ^ IRU LQW L L FRRNLHV OHQJWK L ^

kie - Option C gets a Coo array using requestn .checks for getCookies(), the iĂžed name. a Cookie of a spec

F1DPH FRRNLHV>L@ JHW1DPH LI F1DPH QXOO F1DPH HTXDOV,JQRUH&DVH ³25$B8,'´

^ YDOXH FRRNLHV>L@ JHW9DOXH ` ` `

R $ MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV

- Option D only looks at the Ăžrst Cookie in the array.

LI FRRNLHV OHQJWK ! ^ 6WULQJ YDOXH FRRNLHV> @ JHW9DOXH `

\RX DUH KHUH

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 6. Conversational state

PRFN DQVZHUV

(OW WOULD YOU USE THE +WWS6HUYOHW5HVSRQVH OBJECT IN A SERVLET TO ADD A COOKIE TO THE CLIENT R ! FRQWH[W SDUDP!

( API )

SDUDP QDPH!P\&RRNLH SDUDP QDPH! SDUDP YDOXH!FRRNLH9DOXH SDUDP YDOXH! FRQWH[W SDUDP!

R " UHVSRQVH DGG&RRNLH ³P\&RRNLH´ ´FRRNLH9DOXH´ R # MDYD[ VHUYOHW KWWS &RRNLH QHZ&RRN

cause not correcttabe sa ke -Option Booiskie od th the addC ct, me t Strings.. no je ob ie ok Co

QHZ MDYD[ VHUYOHW KWWS &RRNLH ³P\&RRNLH´ ´FRRNLH9DOXH´ VHW RWKHU &RRNLH SURSHUWLHV UHVSRQVH DGG&RRNLH QHZ&RRN

R $ MDYD[ VHUYOHW KWWS &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV 6WULQJ FQDPH QXOO LI FRRNLHV QXOO ^ IRU LQW L L FRRNLHV OHQJWK L ^ F1DPH FRRNLHV>L@ JHW1DPH LI F1DPH QXOO F1DPH HTXDOV,JQRUH&DVH ³P\&RRNLH´

^ RXW SULQWOQ F1DPH ³ ³ FRRNLHV>L@ JHW9DOXH ` ` `

-Option D is not correct because it shows servlet code retrieving, not creating, a cookie.

FKDSWHU

Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 7. Being a JSP

XVLQJ -63

%HLQJ D -63 +H GRHVQ·W NQRZ D GLUHFWLYH IURP D VFULSWOHW EXW +( JHWV WKH FRUQHU RIÀ FH DQG WKH $HURQ DQG WKH WZLFH D ZHHN PDVVDJH" ,·YH KDG LW

5HOD[ ZKHQ KH IDLOV WKH H[DP ZH %27+ NQRZ ZKDW ZLOO KDSSHQ , MXVW KRSH WKH\ GRQ·W JHW EORRG RQ WKH $HURQ

$ -63 EHFRPHV D VHUYOHW $ VHUYOHW WKDW \RX GRQ¶W FUHDWH 7KH &RQWDLQHU ORRNV DW \RXU -63 WUDQVODWHV LW LQWR -DYD VRXUFH FRGH DQG FRPSLOHV LW LQWR D IXOO À HGJHG -DYD VHUYOHW FODVV %XW \RX¶YH JRW WR NQRZ ZKDW KDSSHQV ZKHQ WKH FRGH \RX ZULWH LQ WKH -63 LV WXUQHG LQWR -DYD FRGH <RX FDQ ZULWH -DYD FRGH LQ \RXU -63 EXW VKRXOG \RX" $QG LI \RX GRQ¶W ZULWH -DYD FRGH WKHQ ZKDW GR \RX ZULWH" +RZ GRHV LW WUDQVODWH LQWR -DYD FRGH" ,Q WKLV FKDSWHU ZH¶OO ORRN DW VL[ GLIIHUHQW NLQGV RI -63 HOHPHQWV²HDFK ZLWK LWV RZQ SXUSRVH DQG \HV XQLTXH V\QWD[ <RX¶OO OHDUQ KRZ ZK\ DQG ZKDW WR ZULWH LQ \RXU -63 3HUKDSV PRUH LPSRUWDQWO\ \RX¶OO OHDUQ ZKDW QRW WR ZULWH LQ \RXU -63

WKLV LV D QHZ FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

RI¿FLDO 6XQ H[DP REMHFWLYHV

7KH -63 7HFKQRORJ\ 0RGHO

&RYHUDJH 1RWHV

Copyright Safari Books Online #729515 ,GHQWLI\ GHVFULEH RU ZULWH -63 FRGH IRU WKH IROORZLQJ

HOHPHQWV D WHPSODWH WH[W E VFULSWLQJ HOHPHQWV FRPPHQWV GLUHFWLYHV GHFODUDWLRQV VFULSWOHWV DQG H[SUHVVLRQV F VWDQGDUG DQG FXVWRP DFWLRQV DQG G H[SUHVVLRQ ODQJXDJH HOHPHQWV

:ULWH -63 FRGH WKDW XVHV WKH GLUHFWLYHV D SDJH ZLWK DWWULEXWHV LPSRUW VHVVLRQ FRQWHQW7\SH DQG LV(/,JQRUHG E LQFOXGH DQG F WDJOLE

:ULWH D -63 'RFXPHQW ;0/ EDVHG GRFXPHQW WKDW XVHV WKH FRUUHFW V\QWD[

'HVFULEH WKH SXUSRVH DQG HYHQW VHTXHQFH RI WKH -63 SDJH OLIHF\FOH -63 SDJH WUDQVODWLRQ -63 SDJH FRPSLODWLRQ ORDG FODVV FUHDWH LQVWDQFH FDOO WKH MVS,QLW PHWKRG FDOO WKH BMVS6HUYLFH PHWKRG DQG FDOO WKH MVS'HVWUR\ PHWKRG

*LYHQ D GHVLJQ JRDO ZULWH -63 FRGH XVLQJ WKH DSSURSULDWH LPSOLFLW REMHFWV D UHTXHVW E UHVSRQVH F RXW G VHVVLRQ H FRQ¿J I DSSOLFDWLRQ J SDJH K SDJH&RQWH[W DQG L H[FHSWLRQ

&RQ¿JXUH WKH GHSOR\PHQW GHVFULSWRU WR GHFODUH RQH RU PRUH WDJ OLEUDULHV GHDFWLYDWH WKH HYDOXDWLRQ ODQJXDJH DQG GHDFWLYDWH WKH VFULSWLQJ ODQJXDJH

*LYHQ D VSHFL¿F GHVLJQ JRDO IRU LQFOXGLQJ D -63 VHJPHQW LQ DQRWKHU SDJH ZULWH WKH -63 FRGH WKDW XVHV WKH PRVW DSSURSULDWH LQFOXVLRQ PHFKDQLVP WKH LQFOXGH GLUHFWLYH RU WKH MVS LQFOXGH VWDQGDUG DFWLRQ

-OST IS COVERED IN THIS CHAPTER BUT THE DETAILS BEHIND C STANDARD AND CUSTOM ACTIONS AND D EXPRESSION LANGUAGE ELEMENTS ARE COVERED IN LATER CHAPTERS

4HE PAGE DIRECTIVE IS COVERED IN THIS CHAPTER BUT INCLUDE AND TAGLIB ARE COVERED IN LATER CHAPTERS .OT COVERED HERE REFER TO THE CHAPTER ON $EPLOYMENT !LL COVERED IN THIS CHAPTER (INT THESE WILL BE SOME OF THE MOST NO BRAINER QUESTIONS ON THE REAL EXAM ONCE YOU VE LEARNED THE FUNDAMENTALS IN THIS CHAPTER

!LL COVERED IN THIS CHAPTER ALTHOUGH YOU RE EXPECTED TO ALREADY KNOW WHAT MOST OF THEM MEAN BASED ON THE PREVIOUS TWO CHAPTERS

7E COVER EVERYTHING HERE EXCEPT DECLARING TAG LIBRARIES 4HAT S COVERED IN THE CHAPTER ON 5SING *34, .OT COVERED HERE REFER TO THE NEXT CHAPTER 3CRIPTLESS *30S

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

1V \PM MVL I 2;8 Q[ R][\ I [MZ ^TM\ 9OUR *30 EVENTUALLY BECOMES A FULL m EDGED SERVLET RUNNING IN YOUR WEB APP )T S A LOT LIKE ANY OTHER SERVLET EXCEPT THAT THE SERVLET CLASS IS WRITTEN FOR YOUˆBY THE #ONTAINER 4HE #ONTAINER TAKES WHAT YOU VE WRITTEN IN YOUR *30 TRANSLATES IT INTO A SERVLET CLASS SOURCE JAVA l LE THEN COMPILES THAT INTO A *AVA SERVLET CLASS !FTER THAT IT S JUST SERVLETS ALL THE WAY DOWN AND THE SERVLET RUNS IN EXACTLY THE SAME WAY IT WOULD IF YOU D WRITTEN AND COMPILED THE CODE YOURSELF )N OTHER WORDS THE #ONTAINER LOADS THE SERVLET CLASS INSTANTIATES AND INITIALIZES IT MAKES A SEPARATE THREAD FOR EACH REQUEST AND CALLS THE SERVLET S SERVICE METHOD

ZULWHV

LV WUDQVODWHG WR KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO!

0\-63 MVS

SRLQW IRU 7KH PRVW LPSRUWDQW O\ ZKDW WKLV FKDSWHU LV VLPS FRGH SOD\ UROH GRHV \RXU -63 DVV" LQ WKH ¿ QDO VHUYOHW FO H GR ,Q RWKHU ZRUGV ZKHU -63 HQG WKH LQ QWV PH HOH WKH GH RI WKH XS LQ WKH VRXUFH FR W" UYOH VH WHG JHQHUD

LV ORDGHG DQG LQLWLDOL]HG DV

FRPSLOHV WR SDFNDJH KHDG ¿ UVW LPSRUW MDYD[ VHUYOHW +WWS6HUYOHW

0\-63BMVS MDYD

0\-63BMVS FODVV

6HUYOHW REMHFW

0\-63BMVS 6HUYOHW

3OME OF THE QUESTIONS WE LL ANSWER IN THIS CHAPTER INCLUDE

7HERE DOES EACH PART OF YOUR *30 l LE END UP IN THE SERVLET SOURCE CODE

$O YOU HAVE ACCESS TO THE hSERVLETNESSv OF YOUR *30 PAGE &OR EXAMPLE DOES A *30 HAVE A CONCEPT OF A 3ERVLET#ONl G OR 3ERVLET#ONTEXT

7HAT ARE THE TYPES OF ELEMENTS YOU CAN PUT IN A *30

7HAT S THE SYNTAX FOR THE DIFFERENT ELEMENTS IN A *30

7HAT S THE LIFECYCLE OF A *30 AND CAN YOU STEP INTO THE MIDDLE OF IT

(OW DO THE DIFFERENT ELEMENTS IN A *30 INTERACT IN THE l NAL SERVLET

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PDNLQJ D -63

5ISQVO I 2;8 \PI\ LQ[XTIa[ PW_ UIVa \QUM[ Q\Âź[ JMMV IKKM[[ML 0AULINE WANTS TO USE *30S IN HER WEB APPSˆSHE S REALLY SICK OF WRITING (4-, INTO A SERVLET S 0RINT7RITER PRINTLN 3HE DECIDES TO LEARN *30S BY MAKING A SIMPLE DYNAMIC PAGE THAT PRINTS THE NUMBER OF TIMES THE PAGE HAS BEEN REQUESTED 3HE UNDERSTANDS THAT YOU CAN PUT REGULAR OLD *AVA CODE IN A *30 USING A SCRIPTLETˆWHICH JUST MEANS *AVA CODE WITHIN A TAG

, NQRZ , FDQ SXW -DYD FRGH LQ WKH -63 VR ,¡OO PDNH D VWDWLF PHWKRG LQ D &RXQWHU FODVV WR KROG WKH DFFHVV FRXQW VWDWLF YDULDEOH DQG WKHQ ,¡OO FDOO WKDW PHWKRG IURP WKH -63

%DVLF&RXQWHU MVS KWPO! ERG\! 7KH SDJH FRXQW LV

RXW SULQWOQ &RXQWHU JHW&RXQW

! ERG\! KWPO!

licitly there. The ÒoutÓ object is imp and %> is a <% n wee Everything bet in old Java. pla just is scriptlet, which

&RXQWHU MDYD SDFNDJH IRR SXEOLF FODVV &RXQWHU ^ SULYDWH VWDWLF LQW FRXQW SXEOLF VWDWLF V\QFKURQL]HG LQW JHW&RXQW ^ FRXQW UHWXUQ FRXQW ` ` old Java

Plain helper class.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

;PM LMXTWa[ IVL \M[\[ Q\ )T S TRIVIAL TO DEPLOY AND TEST 4HE ONLY TRICKY PART IS MAKING SURE THAT THE #OUNTER CLASS IS AVAILABLE TO THE *30 AND THAT S EASYˆJUST BE SURE THE #OUNTER CLASS IS IN THE 7%" ).& CLASSES DIRECTORY OF THE WEB APP 3HE ACCESSES THE *30 DIRECTLY IN THE BROWSER AT KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

:KDW VKH H[SHFWHG KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

ZHEDSSV

7KH SDJH FRXQW LV

WHVW-63

KWPO! ERG\! 7KH SDJH FRXQW LV RXW SULQWOQ

:(% ,1)

&RXQWHU JHW&RXQW

!

ERG\! KWPO!

:KDW VKH JRW

%DVLF&RXQWHU MVS "[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"! '2&7<3(

KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

FODVVHV

+773 6WDWXV

7KH VHUYHU HQFRXQWHUHG DQ LQWHUQDO HUURU WKDW SUHYHQWHG LW IURP IXOILOOLQJ WKLV UHTXHVW H[FHSWLRQ RUJ DSDFKH MDVSHU -DVSHU([FHSWLRQ 8QDEOH WR FRPSLOH FODVV IRU -63

ZHE [PO

To keep it easy, we the JSP at the rooput of this web app. t Put the package directory and class Ăžle in the WEB-INF/classes directory, and any part of this web app will be able to see it.

IRR

&RXQWHU FODVV

$Q HUURU RFFXUUHG DW OLQH LQ WKH MVS ILOH %DVLF&RXQWHU MVS *HQHUDWHG VHUYOHW HUURU >MDYDF@ &RPSLOLQJ VRXUFH ILOH 8VHUV NDWK\ $SSOLFDWLRQV MDNDUWD WRPFDW ZRUN &DWDOLQD ORFDOKRVW WHVW-63 RUJ DSDFKH MVS %DVLF&RXQWHUBMVS MDYD FDQQRW UHVROYH V\PERO V\PERO YDULDEOH &RXQWHU ORFDWLRQ FODVV RUJ DSDFKH MVS EDVLF&RXQWHUBMVS RXW SULQW &RXQWHU JHW&RXQW t whatÕs wrong? Can you Þgure ou A HUURU RUJ DSDFKH MDVSHU FRPSLOHU 'HIDXOW(UURU+DQGOHU MDYDF(UURU 'HIDXOW(UURU+DQGOHU MDYD

RUJ DSDFKH MDVSHU FRPSLOHU (UURU'LVSDWFKHU MDYDF(UURU (UURU'LVSDWFKHU MDYD

RUJ DSDFKH MDVSHU FRPSLOHU &RPSLOHU JHQHUDWH&ODVV &RPSLOHU MDYD

RUJ DSDFKH MDVSHU FRPSLOHU &RPSLOHU FRPSLOH &RPSLOHU MDYD

RUJ DSDFKH MDVSHU FRPSLOHU &RPSLOHU FRPSLOH &RPSLOHU MDYD

RUJ DSDFKH MDVSHU -VS&RPSLODWLRQ&RQWH[W FRPSLOH -VS&RPSLODWLRQ&RQWH[W MDYD

RUJ DSDFKH MDVSHU VHUYOHW -VS6HUYOHW:UDSSHU VHUYLFH -VS6HUYOHW:UDSSHU MDYD

RUJ DSDFKH MDVSHU VHUYOHW -VS6HUYOHW VHUYLFH-VS)LOH -VS6HUYOHW MDYD

RUJ DSDFKH MDVSHU VHUYOHW -VS6HUYOHW VHUYLFH -VS6HUYOHW MDYD

MDYD[ VHUYOHW KWWS +WWS6HUYOHW VHUYLFH +WWS6HUYOHW MDYD

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

SDJH GLUHFWLYH LPSRUW DWWULEXWH

<PM 2;8 LWM[VÂź\ ZMKWOVQbM \PM +W]V\MZ KTI[[ 4HE #OUNTER CLASS IS IN THE FOO PACKAGE BUT THERE S NOTHING IN THE *30 TO ACKNOWLEDGE THAT )T S THE SAME THING THAT HAPPENS TO YOU WITH ANY OTHER *AVA CODE AND YOU KNOW THE RULE IMPORT THE PACKAGE OR USE THE FULLY QUALIlED CLASS NAME IN YOUR CODE

, JXHVV \RX KDYH WR XVH WKH IXOO\ TXDOLĂ€HG FODVV QDPH LQVLGH -63V 7KDW PDNHV VHQVH VLQFH DOO -63V DUH WXUQHG LQWR SODLQ ROG -DYD VHUYOHW FRGH E\ WKH &RQWDLQHU %XW , VXUH ZLVK \RX FRXOG SXW LPSRUWV LQWR \RXU -63 FRGH

&RXQWHU MDYD

SDFNDJH IRR SXEOLF FODVV &RXQWHU ^ SULYDWH VWDWLF LQW FRXQW SXEOLF VWDWLF LQW JHW&RXQW ^ FRXQW UHWXUQ FRXQW ` `

-63 FRGH ZDV RXW SULQWOQ &RXQWHU JHW&RXQW

!

-63 FRGH VKRXOG EH RXW SULQWOQ IRR &RXQWHU JHW&RXQW

!

Now itÕll wor

k.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

%XW \RX &$1 SXW LPSRUW VWDWHPHQWV LQ D -63 \RX MXVW QHHG D GLUHFWLYH

=[M \PM XIOM LQZMK\Q^M \W QUXWZ\ XIKSIOM[ ! DIRECTIVE IS A WAY FOR YOU TO GIVE SPECIAL INSTRUCTIONS TO THE #ONTAINER AT PAGE TRANSLATION TIME $IRECTIVES COME IN THREE mAVORS PAGE INCLUDE AND TAGLIB 7E LL LOOK AT THE INCLUDE AND TAGLIB DIRECTIVES IN LATER CHAPTERS BUT FOR NOW ALL WE CARE ABOUT IS THE PAGE DIRECTIVE BECAUSE IT S THE ONE THAT LETS YOU IMPORT 7R LPSRUW D VLQJOH SDFNDJH # SDJH LPSRUW ´IRR ´ ! KWPO! ERG\! 7KH SDJH FRXQW LV RXW SULQWOQ &RXQWHU JHW&RXQW

! ERG\! KWPO!

This is a page directive with an import attribute. (Notice thereÕs no semicolon at the end of a directive.)

Scriptlets are all statements innormal Java, so must end in a semia scriptlet colon !

7R LPSRUW PXOWLSOH SDFNDJHV # SDJH LPSRUW ´IRR MDYD XWLO ´ !

packages. Use a comma to separate the list of packages ! ire ent the nd arou go es The quot .OTICE WHAT S DIFFERENT BETWEEN THE *AVA CODE THAT PRINTS THE COUNTER AND THE PAGE DIRECTIVE 4HE *AVA CODE IS BETWEEN ANGLE BRACKETS WITH PERCENT SIGNS AND "UT THE DIRECTIVE ADDS AN ADDITIONAL CHARACTER TO THE START OF THE ELEMENTˆTHE SIGN )F YOU SEE *30 CODE THAT STARTS WITH YOU KNOW IT S A DIRECTIVE 7E LL GET INTO MORE DETAILS ABOUT THE PAGE DIRECTIVE LATER IN THE BOOK

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ H[SUHVVLRQV

*]\ \PMV 3QU UMV\QWV[ šM`XZM[[QWV[Âş *UST WHEN YOU THOUGHT IT WAS SAFE +IM NOTICES THE SCRIPTLET WITH AN OUT PRINTLN STATEMENT 4HIS IS *30 FOLKS 0ART OF THE WHOLE POINT OF *30 IS TO AVOID PRINTLN 4HAT S WHY THERE S A *30 EXPRESSION ELEMENTˆIT AUTOMATICALLY PRINTS OUT WHATEVER YOU PUT BETWEEN THE TAGS

<RX GRQ¡W QHHG WR VD\ RXW SULQWOQ LQ D -63 -XVW XVH DQ H[SUHVVLRQ

6FULSWOHW FRGH # SDJH LPSRUW ´IRR ´ ! KWPO! ERG\! 7KH SDJH FRXQW LV

RXW SULQWOQ &RXQWHU JHW&RXQW

! ERG\! KWPO!

([SUHVVLRQ FRGH # SDJH LPSRUW ´IRR ´ ! KWPO! ERG\! 7KH SDJH FRXQW LV QRZ

&RXQWHU JHW&RXQW ! ERG\! KWPO!

e donÕt The expression is shorterÑw t... prin the do ly licit exp to need

.OTICE WHAT S DIFFERENT BETWEEN THE TAG FOR THE SCRIPTLET CODE AND THE TAG FOR THE EXPRESSION 4HE SCRIPTLET CODE IS BETWEEN ANGLE BRACKETS WITH PERCENT SIGNS AND "UT THE EXPRESSION ADDS AN ADDITIONAL CHARACTER TO THE START OF THE ELEMENTˆAN EQUALS SIGN 3O FAR WE VE SEEN THREE DIFFERENT *30 ELEMENT TYPES

3CRIPTLET

$IRECTIVE

%XPRESSION

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

+(//2 ,I \RX¡UH JRQQD WHOO XV KRZ WR LPSURYH RXU FRGH \RX FRXOG $7 /($67 JHW WKH -DYD V\QWD[ ULJKW WKHUH¡V QR IULFNLQ¡ VHPLFRORQ DW WKH HQG RI WKDW H[SUHVVLRQ

colon? WhereÕs the semi &RXQWHU JHW&RXQW !

-`XZM[[QWV[ JMKWUM \PM IZO]UMV\ \W IV W]\ XZQV\ )N OTHER WORDS THE #ONTAINER TAKES EVERYTHING YOU TYPE BETWEEN THE AND AND PUTS IT IN AS THE ARGUMENT TO A STATEMENT THAT PRINTS TO THE IMPLICIT RESPONSE 0RINT7RITER OUT :KHQ WKH &RQWDLQHU VHHV WKLV &RXQWHU JHW&RXQW !

,W WXUQV LW LQWR WKLV RXW SULQW &RXQWHU JHW&RXQW

,I \RX GLG SXW D VHPLFRORQ LQ \RXU H[SUHVVLRQ &RXQWHU JHW&RXQW !

7KDW ZRXOG EH EDG ,W ZRXOG PHDQ WKLV RXW SULQW &RXQWHU JHW&RXQW

Yikes!! This will never compile. 1(9(5 HQG DQ H[SUHVVLRQ ZLWK D VHPLFRORQ QHYHU3XW$6HPLFRORQ,Q+HUH ! EHFDXVH7KLV,V$Q$UJXPHQW7R:ULWH !

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

H[SUHVVLRQV DQG SDJH GLUHFWLYH

WKHUH DUH QR

'XPE 4XHVWLRQV

4

7ELL IF YOU RE SUPPOSED TO USE EXPRESSIONS ).34%!$ OF PUTTING OUT PRINTLN INTO A SCRIPTLET THEN WHY IS THE IMPLICIT hOUTv THERE

$

9OU PROBABLY WON T USE THE IMPLICIT OUT VARIABLE FROM WITHIN YOUR *30 PAGE BUT YOU MIGHT PASS IT TO SOMETHING ELSE SOME OTHER OBJECT THAT S PART OF YOUR APP THAT DOES NOT HAVE DIRECT ACCESS TO THE OUTPUT STREAM FOR THE RESPONSE

6KDUSHQ \RXU SHQFLO 'HFLGH ZKLFK RI WKH IROORZLQJ H[SUHVVLRQV DUH DQG DUH QRW YDOLG DQG ZK\ :H KDYHQ¶W FRYHUHG HYHU\ H[DPSOH KHUH VR PDNH \RXU EHVW JXHVV EDVHG RQ ZKDW \RX NQRZ DERXW KRZ H[SUHVVLRQV ZRUN $QVZHUV DUH ODWHU LQ WKLV FKDSWHU VR GR WKLV 12:

9DOLG" &KHFN LI YDOLG DQG LI QRW H[SODLQ ZK\ QRW

4

R

$

R -ATH RANDOM

)N AN EXPRESSION WHAT HAPPENS IF THE METHOD DOESN T RETURN ANYTHING

9OU LL GET AN ERROR 9OU CANNOT -534 ./4 USE A METHOD WITH A VOID RETURN TYPE AS AN EXPRESSION 4HE #ONTAINER IS SMART ENOUGH TO FIGURE OUT THAT THERE WON T BE ANYTHING TO PRINT IF THE METHOD HAS A VOID RETURN TYPE

4

7HY DOES THE IMPORT DIRECTIVE START WITH THE WORD hPAGEv 7HY IS IT PAGE IMPORT INSTEAD OF JUST IMPORT

$

'OOD QUESTION 2ATHER THAN HAVING A WHOLE BIG PILE OF DIFFERENT DIRECTIVES THE *30 SPEC HAS JUST THREE *30 DIRECTIVES BUT THE DIRECTIVES CAN HAVE ATTRIBUTES 7HAT YOU CALLED hTHE IMPORT DIRECTIVEv IS ACTUALLY hTHE IMPORT ATTRIBUTE OF THE PAGE DIRECTIVEv

R h v R -ATH RANDOM R 3TRING S hFOOv R NEW 3TRING; = R

4

R

$

R FALSE

7HAT ARE THE OTHER ATTRIBUTES FOR THE PAGE DIRECTIVE

2EMEMBER THE PAGE DIRECTIVE IS ABOUT GIVING THE #ONTAINER INFORMATION IT NEEDS WHEN TRANSLATING YOUR *30 INTO A SERVLET 4HE ATTRIBUTES WE CARE ABOUT BESIDES IMPORT ARE SESSION CONTENT 4YPE AND IS%,)GNORED WE LL COME BACK TO THESE LATER IN THE CHAPTER

R NEW #OUNTER

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

3QU LZWX[ \PM NQVIT JWUJ[PMTT <RX GRQ¡W HYHQ 1((' WKH &RXQWHU FODVV \RX FDQ GR WKH ZKROH WKLQJ LQ WKH -63

+PPP , NQRZ WKH -63 WXUQV LQWR D VHUYOHW VR PD\EH , FRXOG GHFODUH D FRXQW YDULDEOH LQ D VFULSWOHW DQG WKDW ZRXOG WXUQ LQWR D YDULDEOH LQ WKH VHUYOHW :RXOG WKDW ZRUN"

:KDW VKH WULHG KWPO! ERG\!

LQW FRXQW ! 7KH SDJH FRXQW LV QRZ

FRXQW ! ERG\! KWPO!

:LOO LW FRPSLOH" :LOO LW ZRUN"

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

VFULSWOHW YDULDEOHV

,MKTIZQVO I ^IZQIJTM QV I [KZQX\TM\ 4HE VARIABLE DECLARATION IS LEGAL BUT IT DIDN T QUITE WORK THE WAY 0AULINE HOPED :KDW VKH WULHG

scriptlet

KWPO! ERG\!

ort anything, so We donÕt need to imp ive. we dropped the page direct Declare the count variable.

LQW FRXQW !

7KH SDJH FRXQW LV QRZ

expression

FRXQW ! ERG\! KWPO!

Increment the co variable and printunt the

value.

:KDW VKH JRW WKH À UVW WLPH VKH KLW WKH SDJH KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

7KH SDJH FRXQW LV

Looks good!

:KDW VKH JRW WKH VHFRQG WKLUG DQG HYHU\ RWKHU WLPH VKH KLW WKH SDJH KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

7KH SDJH FRXQW LV

showing Ò1Ó Uh-oh... itÕs still the variable... It keeps resetting

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

?PI\ :-)44A PIXXMV[ \W aW]Z 2;8 KWLM' 9OU WRITE A *30 BUT IT BECOMES A SERVLET 4HE ONLY WAY TO REALLY TELL WHAT S HAPPENING IS TO LOOK AT WHAT THE #ONTAINER DOES TO YOUR *30 CODE )N OTHER WORDS HOW DOES THE #ONTAINER TRANSLATE YOUR *30 INTO A SERVLET /NCE YOU KNOW WHERE DIFFERENT *30 ELEMENTS LAND IN THE SERVLET S CLASS lLE YOU LL lND IT MUCH EASIER TO KNOW HOW TO STRUCTURE YOUR *30 4HE SERVLET CODE ON THIS PAGE IS NOT THE REAL CODE GENERATED BY THE #ONTAINERˆWE SIMPLIlED IT DOWN TO THE ESSENTIAL PARTS 4HE #ONTAINER GENERATED SERVLET lLE IS WELL UGLIER 4HE REAL GENERATED SERVLET SOURCE CODE IS SLIGHTLY HARDER TO READ BUT WE WILL LOOK AT THE REAL THING IN A FEW PAGES &OR NOW THOUGH ALL WE CARE ABOUT IS WHERE IN THE SERVLET CLASS OUR *30 CODE ACTUALLY ENDS UP

7KLV -63

KWPO! ERG\!

LQW FRXQW ! 7KH SDJH FRXQW LV QRZ

FRXQW ! ERG\! KWPO!

%HFRPHV WKLV VHUYOHW SXEOLF FODVV EDVLF&RXQWHUBMVS H[WHQGV 6RPH6SHFLDO+WWS6HUYOHW ^ SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ RXW ZULWH ³ KWPO! ERG\!´ LQW FRXQW RXW ZULWH ³7KH SDJH FRXQW LV QRZ ´ RXW SULQW FRXQW RXW ZULWH ³ ERG\! KWPO!´ de into a ` `

e co it as er puts all th The Contarvinice method. Think oft. os se oP c /d generi combo doGet a catch-all

$// VFULSWOHW DQG H[SUHVVLRQ FRGH ODQGV LQ D VHUYLFH PHWKRG 7KDW PHDQV YDULDEOHV GHFODUHG LQ D VFULSWOHW DUH DOZD\V /2&$/ YDULDEOHV

Note: if you want to see the generated servlet code from Tomcat, look in yourTomcatHomeDir/work/Catalina/yourServerName/yourWebAppName/org/apache/jsp. (The underlined names will change depending on your system and your web app.) \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 GHFODUDWLRQV

'RQ¡W WHOO PH³WKHUH PXVW EH DQRWKHU NLQG RI -63 HOHPHQW IRU GHFODULQJ LQVWDQFH YDULDEOHV LQVWHDG RI ORFDO YDULDEOHV

?M VMML IVW\PMZ 2;8 MTMUMV\ $ECLARING THE COUNT VARIABLE IN A SCRIPTLET MEANT THAT THE VARIABLE WAS REINITIALIZED EACH TIME THE SERVICE METHOD RAN 7HICH MEANS IT WAS RESET TO WITH EACH REQUEST 7E NEED TO SOMEHOW MAKE COUNT AN INSTANCE VARIABLE 3O FAR WE VE LOOKED AT DIRECTIVES SCRIPTLETS AND EXPRESSIONS $IRECTIVES ARE FOR SPECIAL INSTRUCTIONS TO THE #ONTAINER SCRIPTLETS ARE JUST PLAIN OLD *AVA THAT LANDS AS IS WITHIN THE GENERATED SERVLET S SERVICE METHOD AND THE RESULT OF AN EXPRESSION ALWAYS BECOMES THE ARGUMENT TO A PRINT METHOD "UT THERE S ANOTHER *30 ELEMENT CALLED A DECLARATION

LQW FRXQW !

Put an excla point (!) af mation percent signte(%r ).the

essionÑyou This isnÕt anseexmiprcolon here ! e NEED th

*30 DECLARATIONS ARE FOR DECLARING MEMBERS OF THE GENERATED SERVLET CLASS 4HAT MEANS BOTH VARIABLES AND METHODS )N OTHER WORDS ANYTHING BETWEEN THE AND TAG IS ADDED TO THE CLASS OUTSIDE THE SERVICE METHOD 4HAT MEANS YOU CAN DECLARE BOTH STATIC AND INSTANCE VARIABLES AND METHODS

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

2;8 ,MKTIZI\QWV[ ! *30 DECLARATION IS ALWAYS DElNED INSIDE THE CLASS BUT OUTSIDE THE SERVICE OR ANY OTHER METHOD )T S THAT SIMPLEˆDECLARATIONS ARE FOR STATIC AND INSTANCE VARIABLES AND METHODS )N THEORY YES YOU COULD DElNE OTHER MEMBERS INCLUDING INNER CLASSES BUT OF THE TIME YOU LL USE DECLARATIONS FOR METHODS AND VARIABLES 4HE CODE BELOW SOLVES 0AULINE S PROBLEM NOW THE COUNTER KEEPS INCREMENTING EACH TIME A CLIENT REQUESTS THE PAGE

9DULDEOH 'HFODUDWLRQ 7KLV -63

%HFRPHV WKLV VHUYOHW SXEOLF FODVV EDVLF&RXQWHUBMVS H[WHQGV +WWS6HUYOHW ^

KWPO! ERG\!

LQW FRXQW ! 7KH SDJH FRXQW LV QRZ FRXQW ! ERG\! KWPO!

LQW FRXQW SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV MDYD LR ,2([FHSWLRQ ^ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU UHVSRQVH VHW&RQWHQW7\SH ÂłWH[W KWPO´ RXW ZULWH Âł KWPO! ERG\!´ RXW ZULWH Âł7KH SDJH FRXQW LV QRZ ´ RXW SULQW FRXQW RXW ZULWH Âł ERG\! KWPO!´ incrementing ` This time, wevaĂ•reriable instead ` e nc an insta

.

of a local variable

0HWKRG 'HFODUDWLRQ 7KLV -63

%HFRPHV WKLV VHUYOHW SXEOLF FODVV EDVLF&RXQWHUBMVS H[WHQGV +WWS6HUYOHW ^

just the

LQW GRXEOH&RXQW ^ The method goes in ur JSP. in yo FRXQW FRXQW way you typed it LQW GRXEOH&RXQW ^ UHWXUQ FRXQW FRXQW FRXQW ` ItÕs Java, so no problem with forward-referencing UHWXUQ FRXQW LQW FRXQW (declaring the variable AFTER you used it in a method). ` SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW !

KWPO! ERG\!

LQW FRXQW ! 7KH SDJH FRXQW LV QRZ GRXEOH&RXQW ! ERG\! KWPO!

+WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV MDYD LR ,2([FHSWLRQ ^ 3ULQW:ULWHU RXW UHVSRQVH JHW:ULWHU UHVSRQVH VHW&RQWHQW7\SH ³WH[W KWPO´ RXW ZULWH ³ KWPO! ERG\!´ RXW ZULWH ³7KH SDJH FRXQW LV QRZ ´ RXW SULQW GRXEOH&RXQW RXW ZULWH ³ ERG\! KWPO!´ ` ` \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

WKH JHQHUDWHG VHUYOHW

<QUM \W [MM \PM :-)4 OMVMZI\ML [MZ ^TM\ 7E VE BEEN LOOKING AT A SUPER SIMPLIlED VERSION OF THE SERVLET THE #ONTAINER ACTUALLY CREATES FROM YOUR *30 4HERE S NO NEED TO LOOK AT THE #ONTAINER GENERATED CODE DURING DEVELOPMENT BUT YOU CAN USE IT TO HELP LEARN /NCE YOU VE SEEN WHAT THE #ONTAINER DOES WITH THE DIFFERENT ELEMENTS OF A *30 YOU SHOULDN T NEED TO EVER LOOK AT THE #ONTAINER GENERATED JAVA SOURCE lLES 3OME VENDORS WON T LET YOU SEE THE GENERATED *AVA SOURCE AND KEEP ONLY THE COMPILED CLASS lLES $ON T BE INTIMIDATED WHEN YOU SEE PARTS OF THE !0) THAT YOU DON T RECOGNIZE -OST OF THE CLASS AND INTERFACE TYPES ARE VENDOR SPECIlC IMPLEMENTATIONS YOU SHOULDN T CARE ABOUT

:KDW WKH &RQWDLQHU GRHV ZLWK \RXU -63

/RRNV DW WKH GLUHFWLYHV IRU LQIRUPDWLRQ LW PLJKW QHHG GXULQJ WUDQVODWLRQ

&UHDWHV DQ +WWS6HUYOHW VXEFODVV

)RU 7RPFDW WKH JHQHUDWHG VHUYOHW H[WHQGV

RUJ DSDFKH MDVSHU UXQWLPH +WWS-VS%DVH

,I WKHUH·V D SDJH GLUHFWLYH ZLWK DQ LPSRUW DWWULEXWH LW ZULWHV WKH LPSRUW VWDWHPHQWV DW WKH WRS RI WKH FODVV ILOH MXVW EHORZ WKH SDFNDJH VWDWHPHQW )RU 7RPFDW WKH SDFNDJH VWDWHPHQW ZKLFK \RX GRQ·W FDUH DERXW LV

SDFNDJH RUJ DSDFKH MVS

,I WKHUH DUH GHFODUDWLRQV LW ZULWHV WKHP LQWR WKH FODVV ILOH XVXDOO\ MXVW EHORZ WKH FODVV GHFODUDWLRQ DQG EHIRUH WKH VHUYLFH PHWKRG 7RPFDW GHFODUHV RQH VWDWLF YDULDEOH DQG RQH LQVWDQFH PHWKRG RI LWV RZQ

%XLOGV WKH VHUYLFH PHWKRG 7KH VHUYLFH PHWKRG·V DFWXDO QDPH LV BMVS6HUYLFH ,W·V FDOOHG E\ WKH VHUYOHW VXSHUFODVV· RYHUULGGHQ VHUYLFH PHWKRG DQG UHFHLYHV WKH +WWS6HUYOHW5HTXHVW DQG +WWS6HUYOHW5HVSRQVH $V SDUW RI EXLOGLQJ WKLV PHWKRG WKH &RQWDLQHU GHFODUHV DQG LQLWLDOL]HV DOO WKH LPSOLFLW REMHFWV <RX·OO VHH PRUH LPSOLFLW REMHFWV ZKHQ \RX WXUQ WKH SDJH

&RPELQHV WKH SODLQ ROG +70/ FDOOHG WHPSODWH WH[W VFULSWOHWV DQG H[SUHVVLRQV LQWR WKH VHUYLFH PHWKRG IRUPDWWLQJ HYHU\WKLQJ DQG ZULWLQJ LW WR WKH 3ULQW:ULWHU UHVSRQVH RXWSXW

7KHUH¶V OLWWOH RQ WKH H[DP DERXW WKH JHQHUDWHG FODVV :H¶YH EHHQ VKRZLQJ JHQHUDWHG FRGH VR WKDW \RX FDQ XQGHUVWDQG KRZ WKH -63 LV WUDQVODWHG LQWR VHUYOHW FRGH %XW \RX GRQ¶W QHHG WR NQRZ WKH GHWDLOV DERXW KRZ D SDUWLFXODU YHQGRU GRHV LW RU ZKDW WKH JHQHUDWHG FRGH DFWXDOO\ ORRNV OLNH $OO \RX QHHG WR NQRZ LV WKH EHKDYLRU RI HDFK HOHPHQW W\SH VFULSWOHW GLUHFWLYH GHFODUDWLRQ HWF LQ WHUPV RI KRZ WKDW HOHPHQW ZRUNV LQVLGH WKH JHQHUDWHG VHUYOHW <RX QHHG WR NQRZ IRU H[DPSOH WKDW \RXU VFULSWOHW FDQ XVH LPSOLFLW REMHFWV DQG \RX QHHG WR NQRZ WKH 6HUYOHW $3, W\SH RI WKH LPSOLFLW REMHFWV %XW \RX GR 127 QHHG WR NQRZ WKH FRGH XVHG WR PDNH WKRVH REMHFWV DYDLODEOH 7KH RQO\ RWKHU WKLQJ \RX QHHG WR NQRZ DERXW WKH JHQHUDWHG FRGH DUH WKH WKUHH -63 OLIHF\FOH PHWKRGV MVS,QLW MVS'HVWUR\ DQG BMVS6HUYLFH 7KH\¶UH FRYHUHG ODWHU LQ WKLV FKDSWHU

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63 7RPFDW JHQHUDWHG FODVV SDFNDJH RUJ DSDFKH MVS LPSRUW MDYD[ VHUYOHW LPSRUW MDYD[ VHUYOHW KWWS LPSRUW MDYD[ VHUYOHW MVS

directive If you have pageshow up here Õll ey th s, rt impo y imports (we didnÕt have an . P) JS is for th

KWPO! ERG\!

LQW FRXQW ! 7KH SDJH FRXQW LV QRZ FRXQW ! ERG\! KWPO!

SXEOLF ÂżQDO FODVV %DVLF&RXQWHUBMVS H[WHQGV RUJ DSDFKH MDVSHU UXQWLPH +WWS-VS%DVH LPSOHPHQWV RUJ DSDFKH MDVSHU UXQWLPH -VS6RXUFH'HSHQGHQW ^ LQW FRXQW SULYDWH VWDWLF MDYD XWLO 9HFWRU BMVS[BGHSHQGDQWV SXEOLF MDYD XWLO /LVW JHW'HSHQGDQWV ^ UHWXUQ BMVS[BGHSHQGDQWV `

tions ts YOUR declara The Container pu! %> tags) and any of <% (things inside e class declaration. its own below th

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ -VS)DFWRU\ BMVS[)DFWRU\ QXOO 3DJH&RQWH[W SDJH&RQWH[W QXOO The Container declares a bunch +WWS6HVVLRQ VHVVLRQ QXOO of its own local variables, including 6HUYOHW&RQWH[W DSSOLFDWLRQ QXOO 6HUYOHW&RQÂżJ FRQÂżJ QXOO those that represent the Ă’implicit -VS:ULWHU RXW QXOO objectsĂ“ your code might need, like 2EMHFW SDJH WKLV Ă’outĂ“ and Ă’requestĂ“. -VS:ULWHU BMVS[BRXW QXOO 3DJH&RQWH[W BMVS[BSDJHBFRQWH[W QXOO WU\ ^ BMVS[)DFWRU\ -VS)DFWRU\ JHW'HIDXOW)DFWRU\ UHVSRQVH VHW&RQWHQW7\SH ÂłWH[W KWPO´ SDJH&RQWH[W BMVS[)DFWRU\ JHW3DJH&RQWH[W WKLV UHTXHVW UHVSRQVH QXOO WUXH WUXH jects. BMVS[BSDJHBFRQWH[W SDJH&RQWH[W ze the implicit ob DSSOLFDWLRQ SDJH&RQWH[W JHW6HUYOHW&RQWH[W Now it tries to initiali FRQÂżJ SDJH&RQWH[W JHW6HUYOHW&RQÂżJ VHVVLRQ SDJH&RQWH[W JHW6HVVLRQ RXW SDJH&RQWH[W JHW2XW BMVS[BRXW RXW And it tries to run and output RXW ZULWH Âł?U KWPO!?U ERG\!?U´ your JSP HTML, scriptlet, RXW ZULWH Âł?U7KH SDJH FRXQW LV QRZ ?U´ and expression code. RXW SULQW FRXQW RXW ZULWH Âł?U ERG\!?U KWPO!?U´ ` FDWFK 7KURZDEOH W ^ LI W LQVWDQFHRI 6NLS3DJH([FHSWLRQ

^ Of course things RXW BMVS[BRXW LI RXW QXOO RXW JHW%XIIHU6L]H

might go wrong... RXW FOHDU%XIIHU LI BMVS[BSDJHBFRQWH[W QXOO BMVS[BSDJHBFRQWH[W KDQGOH3DJH([FHSWLRQ W ` ` ILQDOO\ ^ LI BMVS[)DFWRU\ QXOO BMVS[)DFWRU\ UHOHDVH3DJH&RQWH[W BMVS[BSDJHBFRQWH[W ` ` ` \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 LPSOLFLW REMHFWV

<PM W]\ ^IZQIJTM Q[VÂź\ \PM WVTa QUXTQKQ\ WJRMK\ 7HEN A #ONTAINER TRANSLATES THE *30 INTO A SERVLET THE BEGINNING OF THE SERVICE METHOD IS A PILE OF IMPLICIT OBJECT DECLARATIONS AND ASSIGNMENTS 7ITH IMPLICIT OBJECTS YOU CAN WRITE A *30 KNOWING THAT YOUR CODE IS GOING TO BE PART OF A SERVLET )N OTHER WORDS YOU CAN TAKE ADVANTAGE OF YOUR SERVLETNESS EVEN THOUGH YOU RE NOT DIRECTLY WRITING A SERVLET CLASS YOURSELF 4HINK BACK TO CHAPTERS AND 7HAT WERE SOME OF THE IMPORTANT OBJECTS YOU USED (OW DID YOUR SERVLET GET SERVLET INIT PARAMETERS (OW DID YOUR SERVLET GET CONTEXT INIT PARAMETERS (OW DID YOUR SERVLET GET A SESSION (OW DID YOUR SERVLET GET THE PARAMETERS SUBMITTED BY THE CLIENT IN A FORM 4HESE ARE JUST A FEW OF THE REASONS YOUR *30 MIGHT NEED TO USE SOME OF WHAT S AVAILABLE TO A SERVLET !LL OF THE IMPLICIT OBJECTS MAP TO SOMETHING FROM THE 3ERVLET *30 !0) 4HE REQUEST IMPLICIT OBJECT FOR EXAMPLE IS A REFERENCE TO THE (TTP3ERVLET2EQUEST OBJECT PASSED TO THE SERVICE METHOD BY THE #ONTAINER

$3, -VS:ULWHU +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH +WWS6HVVLRQ 6HUYOHW&RQWH[W 6HUYOHW&RQÂżJ -VS([FHSWLRQ 3DJH&RQWH[W 2EMHFW

4

,PSOLFLW 2EMHFW

ibute present the attr Which of theseestre, session, and application? scopes of requ vious). But now thereÕs UHTXHVW (OK, pretty ob scope, Òpage-levelÓ, and a NEW fourth tributes are stored in UHVSRQVH page-scoped at VHVVLRQ pageContext. t is only available DSSOLFDWLRQ This implicit obÒejecrror pagesÓ. (YouÕll d te na FRQ¿J to desig the book.) see that later in RXW

H[FHSWLRQ

SDJH&RQWH[W SDJH

A PageContext encapsulates implicit objects, so if you giveother some helper object a PageContext refe rence, the helper can use that refe rence to get references to the OTHER objects and attributes from allimplicit scopes.

7HAT S THE DIFFERENCE BETWEEN A *SP7RITER AND A 0RINT7RITER ) GET FROM AN (TTP3ERVLET2ESPONSE

$

.OT MUCH 4HE *SP7RITER )3 ! 0RINT7RITER BUT IT ADDS SOME BUFFERING CAPA BILITIES 4HE ONLY TIME YOU REALLY CARE ABOUT THAT IS WHEN YOU RE BUILDING CUSTOM TAGS SO WE LL TALK A LITTLE ABOUT *SP7RITER S SPECIAL CAPABILITIES IN THE CHAPTER ON DEVELOPING CUSTOM TAGS

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

([HUFLVH

(DFK RI WKH OLVWLQJV LV IURP D -63 <RXU MRE LV WR ILJXUH RXW ZKDW ZLOO KDSSHQ ZKHQ WKH &RQWDLQHU WULHV WR WXUQ WKH -63 LQWR D VHUYOHW :LOO WKH &RQWDLQHU EH DEOH WR WUDQVODWH \RXU -63 LQWR OHJDO FRPSLODEOH VHUYOHW FRGH" ,I QRW ZK\ QRW" ,I VR ZKDW KDSSHQV ZKHQ D FOLHQW DFFHVVHV WKH -63"

KWPO! ERG\! 7HVW VFULSWOHWV LQW \ [ ! LQW [ ! ERG\! KWPO!

# SDJH LPSRUW ´MDYD XWLO ´ ! KWPO! ERG\! 7HVW VFULSWOHWV $UUD\/LVW OLVW QHZ $UUD\/LVW OLVW DGG QHZ 6WULQJ ³IRR´

! OLVW JHW ! ERG\! KWPO!

KWPO! ERG\! 7HVW VFULSWOHWV LQW [ ! LQW [ ! [ ! ERG\! KWPO!

KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 H[HUFLVH

0RFN ([DP 0DJQHWV 6WXG\ WKH VFHQDULR DQG HYHU\WKLQJ HOVH RQ WKLV SDJH WKHQ SODFH WKH PDJQHWV RQ WKH -63 WR PDNH D OHJDO ILOH WKDW ZRXOG SURGXFH WKH FRUUHFW UHVXOW <RX GRQ¡W KDYH WR XVH DQ\ PDJQHW PRUH WKDQ RQFH DQG \RX ZRQ¡W XVH DOO RI WKH PDJQHWV 7KLV H[HUFLVH DVVXPHV WKHUH¡V D VHUYOHW ZKLFK \RX GRQ¡W QHHG WR VHH WKDW WDNHV WKH LQLWLDO UHTXHVW ELQGV DQ DWWULEXWH LQWR WKH UHTXHVW VFRSH DQG IRUZDUGV WR WKH -63 \RX¡UH FUHDWLQJ 1RWH ZH FDOOHG WKLV ´0RFN ([DP 0DJQHWV¾ LQVWHDG RI ´&RGH 0DJQHWV¾ EHFDXVH WKH H[DP LV )8// RI 'UDJ DQG 'URS TXHVWLRQV OLNH WKLV RQH

'HVLJQ *RDO &UHDWH D -63 WKDW ZLOO SURGXFH WKLV KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

7KH IULHQGV ZKR VKDUH \RXU KREE\ RI H[WUHPH NQLWWLQJ DUH )UHG 3UDGHHS 3KLOLSSH

7KH WKUHH QDPHV FRPH IURP DQ $UUD\/LVW UHTXHVW DWWULEXWH FDOOHG ´QDPHV¾ <RX¡OO QHHG WR JHW WKH DWWULEXWH IURP WKH UHTXHVW REMHFW $VVXPH D VHUYOHW JRW WKLV UHTXHVW DQG VHW DQ DWWULEXWH LQ UHTXHVW VFRSH

7KH WH[W ´H[WUHPH NQLWWLQJ¾ FRPHV IURP D IRUP UHTXHVW SDUDPHWHU <RX¡OO QHHG WR JHW WKDW SDUDPHWHU IURP \RXU -63 $ VHUYOHW ZLOO JHW WKH UHTXHVW ILUVW DQG WKHQ IRUZDUG WKH UHTXHVW WR \RXU -63 EXW WKDW GRHVQ¡W FKDQJH WKH ZD\ \RX JHW WKH SDUDPHWHU LQ \RXU -63

,PSRUWDQW WLSV DQG FOXHV

7KH UHTXHVW DWWULEXWH LV RI W\SH MDYD XWLO $UUD\/LVW

7KH LPSOLFLW YDULDEOH IRU WKH +WWS6HUYOHW5HTXHVW REMHFW LV QDPHG UHTXHVW DQG \RX FDQ XVH LW ZLWKLQ VFULSWOHWV RU H[SUHVVLRQV EXW QRW ZLWKLQ GLUHFWLYHV RU GHFODUDWLRQV :KDWHYHU \RX FDQ GR ZLWK D UHTXHVW REMHFW LQ D VHUYOHW \RX GR LQVLGH \RXU -63

$ -63¡V VHUYOHW PHWKRG FDQ SURFHVV UHTXHVW SDUDPHWHUV EHFDXVH UHPHPEHU \RXU FRGH LV JRLQJ WR EH LQVLGH D VHUYOHW¡V VHUYLFH PHWKRG <RX GRQ¡W KDYH WR ZRUU\ DERXW ZKLFK RI WKH +773 PHWKRGV *(7 RU 3267 ZDV XVHG LQ WKH UHTXHVW

7KH +70/ IRUP

rvlet that This goes to a se attribute est qu sets the re the request then forwards ng. ´+REE\3DJH GR´! to the JSP youĂ•re writi

KWPO! ERG\! IRUP PHWKRG ´3267´

DFWLRQ &KRRVH D KREE\ S! VHOHFW QDPH ´KREE\´ VL]H ´ ´! RSWLRQ!KRUVH VNLLQJ RSWLRQ!H[WUHPH NQLWWLQJ RSWLRQ!DOSLQH VFXED RSWLRQ!VSHHG GDWLQJ VHOHFW! EU! EU! FHQWHU! LQSXW W\SH ´68%0,7´! FHQWHU! IRUP! ERG\! KWPO!

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63 :H·YH SXW D IHZ OLQHV LQ IRU \RX 7KH FRGH \RX SXW LQ WKLV -63 0867 ZRUN ZLWK WKH FRGH WKDW·V DOUHDG\ KHUH :KHQ \RX·UH GRQH LW VKRXOG EH FRPSLODEOH DQG SURGXFH WKH UHVXOW RQ WKH RSSRVLWH SDJH \RX PXVW $6680( WKDW WKHUH·V DOUHDG\ D ZRUN LQJ VHUYOHW WKDW ILUVW JHWV WKH UHTXHVW VHWV WKH UHTXHVW DWWULEXWH ´QDPHVµ DQG IRUZDUGV WKH UHTXHVW WR WKLV -63

;<78

DO RSWLRQ QRW DQ W RI WKH V L LV K 7 DU H ,W·V S H[HUFLV Q -63 V\QWD[ R Q R V V OH

7KH IULHQGV ZKR VKDUH \RXU KREE\ RI

DUH EU!

,WHUDWRU LW DO LWHUDWRU

Õt use You won

ese!

h all of t

JHW3DUDPHWHU ³KREE\´

$UUD\/LVW

UHTXHVW

$UUD\/LVW

!

SDJH

EU!

JHW$WWULEXWH ³QDPHV´

` !

LPSRUW MDYD XWLO

!

VHVVLRQ

#

JHW$WWULEX

`

LPSRUW

LW KDV1H[W

KWPO! ERG\! UHTXHVW

LW QH[W

ZKLOH

!

ERG\! K WPO!

^ !

!

WH ´KREE\µ

RXW

!

UHTXHVW

#

XWLO ´ LPSRUW ´MDYD JHW3DUDPHWHU ´QDPHVµ

VHVVLRQ

DO

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

H[HUFLVH DQVZHUV

%( WKH &RQWDLQHU $QVZHUV

LV VWUDLJKWIRUZDUG DQG ZRUNV LV D IXQGDPHQWDO -DYD ODQJXDJH LVVXH XVLQJ D ORFDO YDULDEOH EHIRUH LW¡V GHFODUHG DQG DOVR GHPRQVWUDWHV D IXQGDPHQWDO -DYD ODQJXDJH LVVXH³ZKDW KDSSHQV ZKHQ \RX KDYH DQ LQVWDQFH DQG ORFDO YDULDEOH ZLWK WKH VDPH QDPH 6R \RX VHH LI \RX WUDQVODWH WKH -63 FRGH LQWR VHUYOHW -DYD FRGH \RX¡OO KDYH QR WURXEOH ILJXULQJ RXW WKH UHVXOW 2QFH \RXU -63 VWXII LV LQVLGH D VHUYOHW LW¡V MXVW -DYD

This wonÕt compile! ItÕs exactly like writing a method with: void foo() { YouÕre trying to use variable ÔxÕ int y = 5 + x; BEFORE itÕs deÞned. The Java language doesnÕt allow that, and the int x = 2; Container wonÕt bother to rearrange } the order of your scriptlet code.

KWPO! ERG\! 7HVW VFULSWOHWV LQW \ [ ! LQW [ ! ERG\! KWPO!

# SDJH LPSRUW ´MDYD XWLO ´ ! KWPO! ERG\! 7HVW VFULSWOHWV $UUD\/LVW OLVW QHZ $UUD\/LVW OLVW DGG QHZ 6WULQJ ³IRR´

! OLVW JHW ! ERG\! KWPO!

KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS

7HVW VFULSWOHWV IRR

No problems; prints the in Ăžrst (and only ) object the ArrayList.

The

scriptlet de KWPO! ERG\! KWWS ORFDOKRVW WHVW-63 %DVLF&RXQWHU MVS local variable ÒxÓclares a 7HVW VFULSWOHWV hid es the instance (tvahat 7HVW VFULSWOHWV LQW [ ! riable LQW [ ! x) so if you want print the instance to [ ! variable ERG\! KWPO! x (42) inste ad of the local variable x (2 change the ex 2), <%= this.x %>pression to:

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

&RGH 0DJQHWV $QVZHUV

#

SDJH

,I \RXU DQVZHU ORRNV D OLWWOH GLIIHUHQW EXW \RX VWLOO WKLQN LW VKRXOG ZRUN³WU\ LW <RX¡OO KDYH WR PDNH WKH VHUYOHW WKDW WDNHV WKH IRUP UHTXHVW VHWV DQ DWWULEXWH DQG IRUZDUGV GLVSDWFKHV WKH UHTXHVW WR WKH -63

XWLO ´ LPSRUW ´MDYD

!

We need the import page directive because of ArrayList and Iterator.

KWPO! ERG\!

7KH IULHQGV ZKR VKDUH \RXU KREE\ RI

UHTXHVW

JHW3DUDPHWHU ³KREE\´

!

DUH EU!

$UUD\/LVW

$UUD\/LVW

DO

,WHUDWRU LW DO LWHUDWRU

ZKLOH

LW KDV1H[W

LW QH[W

!

^ !

UHTXHVW

JHW$WWULEXWH ³QDPHV´

!

Start a scriptlet up here... and end it here.

Use an expression.

EU! ` !

Finish the while loop block! (If you forget this, it wonÕt compile).

#

ERG\! KWPO!

VHVVLRQ

` LPSRUW UHTXHVW

RXW

LPSRUW MDYD XWLO !

JHW$WWULEX

WH ´KREE\¾

VHVVLRQ JHW3DUDPHWHU ´QDPHV¾

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

YDOLG DQG LQYDOLG H[SUHVVLRQV

6KDUSHQ \RXU SHQFLO $16:(56

) KWUUMV\ 9ES YOU CAN PUT COMMENTS IN YOUR *30 )F YOU RE A *AVA PROGRAMMER WITH VERY LITTLE (4-, EXPERIENCE YOU MIGHT lND YOURSELF TYPING THIS IS A COMMENT WITHOUT THINKING TWICE "UT IF YOU DO THEN UNLESS IT S WITHIN A SCRIPTLET OR DECLARATION TAG YOU LL END UP $)30,!9).' THAT TO THE CLIENT AS PART OF THE RESPONSE )N OTHER WORDS TO THE #ONTAINER THOSE TWO SLASHES ARE JUST MORE TEMPLATE TEXT LIKE h(ELLOv OR h%MAIL IS v 9OU CAN PUT TWO DIFFERENT TYPES OF COMMENTS IN A *30

+70/ FRPPHQW !

7KH &RQWDLQHU MXVW SDVVHV WKLV VWUDLJKW RQ WR WKH FOLHQW ZKHUH WKH EURZVHU LQWHUSUHWV LW DV D FRPPHQW

9DOLG DQG ,QYDOLG ([SUHVVLRQV 9DOLG"

R All primitive literals are Ăžne.

R -ATH RANDOM NO! The semicolon canÕt be here.

R h v String literal is Ăžne.

R -ATH RANDOM Yes, the method returns a double.

R 3TRING S hFOOv NO! You canÕt have a variable declaration here.

R NEW 3TRING; =

-63 FRPPHQW !

7KHVH DUH IRU WKH SDJH GHYHORSHUV DQG MXVW OLNH -DYD FRPPHQWV LQ D -DYD VRXUFH ILOH WKH\¡UH VWULSSHG RXW RI WKH WUDQVODWHG SDJH ,I \RX¡UH W\SLQJ D -63 DQG ZDQW WR SXW LQ FRPPHQWV DERXW ZKDW \RX¡UH GRLQJ WKH ZD\ \RX¡G XVH FRPPHQWV LQ D -DYD VRXUFH ILOH XVH D -63 FRPPHQW ,I \RX ZDQW FRPPHQWV WR VWD\ DV SDUW RI WKH +70/ UHVSRQVH WR WKH FOLHQW DOWKRXJK WKH EURZVHU ZLOO KLGH WKHP IURP WKH FOLHQW¡V YLHZ XVH DQ +70/ FRPPHQW

Yes, because the new String array is an object, and ANY object can be sent to a println() statement.

R NO! The arithmetic is Þne, but thereÕs a space between the % and the =. It canÕt be <% =, it must be <%= .

R Sure, this resolves to a boolean, so it prints ÔtrueÕ.

R FALSE We already said primitive literals are Ăžne.

R NEW #OUNTER No problem. This is just like the String[]... it prints the result of the objectÕs toString() method.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

)81 NWZ \PM OMVMZI\ML [MZ ^TM\ 4HE #ONTAINER GENERATES A CLASS FROM YOUR *30 THAT IMPLEMENTS THE (TTP*SP0AGE INTERFACE 4HIS IS THE ONLY PART OF THE GENERATED SERVLET S !0) THAT YOU NEED TO KNOW 9OU DON T CARE THAT IN 4OMCAT FOR EXAMPLE YOUR GENERATED SERVLET EXTENDS RUJ DSDFKH MDVSHU UXQWLPH +WWS-VS%DVH

!LL YOU NEED TO KNOW ABOUT ARE THE THREE KEY METHODS

MVS,QLW

7KLV PHWKRG LV FDOOHG IURP WKH LQLW PHWKRG <RX FDQ RYHUULGH WKLV PHWKRG &DQ \RX ILJXUH RXW KRZ"

LQWHUIDFH!!

MDYD[ VHUYOHW MVS -VS3DJH MVS,QLW

MVS'HVWUR\

You can override these.

LQWHUIDFH!!

MDYD[ VHUYOHW MVS +WWS-VS3DJH BMVS6HUYLFH +WWS6HUYOHW5HTXHVW +WWS6HUYOHW5HVSRQVH

You CANNOT override this!

MVS'HVWUR\

7KLV PHWKRG LV FDOOHG IURP WKH VHUYOHW·V GHVWUR\ PHWKRG <RX FDQ RYHUULGH WKLV PHWKRG DV ZHOO

BMVS6HUYLFH

7KLV PHWKRG LV FDOOHG IURP WKH VHUYOHW·V VHUYLFH PHWKRG ZKLFK PHDQV LW UXQV LQ D VHSDUDWH WKUHDG IRU HDFK UHTXHVW 7KH &RQWDLQHU SDVVHV WKH 5HTXHVW DQG 5HVSRQVH REMHFWV WR WKLV PHWKRG <RX FDQ·W RYHUULGH WKLV PHWKRG <RX FDQ·W GR $1<7+,1* ZLWK WKLV PHWKRG \RXUVHOI H[FHSW ZULWH FRGH WKDW JRHV LQVLGH LW DQG LW·V XS WR WKH &RQWDLQHU YHQGRU WR WDNH \RXU -63 FRGH DQG IDVKLRQ WKH BMVS6HUYLFH PHWKRG WKDW XVHV LW

1RWH WKH XQGHUVFRUH DW WKH IURQW RI WKH G B MVS6HUYLFH PHWKR KRGV PHW U WZR RWKH WKH QW RI ,W¶V 127 LQ IUR KLQN RI LW WKLV MVS,QLW DQG MVS'HVWUR\ 7 W RI WKH PHWKRG ZD\ WKH XQGHUVFRUH LQ IURQ PHDQV ³GRQ¶W WRXFK ´ RI WKH QDPH 6R QR XQGHUVFRUH LQ IURQW XW LI WKHUH ,6 PHDQV \RX FDQ RYHUULGH % H PHWKRG DQ XQGHUVFRUH LQ IURQW RI WK RYHUULGH LW QDPH \RX PXVW 127 WU\ WR

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 OLIHF\FOH

4QNMKaKTM WN I 2;8 9OU WRITE THE JSP l LE 4HE #ONTAINER WRITES THE JAVA l LE FOR THE SERVLET YOUR *30 BECOMES

.LP ZULWHV D MVS À OH DQG GHSOR\V LW DV SDUW RI D ZHE DSS

7KH &RQWDLQHU ´UHDGVÂľ WKH ZHE [PO '' IRU WKLV DSS EXW GRHVQ¡W GR DQ\WKLQJ HOVH ZLWK WKH MVS Ă€ OH XQWLO WKH Ă€ UVW WLPH LW¡V UHTXHVWHG

"[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"!

KWPO! ERG\! LQW [ ! +HOOR [ ! ERG\! KWPO!

'2&7<3(

ZHE [PO

:HE &RQWDLQHU

7KH FOLHQW KLWV D OLQN WKDW DVNV IRU WKH MVS

0\-63 MVS

7KH &RQWDLQHU WULHV WR 75$16/$7( WKH MVS LQWR MDYD VRXUFH FRGH IRU D VHUYOHW FODVV

UHTXHVW WUDQVODWH

KWPO! ERG\! LQW [ ! +HOOR [ ! ERG\! KWPO!

on sittingtihengrefor a ItÕs justrver ...wai it. the se to re quest client

errors are JSP syntinaxthis phase. caught

JHQHUDWH

SDFNDJH RUJ DSDFKH LPSRUW MD YD[ VHUYOHW +WWS6HUY OHW

0\-63BMVS MDYD

0\-63 MVS

:HE &RQWDLQHU

7KH &RQWDLQHU WULHV WR &203,/( WKH VHUYOHW MDYD VRXUFH LQWR D FODVV À OH

FRPSLOH

SDFNDJH RUJ DSDFKH LPSRUW MD YD[ VHUYOHW +WWS6HUY OHW

0\-63BMVS MDYD

Java errorslaanrguage/syntax e caught here.

JHQHUDWH

0\-63BMVS FODVV

:HE &RQWDLQHU

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

2;8 TQNMKaKTM KWV\QV]ML

7KH &RQWDLQHU /2$'6 WKH QHZO\ JHQHUDWHG VHUYOHW FODVV

ORDG

:HE &RQWDLQHU

0\-63BMVS FODVV

7KH &RQWDLQHU LQVWDQWLDWHV WKH VHUYOHW DQG FDXVHV WKH VHUYOHW¡V MVS,QLW PHWKRG WR UXQ

7KH REMHFW LV QRZ D IXOO Ă HGJHG VHUYOHW UHDG\ WR DFFHSW FOLHQW UHTXHVWV

MVS,QLW

EHFRPHV

:HE &RQWDLQHU

0\-63B MVS

S 0\ -63B MV

7KH &RQWDLQHU FUHDWHV D QHZ WKUHDG WR KDQGOH WKLV FOLHQW¡V UHTXHVW DQG WKH VHUYOHW¡V BMVS6HUYLFH PHWKRG UXQV 0\-63B MVS

B MVS6HUYL FH

(YHU\WKLQJ WKDW KDSSHQV DIWHU WKLV LV MXVW SODLQ ROG VHUYOHW UHTXHVW KDQGOLQJ (YHQWXDOO\ WKH VHUYOHW VHQGV D UHVSRQVH EDFN WR WKH FOLHQW RU IRUZDUGV WKH UHTXHVW WR DQRWKHU ZHE DSS FRPSRQHQW

:HE &RQWDLQHU

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

WUDQVODWLRQ DQG FRPSLODWLRQ

:RZ , DP WUXO\ LPSUHVVHG , ZRXOG QHYHU KDYH JXHVVHG WKDW WKH\ FRXOG PDNH UHTXHVWLQJ D -63 WDNH MXVW DV PXFK RYHUKHDG DV FDOOLQJ D PHWKRG RQ DQ (-% ,·P WKLQNLQJ WKH FOLHQW KDV WR ZDLW ZKDW ÀYH PLQXWHV IRU DOO WKDW WUDQVODWLQJ FRPSLOLQJ DQG LQLWLDOL]LQJ"

<ZIV[TI\QWV IVL KWUXQTI\QWV PIXXMV[ WVTa 76+7HEN YOU DEPLOY A WEB APP WITH A *30 THE WHOLE TRANSLATION AND COMPILATION STEP HAPPENS ONLY ONCE IN THE *30 S LIFE /NCE IT S BEEN TRANSLATED AND COMPILED IT S JUST LIKE ANY OTHER SERVLET !ND JUST LIKE ANY OTHER SERVLET ONCE THAT SERVLET HAS BEEN LOADED AND INITIALIZED THE ONLY THING THAT HAPPENS AT REQUEST TIME IS CREATION OR ALLOCATION OF A THREAD FOR THE SERVICE METHOD 3O THE PICTURE ON THE PREVIOUS TWO PAGES IS FOR ONLY THE lRST REQUEST

4

/+ SO THAT MEANS ONLY THE lRST CLIENT TO ASK FOR THE *30 TAKES THE BIG HIT "UT THERE -534 BE A WAY TO CONlGURE THE SERVER TO PRE TRANSLATE AND COMPILE RIGHT

$

!LTHOUGH IT S ONLY THE FIRST CLIENT THAT HAS TO WAIT MOST #ONTAINER VENDORS $/ GIVE YOU A WAY TO ASK FOR THE WHOLE TRANSLATION COMPILATION THING TO HAPPEN IN ADVANCE SO THAT EVEN THE FIRST REQUEST HAPPENS LIKE ANY OTHER SERVLET REQUEST "UT WATCH OUTˆIT S VENDOR DEPENDENT AND NOT GUARANTEED 4HERE )3 A MENTION IN THE *30 SPEC *30 OF A SUGGESTED PROTOCOL FOR *30 PRECOMPILATION 9OU MAKE A REQUEST FOR THE *30 APPENDING A QUERY STRING h JSP?PRECOMPILEv AND THE #ONTAINER MIGHT IF IT CHOOSES DO THE TRANSLATION COMPILATION RIGHT THEN INSTEAD OF WAITING FOR THE FIRST REAL REQUEST

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

,I WKH -63 WXUQV LQWR D VHUYOHW , ZRQGHU LI , FDQ FRQÀJXUH VHUYOHW LQLW SDUDPHWHUV DQG ZKLOH ,·P DW LW , ZRQGHU LI , FDQ RYHUULGH WKH VHUYOHW·V LQLW PHWKRG

6KDUSHQ \RXU SHQFLO 7KLQN DERXW WKHVH TXHVWLRQV )OLS EDFN WKURXJK HDUOLHU SDJHV DQG FKDSWHUV LI \RX QHHG WR EXW GRQ·W WXUQ WKH SDJH XQWLO \RX·YH GRQH WKLV <HV \RX &$1 JHW VHUYOHW LQLW SDUDPHWHUV IURP D VHUYOHW WKH TXHVWLRQV DUH

+RZ ZRXOG \RX UHWULHYH WKHP LQ \RXU FRGH" %LJ KXJH JUDYHO KDXOLQJ KLQW SUHWW\ FORVH WR WKH VDPH ZD\ \RX UHWULHYH WKHP LQ D ´QRUPDOµ VHUYOHW )URP ZKLFK REMHFW GR \RX QRUPDOO\ JHW VHUYOHW LQLW SDUDP HWHUV" ,V WKDW REMHFW DYDLODEOH WR \RXU -63 FRGH"

+RZ ZKHUH ZRXOG \RX FRQILJXUH WKH VHUYOHW LQLW SDUDPHWHUV"

6XSSRVH \RX GR ZDQW WR RYHUULGH WKH LQLW PHWKRG KRZ ZRXOG \RX GR LW" ,V WKHUH VRPHWKLQJ HOVH \RX FDQ GR WKDW·OO JLYH \RX WKH VDPH UHVXOW"

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

RYHUULGLQJ MVS,QLW

1VQ\QITQbQVO aW]Z 2;8 9OU CAN DO SERVLET INITIALIZATION STUFF IN YOUR *30 BUT IT S SLIGHTLY DIFFERENT FROM WHAT YOU DO IN A REGULAR SERVLET

&RQÀJXULQJ VHUYOHW LQLW SDUDPHWHUV 9OU CONlGURE SERVLET INIT PARAMS FOR YOUR *30 VIRTUALLY THE SAME WAY YOU CONlGURE THEM FOR A NORMAL SERVLET 4HE ONLY DIFFERENCE IS THAT YOU HAVE TO ADD A JSP lLE ELEMENT WITHIN THE SERVLET TAG ZHE DSS ! om e thatÕs differentÒapfrply VHUYOHW! This is the only linIt s, say y all sic ba t. VHUYOHW QDPH!0\7HVW,QLW VHUYOHW QDPH! a regular servle <servlet> tag to the MVS ¿OH! 7HVW,QLW MVS MVS ¿OH! everything in this om this JSP page...Ó LQLW SDUDP! servlet created fr SDUDP QDPH!HPDLO SDUDP QDPH! SDUDP YDOXH!LNLFNHGEXWW#ZLFNHGO\VPDUW FRP SDUDP YDOXH! LQLW SDUDP! VHUYOHW! ZHE DSS!

2YHUULGLQJ MVS,QLW

9ES IT S THAT SIMPLE )F YOU IMPLEMENT A JSP)NIT METHOD THE #ONTAINER CALLS THIS METHOD AT THE BEGINNING OF THIS PAGE S LIFE AS A SERVLET )T S CALLED FROM THE SERVLET S INIT METHOD SO BY THE TIME THIS METHOD RUNS THERE IS A 3ERVLET#ONlG AND 3ERVLET#ONTEXT AVAILABLE TO THE SERVLET 4HAT MEANS YOU CAN CALL GET3ERVLET#ONlG AND GET3ERVLET#ONTEXT FROM WITHIN THE JSP)NIT METHOD 4HIS EXAMPLE USES THE JSP)NIT METHOD TO RETRIEVE A SERVLET INIT PARAMETER CONlGURED IN THE $$ AND USES THE VALUE TO SET AN APPLICATION SCOPED ATTRIBUTE

Init() Override the jspde ration. method using a cla

SXEOLF YRLG MVS,QLW ^

t, so you YouÕre in a servleeri ted inh ur yo l can cal thod ! getServletConÞg() me

6HUYOHW&RQ¿J V&RQ¿J JHW6HUYOHW&RQ¿J 6WULQJ HPDLO$GGU V&RQ¿J JHW,QLW3DUDPHWHU ³HPDLO´ 6HUYOHW&RQWH[W FW[ JHW6HUYOHW&RQWH[W FW[ VHW$WWULEXWH ³PDLO´ HPDLO$GGU ` !

This is EXACTLY what youÕd do in a normal servlet.

Get a reference to the Ser Context and set an application-scopevlet attribute.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

)\\ZQJ]\M[ QV I 2;8 4HE EXAMPLE ON THE OPPOSITE PAGE SHOWS THE *30 SETTING AN APPLICATION SCOPED ATTRIBUTE USING A METHOD DECLARATION THAT OVERRIDES JSP)NIT "UT MOST OF THE TIME YOU LL BE USING ONE OF THE FOUR IMPLICIT OBJECTS TO GET AND SET ATTRIBUTES CORRESPONDING TO THE FOUR ATTRIBUTE SCOPES AVAILABLE IN A *30 9ES FOUR 2EMEMBER IN ADDITION TO THE STANDARD SERVLET REQUEST SESSION AND APPLICATION CONTEXT SCOPES A *30 ADDS A FOURTH SCOPEˆPAGE SCOPE ˆTHAT YOU GET FROM A PAGE#ONTEXT OBJECT 9OU USUALLY WON T NEED OR CARE ABOUT PAGE SCOPE UNLESS YOU RE DEVELOPING CUSTOM TAGS SO WE WON T SAY ANY MORE ABOUT IT UNTIL THE CUSTOM TAGS CHAPTER

,Q D VHUYOHW

,Q D -63 XVLQJ LPSOLFLW REMHFWV

$SSOLFDWLRQ

JHW6HUYOHW&RQWH[W VHW$WWULEXWH ´IRRµ EDU2EM

DSSOLFDWLRQ VHW$WWULEXWH ´IRRµ EDU2EM

5HTXHVW

UHTXHVW VHW$WWULEXWH ´IRRµ EDU2EM

UHTXHVW VHW$WWULEXWH ´IRRµ EDU2EM

6HVVLRQ

UHTXHVW JHW6HVVLRQ VHW$WWULEXWH ´IRRµ EDU2EM

VHVVLRQ VHW$WWULEXWH ´IRRµ EDU2EM

3DJH

'RHV QRW DSSO\

SDJH&RQWH[W VHW$WWULEXWH ´IRRµ EDU2EM

"UT THIS ISN T THE WHOLE STORY )N A *30 THERE S ANOTHER WAY TO GET AND SET ATTRIBUTES AT ANY SCOPE USING ONLY THE PAGE#ONTEXT IMPLICIT OBJECT 4URN THE PAGE AND l ND OUT HOW DV ´FRQWH[Wµ VFRSH 7KHUH·V QR VXFK WKLQJ DSSOLFDWLRQ VFRSH V LQ XWH WULE HYHQ WKRXJK DW QWH[W REMHFW W&R UYOH H 6H DUH ERXQG WR WK LQNLQJ WKDW DWWULEXWHV VWRUHG WR WK RX LQ FN \ KW WUL PLJ 7KH QDPLQJ FRQYHQWLRQ QR VXFK WKLQJ FRQWH[W VFRSH %XW WKHUH¶V UH¶V DQ LQ WKH 6HUYOHW&RQWH[W DUH WKLQN ³DSSOLFDWLRQ´ %XW WKH WH[W´ ³&RQ VHH \RX KHQ 5HPHPEHU Z JHW DSS OHW DQG -63 QDPHV XVHG WR VHUY Q WKH WZHH \ EH WHQF LQFRQVLV OHW \RX VD\ VFRSHG DWWULEXWHV²LQ D VHUY HW$WWULEXWH ³IRR´

J [W QWH W&R YOH JHW6HU D\ RX V 63 \ Q D EXW L XWH ³IRR´

DSSOLFDWLRQ JHW$WWULE

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

SDJH&RQWH[W DQG DWWULEXWHV

=[QVO 8IOM+WV\M`\ NWZ I\\ZQJ]\M[ 9OU CAN USE A 0AGE#ONTEXT REFERENCE TO GET ATTRIBUTES FROM ANY SCOPE INCLUDING THE PAGE SCOPE FOR ATTRIBUTES BOUND TO THE 0AGE#ONTEXT 4HE METHODS THAT WORK WITH OTHER SCOPES TAKE AN INT ARGUMENT TO INDICATE THE SCOPE !LTHOUGH THE ATTRIBUTE ACCESS METHODS COME FROM *SP#ONTEXT YOU LL l ND THE CONSTANTS FOR THE SCOPES INSIDE THE 0AGE#ONTEXT CLASS

-VS&RQWH[W JHW$WWULEXWH 6WULQJ QDPH

JHW$WWULEXWH 6WULQJ QDPH LQW VFRSH

JHW$WWULEXWH1DPHV,Q6FRSH LQW VFRSH

ILQG$WWULEXWH 6WULQJ QDPH

methods that work

PRUH PHWKRGV for ANY scope LQFOXGLQJ VLPLODU PHWKRGV WR VHW DQG UHPRYH DWWULEXWHV IURP DQ\ VFRSH

3DJH&RQWH[W $33/,&$7,21B6&23( 3$*(B6&23( 5(48(67B6&23( 6(66,21B6&23( PRUH ILHOGV JHW5HTXHVW

JHW6HUYOHW&RQILJ

JHW6HUYOHW&RQWH[W

JHW6HVVLRQ

static Ăžnal Ăželds

methods get any imto object plicit

PRUH PHWKRGV

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

-`IUXTM[ ][QVO XIOM+WV\M`\ \W OM\ IVL [M\ I\\ZQJ]\M[ 6HWWLQJ D SDJH VFRSHG DWWULEXWH )ORDW RQH QHZ )ORDW ! SDJH&RQWH[W VHW$WWULEXWH ³IRR´ RQH !

*HWWLQJ D SDJH VFRSHG DWWULEXWH SDJH&RQWH[W JHW$WWULEXWH ³IRR´ !

SDJH&RQWH[W JHW$WWULEXWH 6WULQJ LV IRU SDJH VFRSH

JHW$WWULEXWH 7KHUH DUH 7:2 RYHUORDGHG DJH&RQWH[W RQ S FDOO FDQ \RX RGV PHWK QJ DQG D D RQH DUJ WKDW WDNHV D 6WUL DQG DQ LQW WZR DUJ WKDW WDNHV D 6WULQJ XVW OLNH DOO 7KH RQH DUJ YHUVLRQ ZRUNV M WHV ERXQG 72 WKH RWKHUV²LWœV IRU DWWULEX WKH WZR DUJ WKH SDJH&RQWH[W REMHFW %XW DQ DWWULEXWH YHUVLRQ FDQ EH XVHG WR JHW IURP $1< RI WKH IRXU VFRSHV

8VLQJ WKH SDJH&RQWH[W WR VHW D VHVVLRQ VFRSHG DWWULEXWH )ORDW WZR QHZ )ORDW ! SDJH&RQWH[W VHW$WWULEXWH ³IRR´ WZR 3DJH&RQWH[W 6(66,21B6&23( !

8VLQJ WKH SDJH&RQWH[W WR JHW D VHVVLRQ VFRSHG DWWULEXWH SDJH&RQWH[W JHW$WWULEXWH ³IRR´ 3DJH&RQWH[W 6(66,21B6&23( ! 7HICH IS IDENTICAL TO VHVVLRQ JHW$WWULEXWH ³IRR´ !

8VLQJ WKH SDJH&RQWH[W WR JHW DQ DSSOLFDWLRQ VFRSHG DWWULEXWH (PDLO LV SDJH&RQWH[W JHW$WWULEXWH ³PDLO´ 3DJH&RQWH[W $33/,&$7,21B6&23( !

7ITHIN A *30 THE CODE ABOVE IS IDENTICAL TO (PDLO LV DSSOLFDWLRQ JHW$WWULEXWH ³PDLO´ !

8VLQJ WKH SDJH&RQWH[W WR Ă€ QG DQ DWWULEXWH ZKHQ \RX GRQ¡W NQRZ WKH VFRSH SDJH&RQWH[W Âż QG$WWULEXWH ÂłIRR´ !

? Ăžnd it where

7HERE DOES THE l ND!TTRIBUTE METHOD LOOK )T LOOKS l RST IN THE PAGE CONTEXT SO IF THERE S A hFOOv ATTRIBUTE WITH PAGE CONTEXT SCOPE THEN CALLING l ND!TTRIBUTE 3TRING NAME ON A 0AGE#ONTEXT WORKS JUST LIKE CALLING GET!TTRIBUTE 3TRING NAME ON A 0AGE#ONTEXT "UT IF THERE S NO hFOOv ATTRIBUTE THE METHOD STARTS LOOKING IN OTHER SCOPES FROM MOST RESTRICTED TO LEAST RESTRICTED SCOPE ˆIN OTHER WORDS l RST REQUEST SCOPE THEN SESSION THEN l NALLY APPLICATION SCOPE 4HE l RST ONE IT l NDS WITH THAT NAME WINS \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

WKUHH GLUHFWLYHV

?PQTM _M¼ZM WV \PM []JRMK\ TM\¼[ \ITS UWZM IJW]\ \PM \PZMM LQZMK\Q^M[ 7E ALREADY LOOKED AT THE DIRECTIVE USED FOR GETTING IMPORT STATEMENTS INTO THE GENERATED SERVLET CLASS MADE FROM YOUR *30 4HAT WAS A PAGE DIRECTIVE ONE OF THE THREE DIRECTIVE TYPES WITH AN IMPORT ATTRIBUTE ONE OF ATTRIBUTES OF THE PAGE DIRECTIVE 7E LL TAKE A QUICK LOOK NOW AT THE OTHERS ALTHOUGH SOME WON T BE COVERED IN DETAIL UNTIL LATER CHAPTERS AND SOME WON T BE COVERED IN DETAIL AT ALL IN THIS BOOK BECAUSE THEY RE RARELY USED

7KH SDJH GLUHFWLYH

# SDJH LPSRUW ´IRR ´ VHVVLRQ ´IDOVH´ !

$ElNES PAGE SPECIlC PROPERTIES SUCH AS CHARACTER ENCODING THE CONTENT TYPE FOR THIS PAGE S RESPONSE AND WHETHER THIS PAGE SHOULD HAVE THE IMPLICIT SESSION OBJECT ! PAGE DIRECTIVE CAN USE UP TO THIRTEEN DIFFERENT ATTRIBUTES LIKE THE IMPORT ATTRIBUTE ALTHOUGH ONLY FOUR ATTRIBUTES ARE COVERED ON THE EXAM

7KH WDJOLE GLUHFWLYH

# WDJOLE WDJGLU ´ :(% ,1) WDJV FRRO´ SUH¿[ ´FRRO´ !

$ElNES TAG LIBRARIES AVAILABLE TO THE *30 7E HAVEN T TALKED ABOUT USING CUSTOM TAGS AND STANDARD ACTIONS YET SO THIS MIGHT NOT MAKE ANY SENSE AT THIS POINT *UST GO WITH IT FOR NOW WE HAVE TWO WHOLE CHAPTERS ON TAG LIBRARIES COMING UP SOON

7KH LQFOXGH GLUHFWLYH

# LQFOXGH ¿OH ´ZLFNHG+HDGHU KWPO´ !

$ElNES TEXT AND CODE THAT GETS ADDED INTO THE CURRENT PAGE AT TRANSLATION TIME 4HIS LETS YOU BUILD REUSABLE CHUNKS LIKE A STANDARD PAGE HEADING OR NAVIGATION BAR THAT CAN BE ADDED TO EACH PAGE WITHOUT HAVING TO DUPLICATE ALL THAT CODE IN EACH *30

4

) M CONFUSED THIS PAGE HEADING SAYS hWHILE WE RE ON THE SUBJECT v BUT ) DON T SEE HOW DIRECTIVES HAVE ANYTHING TO DO WITH PAGE#ONTEXT AND ATTRIBUTES

$

4HEY DON T NOT REALLY 7E JUST SAID THAT TO COVER A BAD PATHETIC NONEXISTENT TRANSITION BETWEEN TWO UNRELATED TOPICS 7E HOPED NOBODY WOULD NOTICE BUT ./ YOU JUST COULDN T LET IT GO COULD YOU

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

)\\ZQJ]\M[ \W \PM XIOM LQZMK\Q^M /F THE PAGE DIRECTIVE ATTRIBUTES IN THE *30 SPEC ONLY FOUR ARE COVERED ON THE EXAM 9OU DO ./4 HAVE TO MEMORIZE THE ENTIRE LIST JUST GET A FEEL FOR WHAT YOU CAN DO 7E LL LOOK AT THE IS%,)GNORED AND THE TWO ERROR RELATED ATTRIBUTES IN LATER CHAPTERS

3266,%/< RQ WKH H[DP LPSRUW

'HILQHV WKH -DYD LPSRUW VWDWHPHQWV WKDW·OO EH DGGHG WR WKH JHQHUDWHG VHUYOHW FODVV <RX JHW VRPH LPSRUWV IRU IUHH E\ GHIDXOW MDYD ODQJ GXK MDYD[ VHUYOHW MDYD[ VHUYOHW KWWS DQG MDYD[ VHUYOHW MVS

LV7KUHDG6DIH

'HILQHV ZKHWKHU WKH JHQHUDWHG VHUYOHW QHHGV WR LPSOHPHQW WKH 6LQJOH7KUHDG0RGHO ZKLFK DV \RX NQRZ LV D 6SHFWDFXODUO\ %DG 7KLQJ 7KH GHIDXOW YDOXH LV µWUXHµ ZKLFK PHDQV ´0\ DSS LV WKUHDG VDIH VR , GR 127 QHHG WR LPSOHPHQW 6LQJOH7KUHDG0RGHO ZKLFK , NQRZ LV LQKHUHQWO\ HYLO µ 7KH RQO\ UHDVRQ WR VSHFLIL\ WKLV DWWULEXWH LV LI \RX QHHG WR VHW WKH DWWULEXWH YDOXH WR ´IDOVHµ ZKLFK PHDQV WKDW \RX ZDQW WKH JHQHUDWHG VHUYOHW WR XVH WKH 6LQJOH7KUHDG0RGHO EXW \RX QHYHU ZLOO

FRQWHQW7\SH

'HILQHV WKH 0,0( W\SH DQG RSWLRQDO FKDUDFWHU HQFRGLQJ IRU WKH -63 UHVSRQVH <RX NQRZ WKH GHIDXOW

LV(/,JQRUHG

'HILQHV ZKHWKHU (/ H[SUHVVLRQV DUH LJQRUHG ZKHQ WKLV SDJH LV WUDQVODWHG :H KDYHQ·W WDONHG DERXW (/ \HW WKDW·V FRPLQJ LQ WKH QH[W FKDSWHU )RU QRZ MXVW NQRZ WKDW \RX PLJKW FKRRVH WR LJQRUH (/ V\QWD[ LQ \RXU SDJH DQG WKLV LV RQH RI WKH WZR ZD\V \RX FDQ WHOO WKH &RQWDLQHU

LV(UURU3DJH

'HILQHV ZKHWKHU WKH FXUUHQW SDJH UHSUHVHQWV DQRWKHU -63·V HUURU SDJH 7KH GHIDXOW YDOXH LV ´IDOVHµ EXW LI LW·V WUXH WKH SDJH KDV DFFHVV WR WKH LPSOLFLW H[FHSWLRQ REMHFW ZKLFK LV D UHIHUHQFH WR WKH RIIHQGLQJ 7KURZDEOH ,I IDOVH WKH LPSOLFLW H[FHSWLRQ REMHFW LV QRW DYDLODEOH WR WKH -63

HUURU3DJH

'HILQHV D 85/ WR WKH UHVRXUFH WR ZKLFK XQFDXJKW 7KURZDEOHV VKRXOG EH VHQW ,I \RX GHILQH D -63 KHUH WKHQ WKDW -63 ZLOO KDYH DQ LV(UURU3DJH µWUXHµ DWWULEXWH LQ LWV SDJH GLUHFWLYH

127 RQ WKH H[DP ODQJXDJH

'HILQHV WKH VFULSWLQJ ODQJXDJH XVHG LQ VFULSWOHWV H[SUHVVLRQV DQG GHFODUDWLRQV 5LJKW QRZ WKH RQO\ SRVVLEOH YDOXH LV ´MDYDµ EXW WKH DWWULEXWH LV KHUH EHFDXVH LVQ·W LW MXVW OLNH WKRVH VSHF GHYHORSHUV WR EH WKLQNLQJ RI WKH IXWXUH ZKHQ RWKHU ODQJXDJHV PLJKW EH XVHG

H[WHQGV

'HILQHV WKH VXSHUFODVV RI WKH FODVV WKLV -63 ZLOO EHFRPH <RX ZRQ·W XVH WKLV XQOHVV \RX 5($//< NQRZ ZKDW \RX·UH GRLQJ³LW RYHUULGHV WKH FODVV KLHUDUFK\ SURYLGHG E\ WKH &RQWDLQHU

VHVVLRQ

'HILQHV ZKHWKHU WKH SDJH ZLOO KDYH DQ LPSOLFLW VHVVLRQ REMHFW 7KH GHIDXOW YDOXH LV ´WUXHµ

EXIIHU

'HILQHV KRZ EXIIHULQJ LV KDQGOHG E\ WKH LPSOLFLW RXW REMHFW UHIHUHQFH WR WKH -VS:ULWHU

DXWR)OXVK

'HILQHV ZKHWKHU WKH EXIIHUHG RXWSXW LV IOXVKHG DXWRPDWLFDOO\ 7KH GHIDXOW YDOXH LV ´WUXHµ

LQIR

'HILQHV D 6WULQJ WKDW JHWV SXW LQWR WKH WUDQVODWHG SDJH MXVW VR WKDW \RX FDQ JHW LW XVLQJ WKH JHQHUDWHG VHUYOHW·V LQKHULWHG JHW6HUYOHW,QIR PHWKRG

SDJH(QFRGLQJ

'HILQHV WKH FKDUDFWHU HQFRGLQJ IRU WKH -63 7KH GHIDXOW LV ´,62 µ XQOHVV WKH FRQWHQW7\SH DWWULEXWH DOUHDG\ GHILQHV D FKDUDFWHU HQFRGLQJ RU WKH SDJH XVHV ;0/ 'RFXPHQW V\QWD[ \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

DUH VFULSWOHWV EDG"

7KLV LV 68&+ D QLFH FKDSWHU ZLWK D 9(5< ORYHO\ ORRN DW KRZ WR SXW -DYD FRGH LQ D -63 EXW XP ORRN DW WKLV FRPSDQ\ ZLGH PHPR , MXVW JRW

,QWHURI¿ FH 0HPR IURP WKH &72 85*(17 (IIHFWLYH LPPHGLDWHO\ DQ\RQH FDXJKW XVLQJ VFULSWOHWV H[SUHVVLRQV RU GHFODUDWLRQV LQ WKHLU -63 FRGH ZLOO EH VXVSHQGHG ZLWKRXW SD\ XQWLO VXFK WLPH DV LW FDQ EH GHWHUPLQHG ZKHWKHU WKH SURJUDPPHU ZDV IXOO\ UHVSRQVLEOH RU VLPSO\ WU\LQJ WR PDLQWDLQ VRPH 27+(5 LGLRW¶V FRGH ,I LQ IDFW WKH GHWHUPLQDWLRQ LV PDGH WKDW WKH SURJUDPPHU LV LQ IDFW UHVSRQVLEOH WKH FRPSDQ\ ZLOO JR DKHDG DQG LQ IDFW WHUPLQDWH WKH HPSOR\HH 5LFN )RUHVWHU &KLHI 7HFKQRORJ\ 2I¿ FHU ³5HPHPEHU WKHUH LV QR ³,´ LQ 7($0 ´

³:ULWH \RXU FRGH DV LI WKH QH[W JX\ WR PDLQWDLQ LW LV D KRPLFLGDO PDQLDF ZKR NQRZV ZKHUH \RX OLYH ´ > 1RWH WR +5 ZH XVH ³JX\´ LQ LWV QRQ JHQGHU VSHFL¿ F IRUP @

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

;KZQX\TM\[ KWV[QLMZML PIZUN]T' )S IT TRUE #OULD THERE BE A DOWNSIDE TO PUTTING ALL THIS *AVA INTO YOUR *30 !FTER ALL ISN T THAT THE WHOLE FRICKIN 0/).4 TO A *30 3O THAT YOU WRITE YOUR *AVA IN WHAT IS ESSENTIALLY AN (4-, PAGE AS OPPOSED TO WRITING (4-, IN A *AVA CLASS 3OME PEOPLE BELIEVE /+ TECHNICALLY A LOT OF PEOPLE INCLUDING THE *30 AND 3ERVLET SPEC TEAMS THAT IT S BAD PRACTICE TO PUT ALL THIS *AVA INTO YOUR *30 7HY )MAGINE YOU VE BEEN HIRED TO BUILD A BIG WEB SITE 9OUR TEAM INCLUDES A SMALL HANDFUL OF BACK END *AVA PROGRAMMERS AND A HUGE GROUP OF hWEB DESIGNERSvˆGRAPHIC ARTISTS AND PAGE CREATORS WHO USE $REAMWEAVER AND 0HOTOSHOP TO BUILD FABULOUS LOOKING WEB PAGES 4HESE ARE NOT PROGRAMMERS WELL EXCEPT FOR THE ONES WHO STILL THINK (4-, IS hCODINGv

'R <28 NQRZ -DYD"

'XGH GR , /22. OLNH VRPHRQH ZKR ZRXOG ZULWH FRGH" ,¡P D KLJK SDLG :HE 'HVLJQHU '(6,*1(5 ,¡P DQ $57,67 QRW D FRGHU

$VSLULQJ DFWRUV ZRUNLQJ DV ZHE GHVLJQHUV ZKLOH ZDLWLQJ IRU WKHLU ELJ VKRZEL] EUHDN

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

VFULSWLQJ LV RXW WKHUH

7ZR TXHVWLRQV³:+< DUH \RX PDNLQJ XV OHDUQ LW DQG :+$7 LV WKH DOWHUQDWLYH" :KDW WKH I HOVH ,6 WKHUH EHVLGHV +70/ LI \RX FDQ·W SXW VFULSWOHWV GHFODUDWLRQV DQG H[SUHVVLRQV LQ \RXU -63"

<PMZM LQLV¼\ ][ML \W *- IV IT\MZVI\Q^M 4HAT MEANS THERE ARE ALREADY MOUNTAINS OF *30 lLES BRIMMING WITH *AVA CODE STUCK IN EVERY CONCEIVABLE SPOT IN THE PAGE NESTLED BETWEEN SCRIPTLET EXPRESSION AND DECLARATION TAGS )T S OUT THERE AND THERE ISN T ANYTHING ANYONE CAN DO TO CHANGE THE PAST 3O THAT MEANS YOU VE GOT TO KNOW HOW TO READ AND UNDERSTAND THESE ELEMENTS AND HOW TO MAINTAIN PAGES WRITTEN WITH THEM UNLESS YOU RE GIVEN THE CHANCE TO MASSIVELY REFACTOR THE APP S *30S 3ECRETLY WE THINK THERE S STILL A PLACE FOR SOME OF THIS NOTHING BEATS A LITTLE *AVA IN A *30 FOR QUICKLY TESTING SOMETHING OUT ON YOUR SERVER "UT FOR THE MOST PART YOU DON T WANT TO USE THIS FOR REAL PRODUCTION PAGES 4HE REASON THIS IS ALL ON THE EXAM IS BECAUSE THE ALTERNATIVES ARE STILL FAIRLY NEW SO MOST OF THE PAGES OUT THERE TODAY ARE STILL hOLD SCHOOLv &OR THE TIME BEING YOU STILL HAVE TO BE ABLE TO WORK WITH IT !T SOME POINT WHEN THE NEW *AVA FREE TECHNIQUES HIT CRITICAL MASS THE OBJECTIVES FROM THIS CHAPTER WILL PROBABLY DROP OFF THE EXAM AND WE LL ALL BREATHE A COLLECTIVE SIGH AT THE DEATH OF *AVA IN *30S "UT TODAY IS NOT THAT DAY

1RWH WR SDUHQWV DQG WHDFKHUV WKH IRXU OHW WHU ZRUG LPSOLHG LQ WKLV WKRXJKW EXEEOH WKDW VWDUWV ZLWK ´Iµ IROORZHG E\ WKUHH DVWHULVNV LV 127 ZKDW \RX WKLQN ,W ZDV MXVW D ZRUG WKDW ZH IRXQG WRR IXQQ\ WR LQFOXGH ZLWKRXW GLVWUDFWLQJ WKH UHDGHU VR ZH EOHHSHG LW RXW %HFDXVH LW·V IXQQ\ 1RW EDG

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

2K LI RQO\ WKHUH ZHUH D ZD\ LQ D -63 WR XVH VLPSOH WDJV WKDW FDXVH -DYD PHWKRGV WR UXQ ZLWKRXW KDYLQJ WR SXW DFWXDO -DYD FRGH LQWR WKH SDJH

-4" \PM IV[_MZ \W _MTT M^MZa\PQVO /R ALMOST EVERYTHING "UT CERTAINLY AN ANSWER TO TWO BIG COMPLAINTS ABOUT PUTTING ACTUAL *AVA INTO A *30 7EB PAGE DESIGNERS SHOULDN T HAVE TO KNOW *AVA *AVA CODE IN A *30 IS HARD TO CHANGE AND MAINTAIN %, STANDS FOR h%XPRESSION ,ANGUAGEv AND IT BECAME OFl CIALLY PART OF THE SPEC BEGINNING WITH *30 SPEC %, IS NEARLY ALWAYS A MUCH SIMPLER WAY TO DO SOME OF THE THINGS YOU D NORMALLY DO WITH SCRIPTLETS AND EXPRESSIONS /F COURSE RIGHT NOW YOU RE THINKING h"UT IF ) WANT MY *30 TO USE CUSTOM METHODS HOW CAN ) DECLARE AND WRITE THOSE METHODS IF ) CAN T USE *AVA v !HHHH WRITING THE ACTUAL FUNCTIONALITY METHOD CODE IS NOT THE PURPOSE OF %, 4HE PURPOSE OF %, IS TO OFFER A SIMPLER WAY TO INVOKE *AVA CODEˆBUT THE CODE ITSELF BELONGS SOMEWHERE ELSE 4HAT MEANS IN A REGULAR OLD *AVA CLASS THAT S EITHER A *AVA"EAN A CLASS WITH STATIC METHODS OR SOMETHING CALLED A 4AG (ANDLER )N OTHER WORDS YOU DON T WRITE METHOD CODE INTO YOUR *30 WHEN YOU RE FOLLOWING TODAY S "EST 0RACTICES 9OU WRITE THE *AVA METHOD SOMEWHERE ELSE AND CALL IT USING %,

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

¿ UVW ORRN DW (/

;VMIS XMMS I\ -4 4HE ENTIRE NEXT CHAPTER IS ON %, SO WE WON T GO INTO DETAILS HERE 4HE ONLY REASON WE RE COVERING IT IS BECAUSE IT S YET ANOTHER KIND OF ELEMENT WITH ITS OWN SYNTAX THAT GOES IN A *30 AND THE EXAM OBJECTIVES FOR THIS CHAPTER INCLUDE RECOGNIZING EVERYTHING THAT CAN GO INTO A *30

7KLV (/ H[SUHVVLRQ 3OHDVH FRQWDFW ^DSSOLFDWLRQ6FRSH PDLO`

$/:$<6 $Q (/ H[SUHVVLRQ PHWKLQJ` ORRNV OLNH WKLV ^VR H[SUHVVLRQ ,Q RWKHU ZRUGV WKH G LQ FXUO\ LV $/:$<6 HQFORVH G ZLWK D EUDFHV DQG SUH¿ [H GROODU VLJQ

,V WKH VDPH DV WKLV -DYD H[SUHVVLRQ 3OHDVH FRQWDFW DSSOLFDWLRQ JHW$WWULEXWH ³PDLO´ !

WKHUH DUH QR

'XPE 4XHVWLRQV

4

.OT TO BE ALL NEGATIVE BUT ) M NOT SURE ) SEE AN EARTH SHATTERING DIFFERENCE BETWEEN THE %, AND THE *AVA EXPRESSION 3URE IT S A LITTLE SHORTER BUT IS THAT WORTH A WHOLE NEW SCRIPTING LANGUAGE AND *30 CODING APPROACH

$

9OU 3/ HAVEN T SEEN THE FULL BENEFIT OF %, YET 4HE DIFFERENCES WILL BECOME OBVIOUS IN THE NEXT CHAPTER WHEN WE DIVE IN "UT YOU MUST REMEMBER THAT TO A *AVA PROGRAMMER %, IS ./4 NECCESSARILY A DRAMATIC DEVELOPMENT ADVANTAGE )N FACT TO A *AVA PROGRAMMER IT SIMPLY MEANS hONE MORE THING WITH ITS OWN SYNTAX AND EVERYTHING TO LEARN WHEN HEY ) ALREADY +./7 *AVA v "UT IT S NOT ALWAYS ABOUT YOU %, IS MUCH EASIER FOR A NON *AVA PROGRAMMER TO LEARN AND GET UP TO SPEED IN !ND FOR A *AVA PROGRAMMER IT IS STILL MUCH EASIER TO MAINTAIN A SCRIPTLESS PAGE 9ES IT S STILL SOMETHING TO LEARN )T DOESN T LET WEB PAGE DESIGNERS COMPLETELY OFF THE HOOK BUT YOU LL SOON SEE THAT IT S MORE INTUITIVE AND NATURAL FOR A WEB DESIGNER TO USE %, &OR NOW IN THIS CHAPTER YOU SIMPLY NEED TO BE ABLE TO RECOGNIZE %, WHEN YOU SEE IT !ND DON T WORRY AT THIS POINT ABOUT RECOGNIZING WHETHER THE EXPRESSION ITSELF IS VALIDˆALL WE CARE ABOUT NOW IS THAT YOU CAN PICK OUT AN %, EXPRESSION IN A *30 PAGE

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

$QG MXVW +2: GR \RX H[SHFW PH WR JHW P\ SURJUDPPHUV WR VWRS XVLQJ VFULSWLQJ HOHPHQWV LQ WKHLU -63V"

(DV\³\RX FDQ SXW DQ HOHPHQW LQ WKH '' WKDW GLVDEOHV DOO VFULSWLQJ HOHPHQWV

=[QVO $[KZQX\QVO QV^ITQL& )T S SIMPLEˆYOU CAN MAKE IT INVALID FOR A *30 TO HAVE SCRIPTING ELEMENTS SCRIPTLETS *AVA EXPRESSIONS OR DECLARATIONS BY PUTTING A SCRIPTING INVALID TAG IN THE $$ ZHE DSS ! MVS FRQ¿J! MVS SURSHUW\ JURXS! XUO SDWWHUQ! MVS XUO SDWWHUQ! VFULSWLQJ LQYDOLG!

g elements WUXH les scriptin VFULSWLQJ LQYDOLG! This disab JSPs in the app card MVS SURSHUW\ JURXS! for ALL we used the wild .) MVS FRQ¿J! (because the URL pattern *.jsp as ZHE DSS!

This does not work ! The isScriptingEnabled attribute is no longer in the JSP spec !

7ATCH OUTˆYOU MIGHT HAVE SEEN OTHER BOOKS OR ARTICLES SHOW A PAGE DIRECTIVE THAT DISABLES SCRIPTING )N A DRAFT VERSION OF THE SPEC THERE WAS A PAGE DIRECTIVE ATTRIBUTE PAGE IS3CRIPTING%NABLED vFALSEv BUT IT WAS REMOVED FROM THE lNAL SPEC 4HE ONLY WAY TO INVALIDATE SCRIPTING NOW IS THROUGH THE SCRIPTING INVALID $$ TAG

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

LJQRULQJ (/

AW] KIV KPWW[M \W QOVWZM -4 9ES %, IS A GOOD THING THAT S GOING TO SAVE THE WORLD AS WE KNOW IT "UT SOMETIMES YOU MIGHT WANT TO DISABLE IT 7HY 4HINK BACK TO WHEN THE ASSERT KEYWORD WAS ADDED TO THE *AVA LANGUAGE WITH VERSION 3UDDENLY THE FORMERLY UNRESERVED AND PERFECTLY LEGAL IDENTIl ER hASSERTv MEANT SOMETHING TO THE COMPILER 3O IF YOU HAD SAY A VARIABLE NAMED ASSERT YOU WERE SCREWED %XCEPT THAT * 3% VERSION CAME WITH ASSERTIONS DISABLED BY DEFAULT )F YOU KNEW YOU WERE WRITING OR RECOMPILING CODE THAT DIDN T USE ASSERT AS AN IDENTIl ER THEN YOU COULD CHOOSE TO ENABLE ASSERTIONS 3O IT S KIND OF THE SAME THING WITH DISABLING %,ˆIF YOU HAPPENED TO HAVE TEMPLATE TEXT PLAIN OLD (4-, OR TEXT IN YOUR *30 THAT INCLUDED SOMETHING THAT LOOKED LIKE %, [SOMETHING] YOU D BE IN "IG 4ROUBLE IF YOU COULDN T TELL THE #ONTAINER TO JUST IGNORE ANYTHING THAT APPEARS TO BE %, AND INSTEAD TREAT IT LIKE ANY OTHER UNPROCESSED TEXT %XCEPT THERE S ONE BIG DIFFERENCE BETWEEN %, AND ASSERTIONS

%L IS ENABLED BY DEFAULT )F YOU WANT %, LOOKING THINGS IN YOUR *30 TO BE IGNORED YOU HAVE TO SAY SO EXPLICITLY EITHER THROUGH A PAGE DIRECTIVE OR A $$ ELEMENT

3XWWLQJ HO LJQRUHG! LQ WKH '' ZHE DSS ! MVS FRQÂż J! MVS SURSHUW\ JURXS! XUO SDWWHUQ! MVS XUO SDWWHUQ! HO LJQRUHG!

WUXH HO LJQRUHG! MVS SURSHUW\ JURXS! MVS FRQÂż J! ZHE DSS!

8VLQJ WKH LV(/,JQRUHG SDJH GLUHFWLYH DWWULEXWH # SDJH LV(/,JQRUHG ´WUXH´ !

arts tive attribute stesnÕt ! The page directh do g ta DD with ÒisÓ, but e

7KH SDJH GLUHFWLYH WDNHV SULRULW\ RYHU WKH '' VHWWLQJ

HQ WKH ,I WKHUHÂśV D FRQĂ€ LFW EHWZH '' DQG HO LJQRUHG! VHWWLQJ LQ WKH WLYH DWWUL WKH LV(/,JQRUHG SDJH GLUHF ZLQV 7KDW EXWH WKH GLUHFWLYH DOZD\V EHKDYLRU OHWV \RX VSHFLI\ WKH GHIDXOW U D VSHFLÂż F LQ WKH '' EXW RYHUULGH LW IR SDJH XVLQJ D SDJH GLUHFWLYH

:DWFK R XW IRU WKH QDP LQJ LQFRQVLVW HQF\ 7KH '' WD J LV HO LJ QRUHG! V UHDVRQDE R RQH PLJ O\ WKLQN WK KW DW WK DWWULEXWH Z RXOG EH R H SDJH GLUHFWLYH K PD\EH %XW QR R HO,JQRUHG" QH ZRXOG EH Z WR WKH QDWX UDO FRQFOXV URQJ LI RQH MXPSHG GLUHFWLYH IRU LJQRULQ LRQ 7KH '' DQG J (/ GR Q 'RQÂśW EH IR RW P ROHG E\ L V HO LJQRUH DWFK G!

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

*]\ _IQ\ \PMZMÂź[ [\QTT IVW\PMZ 2;8 MTMUMV\ _M PI^MVÂź\ [MMV" IK\QWV[ 3O FAR YOU VE SEEN lVE DIFFERENT TYPES OF ELEMENTS THAT CAN APPEAR IN A *30 SCRIPTLETS DIRECTIVES DECLARATIONS *AVA EXPRESSIONS AND %, EXPRESSIONS "UT WE HAVEN T SEEN ACTIONS 4HEY COME IN TWO mAVORS STANDARD AND NOT

6WDQGDUG $FWLRQ MVS LQFOXGH SDJH ´ZLFNHG)RRWHU MVS´ !

2WKHU $FWLRQ F VHW YDU ´UDWH´ YDOXH ´ ´ !

worry about For now, donÕt or how they work, do e what thes e an action when just recognizsyntax in a JSP. you see the go into the details. Later, weÕll

!LTHOUGH THAT S MISLEADING BECAUSE THERE ARE SOME ACTIONS THAT AREN T CONSIDERED STANDARD ACTIONS BUT WHICH ARE STILL PART OF A NOW STANDARD LIBRARY )N OTHER WORDS YOU LL LATER LEARN THAT SOME OF THE NON STANDARD THE OBJECTIVES REFER TO THEM AS CUSTOM ACTIONS ARE STANDARD BUT YET THEY STILL AREN T CONSIDERED hSTANDARD ACTIONSv 9ES THAT S RIGHTˆTHEY RE STANDARDIZED NON STANDARD CUSTOM ACTIONS $OESN T THAT JUST CLEAR IT RIGHT UP FOR YOU )N A LATER CHAPTER WHEN WE GET TO hUSING TAGSv WE LL HAVE A SLIGHTLY RICHER VOCABULARY WITH WHICH TO TALK ABOUT THIS IN MORE DETAIL SO RELAX &OR NOW ALL WE CARE ABOUT IS RECOGNIZING AN ACTION WHEN YOU SEE IT IN A *30

6KDUSHQ \RXU SHQFLO /RRN DW WKH V\QWD[ IRU DQ DFWLRQ DQG FRPSDUH LW WR WKH V\QWD[ IRU WKH RWKHU NLQGV RI -63 HOHPHQWV 7KHQ DQVZHU WKLV :KDW DUH WKH GLIIHUHQFHV EHWZHHQ DQ DFWLRQ HOHPHQW DQG D VFULSWOHW"

+RZ ZLOO \RX UHFRJQL]H DQ DFWLRQ ZKHQ \RX VHH LW"

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

HYDOXDWLRQ H[HUFLVH

(YDOXDWLRQ 0DWUL[

([HUFLVH

7KLQN DERXW ZKDW KDSSHQV ZKHQ HDFK RI WKHVH VHWWLQJV RU FRPELQDWLRQ RI VHWWLQJV RFFXUV <RX·OO VHH WKH DQVZHUV ZKHQ \RX WXUQ WKH SDJH VR GR WKLV RQH 12: 3ODFH D FKHFNPDUN LQ WKH HYDOXDWHG FROXPQ LI WKH VHWWLQJV ZRXOG FDXVH WKH (/ H[SUHVVLRQV WR EH HYDOXDWHG 25 SODFH D FKHFNPDUN LQ WKH LJQRUHG FROXPQ LI (/ ZLOO EH WUHDWHG OLNH RWKHU WHPSODWH WH[W 1R URZ ZLOO KDYH WZR FKHFNPDUNV RI FRXUVH

(/ (YDOXDWLRQ

'' FRQÀJXUDWLRQ HO LJQRUHG!

SDJH GLUHFWLYH LV(/,JQRUHG

XQVSHFLILHG

XQVSHFLILHG

IDOVH

XQVSHFLILHG

WUXH

XQVSHFLILHG

IDOVH

IDOVH

IDOVH

WUXH

WUXH

IDOVH

6FULSWLQJ YDOLGLW\ '' FRQÀJXUDWLRQ VFULSWLQJ LQYDOLG!

HYDOXDWHG

HYDOXDWHG

LJQRUHG

3ODFH D FKHFNPDUN LQ WKH HYDOXDWHG FROXPQ LI WKH VHWWLQJV ZRXOG FDXVH WKH VFULSWLQJ H[SUHVVLRQV WR EH HYDOXDWHG 25 SODFH D FKHFNPDUN LQ WKH LJQRUHG FROXPQ LI VFULSWLQJ ZLOO EH WUHDWHG OLNH RWKHU WHPSODWH WH[W

LJQRUHG

XQVSHFLILHG WUXH IDOVH

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

-63 (OHPHQW 0DJQHWV 0DWFK WKH -63 HOHPHQW ZLWK LWV ODEHO E\ SODFLQJ WKH -63 VQLSSHW LQ WKH ER[ ZLWK WKH ODEHO UHSUHVHQWLQJ WKDW HOHPHQW W\SH 5HPHPEHU \RX¡OO KDYH 'UDJ DQG 'URS TXHVWLRQV RQ WKH UHDO H[DP VLPLODU WR WKLV H[HUFLVH VR GRQ¡W VNLS LW

-63 HOHPHQW W\SH

-63 VQLSSHW

GLUHFWLYH

r and hese ove Drag t hem onto the drop ting label. match GHFODUDWLRQ

! QHZ )ORDW )ORDW RQH ! LQW \

(/ H[SUHVVLRQ

# SDJH LPSRUW ´MDYD XWLO ´ !

! OH ´IRR KWPO´ MVS LQFOXGH ¿

VFULSWOHW

SDJH&RQWH[W JHW$WWULEXWH ³IRR´ !

HPDLO ^DSSOL FDWLRQ6FRSH PD LO`

H[SUHVVLRQ

DFWLRQ \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 HOHPHQWV H[HUFLVH

-63 (OHPHQW 0DJQHWV WKH 6HTXHO <RX NQRZ ZKDW WKH\·UH FDOOHG EXW GR \RX UHPHPEHU ZKHUH WKH\ JR LQ WKH JHQHUDWHG VHUYOHW" 2I FRXUVH \RX GR %XW WKLV LV MXVW D OLWWOH UHLQIRUFHPHQW SUDFWLFH EHIRUH ZH PRYH RQ WR D GLIIHUHQW FKDSWHU DQG WRSLF 3XW WKH HOHPHQW LQ WKH ER[ FRUUHVSRQGLQJ WR ZKHUH WKDW HOHPHQW·V JHQHUDWHG FRGH ZLOO JR LQ WKH VHUYOHW FODVV ILOH 1RWH WKDW WKH PDJQHW LWVHOI GRHV QRW UHSUHVHQW WKH $&78$/ FRGH WKDW ZLOO EH JHQHUDWHG

SXEOLF ¿ QDO FODVV %DVLF&RXQWHUBMVS H[WHQGV RUJ DSDFKH MDVSHU UXQWLPH +WWS-VS%DVH LPSOHPHQWV RUJ DSDFKH MDVSHU UXQWLPH -VS6RXUFH'HSHQGHQW ^

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^

The order of these three magnets does not matter.

` `

UHTXHVW JHW$WWULEXWH ³IRR´ !

# SDJH LPSRUW ´MDYD XWLO ´ !

HPDLO ^DSSOLFDWLRQ6FRSH PDLO`

)ORDW RQH QHZ )ORDW !

LQW \ !

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

(YDOXDWLRQ 0DWUL[ $16:(56

([HUFLVH

(/ (YDOXDWLRQ

'' FRQÀJXUDWLRQ HO LJQRUHG!

SDJH GLUHFWLYH LV(/,JQRUHG

XQVSHFLILHG

XQVSHFLILHG

IDOVH

XQVSHFLILHG

WUXH

XQVSHFLILHG

IDOVH

IDOVH

IDOVH

WUXH

WUXH

IDOVH

HYDOXDWHG

LJQRUHG

6FULSWLQJ YDOLGLW\ '' FRQÀJXUDWLRQ VFULSWLQJ LQYDOLG!

HYDOXDWHG

LJQRUHG

XQVSHFLILHG WUXH IDOVH

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

-63 HOHPHQWV DQVZHUV

-63 (OHPHQW 0DJQHWV $16:(56

# SDJH LPSRUW ´MDYD XWLO ´ !

GLUHFWLYH

! LQW \

GHFODUDWLRQ

HPDLO ^DSSOLFDWLRQ6FRSH PDLO`

(/ H[SUHVVLRQ

! QHZ )ORDW )ORDW RQH

VFULSWOHW

SDJH&RQWH[W JHW$WWULEXWH ³IRR´ !

H[SUHVVLRQ

7KH ZRUG ´H[SUHVVLRQµ E\ LWVHOI PHDQV ´VFULSWLQJ H[SUHVVLRQµ 127 ´(/ H[SUHVVLRQµ

VLRQ´ LV RYHU 2I FRXUVH WKH ZRUG ³H[SUHV \RX VHH WKH ORDGHG IRU -63 HOHPHQWV ,I LQJ H[SUHVVLRQ´ ZRUG ³H[SUHVVLRQ´ RU ³VFULSW Q H[SUHVVLRQ LW PHDQV WKH VDPH WKLQJ²D [ XVLQJ -DYD ODQJXDJH V\QWD IRR JHW1DPH ! UHVVLRQ´ UHIHUV 7KH RQO\ WLPH WKH ZRUG ³H[S ³(/´ LQ WKH VHH FDOO\ HFL¿ X VS LI \R WR (/ LV ZD\V DVVXPH R DO HO 6 U ODE QV R ULSWLR GHVF ³H[SUHVVLRQ´ WKDW WKH GHIDXOW IRU WKH ZRUG RQ´ QRW (/ LV ³VFULSWLQJ -DYD H[SUHVVL

MVS LQFOXGH ¿ OH ´IRR KWPO´ !

DFWLRQ

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

-63 (OHPHQW 0DJQHWV WKH 6HTXHO $16:(56

# SDJH LPSRUW ´MDYD XWLO ´ !

ort attribute A page directive with an imp tement. sta ort imp a Jav a into ns tur

SXEOLF Âż QDO FODVV %DVLF&RXQWHUBMVS H[WHQGV RUJ DSDFKH MDVSHU UXQWLPH +WWS-VS%DVH LPSOHPHQWV RUJ DSDFKH MDVSHU UXQWLPH -VS6RXUFH'HSHQGHQW ^ LQW \ !

MBER Declarations are for MEinsi de the class declarations, so they go . and outside any method

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ UHTXHVW JHW$WWULEXWH ³IRR´ !

) Expressions turn into write( hod. statements in the service met

)ORDW RQH QHZ )ORDW !

HPDLO ^DSSOLFDWLRQ6FRSH PDLO`

` `

Scriptlets go id e the service methodins . EL expressions go inside the service method. (Note: the order of these three things doesnÕt matter.)

GO into the servlet JSP code doesnÕt actualle.yThis exercise is just to cod NOTE: remember that the e uag lang a into Jav re like this... itÕs all translaoftedthe generated class these elements GO, buttedweÕinto. show you in what part ual generated code the elements are transla y = 3; not showing you the act tion goes from <%! int y = 3; %> to just int For example, the declara \RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN H[DP

0RFN ([DP &KDSWHU

'IVEN THIS $$ ELEMENT MVS SURSHUW\ JURXS! XUO SDWWHUQ! MVS XUO SDWWHUQ! HO LJQRUHG!WUXH HO LJQRUHG! MVS SURSHUW\ JURXS! 7HAT DOES THE ELEMENT ACCOMPLISH #HOOSE ALL THAT APPLY

R ! !LL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD BE TREATED BY THE *30 CONTAINER AS WELL FORMED 8-, lLES R " !LL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R # "Y DEFAULT ALL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD ./4 HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R $ .OTHING THIS TAG IS ./4 UNDERSTOOD BY THE CONTAINER R % !LTHOUGH THIS TAG IS LEGAL IT IS REDUNDANT BECAUSE THE CONTAINER BEHAVES THIS WAY BY DEFAULT

7HICH DIRECTIVES SPECIFY AN (440 RESPONSE THAT WILL BE OF TYPE hIMAGE SVGv #HOOSE ALL THAT APPLY

R ! # SDJH W\SH ´LPDJH VYJ´ ! R " # SDJH PLPH7\SH ´LPDJH VYJ´ ! R # # SDJH ODQJXDJH ´LPDJH VYJ´ ! R $ # SDJH FRQWHQW7\SH ´LPDJH VYJ´ ! R % # SDJH SDJH(QFRGLQJ ´LPDJH VYJ´ !

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

'IVEN THIS *30 # SDJH LPSRUW ´MDYD XWLO ´ ! KWPO! ERG\! 7KH SHRSOH ZKR OLNH UHTXHVW JHW3DUDPHWHU ³KREE\´ ! DUH EU! $UUD\/LVW DO $UUD\/LVW UHTXHVW JHW$WWULEXWH ³QDPHV´ ! ,WHUDWRU LW DO LWHUDWRU ZKLOH LW KDV1H[W

^ ! LW QH[W ! EU! ` ! ERG\! KWPO! 7HICH TYPES OF CODE ARE USED IN THIS *30 #HOOSE ALL THAT APPLY

R ! %, R " DIRECTIVE R # EXPRESSION R $ TEMPLATE TEXT R % SCRIPTLET

7HICH STATEMENTS ABOUT MVS,QLW ARE TRUE #HOOSE ALL THAT APPLY

R ! )T HAS ACCESS TO A 6HUYOHW&RQ¿J R " )T HAS ACCESS TO A 6HUYOHW&RQWH[W R # )T IS ONLY CALLED ONCE R $ )T CAN BE OVERRIDDEN

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN H[DP

7HICH TYPES OF OBJECTS ARE AVAILABLE TO THE MVS,QLW METHOD #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQ¿J R " 6HUYOHW&RQWH[W R # -VS6HUYOHW&RQ¿J R $ -VS6HUYOHW&RQWH[W R % +WWS6HUYOHW5HTXHVW R & +WWS6HUYOHW5HVSRQVH

'IVEN # SDJH LV(/,JQRUHG ´WUXH´ ! 7HAT IS THE EFFECT #HOOSE ALL THAT APPLY

R ! .OTHING THIS SDJH DIRECTIVE IS ./4 DElNED R " 4HE DIRECTIVE TURNS OFF THE EVALUATION OF %XPRESSION ,ANGUAGE CODE BY THE *30 CONTAINER IN ALL OF THE WEB APPLICATION S *30S

R # 4HE *30 CONTAINING THIS DIRECTIVE SHOULD BE TREATED BY THE *30 CONTAINER AS A WELL FORMED 8-, lLE

R $ 4HE *30 CONTAINING THIS DIRECTIVE SHOULD ./4 HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R % 4HIS PAGE DIRECTIVE WILL ONLY TURN OFF %, EVALUATION IF THE $$ DECLARES A HO LJQRUHG!WUXH HO LJQRUHG! ELEMENT WITH A 52, PATTERN THAT INCLUDES THIS *30

7HICH STATEMENT CONCERNING *30S IS TRUE #HOOSE ONE

R ! /NLY MVS,QLW CAN BE OVERRIDDEN R " /NLY MVS'HVWUR\ CAN BE OVERRIDDEN R # /NLY ?MVS6HUYLFH CAN BE OVERRIDDEN R $ "OTH MVS,QLW AND MVS'HVWUR\ CAN BE OVERRIDDEN R % MVS,QLW

MVS'HVWUR\

AND ?MVS6HUYLFH CAN ALL BE OVERRIDDEN

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

7HICH *30 LIFECYCLE STEP IS OUT OF ORDER

7HICH ARE VALID *30 IMPLICIT VARIABLES #HOOSE ALL THAT APPLY

R ! 4RANSLATE THE *30 INTO A SERVLET R " #OMPILE SERVLET SOURCE CODE R # #ALL BMVS6HUYLFH

R $ )NSTANTIATE THE SERVLET CLASS R % #ALL MVS,QLW

R & #ALL MVS'HVWUR\

R ! VWUHDP R " FRQWH[W R # H[FHSWLRQ R $ OLVWHQHU R % DSSOLFDWLRQ

'IVEN A REQUEST WITH TWO PARAMETERS ONE NAMED hlRSTv REPRESENTS A USER S lRST NAME AND ANOTHER NAMED hLASTv REPRESENTS HIS LAST NAME 7HICH *30 SCRIPTLET CODE OUTPUTS THESE PARAMETER VALUES

R ! RXW SULQWOQ UHTXHVW JHW3DUDPHWHU ³¿UVW´

RXW SULQWOQ UHTXHVW JHW3DUDPHWHU ³ODVW´

!

R " RXW SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³¿UVW´

RXW SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³ODVW´

!

R # SULQWOQ UHTXHVW JHW3DUDPHWHU ³¿UVW´

SULQWOQ UHTXHVW JHW3DUDPHWHU ³ODVW´

!

R $ SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³¿UVW´

SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³ODVW´

!

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN H[DP

'IVEN +HOOR ^XVHU QDPH` <RXU QXPEHU LV F RXW YDOXH ´ ^XVHU SKRQH`´ ! <RXU DGGUHVV LV MVS JHW3URSHUW\ QDPH ´XVHU´ SURSHUW\ ´DGGU´ ! LI XVHU LV9DOLG

^ !<RX DUH YDOLG ` ! 7HICH STATEMENTS ARE TRUE #HOOSE ALL THAT APPLY

R ! ,INES AND AND NO OTHERS CONTAIN EXAMPLES OF %, ELEMENTS R " ,INE IS AN EXAMPLE OF SCRIPTLET CODE

R # .ONE OF THE LINES IN THIS EXAMPLE CONTAIN TEMPLATE TEXT R $ ,INES AND INCLUDE EXAMPLES OF *30 STANDARD ACTIONS R % ,INE DEMONSTRATES AN INVALID USE OF %, R & !LL FOUR LINES IN THIS EXAMPLE WOULD BE VALID IN A *30 PAGE

7HICH *30 EXPRESSION TAG WILL PRINT THE CONTEXT INITIALIZATION PARAMETER NAMED hJAVAX SQL $ATA3OURCEv

R ! R " R # R $

DSSOLFDWLRQ JHW$WWULEXWH ³MDYD[ VTO 'DWD6RXUFH´ ! DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³MDYD[ VTO 'DWD6RXUFH´ ! UHTXHVW JHW3DUDPHWHU ³MDYD[ VTO 'DWD6RXUFH´ ! FRQWH[W3DUDP JHW ³MDYD[ VTO 'DWD6RXUFH´ !

7HICH STATEMENTS ABOUT DISABLING SCRIPTING ELEMENTS ARE TRUE #HOOSE ALL THAT APPLY

R ! 9OU CAN T DISABLE SCRIPTING VIA THE $$ R " 9OU CAN ONLY DISABLE SCRIPTING AT THE APPLICATION LEVEL R # 9OU CAN DISABLE SCRIPTING PROGRAMMATICALLY BY USING THE LV6FULSWLQJ(QDEOHG PAGE DIRECTIVE ATTRIBUTE

R $ 9OU CAN DISABLE SCRIPTING VIA THE $$ BY USING THE VFULSWLQJ LQYDOLG! ELEMENT

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

)N SEQUENCE WHAT ARE THE *AVA TYPES OF THE FOLLOWING *30 IMPLICIT OBJECTS DSSOLFDWLRQ RXW UHTXHVW UHVSRQVH VHVVLRQ

R ! MDYD ODQJ 7KURZDEOH MDYD ODQJ 2EMHFW MDYD XWLO 0DS MDYD XWLO 6HW MDYD XWLO /LVW

R " MDYD[ VHUYOHW 6HUYOHW&RQ¿J MDYD ODQJ 7KURZDEOH MDYD ODQJ 2EMHFW MDYD[ VHUYOHW MVS 3DJH&RQWH[W MDYD XWLO 0DS

R # MDYD[ VHUYOHW 6HUYOHW&RQWH[W MDYD[ VHUYOHW MVS -VS:ULWHU MDYD[ VHUYOHW 6HUYOHW5HTXHVW MDYD[ VHUYOHW 6HUYOHW5HVSRQVH MDYD[ VHUYOHW KWWS +WWS6HVVLRQ

R $ MDYD[ VHUYOHW 6HUYOHW&RQWH[W MDYD LR 3ULQW:ULWHU MDYD[ VHUYOHW 6HUYOHW&RQ¿J MDYD ODQJ ([FHSWLRQ MDYD[ VHUYOHW 5HTXHVW'LVSDWFKHU

7HICH IS AN EXAMPLE OF THE SYNTAX USED TO IMPORT A CLASS IN A *30

R ! SDJH LPSRUW ´MDYD XWLO 'DWH´ ! R " # SDJH LPSRUW ´MDYD XWLO 'DWH´ # ! R # # SDJH LPSRUW ´MDYD XWLO 'DWH´ ! R $ LPSRUW MDYD XWLO 'DWH ! R % # LPSRUW ¿OH ´MDYD XWLO 'DWH´ !

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN DQVZHUV

&KDSWHU $QVZHUV

(JSP v2.0 pg 1-

'IVEN THIS $$ ELEMENT

87)

MVS SURSHUW\ JURXS! XUO SDWWHUQ! MVS XUO SDWWHUQ! HO LJQRUHG!WUXH HO LJQRUHG! MVS SURSHUW\ JURXS! 7HAT DOES THE ELEMENT ACCOMPLISH #HOOSE ALL THAT APPLY

R ! !LL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD BE TREATED BY THE *30 CONTAINER AS WELL FORMED 8-, lLES R " !LL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R # "Y DEFAULT ALL lLES WITH THE SPECIlED EXTENSION MAPPING SHOULD ./4 HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R $ .OTHING THIS TAG IS ./4 UNDERSTOOD BY THE CONTAINER R % !LTHOUGH THIS TAG IS LEGAL IT IS REDUNDANT BECAUSE THE CONTAINER BEHAVES THIS WAY BY DEFAULT

7HICH DIRECTIVES SPECIFY AN (440 RESPONSE THAT WILL BE OF TYPE hIMAGE SVGv #HOOSE ALL THAT APPLY

R ! # SDJH W\SH ´LPDJH VYJ´ ! R " # SDJH PLPH7\SH ´LPDJH VYJ´ ! R # # SDJH ODQJXDJH ´LPDJH VYJ´ ! R $ # SDJH FRQWHQW7\SH ´LPDJH VYJ´ ! R % # SDJH SDJH(QFRGLQJ ´LPDJH VYJ´ !

-Option C turns off the evaluating of EL expressions by a JSP 2.0 container and by default the container does evaluate EL.

(JSP v2.0 section

1.10.1)

-Option D is the correct syntax for this directive.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

(JSP v2.0 section

'IVEN THIS *30

1)

# SDJH LPSRUW ´MDYD XWLO ´ ! KWPO! ERG\! 7KH SHRSOH ZKR OLNH UHTXHVW JHW3DUDPHWHU ³KREE\´ ! DUH EU! $UUD\/LVW DO $UUD\/LVW UHTXHVW JHW$WWULEXWH ³QDPHV´ ! ,WHUDWRU LW DO LWHUDWRU ZKLOH LW KDV1H[W

^ ! LW QH[W ! EU! ` ! ERG\! KWPO! 7HICH TYPES OF CODE ARE USED IN THIS *30 #HOOSE ALL THAT APPLY

R ! %, R " DIRECTIVE R # EXPRESSION R $ TEMPLATE TEXT R % SCRIPTLET

-ThereÕs no EL in this JSP. ThereÕs a directive on line 1, expressions on lines 3 and 8, template text all over (like line 2), and of course scripting elements.

7HICH STATEMENTS ABOUT MVS,QLW ARE TRUE #HOOSE ALL THAT APPLY

(JSP v2.0 section

11.2.1)

R ! )T HAS ACCESS TO A 6HUYOHW&RQÂżJ R " )T HAS ACCESS TO A 6HUYOHW&RQWH[W R # )T IS ONLY CALLED ONCE R $ )T CAN BE OVERRIDDEN

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN DQVZHUV

7HICH TYPES OF OBJECTS ARE AVAILABLE TO THE MVS,QLW METHOD #HOOSE ALL THAT APPLY

R ! 6HUYOHW&RQÂżJ R " 6HUYOHW&RQWH[W R # -VS6HUYOHW&RQÂżJ R $ -VS6HUYOHW&RQWH[W R % +WWS6HUYOHW5HTXHVW R & +WWS6HUYOHW5HVSRQVH

(JSP v2.0 section

11.2.1)

-JSPs turn into plain old servlets, so they have access to the plain old ServletConÞg and ServletContext objects... and itÕs just a little early in the lifecycle to be talking about requests and responses.

49)

(JSP v2.0 pg 1-

'IVEN # SDJH LV(/,JQRUHG ´WUXH´ ! 7HAT IS THE EFFECT #HOOSE ALL THAT APPLY

R ! .OTHING THIS SDJH DIRECTIVE IS ./4 DElNED ct R " 4HE DIRECTIVE TURNS OFF THE EVALUATION OF %XPRESSION ,ANGUAGE CODE BY -Option B is incorreve THE *30 CONTAINER IN ALL OF THE WEB APPLICATION S *30S

R # 4HE *30 CONTAINING THIS DIRECTIVE SHOULD BE TREATED BY THE *30

because the directi only affects the enclosing JSP.

CONTAINER AS A WELL FORMED 8-, lLE

R $ 4HE *30 CONTAINING THIS DIRECTIVE SHOULD ./4 HAVE ANY %XPRESSION ,ANGUAGE CODE EVALUATED BY THE *30 CONTAINER

R % 4HIS PAGE DIRECTIVE WILL ONLY TURN OFF %, EVALUATION IF THE $$ DECLARES A HO LJQRUHG!WUXH HO LJQRUHG! ELEMENT WITH A 52, PATTERN THAT INCLUDES THIS *30

7HICH STATEMENT CONCERNING *30S IS TRUE #HOOSE ONE

(JSP v2.0 section

11)

R ! /NLY MVS,QLW CAN BE OVERRIDDEN R " /NLY MVS'HVWUR\ CAN BE OVERRIDDEN -Remember the underscore is your clue that a method R # /NLY ?MVS6HUYLFH CAN BE OVERRIDDEN canÕt be overridden. R $ "OTH MVS,QLW AND MVS'HVWUR\ CAN BE OVERRIDDEN R % MVS,QLW

MVS'HVWUR\

AND ?MVS6HUYLFH CAN ALL BE OVERRIDDEN

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

7HICH *30 LIFECYCLE STEP IS OUT OF ORDER

7HICH ARE VALID *30 IMPLICIT VARIABLES #HOOSE ALL THAT APPLY

(JSP v2.0 section

11)

R ! 4RANSLATE THE *30 INTO A SERVLET R " #OMPILE SERVLET SOURCE CODE -The _jspService method can R # #ALL BMVS6HUYLFH

never be called before jspInit. R $ )NSTANTIATE THE SERVLET CLASS R % #ALL MVS,QLW

R & #ALL MVS'HVWUR\

R ! VWUHDP R " FRQWH[W R # H[FHSWLRQ R $ OLVWHQHU R % DSSOLFDWLRQ

1.8.3)

(JSP v2.0 section

-Options A, B, and D donÕt exist as implicit objects created by the container for JSPs.

41)

(JSP v2.0 pg 1-

'IVEN A REQUEST WITH TWO PARAMETERS ONE NAMED hlRSTv REPRESENTS A USER S lRST NAME AND ANOTHER NAMED hLASTv REPRESENTS HIS LAST NAME 7HICH *30 SCRIPTLET CODE OUTPUTS THESE PARAMETER VALUES

-Option A uses the ÒoutÓ implicit object and its println() method.

R ! RXW SULQWOQ UHTXHVW JHW3DUDPHWHU ³¿UVW´

RXW SULQWOQ UHTXHVW JHW3DUDPHWHU ³ODVW´

!

R " RXW SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³¿UVW´

RXW SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³ODVW´

!

R # SULQWOQ UHTXHVW JHW3DUDPHWHU ³¿UVW´

SULQWOQ UHTXHVW JHW3DUDPHWHU ³ODVW´

!

-Options C and D are missing the ÒoutÓ implicit object.

R $ SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³¿UVW´

SULQWOQ DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³ODVW´

!

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

PRFN DQVZHUV

(JSP v2.0 pg. 1-

'IVEN

10)

+HOOR ^XVHU QDPH` <RXU QXPEHU LV F RXW YDOXH ´ ^XVHU SKRQH`´ ! <RXU DGGUHVV LV MVS JHW3URSHUW\ QDPH ´XVHU´ SURSHUW\ ´DGGU´ ! LI XVHU LV9DOLG

^ !<RX DUH YDOLG ` ! 7HICH STATEMENTS ARE TRUE #HOOSE ALL THAT APPLY

R ! ,INES AND AND NO OTHERS CONTAIN EXAMPLES OF %, ELEMENTS R " ,INE IS AN EXAMPLE OF SCRIPTLET CODE

R # .ONE OF THE LINES IN THIS EXAMPLE CONTAIN TEMPLATE TEXT R $ ,INES AND INCLUDE EXAMPLES OF *30 STANDARD ACTIONS R % ,INE DEMONSTRATES AN INVALID USE OF %, R & !LL FOUR LINES IN THIS EXAMPLE WOULD BE VALID IN A *30 PAGE

v2.0 pg 1-41)

DSSOLFDWLRQ JHW$WWULEXWH ³MDYD[ VTO 'DWD6RXUFH´ ! DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³MDYD[ VTO 'DWD6RXUFH´ ! UHTXHVW JHW3DUDPHWHU ³MDYD[ VTO 'DWD6RXUFH´ ! FRQWH[W3DUDP JHW ³MDYD[ VTO 'DWD6RXUFH´ !

7HICH STATEMENTS ABOUT DISABLING SCRIPTING ELEMENTS ARE TRUE #HOOSE ALL THAT APPLY

R ! 9OU CAN T DISABLE SCRIPTING VIA THE $$ R " 9OU CAN ONLY DISABLE SCRIPTING AT THE APPLICATION LEVEL R # 9OU CAN DISABLE SCRIPTING PROGRAMMATICALLY BY USING THE LV6FULSWLQJ(QDEOHG PAGE DIRECTIVE ATTRIBUTE

R $ 9OU CAN DISABLE SCRIPTING VIA THE $$ BY USING THE VFULSWLQJ LQYDOLG! ELEMENT

-Option E is incorrect because the EL in line 11 is valid.

(JSP 7HICH *30 EXPRESSION TAG WILL PRINT THE CONTEXT INITIALIZATION PARAMETER NAMED hJAVAX SQL $ATA3OURCEv R ! R " R # R $

ct because all -Option C is incorre t. four lines include template tex -Option D is incorrect because line 12 does not include a JSP standard action.

-Option B shows the correct use of the application implicit object.

(JSP v2.0 section

3.3.3)

-You can only disable scripting elements through the DD. The <jsp-property-group> element allows you to disable scripting in selective JSPs by specifying URL patterns to be disabled.

FKDSWHU

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 7. Being a JSP

XVLQJ -63

)N SEQUENCE WHAT ARE THE *AVA TYPES OF THE FOLLOWING *30 IMPLICIT OBJECTS DSSOLFDWLRQ RXW UHTXHVW UHVSRQVH VHVVLRQ

41)

(JSP v2.0 pg 1-

R ! MDYD ODQJ 7KURZDEOH MDYD ODQJ 2EMHFW MDYD XWLO 0DS MDYD XWLO 6HW MDYD XWLO /LVW

R " MDYD[ VHUYOHW 6HUYOHW&RQ¿J MDYD ODQJ 7KURZDEOH MDYD ODQJ 2EMHFW MDYD[ VHUYOHW MVS 3DJH&RQWH[W MDYD XWLO 0DS

R # MDYD[ VHUYOHW 6HUYOHW&RQWH[W MDYD[ VHUYOHW MVS -VS:ULWHU MDYD[ VHUYOHW 6HUYOHW5HTXHVW MDYD[ VHUYOHW 6HUYOHW5HVSRQVH MDYD[ VHUYOHW KWWS +WWS6HVVLRQ

-Option C shows the Java type of each implicit object.

R $ MDYD[ VHUYOHW 6HUYOHW&RQWH[W MDYD LR 3ULQW:ULWHU MDYD[ VHUYOHW 6HUYOHW&RQ¿J MDYD ODQJ ([FHSWLRQ MDYD[ VHUYOHW 5HTXHVW'LVSDWFKHU

7HICH IS AN EXAMPLE OF THE SYNTAX USED TO IMPORT A CLASS IN A *30

R ! SDJH LPSRUW ´MDYD XWLO 'DWH´ ! R " # SDJH LPSRUW ´MDYD XWLO 'DWH´ # ! R # # SDJH LPSRUW ´MDYD XWLO 'DWH´ ! R $ LPSRUW MDYD XWLO 'DWH ! R % # LPSRUW ¿OH ´MDYD XWLO 'DWH´ !

44) (JSP v2.0 pg. 1because -Options A & D are invalid be only Java statements may included within <% ... %> tags. -Option C is the only example that shows the correct syntax.

-Option E is invalid because there is no import directive.

\RX DUH KHUH

Chapter 7. Being a JSP Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/1/2004 User number: 729515 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 8. Script-free pages

VFULSWOHVV -63

6FULSW IUHH SDJHV (YHU\WKLQJ LQ P\ OLIH LV EHWWHU VLQFH , VWRSSHG XVLQJ VFULSWOHWV ,·P WDOOHU ,·YH DGGHG IRXU SRXQGV RI OHDQ PXVFOH PDVV DQG P\ NQLWWLQJ KDV UHDOO\ LPSURYHG

7KDW·V ZRQGHUIXO %XW \RX NQRZ DOO WHFKQRORJLHV KDYH WUDGH RIIV \RX XVHG WR KDYH KDLU

/RVH WKH VFULSWLQJ 'R \RXU ZHE SDJH GHVLJQHUV UHDOO\ KDYH WR NQRZ -DYD" ,V WKDW IDLU" 'R WKH\ H[SHFW VHUYHU VLGH -DYD SURJUDPPHUV WR EH VD\ JUDSKLF GHVLJQHUV" $QG HYHQ LI LW¶V MXVW \RX RQ WKH WHDP GR \RX UHDOO\ ZDQW D SLOH RI ELWV DQG SLHFHV RI -DYD FRGH LQ \RXU -63V" &DQ \RX VD\ ³PDLQWHQDQFH QLJKWPDUH´" :ULWLQJ VFULSWOHVV SDJHV LV QRW MXVW SRVVLEOH LW¶V EHFRPH PXFK HDVLHU DQG PRUH À H[LEOH ZLWK WKH QHZ -63 VSHF WKDQNV WR WKH QHZ ([SUHVVLRQ /DQJXDJH (/ 3DWWHUQHG DIWHU -DYD6FULSW DQG ;3$7+ ZHE GHVLJQHUV IHHO ULJKW DW KRPH ZLWK (/ DQG \RX¶OO OLNH LW WRR RQFH \RX JHW XVHG WR LW %XW WKHUH DUH VRPH WUDSV (/ ORRNV OLNH -DYD EXW LVQ¶W 6RPHWLPHV (/ EHKDYHV GLIIHUHQWO\ WKDQ LI \RX XVHG WKH VDPH V\QWD[ LQ -DYD VR SD\ DWWHQWLRQ WKLV LV D QHZ FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

RI¿FLDO 6XQ H[DP REMHFWLYHV

%XLOGLQJ -63 SDJHV XVLQJ WKH ([SUHVVLRQ /DQJXDJH (/ DQG 6WDQGDUG $FWLRQV

&RYHUDJH 1RWHV

Copyright Safari Books Online #747221 :ULWH D FRGH VQLSSHW XVLQJ WRS OHYHO YDULDEOHV LQ WKH (/ 7KLV LQFOXGHV WKH IROORZLQJ LPSOLFLW YDULDEOHV SDJH6FRSH UHTXHVW6FRSH VHVVLRQ6FRSH DQG DSSOLFDWLRQ6FRSH SDUDP DQG SDUDP9DOXHV KHDGHU DQG KHDGHU9DOXHV FRRNLHV DQG LQLW3DUDP

!LL OF THE OBJECTIVES IN THIS SECTION ARE COVERED COMPLETELY IN THIS CHAPTER !ND IT S A BIG ONE 4AKE YOUR TIME IN THIS CHAPTER THERE S A LOT OF PICKY DETAILS TO GO THROUGH

:ULWH D FRGH VQLSSHW XVLQJ WKH IROORZLQJ (/ RSHUDWRUV SURSHUW\ DFFHVV WKH RSHUDWRU FROOHFWLRQ DFFHVV WKH >@ RSHUDWRU

:ULWH D FRGH VQLSSHW XVLQJ WKH IROORZLQJ (/ RSHUDWRUV DULWPHWLF RSHUDWRUV UHODWLRQDO RSHUDWRUV DQG ORJLFDO RSHUDWRUV

)RU (/ IXQFWLRQV :ULWH D FRGH VQLSSHW XVLQJ DQ (/

IXQFWLRQ LGHQWLI\ RU FUHDWH WKH 7/' ¿OH VWUXFWXUH XVHG WR GHFODUH DQ (/ IXQFWLRQ DQG LGHQWLI\ RU FUHDWH D FRGH H[DPSOH WR GH¿QH DQ (/ IXQFWLRQ

*LYHQ D GHVLJQ JRDO FUHDWH D FRGH VQLSSHW XVLQJ WKH IROORZLQJ VWDQGDUG DFWLRQV MVS XVH%HDQ ZLWK DWWULEXWHV µLG¶ µVFRSH¶ µW\SH¶ DQG µFODVV¶ MVS JHW3URSHUW\ DQG MVS VHW3URSHUW\ ZLWK DOO DWWULEXWH FRPELQDWLRQV

*LYHQ D GHVLJQ JRDO FUHDWH D FRGH VQLSSHW XVLQJ WKH IROORZLQJ VWDQGDUG DFWLRQV MVS LQFOXGH MVS IRUZDUG DQG MVS SDUDP

*LYHQ D VSHFL¿F GHVLJQ JRDO IRU LQFOXGLQJ D -63 VHJPHQW LQ DQRWKHU SDJH ZULWH WKH -63 FRGH WKDW XVHV WKH PRVW DSSURSULDWH LQFOXVLRQ PHFKDQLVP WKH LQFOXGH GLUHFWLYH RU WKH MVS LQFOXGH! VWDQGDUG DFWLRQ

)N THIS CHAPTER WE COVER "/4( INCLUDE MECHANISMS JSP INCLUDE FROM OBJECTIVE AND THE INCLUDE PAGE DIRECTIVE FROM OBJECTIVE MOST OF THE OBJECTIVES IN SECTION WERE COVERED IN THE PREVIOUS CHAPTER ON *30S

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

7]Z 5>+ IXX LMXMVL[ WV I\\ZQJ]\M[ 2EMEMBER IN THE ORIGINAL -6# BEER APP THE 3ERVLET CONTROLLER TALKED TO THE MODEL *AVA CLASS WITH BUSINESS LOGIC THEN SET AN ATTRIBUTE IN THE REQUEST SCOPE BEFORE FORWARDING TO THE *30 VIEW 4HE *30 HAD TO GET THE ATTRIBUTE FROM THE REQUEST SCOPE AND USE IT TO RENDER A RESPONSE TO SEND BACK TO THE CLIENT (ERE S A QUICK SIMPLIl ED LOOK AT HOW THE ATTRIBUTE GOES FROM CONTROLLER TO VIEW JUST IMAGINE THE SERVLET TALKS TO THE MODEL 6HUYOHW FRQWUROOHU FRGH SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ 6WULQJ QDPH UHTXHVW JHW3DUDPHWHU ³XVHU1DPH´ UHTXHVW VHW$WWULEXWH ³QDPH´ QDPH

parameter from Use the requestt a request-scoped se to rm fo the e JSP will use. attribute that th

5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³ UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH `

Forward the request to the view.

-63 YLHZ FRGH KWPO! ERG\! +HOOR UHTXHVW JHW$WWULEXWH ³QDPH´ ! ERG\! KWPO!

to get the Use a scripting expression the response. attribute and print it to ions are ALWAYS (Remember: scripting express e() method.) writ out. the to t men the argu

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

+HOOR 3DXO

ÒPaulÓ was the value of the ÒnameÓ attribute

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

QRQ 6WULQJ DWWULEXWHV

*]\ _PI\ QN \PM I\\ZQJ]\M Q[ VW\ I ;\ZQVO J]\ IV QV[\IVKM WN 8MZ[WV'

aBean. A simple Jav

!ND NOT JUST A 0ERSON BUT A 0ERSON WITH A hNAMEv PROPERTY 7E RE USING THE TERM hPROPERTYv IN THE NON ENTERPRISE *AVA"EAN WAYˆTHE 0ERSON CLASS HAS A GET.AME AND SET.AME METHOD PAIR WHICH IN THE *AVA"EAN SPEC MEANS 0ERSON HAS A PROPERTY CALLED hNAMEv $ON T FORGET THAT THE hNAMEv PROPERTY MEANS A CHANGE IN CASE FOR THE l RST LETTER hNv )N OTHER WORDS THE NAME OF THE PROPERTY IS WHAT YOU GET WHEN YOU STRIP OFF THE PREl X hGETv AND hSETv AND MAKE THE l RST CHARACTER AFTER THAT LOWER CASE 3O GET.AME SET.AME BECOMES NAME

IRR 3HUVRQ SXEOLF 6WULQJ JHW1DPH SXEOLF YRLG VHW1DPH 6WULQJ

We can tell from pair that Person haths e getter/setter ÒnameÓ (note the low a property called ercase ÒnÓ).

6HUYOHW FRGH SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ IRR 3HUVRQ S QHZ IRR 3HUVRQ S VHW1DPH ³(YDQ´ UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S 5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH `

turn?

ttribute() re

-63 FRGH

tA What does ge

KWPO! ERG\! 3HUVRQ LV UHTXHVW JHW$WWULEXWH ³SHUVRQ´ ! ERG\! KWPO!

:KDW ZH :$17 KWWS ORFDOKRVW WHVW-63 7HVWHU GR

3HUVRQ LV (YDQ

:KDW ZH *27 KWWS ORFDOKRVW WHVW-63 7HVWHU GR

3HUVRQ LV IRR 3HUVRQ# G

Oh... obviously th just called the atetrexpression ibuteĂ•s default toString() me thod... :H¡OO WDON DERXW -DYD%HDQV LQ D IHZ SDJHV EXW IRU QRZ MXVW NQRZ WKDW LW¡V D SODLQ ROG -DYD FODVV WKDW KDV JHWWHUV DQG VHWWHUV WKDW IROORZ D QDPLQJ FRQYHQWLRQ

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

?M VMML UWZM KWLM \W OM\ \PM 8MZ[WV¼[ VIUM 3ENDING THE RESULT OF GET!TTRIBUTE TO PRINT WRITE STATEMENT DOESN T GIVE US WHAT WE WANTˆIT JUST RUNS THE OBJECT S TO3TRING METHOD !ND SINCE CLASS 0ERSON DOESN T OVERRIDE ITS INHERITED /BJECT TO3TRING WELL YOU KNOW WHAT HAPPENS "UT WE WANT TO PRINT THE 0ERSON S NAME -63 FRGH KWPO! ERG\! IRR 3HUVRQ S IRR 3HUVRQ UHTXHVW JHW$WWULEXWH ³SHUVRQ´ ! 3HUVRQ LV S JHW1DPH !

esult the r Print etName (). of g

ERG\! KWPO!

25 XVLQJ DQ H[SUHVVLRQ KWPO! ERG\! 3HUVRQ LV IRR 3HUVRQ UHTXHVW JHW$WWULEXWH ³SHUVRQ´

JHW1DPH ! ERG\! KWPO!

:KDW ZH *27 KWWS ORFDOKRVW WHVW-63 7HVWHU GR

3HUVRQ LV (YDQ

%XW WKHQ ZH UHPHPEHU WKDW 0(02 7KH RQH WKDW FDQ EH VXPPDUL]HG DV ´8VH 6FULSWLQJ DQG 'LHµ :H QHHG D GLIIHUHQW DSSURDFK

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

-DYD%HDQ VWDQGDUG DFWLRQV

8MZ[WV Q[ I 2I^I*MIV [W _MÂźTT ][M \PM JMIV ZMTI\ML [\IVLIZL IK\QWV[ 7ITH A COUPLE OF STANDARD ACTIONS WE CAN ELIMINATE ALL THE SCRIPTING CODE IN OUR *30 REMEMBER SCRIPTING CODE INCLUDES DECLARATIONS SCRIPTLETS AND EXPRESSIONS AND STILL PRINT OUT THE VALUE OF THE PERSON ATTRIBUTE S NAME PROPERTY $ON T FORGET THAT NAME IS NOT AN ATTRIBUTEˆ ONLY THE PERSON OBJECT IS AN ATTRIBUTE 4HE NAME PROPERTY IS SIMPLY THE THING RETURNED FROM A 0ERSON S GET.AME METHOD

:LWKRXW VWDQGDUG DFWLRQV XVLQJ VFULSWLQJ

KWPO! ERG\!

weÕve The wayoing it. d n e e b

IRR 3HUVRQ S IRR 3HUVRQ UHTXHVW JHW$WWULEXWH ³SHUVRQ´ ! 3HUVRQ LV S JHW1DPH ! ERG\! KWPO!

:LWK VWDQGDUG DFWLRQV QR VFULSWLQJ

KWPO! ERG\!

ing, No script ode here!action tags. c va a J NO o standard just tw

MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´UHTXHVW´ ! 3HUVRQ FUHDWHG E\ VHUYOHW MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ ! ERG\! KWPO!

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

,MKWV[\Z]K\QVO $R[X"][M*MIV& IVL $R[X"OM\8ZWXMZ\a& !LL WE REALLY WANTED WAS THE FUNCTIONALITY OF JSP GET0ROPERTY BECAUSE WE WANTED ONLY TO DISPLAY THE VALUE OF THE PERSON S hNAMEv PROPERTY "UT HOW DOES THE #ONTAINER KNOW WHAT hPERSONv MEANS )F WE HAD ONLY THE JSP GET0ROPERTY TAG IN THE *30 IT S ALMOST LIKE USING AN UNDECLARED VARIABLEˆTHE NAME hPERSONv 4HE #ONTAINER USUALLY HAS NO IDEA WHAT YOU RE TALKING ABOUT UNLESS YOU &)234 PUT A JSP USE"EAN INTO THE PAGE 4HE JSP USE"EAN IS A WAY OF DECLARING AND INITIALIZING THE ACTUAL BEAN OBJECT YOU RE USING IN JSP GET0ROPERTY

'HFODUH DQG LQLWLDOL]H D EHDQ DWWULEXWH ZLWK MVS XVH%HDQ!

MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´UHTXHVW´ !

IdentiĂžes the er for the standard action. Declares the identicoĂžrresponds This bean objecte. used when the to the nam said: servlet code Ă“, p);

class type Declares thiĂže ed, of course) al qu ly ul (f object. for the bean

IdentiĂžes the att ute scope for this beanribobj ect

.

on ttribute(Ă’pers request.setA

*HW D EHDQ DWWULEXWH¡V SURSHUW\ YDOXH ZLWK MVS JHW3URSHUW\!

MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ !

es the IdentiĂžrd action. a d n a st

IdentiÞes the actual bean object. This will match the ÒidÓ value from the <jsp:useBean> tag.

(in other property name tt IdentiÞes the ing ge er and e th th wi words, the th bean class ). setter in the eÓ property has Note: this Ònamwith the name=ÓpersonÓ do to is nothing g. The property part of this tasimply because of the eÓ am called Òn on class is deÞned. way the Pers

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

MVS XVH%HDQ!

$R[X"][M*MIV& KIV IT[W +:-)<- I JMIV )F THE JSP USE"EAN CAN T lND AN ATTRIBUTE OBJECT NAMED hPERSONv IT CAN MAKE ONE )T S KIND OF THE WAY REQUEST GET3ESSION OR GET3ESSION TRUE WORKSˆIT lRST SEARCHES FOR AN EXISTING THING BUT IF IT DOESN T lND ONE IT CREATES ONE ,OOK AT THE CODE FROM THE GENERATED SERVLET AND YOU LL SEE WHAT S HAPPENINGˆTHERE S AN IF TEST IN THERE )T CHECKS FOR A BEAN BASED ON THE VALUES OF ID AND SCOPE IN THE TAG AND IF IT DOESN T GET ONE IT MAKES AN INSTANCE OF THE CLASS SPECIlED IN CLASS ASSIGNS THE OBJECT TO THE ID VARIABLE THEN SETS IT AS AN ATTRIBUTE IN THE SCOPE YOU DElNED IN THE TAG

7KLV WDJ

MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´UHTXHVW´ !

7XUQV LQWR WKLV FRGH LQ WKH BMVS6HUYLFH PHWKRG

IRR 3HUVRQ SHUVRQ QXOO

V\QFKURQL]HG UHTXHVW ^

of id. This based on the value Declare a variable s other parts of your JSP . variable is what letan tags) refer to that variable (including other be ope you deĂžned in tribute at the sc at e th t ge e id variable. to th Tries ns the result to the tag, and assig

SHUVRQ IRR 3HUVRQ BMVS[BSDJHBFRQWH[W JHW$WWULEXWH ³SHUVRQ´ 3DJH&RQWH[W 5(48(67B6&23(

LI SHUVRQ

SHUVRQ

BUT, if there was NOT an attribute with that name at tha t scope... variable. sign it to the id Make one, and as QHZ IRR 3HUVRQ QXOO ^

BMVS[BSDJHBFRQWH[W VHW$WWULEXWH ³SHUVRQ´ SHUVRQ 3DJH&RQWH[W 5(48(67B6&23( ` `

Finally, set the new object as an attribute at the scope you deĂžned.

L

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

7KLV FRXOG EH D EDG WKLQJ³, GRQ·W :$17 WR KDYH D EHDQ WKDW GRHVQ·W KDYH LWV SURSHUW\ YDOXHV VHW ,I WKH &RQWDLQHU PDNHV D EHDQ XVLQJ WKDW WDJ WKH EHDQ ZRQ·W KDYH SURSHUW\ YDOXHV

AW] KIV ][M $R[X"[M\8ZWXMZ\a& "UT YOU ALREADY KNEW THAT WHERE THERE S A GET THERE S USUALLY A SET 4HE JSP SET0ROPERTY TAG IS THE THIRD AND lNAL BEAN STANDARD ACTION )T S SIMPLE TO USE MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´UHTXHVW´ !

MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ YDOXH ´)UHG´ ! 7KDW·V ZRUVH 12: LW PHDQV WKDW LI WKH EHDQ DOUHDG\ H[LVWHG P\ -63 ZLOO UHVHW WKH H[LVWLQJ EHDQ·V SURSHUW\ YDOXH , ZDQW WR VHW WKH SURSHUW\ RQ RQO\ WKH 1(: EHDQV

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

MVS XVH%HDQ! ZLWK D ERG\

$R[X"][M*MIV& KIV PI^M I JWLa )F YOU PUT YOUR SETTER CODE JSP SET0ROPERTY INSIDE THE BODY OF JSP USE"EAN THE PROPERTY SETTING IS CONDITIONAL )N OTHER WORDS THE PROPERTY VALUES WILL BE SET ONLY IF A NEW BEAN IS CREATED )F AN EXISTING BEAN WITH THAT SCOPE AND ID ARE FOUND THE BODY OF THE TAG WILL NEVER RUN SO THE PROPERTY WON T BE RESET FROM YOUR *30 CODE

:LWK D MVS XVH%HDQ ! ERG\ \RX FDQ KDYH FRGH WKDW UXQV FRQGLWLRQDOO\ 21/ < LI WKH EHDQ DWWULEXWH FDQ¡ W EH IRXQG DQG D QHZ EHDQ LV F UHDWHG

ash ! ThereÕs no sl

MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´SDJH´ !

body. This is the

MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ YDOXH ´)UHG´ !

MVS XVH%HDQ !

f the tag. Finally we closetwof n the opening ee be g Everythin gs is the body. ta g and closin

4

7HY DIDN T THEY JUST LET YOU SPECIFY ARGUMENTS TO THE CONSTRUCTOR OF THE BEAN 7HY DO YOU HAVE TO GO THROUGH THE EXTRA TROUBLE OF SETTING VALUES ANYWAY

$

4HE SIMPLE ANSWER IS THIS BEANS CAN T (!6% CON STRUCTORS WITH ARGUMENTS 7ELL AS A *AVA CLASS THEY CAN BUT WHEN AN OBJECT IS GOING TO BE TREATED AS A BEAN "EAN ,AW STATES THAT /.,9 THE BEAN S PUBLIC NO ARG CONSTRUCTOR WILL BE CALLED %ND OF STORY )N FACT IF YOU DO ./4 HAVE A PUBLIC NO ARG CONSTRUCTOR IN YOUR BEAN CLASS THIS WHOLE THING WILL FAIL ANYWAY

4 $

7HAT THE HECK IS "EAN ,AW

4HE LAW ACCORDING TO THE CREAKINGLY ANCIENT *AVA"EANS SPECIFICATION 7E RE TALKING *AVA"EANSˆ./4 %NTERPRISE *AVA"EANS %*" WHICH IS COMPLETELY UNRELATED 'O FIGURE 4HE PLAIN OLD NON ENTERPRISE *AVA"EANS SPEC DEFINES WHAT IT TAKES FOR A CLASS TO BE A *AVA"EAN !LTHOUGH THE SPEC ACTUALLY GETS PRETTY COMPLEX THE ONLY THINGS YOU NEED TO KNOW FOR USING BEANS WITH *30 AND SERVLETS ARE

Any code inside CONDITIONAL.thIte body of <jsp:useBean > is isnÕt found and a ne runs ONLY if the bean w one is created.

THESE FEW RULES WE RE SHOWING ONLY THOSE THAT APPLY TO WHAT WE RE DOING WITH SERVLETS AND *30S 9OU -534 HAVE A PUBLIC NO ARG CONSTRUCTOR 9OU -534 NAME YOUR PUBLIC GETTER AND SETTER METHODS STARTING WITH hGETv OR hISv FOR A BOOLEAN AND hSETv FOLLOWED BY THE SAME WORD GET&OO SET&OO 4HE PROPERTY NAME IS DERIVED FROM STRIPPING OFF THE hGETv AND hSETv AND CHANG ING THE FIRST CHARACTER OF WHAT S LEFT TO LOWERCASE 4HE SETTER ARGUMENT TYPE AND THE GETTER RETURN TYPE -534 BE IDENTICAL 4HIS DEFINES THE PROPERTY TYPE INT GET&OO VOID SET&OO INT FOO 4HE PROPERTY NAME AND TYPE ARE DERIVED FROM THE GET TERS AND SETTERS AND ./4 FROM A MEMBER IN THE CLASS &OR EXAMPLE JUST BECAUSE YOU HAVE A PRIVATE INT FOO VARIABLE DOES ./4 MEAN A THING IN TERMS OF PROPERTIES 9OU CAN NAME YOUR VARIABLES WHATEVER YOU LIKE 4HE hFOOv PROPERTY NAME COMES FROM THE METHODS )N OTHER WORDS YOU HAVE A PROPERTY SIMPLY BECAUSE YOU HAVE A GETTER AND SETTER (OW YOU IMPLEMENT THEM IS UP TO YOU &OR USE WITH *30S THE PROPERTY TYPE 3(/5,$ BE A TYPE THAT IS EITHER A 3TRING OR A PRIMITIVE )F IT ISN T IT CAN STILL BE A LEGAL BEAN BUT YOU WON T BE ABLE TO RELY ONLY ON STANDARD ACTIONS AND YOU MIGHT HAVE TO USE SCRIPTING

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

/MVMZI\ML [MZ ^TM\ _PMV $R[X"][M*MIV& PI[ I JWLa )T S SIMPLE 4HE #ONTAINER PUTS THE EXTRA PROPERTY SETTING CODE INSIDE THE IF TEST

&RGH LQ BMVS6HUYLFH :,7+ WKH MVS XVH%HDQ! ERG\ IRR 3HUVRQ SHUVRQ QXOO

Declare the reference variable.

h tribute wit. existing atom ag an t r he fo t k oo L d scope fr the name an

SHUVRQ IRR 3HUVRQ BMVS[BSDJHBFRQWH[W JHW$WWULEXWH ³SHUVRQ´ 3DJH&RQWH[W 3$*(B6&23( LI SHUVRQ

QXOO ^

SHUVRQ QHZ IRR 3HUVRQ

If there isnÕt one, make a new instance.

Bind the new bean object to the speciĂžed scope.

BMVS[BSDJHBFRQWH[W VHW$WWULEXWH ³SHUVRQ´ SHUVRQ 3DJH&RQWH[W 3$*(B6&23(

THIS is the part thatĂ•s new. ItĂ•s ONLY when useBean has a body. here RUJ DSDFKH MDVSHU UXQWLPH -VS5XQWLPH/LEUDU\ LQWURVSHFWKHOSHU BMVS[BSDJHBFRQWH[W ÂżQG$WWULEXWH ÂłSHUVRQ´ ÂłQDPH´ Âł)UHG´ QXOO QXOO IDOVH `

You were expecting: person.setName(ÒFredÓ); but thatÕs what this code does. Except it uses a generic property-setting method attribute, the property, and the that takes the The end result is still the same: value as arguments. setName() on the Person object.ultimately it invokes (Remember you arenÕt expected to know the Tomcat implementation code...only the end result.)

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

SRO\PRUSKLF UHIHUHQFHV

+IV aW] UISM XWTaUWZXPQK JMIV ZMNMZMVKM[' 7HEN YOU WRITE A JSP USE"EAN THE CLASS ATTRIBUTE DETERMINES THE CLASS OF THE NEW OBJECT IF ONE IS CREATED )T ALSO DETERMINES THE TYPE OF THE REFERENCE VARIABLE USED IN THE GENERATED SERVLET 7KH ZD\ LW LV 12: LQ WKH -63

MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´SDJH´ ! *HQHUDWHG VHUYOHW

IRR 3HUVRQ SHUVRQ QXOO

he tribute in t The class atents both the tag repres AND object type. reference

FRGH WR JHW WKH SHUVRQ DWWULEXWH LI SHUVRQ

QXOO ^

SHUVRQ QHZ IRR 3HUVRQ

"UT WHAT IF WE WANT THE REFERENCE TYPE TO BE DIFFERENT FROM THE ACTUAL OBJECT TYPE 7E LL CHANGE THE 0ERSON CLASS TO MAKE IT ABSTRACT AND MAKE A CONCRETE SUBCLASS %MPLOYEE )MAGINE WE WANT THE REFERENCE TYPE TO BE 0ERSON AND THE NEW OBJECT TYPE TO BE %MPLOYEE SDFNDJH IRR SXEOLF DEVWUDFW FODVV 3HUVRQ ^ SULYDWH 6WULQJ QDP H SXEOLF YRLG VHW1DP H 6WULQJ QDPH ^ WKLV QDPH QDP H ` SXEOLF 6WULQJ JHW1 DPH ^ UHWXUQ QDPH ` `

SDFNDJH IRR

^ WHQGV 3HUVRQ (PSOR\HH H[ SXEOLF FODVV ' S, LQW HP SULYDWH ^ LQW HPS,' RLG VHW(PS,' SXEOLF Y ,' PS H ' S, HP WKLV `

^ QW JHW(PS,' SXEOLF L HPS,' Q XU HW U ` `

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

)LLQVO I \aXM I\\ZQJ]\M \W $R[X"][M*MIV&

abstract class

7ITH THE CHANGES WE JUST MADE TO THE 0ERSON CLASS WE RE IN TROUBLE IF THE ATTRIBUTE CAN T BE FOUND 2XU RULJLQDO -63 MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´SDJH´ !

FODVV 3HUVRQ 6WULQJ JHW1DPH

YRLG VHW1DPH 6WULQJ

+DV WKLV UHVXOW MDYD ODQJ ,QVWDQWLDWLRQ(UURU IRR 3HUVRQ %HFDXVH WKH &RQWDLQHU WULHV WR QHZ IRR 3HUVRQ

! Obviously, w abstract t the no is on rs Pe he ake one, bu you canÕt mstill tries, based on t r ne . ai ag t t Con te in the class attribu

FODVV (PSOR\HH LQW JHW(PS,'

YRLG VHW(PS,' LQW

7E NEED TO MAKE THE REFERENCE VARIABLE TYPE 0ERSON AND THE OBJECT AN INSTANCE OF CLASS %MPLOYEE !DDING A TYPE ATTRIBUTE TO THE TAG LETS US DO THAT 2XU QHZ -63 ZLWK D W\SH MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR 3HUVRQ´ FODVV ´IRR (PSOR\HH´ VFRSH ´SDJH´!

*HQHUDWHG VHUYOHW

IRR 3HUVRQ SHUVRQ QXOO FRGH WR JHW WKH SHUVRQ DWWULEXWH LI SHUVRQ

QXOO ^

SHUVRQ QHZ IRR (PSOR\HH

type is the Now the referencane d the object n rso abstract Pe rete Employee. type is the conc

4YPE CAN BE A CLASS TYPE ABSTRACT TYPE OR AN INTERFACEˆANYTHING THAT YOU CAN USE AS A DECLARED REFERENCE TYPE FOR THE CLASS TYPE OF THE BEAN OBJECT 9OU CAN T VIOLATE *AVA TYPING RULES OF COURSE )F THE CLASS TYPE CAN T BE ASSIGNED TO THE REFERENCE TYPE YOU RE SCREWED 3O THAT MEANS THE CLASS MUST BE A SUBCLASS OR CONCRETE IMPLEMENTATION OF THE TYPE

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

W\SH ZLWKRXW FODVV

=[QVO \aXM _Q\PW]\ KTI[[ 7HAT HAPPENS IF WE DECLARE A TYPE BUT NOT A CLASS $OES IT MATTER IF THE TYPE IS ABSTRACT OR CONCRETE

no class, just type

-63

MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR 3HUVRQ´ VFRSH ´SDJH´ !

5HVXOW LI WKH SHUVRQ DWWULEXWH DOUHDG\ H[LVWV LQ ´SDJHµ VFRSH )T WORKS PERFECTLY

5HVXOW LI WKH SHUVRQ DWWULEXWH GRHV 127 H[LVW LQ ´SDJHµ VFRSH

!! WONÕT WORK

MDYD ODQJ ,QVWDQWLDWLRQ([FHSWLRQ EHDQ SHUVRQ QRW IRXQG ZLWKLQ VFRSH

,I W\SH LV XVHG ZLWKRXW FODVV WKH EHDQ PXVW DOUHDG\ H[LVW ,I FODVV LV XVHG ZLWK RU ZLWKRXW W\SH WKH FODVV PXVW 127 EH DEVWUDFW DQG PXVW KDYH D SXEOLF QR DUJ FRQVWUXFWRU

4

)N YOUR EXAMPLE hFOO 0ERSONv IS AN ABSTRACT TYPE SO OF #/523% IT CAN T BE INSTANTIATED 7HAT IF YOU CHANGE THE TYPE TO hFOO %MPLOYEEv 7ILL IT USE THE TYPE FOR BOTH THE REFERENCE !.$ THE OBJECT TYPE

$

4HERE IS NO FALLBACK RULE THAT SAYS h)F YOU CAN T FIND THE OBJECT GO AHEAD AND USE THE TYPE FOR "/4( THE REFERENCE AND THE OBJECT v .O THAT IS ./4 HOW IT WORKS "OTTOM LINE IF YOU USE TYPE WITHOUT CLASS YOU BETTER MAKE #%24!). THAT THE BEAN IS ALREADY STORED AS AN ATTRIBUTE AT THE SCOPE AND WITH THE ID YOU PUT IN THE TAG

./ )T NEVER WORKS )F THE #ONTAINER DISCOVERS THAT THE BEAN DOESN T EXIST AND IT SEES ONLY A TYPE ATTRIBUTE WITHOUT A CLASS IT KNOWS THAT YOU VE GIVEN IT ONLY (!,& OF WHAT IT NEEDSˆTHE REFERENCE TYPE BUT NOT THE OBJECT TYPE )N OTHER WORDS YOU HAVEN T TOLD IT WHAT TO MAKE A NEW INSTANCE OF

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

<PM [KWXM I\\ZQJ]\M LMNI]T\[ \W ¹XIOMº )F YOU DON T SPECIFY A SCOPE IN EITHER THE JSP USE"EAN OR JSP GET0ROPERTY TAGS THE #ONTAINER USES THE DEFAULT OF hPAGEv

7KLV MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR (PSOR\HH´ VFRSH ´SDJH´ ! ,V WKH VDPH DV WKLV MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR (PSOR\HH´ !

WK FODVV 'RQ·W FRQIXVH W\SH ZL

&KHFN RXW WKLV FRGH

RQ´ ! R\HH´ FODVV ´IRR 3HUV UVRQ´ W\SH ´IRR (PSO MVS XVH%HDQ LG ´SH X¶OO JHW D ELJ IDW DW WKLV ZLOO 1(9(5 ZRUN <R %H SUHSDUHG WR UHFRJQL]H WK -63 WR FRPSLOH FODVV IRU SHU([FHSWLRQ 8QDEOH G RUJ DSDFKH MDVSHU -DV DWH QWL VWD LQ W FDQQRW EH IRR 3HUVRQ LV DEVWUDF Z IRR 3HUVRQ 3HUVRQ QH HU %H 685( WKDW \RX UHPHPE

H W\SH UHIHUHQFH W\S FODVV REMHFW W\SH

2U WR SXW LW DQRWKHU ZD\

$5( FDQ EH DEVWUDFW W\SH LV ZKDW \RX '(&/ H

UHW RQF H F VW E PX 7$17,$7( FODVV LV ZKDW \RX ,16

VV FOD Z QH [ H W\S

RHVQ¶W KDYH WR OZD\V D FODVV ZKLOH W\SH G LQJ ³:HOO '8+²FODVV LV D SUHVHQW WKLQJV WKDW PXVW 1RZ \RX¶UH SUREDEO\ WKLQN WR UH DVV´ G ³FO XVH WKH\ H 6R RI &2856( ¶G EH ULJKW EH²W\SH FDQ EH DQ LQWHUIDF QWHUIDFHV DV ZHOO ´ $QG \RX SH´ IRU WKLQJV WKDW FDQ EH L H PRVW LQWXLWLYH DQG DV WK $/:$<6 EH D FODVV DQG ³W\ HF K H VS LQ WK ,1* I FRXUVH QRW (9(5<7+ WKH QDPH RI D %XW \RX¶UH DOVR WKLQNLQJ ³2 HFXULW\ DXWK FRQVWUDLQW! H VXUH ´ 6RPHWLPHV OLNH V H LV W\SH G W\S V DQ FODV REYLRXV QDPH VR , EHWWHU E V LV FODV DVH W LW DFWXDOO\ LV %XW LQ WKLV F WKLQJ LV WKH RSSRVLWH RI ZKD

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

EHDQ UHODWHG VWDQGDUG DFWLRQV H[HUFLVH

%( WKH &RQWDLQHU /RRN DW WKLV VWDQGDUG DFWLRQ MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR (PSOR\HH´ ! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ YDOXH ´)UHG´ ! MVS XVH%HDQ !

abstract class 3HUVRQ 6WULQJ JHW1DPH

YRLG VHW1DPH 6WULQJ

1DPH LV MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ ! 1RZ LPDJLQH WKDW D VHUYOHW GRHV VRPH ZRUN DQG WKHQ IRUZDUGV WKH UHTXHVW WR WKH -63 WKDW KDV WKH FRGH DERYH )LJXUH RXW ZKDW WKH -63 FRGH DERYH ZRXOG GR IRU HDFK RI WKH WKUHH GLIIHUHQW VHUYOHW FRGH H[DPSOHV 7KH DQVZHUV DUH DW WKH HQG RI WKH FKDSWHU

:KDW KDSSHQV LI WKH VHUYOHW FRGH ORRNV OLNH IRR 3HUVRQ S QHZ IRR (PSOR\HH S VHW1DPH ³(YDQ´ UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S

(PSOR\HH LQW JHW(PS,'

YRLG VHW(PS,' LQW

concrete class

:KDW KDSSHQV LI WKH VHUYOHW FRGH ORRNV OLNH IRR 3HUVRQ S QHZ IRR 3HUVRQ S VHW1DPH ³(YDQ´ UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S

(Both classes are in package ÒfooÓ.)

:KDW KDSSHQV LI WKH VHUYOHW FRGH ORRNV OLNH IRR (PSOR\HH S QHZ IRR (PSOR\HH S VHW1DPH ³(YDQ´ UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

, MXVW WKRXJKW RI VRPHWKLQJ VXSSRVH ZH DUHQ·W XVLQJ D VHUYOHW FRQWUROOHU DQG WKH +70/ IRUP DFWLRQ JRHV VWUDLJKW WR WKH -63 LV WKHUH D ZD\ , FDQ XVH WKH UHTXHVW SDUDPHWHUV WR VHW D EHDQ SURSHUW\ :,7+287 XVLQJ VFULSWLQJ"

/WQVO [\ZIQOP\ NZWU \PM ZMY]M[\ \W \PM 2;8 _Q\PW]\ OWQVO \PZW]OP I [MZ ^TM\ )MAGINE THIS IS OUR FORM KWPO! ERG\!

goes The requesTt to the JSP. STRAIGH

IRUP DFWLRQ ³7HVW%HDQ MVS´! QDPH LQSXW W\SH ³WH[W´ QDPH ³XVHU1DPH´! ,' LQSXW W\SH ³WH[W´ QDPH ³XVHU,'´! LQSXW W\SH ´VXEPLW´! IRUP! ERG\! KWPO!

7E KNOW WE CAN DO IT WITH A COMBINATION OF STANDARD ACTIONS AND SCRIPTING MVS XVH%HDQ LG ³SHUVRQ´ W\SH ³IRR 3HUVRQ´ FODVV ³IRR (PSOR\HH´ ! SHUVRQ VHW1DPH UHTXHVW JHW3DUDPHWHU ³XVHU1DPH´

!

7E CAN EVEN DO IT WITH SCRIPTING ).3)$% A STANDARD ACTION MVS XVH%HDQ LG ³SHUVRQ´ W\SH ³IRR 3HUVRQ´ FODVV ³IRR (PSOR\HH´! MVS VHW3URSHUW\ QDPH ³SHUVRQ´ SURSHUW\ ³QDPH´ YDOXH ³ UHTXHVW JHW3DUDPHWHU ³XVHU1DPH´ !´ ! MVS XVH%HDQ!

Yes, you ARE <jsp:setPropertseeing an expression INSIDE inside the body y> tag (which happens to bethe of a <jsp:useBea n> tag) And yes, it DOE S look bad.

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

XVLQJ SDUDP

<PM X IZIU I\\ZQJ]\M \W \PM ZM[K]M )T S SO SIMPLE 9OU CAN SEND A REQUEST PARAMETER STRAIGHT INTO A BEAN WITHOUT SCRIPTING USING THE PARAM ATTRIBUTE

7KH SDUDP DWWULEXWH OHWV \RX VHW WKH YDOXH RI D EHDQ SURSHUW\ WR WKH YDOXH RI D UHTXHVW SDUDPHWHU -867 E\ QDPLQJ WKH UHTXHVW SDUDPHWHU

,QVLGH 7HVW%HDQ MVS MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR 3HUVRQ´ FODVV ´IRR (PSOR\HH´! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ SDUDP ´XVHU1DPH´ ! MVS XVH%HDQ! KWPO! ERG\! IRUP DFWLRQ ´7HVW%HDQ MVS´! QDPH LQSXW W\SH ´WH[W´ QDPH ´XVHU1DPH´! ,' LQSXW W\SH ´WH[W´ QDPH ´XVHU,'´! LQSXW W\SH ´VXEPLW´! IRUP!

NameÓ e value Òuser but The pararmom the name att.ri comes f ormÕs input Þeld of the f

ERG\! KWPO!

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

*]\ _IQ\ 1\ OM\[ M^MV JM\\MZ !ND ALL YOU HAVE TO DO IS MAKE SURE YOUR FORM INPUT l ELD NAME WHICH BECOMES THE REQUEST PARAMETER NAME IS THE SAME AS THE PROPERTY NAME IN YOUR BEAN 4HEN IN THE JSP SET0ROPERTY TAG YOU DON T HAVE TO SPECIFY THE PARAM ATTRIBUTE )F YOU NAME THE PROPERTY BUT DON T SPECIFY A VALUE OR PARAM YOU RE TELLING THE #ONTAINER TO GET THE VALUE FROM A REQUEST PARAMETER WITH A MATCHING NAME ,I ZH FKDQJH WKH +70/ VR WKDW WKH LQSXW À HOG QDPH PDWFKHV WKH SURSHUW\ QDPH

DEVWUDFW IRR 3HUVRQ KWPO! ERG\! 6WULQJ JHW1DPH

IRUP DFWLRQ ´7HVW%HDQ MVS´! QDPH LQSXW W\SH ´WH[W´ QDPH ´QDPH´! ,' LQSXW W\SH ´WH[W´ QDPH ´XVHU,'´! LQSXW W\SH ´VXEPLW´! IRUP!

YRLG VHW1DPH 6WULQJ

ERG\! KWPO!

IRR (PSOR\HH

eld r name for this Ăž Now the parameante property (name). be e matches th

LQW JHW(PS,'

YRLG VHW(PS,' LQW

:H JHW WR GR 7+,6 MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR 3HUVRQ´ FODVV ´IRR (PSOR\HH´! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ ! MVS XVH%HDQ!

ANY We didnÕt specify

value!

,I WKH UHTXHVW SDUDPHWHU QDPH PDWFKHV WKH EHDQ SURSHUW\ QDPH \RX GRQ¡W QHHG WR VSHFLI\ D YDOXH LQ WKH MVS VHW3URSHUW\! WDJ IRU WKDW SURSHUW\

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

SURSHUWLHV DQG UHTXHVW SDUDPHWHUV

1N aW] KIV [\IVL Q\ Q\ OM\[ M^MV *-<<-: 7ATCH WHAT HAPPENS IF YOU MAKE !,, THE REQUEST PARAMETER NAMES MATCH THE BEAN PROPERTY NAMES 4HE PERSON BEAN WHICH IS AN INSTANCE OF FOO %MPLOYEE ACTUALLY HAS TWO PROPERTIESˆNAME AND EMP)$

,I ZH FKDQJH WKH +70/ DJDLQ

meters match the Now BOTH para the bean. property names of

DEVWUDFW IRR 3HUVRQ 6WULQJ JHW1DPH

KWPO! ERG\!

YRLG VHW1DPH 6WULQJ

IRUP DFWLRQ ´7HVW%HDQ MVS´! QDPH LQSXW W\SH ´WH[W´ QDPH ´QDPH´! ,' LQSXW W\SH ´WH[W´ QDPH ´HPS,'´! LQSXW W\SH ´VXEPLW´! IRUP! ERG\! KWPO!

IRR (PSOR\HH LQW JHW(PS,'

YRLG VHW(PS,' LQW

:H JHW WR GR WKLV MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR 3HUVRQ´ FODVV ´IRR (PSOR\HH´! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´ ´ ! MVS XVH%HDQ! ??

How cool is that

, ZDQW \RX WR LWHUDWH WKURXJK WKH UHTXHVW SDUDPHWHUV DQG À QG DQ\ WKDW PDWFK WKLV EHDQ·V SURSHUW\ QDPHV DQG VHW WKH 9$/8( RI WKH PDWFKLQJ SURSHUWLHV HTXDO WR WKH YDOXH RI WKH FRUUHVSRQGLQJ UHTXHVW SDUDPHWHU

2K VXUH PDNH 0( GR DOO WKH ZRUN , KDYH WR ORRN DW WKH EHDQ FODVV JHWWHUV DQG VHWWHUV WR À JXUH RXW WKH EHDQ SURSHUWLHV WKHQ PDWFK WKDW WR WKH SDUDPHWHU QDPHV

KWPO! ERG\! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´ ´! ERG\! KWPO!

-63

&RQWDLQHU

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

*MIV \IO[ KWV^MZ\ XZQUQ\Q^M XZWXMZ\QM[ I]\WUI\QKITTa )F YOU RE FAMILIAR WITH *AVA"EANS FROM ANY EARLIER LIFETIME THIS IS NO SURPRISE TO YOU *AVA"EAN PROPERTIES CAN BE ANYTHING BUT IF THEY RE 3TRINGS OR PRIMITIVES ALL THE COERCING IS DONE FOR YOU 4HAT S RIGHTˆYOU DON T HAVE TO DO THE PARSING AND CONVERSION YOURSELF ,I ZH PDNH WKH W\SH (PSOR\HH LQVWHDG RI 3HUVRQ

DEVWUDFW IRR 3HUVRQ 6WULQJ JHW1DPH

YRLG VHW1DPH 6WULQJ

IRR (PSOR\HH LQW JHW(PS,'

YRLG VHW(PS,' LQW

KWPO! ERG\! MVS XVH%HDQ LG ´SHUVRQ´ W\SH ´IRR (PSOR\HH´ FODVV ´IRR (PSOR\HH´ ! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´ ´ ! MVS XVH%HDQ! 3HUVRQ LV MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´QDPH´ ! ,' LV MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´HPS,'´ !

d servlet will say: Now the generante= new Employee(); instead of: Employee perso new Employee(); Person person =

ERG\! KWPO!

,W DOO ZRUNV KWWS ORFDOKRVW WHVW-63 7HVW%HDQ MVS

.DWK\

KWWS ORFDOKRVW WHVW-63 7HVW%HDQ MVS

7KH MVS VHW3URSHUW\! DFWLRQ WDNHV WKH 6WULQJ UHTXHVW SDUDPHWHU FRQYHUWV LW WR DQ LQW DQG SDVVHV WKDW LQW WR WKH EHDQ·V VHWWHU PHWKRG IRU WKDW SURSHUW\

3HUVRQ LV .DWK\ ,' LV

the empID This time worked too. property

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

SULPLWLYH FRQYHUVLRQ

WKHUH DUH QR

'XPE 4XHVWLRQV

4

4

/+ ) M THINKING THAT THE #ONTAINER CODE IS DOING SOME KIND OF )NTEGER PARSE)NT h v SO WOULDN T YOU GET A .UMBER&ORMAT%XCEPTION IF THE USER DOESN T TYPE IN SOMETHING THAT CAN BE PARSED TO AN INT ,IKE WHAT IF THE USER TYPES hTHREEv IN THE EMPLOYEE )$ l ELD

)F A BEAN PROPERTY DOESN T HAVE TO BE A 3TRING OR A PRIMITIVE THEN (/7 CAN YOU SET THE PROPERTY WITH OUT SCRIPTING 4HE VALUE ATTRIBUTE OF THE TAG IS ALWAYS A 3TRING RIGHT

$

)T IS POSSIBLE BUT POTENTIALLY A LOT OF EXTRA WORK TO CREATE A SPECIAL CLASS CALLED A CUSTOM PROPERTY EDITOR THAT SUPPORTS THE BEAN )T TAKES YOUR 3TRING VALUE AND FIGURES OUT HOW TO PARSE THAT INTO SOMETHING THAT CAN BE USED TO SET A MORE COMPLEX TYPE 4HIS IS PART OF THE *AVA"EANS SPEC THOUGH NOT THE *30 SPEC !LSO IF THE VALUE ATTRIBUTE IN THE JSP SET0ROPERTY TAG IS AN EXPRESSION RATHER THAN A 3TRING LITERAL THEN )& THAT EXPRESSION EVALUATES TO AN OBJECT THAT S COMPATIBLE WITH BEAN PROPERTY TYPE THEN IT WILL PROBABLY WORK )F YOU PASS IN AN EXPRESSION THAT EVALUATES TO A $OG FOR EXAMPLE THE 0ERSON BEAN S SET$OG $OG METHOD WILL BE CALLED "UT THINK ABOUT ITˆTHIS MEANS THE $OG OBJECT MUST ALREADY EXIST !NYWAY YOU RE WAY BETTER OFF ./4 TRYING TO CONSTRUCT NEW THINGS IN YOUR *30 4RYING TO GET AWAY WITH CONSTRUCTING AND SETTING EVEN MARGINALLY COMPLEX DATA TYPES IS GONNA BE TOUGH WITHOUT SCRIPTING !ND NONE OF THAT IS ON THE EXAM

'OOD CATCH 9ES SOMETHING WILL DEFINITELY GO WRONG IF THE REQUEST PARAMETER FOR THE EMP)$ PROPERTY CAN T BE PARSED INTO AN INT 9OU NEED TO VALIDATE THE CONTENTS OF THAT FIELD TO MAKE SURE IT CONTAINS ONLY NUMERIC CHARACTERS 9OU COULD SEND THE FORM DATA TO A SERVLET FIRST INSTEAD OF SENDING IT STRAIGHT TO THE *30 "UT IF YOU RE COMMITTED TO GOING FROM THE FORM STRAIGHT TO THE *30 AND YOU DON T WANT SCRIPTING JUST USE *AVA3CRIPT IN THE (4-, FORM TO CHECK THE FIELD BEFORE SENDING THE REQUEST )F YOU RE NOT FAMILIAR WITH *AVA3CRIPT WHICH OF COURSE HAS VIRTUALLY ./4().' TO DO WITH *AVA IT S A SIMPLE SCRIPTING LANGUAGE THAT S PROCESSED ON THE CLIENT SIDE )N OTHER WORDS BY THE BROWSER ! QUICK 'OOGLE SEARCH ON h*AVA3CRIPT VALIDATE INPUT FIELDv SHOULD TURN UP SOME SCRIPTS YOU CAN USE TO STOP USERS FROM ENTER ING SAY ANYTHING BUT NUMBERS INTO AN INPUT FIELD

:DWFK LW

$

$XWRPDWLF 6WULQJ WR SULPLWLYH FRQYHUVLRQ GRHV 127 ZRUN LI \RX XVH VFULSWLQJ ,W IDLOV HYHQ LI DQ H[SUHVVLRQ LV ,16,'( WKH MVS VHW3URSHUW\! WDJ

,I \RX XVH WKH MVS VHW3URSHUW\! VWDQGDUG DFWLRQ WDJ ZLWK WKH SURSHUW\ ZLOGFDUG 25 MXVW D SURSHUW\ QDPH ZLWKRXW D YDOXH RU SDUDP DWWULEXWH ZKLFK PHDQV WKH SURSHUW\ QDPH PDWFKHV WKH UHTXHVW SDUDPHWHU QDPH 25 \RX XVH D SDUDP DWWULEXWH WR LQGLFDWH WKH UHTXHVW SDUDPHWHU ZKRVH YDOXH VKRXOG EH DVVLJQHG WR WKH EHDQ¶V SURSHUW\ 25 \RX W\SH D OLWHUDO YDOXH WKH DXWRPDWLF FRQYHUVLRQ IURP 6WULQJ WR LQW ZRUNV (DFK RI WKHVH H[DPSOHV FRQYHUWV DXWRPDWLFDOO\

MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´ ´ ! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´HPS,'´ ! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´HPS,'´ YDOXH ´ ´ ! MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´HPS,'´ SDUDP ´HPS,'´ !

! These all work This does NOT work!

%87 LI \RX XVH VFULSWLQJ WKH DXWRPDWLF FRQYHUVLRQ GRHV 127 ZRUN

MVS VHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´HPS,'´ YDOXH ´ UHTXHVW JHW3DUDPHWHU ³HPS,'´ !´ !

RUJ DSDFKH MDVSHU -DVSHU([FHSWLRQ 8QDEOH WR FRPSLOH FODVV IRU -63 *HQHUDWHG VHUYOHW HUURU VHW(PS,' LQW LQ IRR (PSOR\HH FDQQRW EH DSSOLHG WR MDYD ODQJ 6WULQJ

3HUVRQ VHW(PS,' UHTXHVW JHW3DUDPHWHU ³HPS,'´

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

:KHZ , DP MXVW 62 UHOLHYHG DW KRZ PXFK HDVLHU LW LV WR XVH WKRVH WDJV LQVWHDG RI VFULSWLQJ 7KH EHQHÀWV WR PH DUH VWDJJHULQJO\ REYLRXV

,W·V QRW DERXW KHU %XW VKH WKLQNV HYHU\WKLQJ LV DOO DERXW KHU 7KLV LV DERXW WKHP

7KH EHDQ VWDQGDUG DFWLRQ WDJV DUH PRUH QDWXUDO WR D QRQ SURJUDPPHU /NCE AGAIN THE BENElT OF USING TAGS OVER SCRIPTING IS MORE ABOUT THE WEB PAGE DESIGNERS THAN ABOUT YOU THE *AVA PROGRAMMER !LTHOUGH EVEN *AVA PROGRAMMERS lND THAT TAGS ARE EASIER TO MAINTAIN THAN HARD CODED *AVA SCRIPTING ELEMENTS 7ITH THE BEAN RELATED TAGS THE DESIGNER NEEDS ONLY THE BASIC IDENTIlCATION INFO ATTRIBUTE NAME SCOPE AND PROPERTY NAME 4RUE THEY DO HAVE TO KNOW THE FULLY QUALIlED CLASS NAME BUT AS FAR AS THE WEB PAGE DESIGNER KNOWSˆIT S JUST A NAME WITH DOTS IN IT 4HE WEB DESIGNER DOESN T NEED ANY KNOWLEDGE OF WHAT S REALLY BEHIND IT AND THEY CAN THINK OF BEANS AS SIMPLY RECORDS WITH lELDS 9OU TELL THE DESIGNERS THE RECORD THE CLASS AND THE IDENTIlER AND THE lELDS THE PROPERTIES 3TILL THE BEAN STANDARD ACTIONS AREN T AS ELEGANT AS THEY COULD BE !ND THAT S WHY THIS ISN T THE END OF THE STORY ON SCRIPTLESS PAGES 2EAD ON

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

REMHFW SURSHUWLHV

*]\ _PI\ QN \PM XZWXMZ\a Q[ [WUM\PQVO 7<0-: \PIV I ;\ZQVO WZ XZQUQ\Q^M'

Note: Person is a con class in this example. crete

7E KNOW HOW EASY IT IS TO PRINT AN ATTRIBUTE WHEN THE ATTRIBUTE ITSELF IRR 3HUVRQ IS A 3TRING 4HEN WE MADE AN ATTRIBUTE THAT WAS A NON 3TRING OBJECT A 0ERSON BEAN INSTANCE "UT WE DIDN T WANT TO PRINT THE ATTRIBUTE HW1DPH SXEOLF 6WULQJ J PERSON ˆWE WANTED TO PRINT A PROPERTY OF THE ATTRIBUTE IN OUR H 6WULQJ

W1 EXAMPLE THE PERSON S NAME AND EMP)$ 4HAT WORKED l NE BECAUSE THE SXEOLF YRLG VH DP STANDARD ACTIONS CAN HANDLE 3TRING AND PRIMITIVE PROPERTIES 3O WE HW'RJ KNOW THAT STANDARD ACTIONS CAN DEAL WITH AN ATTRIBUTE OF ANY TYPE AS SXEOLF 'RJ J W'RJ 'RJ

LONG AS ALL THE ATTRIBUTE S PROPERTIES ARE 3TRINGS OR PRIMITIVES VH SXEOLF YRLG "UT WHAT IF THEY RE NOT 7HAT IF THE BEAN HAS A PROPERTY THAT IS NOT A 3TRING OR PRIMITIVE 7HAT IF THE PROPERTY IS YET ANOTHER /BJECT TYPE !N /BJECT TYPE WITH PROPERTIES OF ITS OWN

7HAT IF WHAT WE REALLY WANT IS TO PRINT A PROPERTY OF THAT PROPERTY 3HUVRQ KDV D 6WULQJ ´QDPH¾ SURSHUW\ 3HUVRQ KDV D 'RJ ´GRJ¾ SURSHUW\

IRR 'RJ SXEOLF 6WULQJ JHW1DPH SXEOLF YRLG VHW1DPH 6WULQJ

'RJ KDV D 6WULQJ ´QDPH¾ SURSHUW\

7HAT IF WE WANT TO PRINT THE NAME OF THE 0ERSON S DOG

6HUYOHW FRGH SXEOLF YRLG GR3RVW +WWS6HUYOHW5HTXHVW UHTXHVW +WWS6HUYOHW5HVSRQVH UHVSRQVH WKURZV ,2([FHSWLRQ 6HUYOHW([FHSWLRQ ^ IRR 3HUVRQ S QHZ IRR 3HUVRQ S VHW1DPH ³(YDQ´ IRR 'RJ GRJ QHZ IRR 'RJ GRJ VHW1DPH ³6SLNH´ S VHW'RJ GRJ UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S

it a name, ke a Dog, give . on rs Pe This time we ma () on the and call setDog value for og D rson has a e Person (just Pe e th at th Now ty, we set th its ÒdogÓ propasera request attribute. ) on rs the Pe

5HTXHVW'LVSDWFKHU YLHZ UHTXHVW JHW5HTXHVW'LVSDWFKHU ³UHVXOW MVS´ YLHZ IRUZDUG UHTXHVW UHVSRQVH `

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

<ZaQVO \W LQ[XTIa \PM XZWXMZ\a WN \PM XZWXMZ\a 7E KNOW WE CAN DO IT WITH SCRIPTING BUT CAN WE DO IT WITH THE BEAN STANDARD ACTIONS 7HAT HAPPENS IF WE PUT hDOGv AS THE PROPERTY IN THE JSP GET0ROPERTY TAG

:LWKRXW VWDQGDUG DFWLRQV XVLQJ VFULSWLQJ

KWPO! ERG\!

t rfectly... bu g. This works pe in pt ri e sc we had to us

IRR 3HUVRQ UHTXHVW JHW$WWULEXWH ³SHUVRQ´

JHW'RJ JHW1DPH ! ERG\! KWPO!

:LWK VWDQGDUG DFWLRQV QR VFULSWLQJ

KWPO! ERG\! MVS XVH%HDQ LG ´SHUVRQ´ FODVV ´IRR 3HUVRQ´ VFRSH ´UHTXHVW´ ! 'RJœV QDPH LV MVS JHW3URSHUW\ QDPH ´SHUVRQ´ SURSHUW\ ´GRJ´ !

the But whatÕs ogÓ? Òd of e valu

ERG\! KWPO!

:KDW ZH :$17 KWWS ORFDOKRVW WHVW-63 7HVWHU GR

'RJ¡V QDPH LV 6SLNH

:KDW ZH *27 KWWS ORFDOKRVW WHVW-63 7HVWHU GR

'RJ¡V QDPH LV IRR 'RJ#

All we got was the result of DogÕs toString() me thod

<RX FDQ¡W VD\ SURSHUW\ ´GRJ QDPH¾ 4HERE S NO COMBINATION OF THE BEAN STANDARD ACTIONS THAT LL WORK GIVEN THE ORIGINAL SERVLET CODE BECAUSE THE $OG IS NOT AN ATTRIBUTE $OG IS A PROPERTY OF THE ATTRIBUTE SO YOU CAN DISPLAY THE $OG BUT YOU CAN T NAVIGATE TO THE NAME PROPERTY OF THE $OG PROPERTY OF THE 0ERSON ATTRIBUTE 4HE JSP GET0ROPERTY LETS YOU ACCESS ONLY THE PROPERTIES OF THE BEAN ATTRIBUTE 4HERE S NO CAPABILITY FOR NESTED PROPERTIES WHERE YOU WANT A PROPERTY OF A PROPERTY RATHER THAN A PROPERTY OF THE ATTRIBUTE \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

(/ WR WKH UHVFXH

-`XZM[[QWV 4IVO]IOM -4 [I^M[ \PM LIa 9ES JUST IN TIME TO SAVE US THE *30 %XPRESSION ,ANGUAGE %, WAS ADDED TO THE *30 SPEC RELEASING US FROM THE TYRANNY OF SCRIPTING ,OOK HOW BEAUTIFULLY SIMPLE OUR *30 IS NOW

-63 FRGH ZLWKRXW VFULSWLQJ XVLQJ (/

KWPO! ERG\! 'RJÂśV QDPH LV ^SHUVRQ GRJ QDPH` ERG\! KWPO!

(/ PDNHV LW HDV\ WR SULQW Q HVWHG SURSHUWLH V ZRUGV SUR LQ RWKHU SHUWLHV R I SURSHUWLH V

lare This is it! We didnÕt even decknows. what person means... it just

7KLV ^SHUVRQ GRJ QDPH`

5HSODFHV WKLV IRR 3HUVRQ UHTXHVW JHW$WWULEXWH ³SHUVRQ´

JHW'RJ JHW1DPH !

<RX GRQÂśW QHHG WR NQRZ (9(5<7+,1* DERXW (/ 7KH H[DP GRHVQÂśW H[SHFW \RX WR EH D FRPSOHWH (/ EHLQJ (YHU\WKLQJ \RX PLJKW W\SLFDOO\ XVH RU EH WHVWHG RQ LV FRYHUHG LQ WKH QH[W IHZ SDJHV 6R LI \RX ZDQW WR VWXG\ WKH (/ VSHF NQRFN \RXUVHOI RXW -XVW VR \RXÂśUH FOHDU WKDW :( GLGQÂśW WHOO \RX WR GR WKDW

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

,MKWV[\Z]K\QVO \PM 2;8 -`XZM[[QWV 4IVO]IOM - 4 4HE SYNTAX AND RANGE OF THE LANGUAGE ARE DIRT SIMPLE 4HE TRICKY PART IS THAT SOME OF %, LOOKS LIKE *AVA BUT BEHAVES DIFFERENTLY 9OU LL SEE WHEN WE GET TO THE ;= OPERATOR IN A MOMENT 3O YOU LL l ND THINGS THAT WOULDN T WORK IN *AVA BUT WILL WORK IN %, AND VICE VERSA *UST DON T TRY TO MAP *AVA LANGUAGE SYNTAX RULES ONTO %, AND YOU LL BE l NE &OR THE NEXT FEW PAGES THINK OF %, AS A WAY TO ACCESS *AVA OBJECTS WITHOUT USING *AVA (/ H[SUHVVLRQV DUH $/:$<6 ZLWKLQ FXUO\ EUDFHV DQG SUHĂ€ [HG ZLWK WKH GROODU VLJQ

^SHUVRQ QDPH` HG YDULDEOH LQ 7KH À UVW QDP DQ HU LWK H LV RQ WKH H[SUHVVL XWH FW RU DQ DWWULE LPSOLFLW REMH

^Âż UVW7KLQJ VHFRQG7KLQJ` (/ ,03/,&,7 2%-(&7

All these arse map object

SDJH6FRSH UHTXHVW6FRSH VHVVLRQ6FRSH DSSOLFDWLRQ6FRSH

OR

$775,%87(

LQ SDJH VFRSH LQ UHTXHVW VFRSH LQ VHVVLRQ VFRSH LQ DSSOLFDWLRQ VFRSH

SDUDP SDUDP9DOXHV

If the Ăžrst thing in EL expression is an attribthe , it can be the name of ute attribute stored in anyanof the four available scopes.

KHDGHU KHDGHU9DOXHV FRRNLH LQLW3DUDP SDJH&RQWH[W

Note: EL implicit objects are not the same as the implicit objects available to JSP scripting, except for pageContext.

Of all the implicit object s, only pageContext is not a map an actual reference to the. ItÕs pageContext object! (An d the pageContext is a JavaB ean.)

(Java reminder: a map is a collection that holds key/value pairs, like Hashtable and HashMap.) \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

WKH GRW RSHUDWRU LQ (/

=[QVO \PM LW\ WXMZI\WZ \W IKKM[[ XZWXMZ\QM[ IVL UIX ^IT]M[ 4HE lRST VARIABLE IS EITHER AN IMPLICIT OBJECT OR AN ATTRIBUTE AND THE THING TO THE RIGHT OF THE DOT IS EITHER A MAP VALUE IF THE lRST VARIABLE IS A MAP OR A BEAN PROPERTY IF THE lRST VARIABLE IS AN ATTRIBUTE THAT S A *AVA"EAN

,I WKH H[SUHVVLRQ KDV D YDULDEOH IROORZHG E\ D GRW WKH OHIW KDQG YDULDEOH 0867 EH D 0DS RU D EHDQ

^SHUVRQ QDPH`

MD YD

XWLO 0D

S

D EHDQ

7KH WKLQJ WR WKH ULJKW RI WKH GRW 0867 EH D 0DS NH\ RU D EHDQ SURSHUW\

^SHUVRQ QDPH` ´QDPHµ ´(YDQµ

MD YD

XWLO 0D

JHW1DPH VHW1DPH

S

:KHQ WKH YDULDEOH LV RQ WKH OHIW VLGH RI WKH GRW LW·V HLWKHU D 0DS VRPHWKLQJ ZLWK NH\V RU D EHDQ VRPHWKLQJ ZLWK SURSHUWLHV 7KLV LV WUXH UHJDUGOHVV RI ZKHWKHU WKH YDULDEOH LV DQ LPSOLFLW REMHFW RU DQ DWWULEXWH 7KH SDJH&RQWH[W LPSOLFLW REMHFW LV D EHDQ³LW KDV JHWWHU PHWKRGV $OO RWKHU LPSOLFLW REMHFWV DUH 0DSV

$QG WKH WKLQJ RQ WKH ULJKW PXVW IROORZ QRUPDO -DYD QDPLQJ UXOHV IRU LGHQWLÀHUV

^SHUVRQ QDPH` 0XVW VWDUW ZLWK D OHWWHU B RU $IWHU WKH ÀUVW FKDUDFWHU \RX FDQ LQFOXGH QXPEHUV &DQ·W EH D -DYD NH\ZRUG

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

<PM C E WXMZI\WZ Q[ TQSM \PM LW\ WVTa _Ia JM\\MZ 4HE DOT OPERATOR WORKS ONLY WHEN THE THING ON THE RIGHT IS A BEAN PROPERTY OR MAP KEY FOR THE THING ON THE LEFT 4HAT S IT "UT THE ; = OPERATOR IS A LOT MORE POWERFUL AND mEXIBLE

7KLV

^SHUVRQ>³QDPH´@` ,V WKH VDPH DV WKLV

^SHUVRQ QDPH`

7KDW GRHVQ·W ORRN EHWWHU 7KDW MXVW ORRNV OLNH PRUH ZRUN DGGLQJ EUDFNHWV DQG TXRWHV

7KH VLPSOH GRW RSHUDWRU YHUVLRQ ZRUNV EHFDXVH SHUVRQ LV D EHDQ DQG QDPH LV D SURSHUW\ RI SHUVRQ %XW ZKDW LI SHUVRQ LV DQ DUUD\" 2U ZKDW LI SHUVRQ LV D /LVW" 2U ZKDW LI QDPH LV VRPHWKLQJ WKDW FDQ·W EH H[SUHVVHG ZLWK WKH QRUPDO -DYD QDPLQJ UXOHV"

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

EXW WKH > @ LV EHWWHU

<PM C E OQ^M[ aW] UWZM WX\QWV[ 7HEN YOU USE THE DOT OPERATOR THE THING ON THE LEFT CAN BE ONLY A -AP OR A BEAN AND THE THING ON THE RIGHT MUST FOLLOW *AVA NAMING RULES FOR IDENTIlERS "UT WITH THE ; = THE THING ON THE LEFT CAN ALSO BE A ,IST OR AN ARRAY OF ANY TYPE 4HAT ALSO MEANS THE THING ON THE RIGHT CAN BE A NUMBER OR ANYTHING THAT RESOLVES TO A NUMBER OR AN IDENTIlER THAT DOESN T lT THE *AVA NAMING RULES &OR EXAMPLE YOU MIGHT HAVE A -AP KEY THAT S A 3TRING WITH DOTS IN THE NAME hCOM FOO TROUBLEv

,I WKH H[SUHVVLRQ KDV D YDULDEOH IROORZHG E\ D EUDFNHW > @ WKH OHIW KDQG YDULDEOH FDQ EH D 0DS D EHDQ D /LVW RU DQ DUUD\

^PXVLF/LVW>³VRPHWKLQJ´@`

D XWLO 0D S

D Q DU U D \

D %HDQ

MDY D

XWLO /L

V

W

MD Y

,I WKH WKLQJ LQVLGH WKH EUDFNHWV LV D 6WULQJ OLWHUDO L H LQ TXRWHV LW FDQ EH D 0DS NH\ RU D EHDQ SURSHUW\ RU DQ LQGH[ LQWR D /LVW RU DUUD\

^PXVLF/LVW>³VRPHWKLQJ´@` ´VXUIµ ´7DKLWL µ

MD YD

XWLO 0D

´=HUR µ ´%7µ

S DQ DUUD\

JHW6RQJ/LVW VHW6RQJ/LVW

´=HUR µ ´%7µ

XWLO /LV

W

MD YD FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

=[QVO \PM C E WXMZI\WZ _Q\P IV IZZIa ,Q D 6HUYOHW 6WULQJ>@ IDYRULWH0XVLF ^³=HUR ´ ³7DKLWL ´ ³%7´ ³)URX )URX´` UHTXHVW VHW$WWULEXWH ³PXVLF/LVW´ IDYRULWH0XVLF

,Q D -63 0XVLF LV ^PXVLF/LVW`

s nse... call y. Makes seg() on the arra in r t S to

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

0XVLF LV >/MDYD ODQJ 6WULQJ #G GG

)LUVW VRQJ LV ^PXVLF/LVW> @`

duh..

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

)LUVW VRQJ LV =HUR

7KLV LV D MRNH ULJKW" 2U HOVH WKHUH¡V PRUH WKDQ SXQFK LQ WKLV GULQN , FRXOG 6:($5 WKDW WKRVH DUH TXRWHV DURXQG WKH DUUD\ LQGH[ DQG WKDW¡V MXVW QRW ULJKW GXGH

? WTF??

6HFRQG VRQJ LV ^PXVLF/LVW>³ ´@` KWWS ORFDOKRVW WHVW-63 7HVWHU GR

6HFRQG VRQJ LV 7DKLWL

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

DFFHVVLQJ OLVWV DQG DUUD\V

) ;\ZQVO QVLM` Q[ KWMZKML \W IV QV\ NWZ IZZIa[ IVL 4Q[\[ 4HE %, FOR ACCESSING AN ARRAY IS THE SAME AS THE %, FOR ACCESSING A ,IST 2EMEMBER FOLKS THIS IS ./4 *AVA )N %, THE ; = OPERATOR IS ./4 THE ARRAY ACCESS OPERATOR .O IT S JUST CALLED THE ; = OPERATOR 7E SWEAR LOOK IT UP IN THE SPECˆIT HAS NO NAME *UST THE SYMBOL ; = ,IKE 0RINCE KIND OF )F IT $)$ HAVE A NAME IT WOULD BE THE ARRAY ,IST -AP BEAN 0ROPERTY ACCESS OPERATOR

,Q D 6HUYOHW MDYD XWLO $UUD\/LVW IDYRULWH)RRG QHZ MDYD XWLO $UUD\/LVW IDYRULWH)RRG DGG ³FKDL LFH FKUHDP´ IDYRULWH)RRG DGG ³IDMLWDV´ IDYRULWH)RRG DGG ³WKDL SL]]D´ IDYRULWH)RRG DGG ³DQ\WKLQJ LQ GDUN FKRFRODWH´ UHTXHVW VHW$WWULEXWH ³IDYRULWH)RRG´ IDYRULWH)RRG

,Q D -63 )RRGV DUH ^IDYRULWH)RRG`

overridden toString(). Obviously ArrayList has a nice

,I WKH WKLQJ WR WKH OHIW RI WKH EUDFNHW LV DQ DUUD\ RU D /LVW DQG WKH LQGH[ LV D 6WULQJ OLWHUDO WKH LQGH[ LV FRHUFHG WR DQ LQW

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

)RRGV DUH >FKDL LFH FKUHDP ID MLWDV WKDL SL]]D DQ\WKLQJ LQ GDUN FKRFRODWH@

right )LUVW IRRG LV ^IDYRULWH)RRG> @` KWWS ORFDOKRVW WHVW-63 7HVWHU GR

)LUVW IRRG LV FKDL LFH FKUHDP

6HFRQG IRRG LV ^IDYRULWH)RRG>³ ´@` KWWS ORFDOKRVW WHVW-63 7HVWHU GR

6HFRQG IRRG LV IDMLWDV

7KLV ZRXOG 127 ZRUN ^IDYRULWH)RRG>³RQH´@`

%HFDXVH ´RQHÂľ FDQ¡W EH WXUQHG LQWR DQ LQW <RX¡OO JHW DQ HUURU LI WKH LQGH[ FDQ¡W EH FRHUFHG Very, very if thatĂ•s theweird, but OK... IĂ•ll have to way it works, get used to it.

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

)DFH WKH IDFWV GRW ER\ ,·P ZD\ FRROHU WKDQ \RX 'R \RX NQRZ ZKDW LW VD\V LQ WKH VSHF DERXW \RX" ,W FDOOV \RX ´D FRQYHQLHQFH RSHUDWRUµ 7KDW·V DOPRVW WRR FXWH

5LLLLJKW OLNH +DVKWDEOHV KDYHQ·W EHHQ DURXQG VLQFH WKH VWRQH DJH

+DYH \RX DFWXDOO\ FKHFNHG WKH GDWH RQ WKH -DYD%HDQV VSHF ODWHO\" ,I WKDW VSHF ZHUH PLON LW ZRXOG EH VRPH KLGHRXV FUHDWXUH IURP WKH ; )LOHV E\ QRZ

2K FRPH RQ VHULRXVO\ GRHV $1<%2'< XVH DUUD\V DQ\PRUH" $UUD\V DQG OLVWV DUH VR /LQHDU %RULQJ

7KH IDFW WKDW \RX HYHQ XVHG WKH ZRUG +DVKWDEOH VKRZV KRZ XS WR GDWH <28 DUH 7KH\·UH OLNH OHJDF\ FRGH QRZ ,·P WDONLQJ DERXW 0DSV DQG -DYD%HDQV 7KDW·V DOO DQ\RQH XVHV WKHVH GD\V

<RX MXVW GRQ·W JHW LW

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

> @ DQG WKH GRW

.WZ JMIV[ IVL 5IX[ aW] KIV ][M MQ\PMZ WXMZI\WZ &OR *AVA"EANS AND -APS YOU CAN USE EITHER THE ;= OPERATOR OR THE CONVENIENT DOT OPERATOR *UST THINK OF MAP KEYS THE SAME WAY YOU THINK OF PROPERTY NAMES IN A BEAN 9OU ASK FOR THE KEY OR PROPERTY NAME AND YOU GET BACK THE VALUE OF THE KEY OR PROPERTY

,Q D 6HUYOHW MDYD XWLO 0DS PXVLF0DS QHZ MDYD XWLO +DVK0DS PXVLF0DS SXW ³$PELHQW´ ³=HUR ´ PXVLF0DS SXW ³6XUI´ ³7DKLWL ´ PXVLF0DS SXW ³'-´ ³%7´ PXVLF0DS SXW ³,QGLH´ ³7UDYLV´ UHTXHVW VHW$WWULEXWH ³PXVLF0DS´ PXVLF0DS

Make a Map, put some String keys and objects in it, then make it a request attribute.

,Q D -63 $PELHQW LV ^PXVLF0DS $PELHQW` KWWS ORFDOKRVW WHVW-63 7HVWHU GR

$PELHQW LV =HUR

Both expressions use Ambient as the key into a Map (since musicMap is a Map!). $PELHQW LV ^PXVLF0DS>³$PELHQW´@` KWWS ORFDOKRVW WHVW-63 7HVWHU GR

$PELHQW LV =HUR

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

1N Q\Âź[ 67< I ;\ZQVO TQ\MZIT Q\Âź[ M^IT]I\ML )F THERE ARE NO QUOTES INSIDE THE BRACKETS THE #ONTAINER EVALUATES WHAT S INSIDE THE BRACKETS BY SEARCHING FOR AN ATTRIBUTE BOUND UNDER THAT NAME AND SUBSTITUTES THE VALUE OF THE ATTRIBUTE )F THERE IS AN IMPLICIT OBJECT WITH THE SAME NAME THE IMPLICIT OBJECT WILL ALWAYS BE USED

, this does Without quotes around Ambient d attribute boun no eÕs ther e Sinc NOT work!! back null.. es com lt resu the tÓ, named ÒAmbien

0XVLF LV ^PXVLF0DS>$PELHQW@`

)LQG DQ DWWULEXWH QDPHG ´$PELHQW¾ 8VH WKH 9$/8( RI WKDW DWWULEXWH DV WKH NH\ LQWR WKH 0DS RU UHWXUQ QXOO ,Q D VHUYOHW

MDYD XWLO 0DS PXVLF0DS QHZ MDYD XWLO +DVK0DS PXVLF0DS SXW ³$PELHQW´ ³=HUR ´ PXVLF0DS SXW ³6XUI´ ³7DKLWL ´ PXVLF0DS SXW ³'-´ ³%7´ PXVLF0DS SXW ³,QGLH´ ³)URX )URX´ UHTXHVW VHW$WWULEXWH ³PXVLF0DS´ PXVLF0DS

UHTXHVW VHW$WWULEXWH ³*HQUH´ ³$PELHQW´

7KLV '2(6 ZRUN LQ D -63 0XVLF LV ^PXVLF0DS>*HQUH@`

HYDOXDWHV WR

0XVLF LV ^PXVLF0DS>³$PELHQW´@`

EHFDXVH WKHUH ,6 D UHTXHVW DWWULEXWH QDPHG ³*HQUH´ ZLWK D YDOXH RI ³$PELHQW´ DQG ³$PELHQW´ LV D NH\ LQWR PXVLF0DS

7KLV GRHV 127 ZRUN LQ D -63 JLYHQ WKH VHUYOHW FRGH

0XVLF LV ^PXVLF0DS>³*HQUH´@`

GRHVQ¡W FKDQJH

EHFDXVH WKHUH ,6 QR NH\ LQ PXVLF0DS QDPHG ³*HQUH´ :LWK WKH TXRWHV DURXQG LW WKH &RQWDLQHU GLGQœW WU\ WR HYDOXDWH LW DQG MXVW DVVXPHG LW ZDV D OLWHUDO NH\ QDPH

0XVLF LV ^PXVLF0DS>³*HQUH´@`

but This is a valid EL expression, . it doesnÕt do what we wanted

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

QHVWHG H[SUHVVLRQV

AW] KIV ][M VM[\ML M`XZM[[QWV[ QV[QLM \PM JZIKSM\[ )T S EXPRESSIONS ALL THE WAY DOWN IN %, 9OU NEST EXPRESSIONS TO ANY ARBITRARY LEVEL )N OTHER WORDS YOU CAN PUT A COMPLEX EXPRESSION INSIDE A COMPLEX EXPRESSION INSIDE A IT KEEPS GOING !ND THE EXPRESSIONS ARE EVALUATED FROM THE INNER MOST BRACKETS OUT 4HIS PART WILL SEEM COMPLETELY INTUITIVE TO YOU BECAUSE IT S NO DIFFERENT THAN NESTING *AVA CODE WITHIN PARENS 4HE TRICKY PART IS TO WATCH OUT FOR QUOTES VS NO QUOTES

,Q D VHUYOHW MDYD XWLO 0DS PXVLF0DS QHZ MDYD XWLO +DVK0DS PXVLF0DS SXW ³$PELHQW´ ³=HUR ´ PXVLF0DS SXW ³6XUI´ ³7DKLWL ´ PXVLF0DS SXW ³'-´ ³%7´ PXVLF0DS SXW ³,QGLH´ ³)URX )URX´ UHTXHVW VHW$WWULEXWH ³PXVLF0DS´ PXVLF0DS 6WULQJ>@ PXVLF7\SHV ^³$PELHQW´ ³6XUI´ ³'-´ ³,QGLH´` UHTXHVW VHW$WWULEXWH ³0XVLF7\SH´ PXVLF7\SHV

7KLV '2(6 ZRUN LQ D -63 0XVLF LV ^PXVLF0DS>0XVLF7\SH> @@`

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

0XVLF LV =HUR

becomes 0XVLF LV ^PXVLF0DS>³$PELHQW´@`

becomes 0XVLF LV =HUR

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

AW] KIVÂź\ LW ^IRR ` 7ITH BEANS AND -APS YOU CAN USE THE DOT OPERATOR BUT ONLY IF THE THING YOU TYPE AFTER THE DOT IS A LEGAL *AVA IDENTIFER

7KLV ^PXVLF0DS $PELHQW`

works

,V WKH VDPH DV WKLV ^PXVLF0DS>³$PELHQW´@`

works

%XW WKLV ^PXVLF/LVW>³ ´@`

Q¡W XVH LW IRU D ,I \RX ZRXOG DYD FRGH U RX \ LQ H YDULDEOH QDP W DIWHU WKH GR '21¡7 SXW LW

&$1127 EH WXUQHG LQWR WKLV ^PXVLF/LVW `

!

O NO! NO! N

6KDUSHQ \RXU SHQFLO :KDW SULQWV" *LYHQ WKH VHUYOHW FRGH EHORZ ¿ JXUH RXW ZKDW ZRXOG SULQW RU LI WKHUHœG EH DQ HUURU MXVW ZULWH \RX NQRZ ³HUURU´ $QVZHUV DUH DW WKH ERWWRP RI WKH QH[W SDJH

MDYD XWLO $UUD\/LVW QXPV QHZ MDYD XWLO $UUD\/LVW QXPV DGG ³ ´ QXPV DGG ³ ´ QXPV DGG ³ ´ UHTXHVW VHW$WWULEXWH ³QXPEHUV´ QXPV 6WULQJ>@ IDYRULWH0XVLF ^³=HUR ´ ³7DKLWL ´ ³%7´ ³)URX )URX´` UHTXHVW VHW$WWULEXWH ³PXVLF/LVW´ IDYRULWH0XVLF

^PXVLF/LVW>QXPEHUV> @@`

^PXVLF/LVW>QXPEHUV> @ @`

^PXVLF/LVW>QXPEHUV>³ ´@@`

^PXVLF/LVW>QXPEHUV>QXPEHUV> @@@`

(WeÕll talk more about EL operators in a few pages.)

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

ELJ H[HUFLVH RQ (/

&RGH 0DJQHWV $ON T BE SURPRISED IF YOU l ND SOMETHING LIKE THIS ON THE EXAM EXCEPT IN THE REAL EXAM IT LL LOOK UGLIER

IRR 7R\ SDFNDJH IRR SXEOLF FODVV 7R\ ^ SULYDWH 6WULQJ QDPH SXEOLF YRLG VHW1DPH 6WULQJ QDPH ^ WKLV QDPH QDPH ` SXEOLF 6WULQJ JHW1DPH ^ UHWXUQ QDPH ` `

3TUDY THE THREE CLASSES ON THE PAGE AND THE SERVLET CODE ON THE OPPOSITE PAGE THEN CONSTRUCT THE CODE MAGNETS TO MAKE THE %, THAT LL PRODUCE THE RESPONSE SHOWN IN THE BROWSER 4URN THE PAGE FOR THE ANSWERS BUT NOT UNTIL YOU $/ 4()3 ESPECIALLY IF YOU RE GOING TO TAKE THE EXAM

IRR 3HUVRQ SDFNDJH IRR SXEOLF FODVV 3HUVRQ ^ SULYDWH 'RJ GRJ SULYDWH 6WULQJ QDPH SXEOLF YRLG VHW'RJ 'RJ GRJ ^ WKLV GRJ GRJ ` SXEOLF 'RJ JHW'RJ ^ UHWXUQ GRJ ` SXEOLF YRLG VHW1DPH 6WULQJ QDPH ^ WKLV QDPH QDPH ` SXEOLF 6WULQJ JHW1DPH ^ UHWXUQ QDPH ` `

IRR 'RJ SDFNDJH IRR SXEOLF FODVV 'RJ ^ SULYDWH 6WULQJ QDPH SULYDWH 7R\> @ WR\V SXEOLF YRLG VHW1DPH 6WULQJ QDPH ^ WKLV QDPH QDPH ` SXEOLF 6WULQJ JHW1DPH ^ UHWXUQ QDPH ` SXEOLF YRLG VHW7R\V 7R\>@ WR\V ^ WKLV WR\V WR\V ` SXEOLF 7R\> @ JHW7R\V ^ UHWXUQ WR\V ` `

$QVZHUV WR 6KDUSHQ RQ SUHYLRXV SDJH 7DKLWL %7 )URX )URX )URX )URX

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

6HUYOHW FRGH

&RPSRVH WKH (/ IRU WKLV RXWSXW

IRR 3HUVRQ S QHZ IRR 3HUVRQ S VHW1DPH Âł/HHOX´ IRR 'RJ G QHZ IRR 'RJ G VHW1DPH Âł&O\GH´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH ÂłVWLFN´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH ÂłQHLJKERUÂśV FDW´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH Âł%DUELHÂŒ GROO KHDG´ G VHW7R\V QHZ IRR 7R\>@^W W W ` S VHW'RJ G UHTXHVW VHW$WWULEXWH ÂłSHUVRQ´ S

^SH UVRQ QDP H`

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

/HHOXÂśV GRJ &O\GHÂśV WR\V DUH VWLFN QHLJKERUÂśV FDW DQG D %DUELHÂŒ GROO KHDG

ÂśV

^SHUVRQ GRJ WR\V>³QDPH´@

GRJ

ÂśV

DQG D

QDPH

WR\V

QDPH

QDPH

WR\V

`

^SHUVRQ

WR\V

`

`

> @

WR\V> @

QDPH

^SHUVRQ GRJ

^SHUVRQ GRJ QDPH`

> @

QDPH` ^SHUVRQ GRJ WR\V

^SHUVRQ GRJ WR\V

> @

^SHUVRQ GRJ WR\V QDPH` WR\V

WR\V

WR\V>³QDPH´@ GRJ

> @

WR\V DUH

WR\V>³QDPH´@ \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

H[HUFLVH DQVZHUV

&RGH 0DJQHWV $QVZHUV

This is not the ONLY way to produce the output, but itÕs the only way using this set of magnets. Bonus exercise: write the EL expressions a little differently (forget the magnets), but print the same result.

6HUYOHW FRGH

&RPSRVH WKH (/ IRU WKLV RXWSXW

IRR 3HUVRQ S QHZ IRR (PSOR\HH S VHW1DPH Âł/HHOX´ IRR 'RJ G QHZ IRR 'RJ G VHW1DPH Âł&O\GH´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH ÂłVWLFN´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH ÂłQHLJKERUÂśV FDW´ IRR 7R\ W QHZ IRR 7R\ W VHW1DPH Âł%DUELHÂŒ GROO KHDG´ G VHW7R\V QHZ IRR 7R\>@^W W W ` S VHW'RJ G UHTXHVW VHW$WWULEXWH ÂłSHUVRQ´ S

^SHUVRQ QDPH`

ÂśV

^SHUVRQ GRJ

WR\V> @ QDPH

GRJ

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

/HHOXÂśV GRJ &O\GHÂśV WR\V DUH VWLFN QHLJKERUÂśV FDW DQG D %DUELHÂŒ GROO KHDG

^SHUVRQ GRJ QDPH`

`

DQG D

ÂśV

^SHUVRQ GRJ WR\V > @ QDPH `

WR\V DUH

^SHUVRQ

GRJ

WR\V

> @

QDPH

`

^SHUVRQ QDPH`ÂśV GRJ ^SHUVRQ GRJ QDPH`ÂśV WR\V DUH ^SHUVRQ GRJ WR\V> @ QDPH` ^SHUVRQ GRJ WR\V> @ QDPH` DQG D ^SHUVRQ GRJ WR\V> @ QDPH`

WR\V

WR\V

WR\V

WR\V

> @

QDPH ^SHUVRQ GRJ WR\V>³QDPH´@

^SHUVRQ GRJ WR\V QDPH` WR\V>³QDPH´@

^SHUVRQ GRJ WR\V QDPH`

> @

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V 5HPHPEHU WKDW P\ +70/ IRUP DFWLRQ JRHV VWUDLJKW WR WKH -63 LV WKHUH D ZD\ , FDQ XVH WKH UHTXHVW SDUDPHWHUV MXVW XVLQJ (/"

<PM -4 QUXTQKQ\ WJRMK\[ 2EMEMBER %, HAS SOME IMPLICIT OBJECTS "UT THESE ARE NOT THE SAME AS THE *30 IMPLICIT OBJECTS EXCEPT FOR ONE PAGE#ONTEXT (ERE S A QUICK LIST WE LL LOOK AT SOME OF THEM IN MORE DETAIL ON THE NEXT FEW PAGES 9OU LL NOTICE THAT ALL BUT ONE PAGE#ONTEXT AGAIN ARE SIMPLE -APSˆNAME VALUE PAIRS SDJH6FRSH

A Map of the scope attributes.

UHTXHVW6FRSH VHVVLRQ6FRSH DSSOLFDWLRQ6FRSH SDUDP SDUDP9DOXHV KHDGHU KHDGHU9DOXHV FRRNLH

Maps of the request parameters.

Maps of thade ers. request he

Ooohhhh... this is a tough one... could it be a Map of... cookies?

LQLW3DUDP

A Map of the context init parameters (NOT servlet init param eters!)

SDJH&RQWH[W

T a Map. This is The only thing that is NOreference to the ual act n lÑa dea l the rea you can think of as pageContext object, whiI chfor the PageContext AP the in k a bean. Loo getter methods. \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

SDUDP DQG SDUDP9DOXHV

:MY]M[\ XIZIUM\MZ[ QV -4 0IECE OF CAKE 4HE PARAM IMPLICIT OBJECT IS l NE WHEN YOU KNOW YOU HAVE ONLY ONE PARAMETER FOR THAT PARTICULAR PARAMETER NAME 5SE PARAM6ALUES WHEN YOU MIGHT HAVE MORE THAN ONE PARAMETER VALUE FOR A GIVEN PARAMETER NAME

,Q WKH +70/ IRUP IRUP DFWLRQ ´7HVW%HDQ MVS´! 1DPH LQSXW W\SH ´WH[W´ QDPH ´QDPH´! ,' LQSXW W\SH ´WH[W´ QDPH ´HPS,'´! )LUVW IRRG LQSXW W\SH ´WH[W´ QDPH ´IRRG´! 6HFRQG IRRG LQSXW W\SH ´WH[W´ QDPH ´IRRG´! LQSXW W\SH ´VXEPLW´! IRUP!

,Q WKH -63 5HTXHVW SDUDP QDPH LV ^SDUDP QDPH` EU!

pIDÓ will each have acould The ÒnameÓ and Òem ÒfoodÓ parameter single value. But thethe user Þlls in both if , ues val o tw have the submit button... Þelds before hitting is just a Map of ings to Remember, param and values. The th parameter names dot come from the names e th of ht rig e th Þelds of the form. speciÞed in the input

5HTXHVW SDUDP HPS,' LV ^SDUDP HPS,'` EU! 5HTXHVW SDUDP IRRG LV ^SDUDP IRRG` EU! )LUVW IRRG UHTXHVW SDUDP ^SDUDP9DOXHV IRRG> @` EU! 6HFRQG IRRG UHTXHVW SDUDP ^SDUDP9DOXHV IRRG> @` EU!

Even though there might be mult values for the ÒfoodÓ parameter, you caniplestill the single param implicit object, but use youÕll get only the Þrst value.

5HTXHVW SDUDP QDPH ^SDUDP9DOXHV QDPH> @`

,Q WKH FOLHQW¡V EURZVHU FOLHQW Ă€ OOV LQ WKH IRUP DQG KLWV WKH VXEPLW EXWWRQ

KWWS ORFDOKRVW WHVW-63 7HVW%HDQ MVS

1DPH )OXII\

,'

)LUVW IRRG 6XVKL 6HFRQG IRRG

0DFDURQL &KHHVH

7KH UHVSRQVH

KWWS ORFDOKRVW WHVW-63 7HVWHU GR

5HTXHVW SDUDP QDPH LV )OXII\ 5HTXHVW SDUDP HPS,' LV 5HTXHVW SDUDP IRRG LV 6XVKL )LUVW IRRG UHTXHVW SDUDP 6XVKL 6HFRQG IRRG UHTXHVW SDUDP 0DFDURQL &KHHVH 5HTXHVW SDUDP QDPH )OXII\

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

?PI\ QN aW] _IV\ UWZM QVNWZUI\QWV NZWU \PM ZMY]M[\' 7HAT IF YOU WANT SAY THE SERVER HOST INFORMATION THAT COMES WITH THE hHOSTv HEADER IN THE REQUEST )F YOU LOOK IN THE (TTP3ERVLET2EQUEST !0) YOU CAN SEE A GET(EADER 3TRING METHOD 7E KNOW THAT IF WE PASS hHOSTv TO THE GET(EADER METHOD WE LL GET BACK SOMETHING LIKE hLOCALHOST v BECAUSE THAT S WHERE THE WEB SERVER IS

*HWWLQJ WKH ´KRVW¾ KHDGHU :H NQRZ ZH FDQ GR LW ZLWK VFULSWLQJ +RVW LV UHTXHVW JHW+HDGHU ³KRVW´ !

%XW ZLWK (/ ZHœYH JRW WKH KHDGHU LPSOLFLW REMHFW +RVW LV ^KHDGHU>³KRVW´@` +RVW LV ^KHDGHU KRVW`

of all the headers. object keeps a Map The header implicitoperator to pass in the header nameoand Use either access header will print. (Note: thereÕs als the value of that licit object for headers with multiple a headerValues imp t like paramValues.) values. It works jus

*HWWLQJ WKH +773 UHTXHVW PHWKRG 5H OH 4HIS IS A LITTLE TRICKIER THERE S A METHOD IN THE (TTP3ERVLET2EQUEST !0) FOR GET-ETHOD THAT RETURNS '%4 0/34 ETC "UT HOW DO ) GET IT USING %, :H NQRZ ZH FDQ GR LW ZLWK VFULSWLQJ 0HWKRG LV UHTXHVW JHW0HWKRG ! %XW ZLWK (/ WKLV ZLOO 127 ZRUN 0HWKRG LV ^UHTXHVW PHWKRG`

IS no NO! NO! NO! There t! jec ob t ues req implicit

$QG WKLV ZLOO 127 ZRUN 0HWKRG LV ^UHTXHVW6FRSH PHWKRG`

IS an implicit NO! NO! NO! There s NOT the itÕ but e, cop tS ues req . request object itself

&DQ \RX ÀJXUH RXW KRZ WR GR LW" +LQW ORRN DW WKH RWKHU LPSOLFLW REMHFWV \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFRSH PDSV DUH 127 WKH UHDO REMHFW

<PM ZMY]M[\;KWXM Q[ 67< \PM ZMY]M[\ WJRMK\ 4HE IMPLICIT REQUEST3COPE IS JUST A -AP OF THE REQUEST SCOPE ATTRIBUTES NOT THE REQUEST OBJECT ITSELF 7HAT YOU WANT THE (440 METHOD IS A PROPERTY OF THE REQUEST OBJECT NOT AN ATTRIBUTE AT REQUEST SCOPE )N OTHER WORDS YOU WANT SOMETHING THAT COMES FROM CALLING A GETTER METHOD ON THE REQUEST OBJECT IF WE TREAT THE REQUEST OBJECT LIKE A BEAN "UT THERE IS NO REQUEST IMPLICIT OBJECT ONLY REQUEST3COPE 7HAT TO DO 9OU NEED SOMETHING ELSE

8VH UHTXHVW6FR SH WR JHW UHTXHV W $775,%87(6 Q RW UHTXHVW 352 3(57,(6 )RU UHTXHVW SUR SHUWLHV \RX QH HG WR JR WKURXJK SDJH&R QWH[W

8VH SDJH&RQWH[W WR JHW WR HYHU\WKLQJ HOVH 0HWKRG LV ^SDJH&RQWH[W UHTXHVW PHWKRG`

SDJH&RQWH[W KDV D UHTXHVW SURSHUW\ UHTXHVW KDV D PHWKRG SURSHUW\

VFRSH REMHFWV ZLWK WKH 'RQ·W FRQIXVH WKH 0DS DWWULEXWHV DUH ERXQG REMHFWV WR ZKLFK WKH

UHQFH WR 6HUY \ DSSOLFDWLRQ6FRSH LV D UHIH ,W¶V VR HDV\ WR WKLQN WKDW VD SHG DWWULEXWHV DUH ERXQG VFR DWLRQ SSOLF HUH D ¶V ZK OHW&RQWH[W VLQFH WKDW H VFRSH 0DS IRU SH DQG WKH UHTXHVW REMHFW WK DQG QRWK %XW MXVW DV ZLWK UHTXHVW6FR WKDW²D 0DS RI DWWULEXWHV MXVW HV LV ULEXW G DWW FRSH SHFW WR JHW DSSOLFDWLRQ V HUYOHW &RQWH[W VR GRQ¶W H[ H D 6 W LW OLN W WUHD FDQ¶ <RX LQJ PRUH FRSH LPSOLFLW REMHFW EDFN IURP WKH DSSOLFDWLRQ6 6HUYOHW&RQWH[W SURSHUWLHV

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

,I (/ ORRNV WKURXJK DOO WKH VFRSHV DQ\ZD\ ZK\ ZRXOG , HYHU XVH RQH RI WKH VFRSH LPSOLFLW REMHFWV" 7KH RQO\ WKLQJ , FDQ WKLQN RI LV D QDPLQJ FRQĂ LFW EXW , ZRQGHU LI WKHUH PLJKW EH DQRWKHU UHDVRQ

;KWXM QUXTQKQ\ WJRMK\[ KIV [I^M aW] )F ALL YOU NEED IS TO PRINT THE NAME OF A PERSON AND YOU REALLY DON T CARE WHAT SCOPE THE PERSON IS IN OR YOU DO CARE BUT YOU KNOW THERE S ONLY ONE PERSON OUT OF ALL FOUR SCOPES YOU JUST USE

^SHUVRQ QDPH` /R IF YOU RE WORRIED ABOUT A POTENTIAL NAMING CONmICT YOU CAN BE EXPLICIT ABOUT WHICH PERSON YOU WANT

^UHTXHVW6FRSH SHUVRQ QDPH` "UT IS THERE ANOTHER REASON YOU MIGHT HAVE TO PREFACE THE ATTRIBUTE WITH THE IMPLICIT SCOPE OBJECT /THER THAN TO CONTROL SCOPING 4HINK ABOUT THIS SCENARIO IF YOU HAVE A NAME THAT S NOT IN QUOTES IN BRACKETS ; = THAT MEANS IT -534 ADHERE TO *AVA NAMING RULES RIGHT (ERE WE RE /+ BECAUSE PERSON IS A PERFECTLY LEGAL *AVA VARIABLE NAME "UT THAT S BECAUSE SOMEWHERE SOMEONE SAID

UHTXHVW VHW$WWULEXWH ³SHUVRQ´ S

"UT AN ATTRIBUTE NAME IS A 3TRING 3TRINGS DON T FOLLOW *AVA VARIABLE NAME RULES

NO! This is certainly l, but the Container just thinks that ÒfooÓlega is an attribute somewhere, with a Òperso Container never Þnds a nÓ property. But the ÒfooÓ attribute.

Perfect! Using the requestScope object give us a way to put the s attribute name in quotes .

4HAT MEANS SOMEONE COULD SAY

UHTXHVW VHW$WWULEXWH ³IRR SHUVRQ´ S !ND THEN YOU D BE IN TROUBLE BECAUSE 4()3 WON T WORK

^IRR SHUVRQ QDPH` "UT YOU LL BE SO THANKFUL FOR SCOPE OBJECTS BECAUSE USING A SCOPE OBJECT LETS YOU SWITCH TO THE ; = OPERATOR THAT CAN TAKE 3TRING NAMES THAT DON T CONFORM TO *AVA NAMING RULES

^UHTXHVW6FRSH>³IRR SHUVRQ´@ QDPH`

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

WZR PRUH LPSOLFLW REMHFWV

/M\\QVO +WWSQM[ IVL QVQ\ XIZIU[ 7E VE LOOKED AT ALL THE IMPLICIT OBJECTS EXCEPT COOKIES AND INIT PARAMS SO HERE WE ARE !ND YES ANY OF THE IMPLICIT OBJECTS CAN SHOW UP ON THE EXAM

3ULQWLQJ WKH YDOXH RI WKH ´XVHU1DPH¾ &RRNLH :H NQRZ ZH FDQ GR LW ZLWK VFULSWLQJ &RRNLH>@ FRRNLHV UHTXHVW JHW&RRNLHV

This is kind of a pain, because the request object does NOT have a getCookie(cookieName) method! We have to get the whole Cookie array and iterate through it ourselves.

IRU LQW L L FRRNLHV OHQJWK L ^ LI FRRNLHV>L@ JHW1DPH

HTXDOV ³XVHU1DPH´

^ RXW SULQWOQ FRRNLHV>L@ JHW9DOXH

` ` !

%XW ZLWK (/ ZHÂśYH JRW WKH &RRNLH LPSOLFLW REMHFW FRRNLH XVHU1DPH YDOX` ^

the value WAY easier. Just give it the name, andnames /values. comes back from the Map of Cookie

3ULQWLQJ WKH YDOXH RI D FRQWH[W LQLW SDUDPHWHU

Remember. this is how you conĂžgure context (app-wide) parameters. These s. are NOT the same as servlet init param

:H KDYH WR FRQÂż JXUH WKH SDUDPHWHU LQ WKH ''

FRQWH[W SDUDP! SDUDP QDPH!PDLQ(PDLO SDUDP QDPH! SDUDP YDOXH!OLNHZHFDUH#ZLFNHGO\VPDUW FRP SDUDP YDOXH! FRQWH[W SDUDP!

:H NQRZ ZH FDQ GR LW ZLWK VFULSWLQJ HPDLO LV DSSOLFDWLRQ JHW,QLW3DUDPHWHU ³PDLQ(PDLO´ !

$QG ZLWK (/ LWÂśV HYHQ HDVLHU HPDLO LV ^ LQLW3DUDP PDLQ(PDLO`

27 7KH (/ LQLW3DUDP LV 1 IRU SDUDPV FRQĂ€ JXUHG XVLQJ LQLW SDUDP! UYOHW LQLW SDUDPV DUH +HUHÂśV ZKDWÂśV FRQIXVLQJ VH ! ZKLOH FRQWH[W SDUDPV FRQÂż JXUHG XVLQJ LQLW SDUDP H (/ LPSOLFLW ÂłLQLW3DUDP´ XVH FRQWH[W SDUDP! EXW WK G WKH\ FRQVXOWHG XV ZH LV IRU FRQWH[W SDUDPV +D WKH VSHF GHVLJQHUV PLJKW WKDW VWHG XJJH ZRXOG KDYH V OH RK ÂłFRQWH[W3DUDP´ DULDE KLV Y LQJ W QDP FRQVLGHU WR DVN XV UJRW H\ IR Q WK DJDL QFH EXW R

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

(/ LV ZRQGHUIXO EXW VRPHWLPHV , QHHG IXQFWLRQDOLW\ QRW MXVW DWWULEXWH RU SURSHUW\ YDOXHV ,I RQO\ WKHUH ZHUH D ZD\ WR KDYH DQ (/ H[SUHVVLRQ FDOO D -DYD PHWKRG WKDW UHWXUQV D YDOXH WKHQ , ZRXOG EH KDSS\

;PM LWM[V¼\ SVW_ IJW]\ -4 N]VK\QWV[ 7HEN YOU NEED A LITTLE EXTRA HELP FROM SAY A *AVA METHOD BUT YOU DON T WANT SCRIPTING YOU CAN USE AN %, FUNCTION )T S AN EASY WAY TO WRITE A SIMPLE %, EXPRESSION THAT CALLS A STATIC METHOD IN A PLAIN OLD *AVA CLASS THAT YOU WRITE 7HATEVER THE METHOD RETURNS IS USED IN THE EXPRESSION )T DOES TAKE A TINY BIT MORE WORK TO CONl GURE THINGS BUT FUNCTIONS GIVE YOU A LOT MORE FUNCTIONALITY

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

IXQFWLRQV LQ (/

1UIOQVM aW] _IV\ aW]Z 2;8 \W ZWTT LQKM 9OU VE DECIDED IT WOULD BE AWESOME TO HAVE A WEB BASED DICE ROLLING SERVICE 4HAT WAY INSTEAD OF HUNTING AROUND BEHIND DESKS AND IN THE SOFA CUSHIONS FOR REAL DICE A USER COULD JUST GO TO YOUR WEB PAGE CLICK ON THE VIRTUAL DICE AND VOILA 4HEY ROLL /F COURSE YOU HAVE NO IDEA THAT A 'OOGLE SEARCH WILL PROBABLY BRING UP OH ABOUT SITES THAT DO THIS

:ULWH D -DYD FODVV ZLWK D SXEOLF VWDWLF PHWKRG 7KLV LV MXVW D SODLQ ROG -DYD FODVV 7KH PHWKRG 0867 EH SXEOLF DQG VWDWLF DQG LW FDQ KDYH DUJXPHQWV ,W VKRXOG EXW LVQ·W UHTXLUHG WR KDYH D QRQ YRLG UHWXUQ W\SH $IWHU DOO WKH ZKROH SRLQW LV WR FDOO WKLV IURP D -63 DQG JHW VRPHWKLQJ EDFN WKDW \RX FDQ XVH DV SDUW RI WKH H[SUHVVLRQ RU WR SULQW RXW 3XW WKH FODVV ILOH LQ WKH :(% ,1) FODVVHV GLUHFWRU\ VWUXFWXUH PDWFKLQJ WKH DSSURSULDWH SDFNDJH GLUHFWRU\ VWUXFWXUH MXVW OLNH \RX ZRXOG ZLWK DQ\ RWKHU FODVV

:ULWH D 7DJ /LEUDU\ 'HVFULSWRU 7/' ÀOH )RU DQ (/ IXQFWLRQ WKH 7/' SURYLGHV D PDSSLQJ EHWZHHQ WKH -DYD FODVV WKDW GHILQHV WKH IXQFWLRQ DQG WKH -63 WKDW FDOOV WKH IXQFWLRQ 7KDW ZD\ WKH IXQFWLRQ QDPH DQG WKH DFWXDO PHWKRG QDPH FDQ EH GLIIHUHQW <RX PLJKW EH VWXFN ZLWK D FODVV ZLWK D UHDOO\ VWXSLG PHWKRG QDPH IRU H[DPSOH DQG PD\EH \RX ZDQW WR SURYLGH D PRUH REYLRXV RU LQWXLWLYH QDPH WR SDJH GHVLJQHUV XVLQJ (/ 1R SUREOHP³WKH 7/' VD\V ´7KLV LV WKH -DYD FODVV WKLV LV WKH PHWKRG VLJQDWXUH IRU WKH IXQFWLRQ LQFOXGLQJ UHWXUQ W\SH DQG WKLV LV WKH QDPH ZH·OO XVH LQ (/ H[SUHVVLRQVµ ,Q RWKHU ZRUGV WKH QDPH XVHG LQ (/ GRHVQ·W KDYH WR EH WKH VDPH DV WKH DFWXDO PHWKRG QDPH DQG WKH 7/' LV ZKHUH \RX PDS WKDW 3XW WKH 7/' ILOH LQVLGH WKH :(% ,1) GLUHFWRU\ 1DPH LW ZLWK D WOG H[WHQVLRQ 7KHUH DUH RWKHU SODFHV WKH 7/' FDQ JR ZH·OO WDON DERXW WKDW LQ WKH QH[W WZR FKDSWHUV

3XW D WDJOLE GLUHFWLYH LQ \RXU -63 7KH WDJOLE GLUHFWLYH WHOOV WKH &RQWDLQHU ´,·P JRLQJ WR XVH WKLV 7/' DQG LQ WKH -63 ZKHQ , ZDQW WR XVH D IXQFWLRQ IURP WKLV 7/' ,·P JRLQJ WR SUHIL[ LW ZLWK WKLV QDPH µ ,Q RWKHU ZRUGV LW OHWV \RX GHILQH WKH QDPHVSDFH <RX FDQ XVH IXQFWLRQV IURP PRUH WKDQ RQH 7/' DQG HYHQ LI WKH IXQFWLRQV KDYH WKH VDPH QDPH WKDW·V 2. 7KH WDJOLE GLUHFWLYH LV NLQG RI OLNH JLYLQJ DOO \RXU IXQFWLRQV IXOO\ TXDOLILHG QDPHV <RX LQYRNH WKH IXQFWLRQ E\ JLYLQJ ERWK WKH IXQFWLRQ QDPH $1' WKH 7/' SUHIL[ 7KH SUHIL[ FDQ EH DQ\WKLQJ \RX OLNH

8VH (/ WR LQYRNH WKH IXQFWLRQ 7KLV LV WKH HDV\ SDUW <RX MXVW FDOO WKH IXQFWLRQ IURP DQ H[SUHVVLRQ XVLQJ ^SUHIL[ QDPH `

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

7KH IXQFWLRQ FODVV WKH 7/' DQG WKH -63 7KH FODVV ZLWK WKH IXQFWLRQ

The function method MUST be public AND static.

SDFNDJH IRR SXEOLF FODVV ' LFH5ROOHU ^ SXEOLF VWD WLF LQW UROO'L FH ^ UHWXUQ LQW 0DWK UD ` QGRP `

7KH 7DJ /LEUDU\ 'HVFULSWRU 7/' Ă€ OH "[PO YHUVLRQ ´ ´ HQFRGLQJ ´,62 ´ "! WDJOLE [POQV ´KWWS MDYD VXQ FRP [PO QV M HH´ [POQV [VL ´KWWS ZZZ Z RUJ ;0/6FKHPD LQVWDQFH´ [VL VFKHPD/RFDWLRQ ´KWWS MDYD VXQ FRP [PO QV M HH ZHE MVSWDJOLEUDU\B B [VG´ YHUVLRQ ´ ´! WOLE YHUVLRQ! WOLE YHUVLRQ! XUL!'LFH)XQFWLRQV XUL! IXQFWLRQ! QDPH!UROO,W QDPH! OOHU IXQ IXQFWLRQ FODVV!IRR 'LFH5ROOHU IXQFWLRQ FODVV! IXQFWLRQ VLJQDWXUH! LQW UROO'LFH

IXQFWLRQ VLJQDWXUH! IXQFWLRQ!

all the Do NOT worry about ...> tag. ib agl <t e th ide stuff ins TLDs in WeÕll talk more about . ers pt cha o tw t nex e th directive The uri in the taglibthe name of tells the Container es NOT have the TLD (which dothe FILE!), to be the name ofer needs so which the Contain od to call it knows which meth s the EL when the JSP invoke function.

WDJOLE!

7KH -63

The preÞx the nicknameÒmineÓ is just inside THIS weÕll use we can tell onpage, so that from another e TLD DO have mor (in case you e than one).

XQFWLRQV´ ! P QH´ XUL ´'LFH) SUH¿ [ ´PL # WDJOLE KWPO! ERG\!

` ^PLQH UROO,W

mes from e rollIt() cot from m na n io ct The fun e> in the TLD, no s. the <nam the actual Java clas anything in

ERG\! KWPO!

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

GHSOR\LQJ ZLWK D IXQFWLRQ

,MXTWaQVO IV IXX _Q\P [\I\QK N]VK\QWV[ 4HE ONLY THING THAT S NEW HERE IS THE hMY&UNCTIONS TLDv l LE )T HAS TO BE SOMEWHERE WITHIN 7%" ).& OR ONE OF ITS SUBDIRECTORIES UNLESS IT S DEPLOYED IN A *!2 l LE BUT WE LL TALK ABOUT THAT LATER IN THE BOOK (ERE BECAUSE THIS APP IS SO SIMPLE WE HAVE BOTH THE $$ WEB XML AND THE 4,$ MY&UNCTIONS TLD AT THE TOP LEVEL OF 7%" ).& BUT YOU COULD ORGANIZE THEM INTO SUBDIRECTORIES 4HE KEY POINT IS THAT THE CLASS WITH THE STATIC FUNCTION -534 BE AVAILABLE TO THE APP SO FOR NOW YOU KNOW THAT PUTTING IT INSIDE 7%" ).& CLASSES WILL WORK !ND REMEMBER THAT IN THE TAGLIB DIRECTIVE IN THE *30 WE SPECIl ED A 52) THAT MATCHES THE 52) DECLARED IN THE 4,$ &OR NOW THINK OF THE 52) AS SIMPLY WHATEVER YOU DECIDED TO NAME THE 4,$ )T S JUST A NAME )N THE NEXT CHAPTER ON USING CUSTOM TAGS WE LL GO INTO ALL THE DETAILS ABOUT 4,$S AND 52)S # WDJOLE SUH¿ [ ´PLQH´ XUL ´'LFH)XQFWLRQV´ !

This is an identiĂžer that must match the <uri> inside the TLD. ZHEDSSV

6DPSOH$SS

The JSP th okes the EL funcattioinv n

# WDJOLE SUH¿ [ ´PLQH´ XUL ´ :(% ,1) P\)XQFWLRQV WOG´ !

KWPO! ERG\! ^PLQH UROO,W `

ERG\! KWPO!

:(% ,1)

7HVW%HDQ MVS IXQFWLRQ! QDPH! UROO,W QDPH! IXQFWLRQ FODVV!

"[PO YHU VLRQ ´ ´ HQFRGLQJ ´87) ´"!

FODVVHV

'2&7<3(

IRR 'LFH5ROOHU

IXQFWLRQ

P\)XQFWLRQV WOG ZHE [PO

The TLD that nction declares the fusignature, class, method e. and function nam

3XW WKH 7/' ILOH VRPHZKHUH XQGHU :(% ,1) DQG PDNH VXUH WKH WDJOLE GLUHFWLYH LQ WKH -63 LQFOXGHV D XUL DWWULEXWH WKDW PDWFKHV WKH XUL! HOHPHQW LQ WKH 7/'

IRR

h The Java class wit blic pu (a n tio nc fu e th ). od th me tic sta

7KH FODVV ZLWK WKH IXQFWLRQ WKH SXEOLF VWDWLF PHWKRG PXVW EH DYDLODEOH WR WKH ZHE DSS MXVW OLNH VHUYOHW EHDQ DQG OLVWHQHU FODVVHV 7KDW PHDQV VRPHZKHUH LQ :(% ,1) FODVVHV

'LFH5ROOHU FODVV

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

WKHUH DUH QR

'XPE 4XHVWLRQV

4

! REGULAR SCRIPTLET EXPRESSION -534 RETURN SOMETHING )F YOU SAY FOO GET&OO GET&OO MUST ./4 HAVE A VOID RETURN TYPE !T LEAST THAT S WHAT YOU SAID EARLIER 3O ) M THINKING IT S THE SAME WITH %, FUNCTIONS

$

.O )T S ./4 THE SAME WITH %, FUNCTIONS ALTHOUGH JUST ABOUT EVERYBODY FINDS THAT SURPRISING 4HINK ABOUT THISˆIF YOU RE CALLING AN %, FUNCTION THAT DOESN T RETURN ANYTHING THEN YOU RE CALLING IT JUST FOR ITS SIDE EFFECTS 'IVEN THAT PART OF THE GOAL FOR %, IS TO REDUCE THE AMOUNT OF LOGIC IN A *30 A *30 SUPPOSED TO BE THE 6)%7 INVOKING AN %, FUNCTION JUST FOR ITS SIDE EFFECTS DOESN T SOUND LIKE A GOOD IDEA

4

(OW DID THE #ONTAINER l ND THE 4,$ 4HE 52) DOESN T MATCH THE PATH OR l LE NAME OF THE 4,$ 7AS THIS A MIRACLE

$

*UST THE QUESTION WE WERE HOPING SOMEONE WOULD ASK 9ES YOU RE RIGHTˆWE NEVER DID TELL THE #ONTAINER EXACTLY WHERE TO FIND THE REAL 4,$ FILE 7HEN THE APP IS DEPLOYED THE #ONTAINER SEARCHES THROUGH 7%" ).& AND ITS SUBDIRECTORIES OR IN *!2 FILES WITHIN 7%" ).& LIB LOOKING FOR TLD FILES 7HEN IT FINDS ONE IT READS THE 52) AND CREATES A MAP THAT SAYS h4HE 4,$ WITH THIS 52) IS ACTUALLY THIS FILE AT THIS LOCATION v 4HERE S A LITTLE MORE TO THE STORY THAT WE LL COVER IN THE NEXT CHAPTER

4 $

#AN AN %, FUNCTION HAVE ARGUMENTS

$EFINITELY *UST REMEMBER IN THE 4,$ TO SPECIFY THE FULLY QUALIFIED CLASS NAME UNLESS IT S A PRIMITIVE FOR EACH ARGUMENT ! FUNCTION THAT TAKES A -AP WOULD BE

:DWFK LW

RW WKH 7KH 0(7+2' QDPH LV Q QDPH VDPH DV WKH )81&7,21

EHWZHHQ WKH FODVV WKH 0HPRUL]H WKH UHODWLRQVKLSV RUWDQWO\ UHPHPEHU WKDW WKH 7/' DQG WKH -63 0RVW LPS DWFK WKH )81&7,21 WR P DYH 27 K HV 1 H GR 0(7+2' QDP W WR LQYRNH WKH IXQFWLRQ PXV QDPH :KDW \RX XVH LQ (/ Q WKH IXQFWLRQ! GHFODUDWLRQ HQW L HOHP PH! QD K WKH PDWF UH U IXQFWLRQ VLJQDWXUH! LV WKH LQ WKH 7/' 7KH HOHPHQW IR HWKRG WR FDOO ZKHQ WKH -63 WR WHOO WKH &RQWDLQHU ZKLFK P XVHV WKH QDPH! H V QDPH DSSHDUV EHVLGHV WK $QG WKH RQO\ SODFH WKH FODV QFWLRQ FODVV! HOHPHQW H IX LQ WK I LV LWVHO WLRQ FODVV GHFODUD QJ GLG \RX QRWLFH WKDW HYHU\WKL 2K DQG ZKLOH ZH¶UH KHUH ZRUG IXQFWLRQ! LQ LW LQ WKH IXQFWLRQ! WDJ KDV WKH V J" 6R GRQ¶W EH IRROHG E\ WKL (;&(37 IRU WKH QDPH! WD

NO !! IXQFWLRQ! ROO,W IXQFWLRQ QDPH! IXQFWLRQ QDPH!U VV! FOD RQ FWL IXQ HU IXQFWLRQ FODVV! IRR 'LFH5ROO XUH! QDW VLJ RQ FWL IXQ H

LQW UROO'LF WXUH! IXQFWLRQ VLJQD IXQFWLRQ! WLRQ QDPH LV QDPH! 7KH FRUUHFW WDJ IRU WKH IXQF

!

Good IXQFWLRQ! ,W QDPH! ROO H!U QDP IXQFWLRQ FODVV! HU IXQFWLRQ FODVV! IRR 'LFH5ROO XUH! IXQFWLRQ VLJQDW H

LQW UROO'LF WXUH! IXQFWLRQ VLJQD IXQFWLRQ!

IXQFWLRQ VLJQDWXUH! LQW UROO'LFH MDYD XWLO 0DS

IXQFWLRQ VLJQDWXUH! \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

(/ RSHUDWRUV

)VL I NM_ W\PMZ -4 W XMZI\WZ[ 9OU PROBABLY WON T AND SHOULDN T DO CALCULATIONS AND LOGIC FROM %, 2EMEMBER A *30 IS THE 6IEW AND THE 6IEW S JOB IS TO RENDER THE RESPONSE NOT TO MAKE "IG )MPORTANT $ECISIONS OR DO "IG 0ROCESSING )F YOU NEED REAL FUNCTIONALITY THAT S NORMALLY THE JOB OF THE #ONTROLLER AND -ODEL &OR LESSER FUNCTIONALITY YOU VE GOT CUSTOM TAGS INCLUDING THE *34, TAGS AND %, FUNCTIONS "UT FOR LITTLE THINGS SOMETIMES A LITTLE ARITHMETIC OR A SIMPLE BOOLEAN TEST MIGHT COME IN HANDY 3O WITH THAT PERSPECTIVE HERE S A LOOK AT THE MOST USEFUL %, ARTITHMETIC RELATIONAL AND LOGICAL OPERATORS

$ULWKPHWLF

$GGLWLRQ

6XEWUDFWLRQ

0XOWLSOLFDWLRQ

'LYLVLRQ

DQG GLY

5HPDLQGHU

DQG PRG

$1'

DQG DQG

25

__ DQG RU

127

DQG QRW

ide by zero in By the way... you CAN div an error. not , TY INI INF ELÑyou get Remainder operator But you CANNOT use the an exception. get Õll against a zero--you

/RJLFDO

:DWFK LW

RUGV 'RQ·W XVH (/ UHVHUYHG Z DV LGHQWLÀ HUV

WKHP RQ WKLV SDJH²WKH <RX FDQ DOUHDG\ VHH RI DWLRQDO ORJLFDO DQG VRPH DOWHUQDWH ³ZRUGV´ IRU WKH UHO HUH DUH D IHZ PRUH XW WK UV % HUDWR F RS DULWKPHWL

5HODWLRQDO

DQG HT

(TXDOV

1RW HTXDOV

DQG QH

/HVV WKDQ

DQG OW

*UHDWHU WKDQ

/HVV WKDQ RU HTXDO WR

DQG OH

DQG JH

D ERROHDQ OLWHUDO

IDOVH

WKH 27+(5 ERROHDQ OLWHUDO

QXOO

,W PHDQV QXOO XUH´

HG IRU ³WKH IXW LQVWDQFHRI WKLV LV UHVHUY

! DQG JW

*UHDWHU WKDQ RU HTXDO WR !

WUXH

LI VRPHWKLQJ LV QXOO RU HPSW\ DQ RSHUDWRU WR VHH W\ $` UHWXUQV WUXH LI $ LV HPS J ^ W\ H HPS V LQ DFWLRQ D OLWWOH QXOO RU HPSW\ \RX¶OO VHH WKL ODWHU LQ WKH FKDSWHU

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

6KDUSHQ \RXU SHQFLO /RRN DW WKH VHUYOHW FRGH WKHQ ¿JXUH RXW ZKDW SULQWV QH[W WR HDFK (/ H[SUHVVLRQ <RX¶OO KDYH WR JXHVV LQ D IHZ SODFHV VLQFH ZH KDYHQ¶W FRYHUHG HYHU\ SRVVLEOH UXOH 7KLV H[HUFLVH ZLOO KHOS \RX ¿JXUH RXW KRZ (/ EHKDYHV +LQW (/ LV ÀH[LEOH DQG IRUJLY LQJ $QRWKHU KLQW WKH DFWXDO QLQH DQVZHUV DUH SULQWHG DW WKH ERWWRP RI WKLV SDJH XS VLGH GRZQ EXW WKH\ DUH 127 LQ DQ\ RUGHU %XW LI \RX UHDOO\ QHHG KHOS DW OHDVW \RX¶OO KDYH WKH QLQH DQVZHUV DQG \RX FDQ XVH HOLPLQDWLRQ WR ¿JXUH RXW ZKHUH WKH\ DOO JR

*LYHQ WKLV VHUYOHW FRGH 6WULQJ QXP ³ ´ UHTXHVW VHW$WWULEXWH ³QXP´ QXP ,QWHJHU L QHZ ,QWHJHU UHTXHVW VHW$WWULEXWH ³LQWHJHU´ L MDYD XWLO $UUD\/LVW OLVW QHZ MDYD XWLO $UUD\/LVW OLVW DGG ³WUXH´ OLVW DGG ³IDOVH´ OLVW DGG ³ ´ OLVW DGG ³ ´ UHTXHVW VHW$WWULEXWH ³OLVW´ OLVW

:KDW SULQWV IRU HDFK RI WKHVH"

Assume that the Dog bean class and rollIt() function are both available.

^QXP ! ` ^LQWHJHU OH ` ^UHTXHVW6FRSH>LQWHJHU@ QH DQG OH QXP __ IDOVH` ^OLVW> @ __ OLVW>³ ´@ DQG WUXH` ^QXP ! LQWHJHU` ^QXP

LQWHJHU `

MVS XVH%HDQ FODVV ´IRR 'RJ´ LG ´P\'RJ´ ! MVS VHW3URSHUW\ QDPH ´P\'RJ´ SURSHUW\ ´QDPH´ YDOXH ´ ^OLVW> @`´ ! MVS XVH%HDQ! ^P\'RJ QDPH DQG WUXH` ^ GLY ` ^PLQH UROO,W OH `

IDOVH IDOVH IDOVH IDOVH IDOVH WUXH WUXH WUXH ,QILQLW\ \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

(/ RSHUDWRU DQVZHUV

6KDUSHQ \RXU SHQFLO *LYHQ WKLV VHUYOHW FRGH 6WULQJ QXP ³ ´ UHTXHVW VHW$WWULEXWH ³QXP´ QXP ,QWHJHU L QHZ ,QWHJHU UHTXHVW VHW$WWULEXWH ³LQWHJHU´ L MDYD XWLO $UUD\/LVW OLVW QHZ MDYD XWLO $UUD\/LVW OLVW DGG ³WUXH´ OLVW DGG ³IDOVH´ OLVW DGG ³ ´ OLVW DGG ³ ´ UHTXHVW VHW$WWULEXWH ³OLVW´ OLVW

:KDW SULQWV IRU HDFK RI WKHVH" false true false true false true false InĂžnity false

and ibute was founind, t. The Ă’numĂ“ attr an to d lue Ă’2Ă“ coerce was converted ^QXP ! ` its va Integer value co he T ! er ared. tt be Even , and then mp ^LQWHJHU OH ` to its primitive value ^UHTXHVW6FRSH>LQWHJHU@ QH DQG OH QXP __ IDOVH` ^OLVW> @ __ OLVW>Âł ´@ DQG WUXH` ^QXP ! LQWHJHU` ^QXP

instead of out for using in= EL. = ==. There is NO

LQWHJHU ` Watch

n Þgure See if you caed ence ec pr out the you rules for when ns. ItÕs donÕt use pare (left to very intuitive u should right), and yolems with have NO prob the exam. precedence on

MVS XVH%HDQ FODVV ´IRR 'RJ´ LG ´P\'RJ´ ! MVS VHW3URSHUW\ QDPH ´P\'RJ´ SURSHUW\ ´QDPH´ YDOXH ´ ^OLVW> @`´ ! MVS XVH%HDQ! Yes, you ^P\'RJ QDPH DQG WUXH` inside a tcan use EL ag! ^ GLY ` ^PLQH UROO,W OH `

If you re code, it amember the rollIt number grelways returns a () ater than zero.

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

-4 PIVLTM[ V]TT ^IT]M[ OZIKMN]TTa ! KEY DESIGN DECISION THE DEVELOPERS OF %, CAME UP WITH IS TO HANDLE NULL VALUES WITHOUT THROWING EXCEPTIONS 7HY "ECAUSE THEY lGURED hIT S BETTER TO SHOW A PARTIAL INCOMPLETE PAGE THAN TO SHOW THE USER AN ERROR PAGE v !SSUME THAT THERE IS NOT AN ATTRIBUTE NAMED hFOOv BUT THERE )3 AN ATTRIBUTE NAMED hBARv BUT THAT hBARv DOES NOT HAVE A PROPERTY OR KEY NAMED hFOOv

(/

:KDW SULQWV

^IRR`

Nothing prints out for these expressions. If you say ÒThe value is: ${foo}.Ó YouÕll just see ÒThe value is.Ó

^IRR>EDU@` ^EDU>IRR@` ^IRR EDU`

In arithmeticEL expressions, known treats the un eroÓ. variable as Òz

^ IRR`

^ IRR`

,QÂżQLW\

^ IRR`

^ IRR`

([FHSWLRQ LV WKURZQ

^ IRR`

IDOVH

^

IDOVH

IRR`

^IRR

IRR`

WUXH

^ IRR`

WUXH

^WUXH DQG IRR`

IDOVH

^WUXH RU IRR`

WUXH

^QRW IRR`

WUXH

(/ LV QXOO IULHQGO\ ,W KDQGOHV XQNQRZQ RU QXOO YDOXHV VR WKDW WKH SDJH VWLOO GLVSOD\V HYHQ LI LW FDQ¡W ILQG DQ DWWULEXWH SURSHUW\ NH\ ZLWK WKH QDPH LQ WKH H[SUHVVLRQ ,Q DULWKPHWLF (/ WUHDWV WKH QXOO YDOXH DV ´]HUR¾ ,Q ORJLFDO H[SUHVVLRQV (/ WUHDWV WKH QXOO YDOXH DV ´IDOVH¾

In logical expressio ns, EL treats the un variable as ÒfalseÓ known .

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

(/ UHYLHZ

2;8 -`XZM[[QWV 4IVO]IOM -4 ZM^QM_ :MDD=L HGAFLK

(/ H[SUHVVLRQV DUH DOZD\V ZLWKLQ FXUO\ EUDFHV DQG SUHIL[HG ZLWK D GROODU VLJQ ^H[SUHVVLRQ`

7KH ILUVW QDPHG YDULDEOH LQ WKH H[SUHVVLRQ LV HLWKHU DQ LPSOLFLW REMHFW RU DQ DWWULEXWH LQ RQH RI WKH IRXU VFRSHV SDJH UHTXHVW VHVVLRQ RU DS SOLFDWLRQ

7KH GRW RSHUDWRU OHWV \RX DFFHVV YDOXHV E\ XVLQJ D 0DS NH\ RU D EHDQ SURSHUW\ QDPH IRU H[DPSOH ^IRR EDU` JLYHV \RX WKH YDOXH RI EDU ZKHUH EDU LV WKH QDPH RI 0DS NH\ LQWR WKH 0DS IRR RU EDU LV WKH SURSHUW\ RI EHDQ IRR :KDWHYHU FRPHV WR WKH ULJKW RI WKH GRW RSHUDWRU PXVW IROORZ QRUPDO -DYD QDPLQJ UXOHV IRU LGHQWLILHUV ,Q RWKHU ZRUGV PXVW VWDUW ZLWK D OHWWHU XQGHUVFRUH RU GROODU VLJQ FDQ LQFOXGH QXPEHUV DIWHU WKH ILUVW FKDUDF WHU EXW QRWKLQJ HOVH HWF

<RX FDQ 1(9(5 SXW DQ\WKLQJ WR WKH ULJKW RI WKH GRW WKDW ZRXOGQ·W EH OHJDO DV D -DYD LGHQWLILHU )RU H[DPSOH \RX FDQ·W VD\ ^IRR `

7KH > @ RSHUDWRU LV PRUH SRZHUIXO WKDQ WKH GRW EHFDXVH LW OHWV \RX DFFHVV DUUD\V DQG /LVWV DQG \RX FDQ SXW RWKHU H[SUHVVLRQV LQFOXGLQJ QDPHG YDULDEOHV ZLWKLQ WKH EUDFNHWV DQG \RX FDQ QHVW WKHP WR DQ\ OHYHO \RX FDQ VWDQG

'RQ·W FRQIXVH WKH LPSOLFLW (/ VFRSH REMHFWV 0DSV RI WKH DWWULEXWHV ZLWK WKH REMHFWV WR ZKLFK WKH DWWULEXWHV DUH ERXQG ,Q RWKHU ZRUGV GRQ·W FRQIXVH WKH UHTXHVW6FRSH LPSOLFLW REMHFW ZLWK WKH DFWXDO -63 LPSOLFLW UHTXHVW REMHFW 7KH RQO\ ZD\ WR DFFHVV WKH UHTXHVW REMHFW LV E\ JRLQJ WKURXJK WKH SDJH&RQWH[W LPSOLFLW REMHFW $OWKRXJK VRPH RI ZKDW \RX PLJKW ZDQW IURP WKH UHTXHVW LV DOUHDG\ DYDLODEOH WKURXJK RWKHU (/ LPSOLFLW REMHFWV LQFOXGLQJ SDUDP SDUDP9DOXHV KHDGHU KHDGHU9DOXHV DQG FRRNLH

(/ IXQFWLRQV DOORZ \RX WR FDOO D SXEOLF VWDWLF PHWKRG LQ D SODLQ ROG -DYD FODVV 7KH IXQFWLRQ QDPH GRHV QRW KDYH WR PDWFK WKH DFWXDO PHWKRG QDPH )RU H[DPSOH ^IRR UROO,W ` GRHV QRW PHDQ WKDW WKHUH PXVW EH D PHWKRG QDPHG UROO,W LQ D FODVV WKDW KDV D IXQFWLRQ

7KH IXQFWLRQ QDPH H J UROO,W

LV PDSSHG WR

FDQ DFFHVV WKH ILUVW YDOXH LQ WKH OLVW E\ VD\LQJ ^PXVLF/LVW> @` 25 ^PXVLF/LVW>´ µ@` (/ GRHVQ·W FDUH LI \RX SXW TXRWHV DURXQG WKH OLVW LQGH[

D UHDO VWDWLF PHWKRG XVLQJ D 7/' 7DJ /LEUDU\ 'HVFULSWRU ILOH 'HFODUH D IXQFWLRQ XVLQJ WKH IXQFWLRQ! HOHPHQW LQFOXGLQJ WKH QDPH! RI WKH IXQFWLRQ UROO,W

WKH IXOO\ TXDOLILHG IXQF WLRQ FODVV! DQG WKH IXQFWLRQ VLJQDWXUH! ZKLFK LQFOXGHV WKH UHWXUQ W\SH DV ZHOO DV WKH PHWKRG QDPH DQG DUJXPHQW OLVW

,I ZKDW·V LQVLGH WKH EUDFNHWV LV QRW LQ TXRWHV WKH

7R XVH D IXQFWLRQ LQ D -63 \RX PXVW GHFODUH WKH

)RU H[DPSOH LI PXVLF/LVW LV DQ $UUD\/LVW \RX

&RQWDLQHU HYDOXDWHV LW ,I LW LV LQ TXRWHV DQG LW·V QRW DQ LQGH[ LQWR DQ DUUD\ RU /LVW WKH &RQWDLQHU VHHV LW DV WKH OLWHUDO QDPH RI D SURSHUW\ RU NH\

$OO EXW RQH RI WKH (/ LPSOLFLW REMHFWV DUH 0DSV )URP WKH 0DS LPSOLFLW REMHFWV \RX FDQ JHW DWWULEXWHV IURP DQ\ RI WKH IRXU VFRSHV UHTXHVW

SDUDPHWHU YDOXHV KHDGHU YDOXHV FRRNLH YDOXHV DQG FRQWH[W LQLW SDUDPHWHUV 7KH QRQ PDS LP SOLFLW REMHFW LV SDJH&RQWH[W ZKLFK LV D UHIHUHQFH WR WKH 3DJH&RQWH[W REMHFW

QDPHVSDFH XVLQJ D WDJOLE GLUHFWLYH 3XW D SUHIL[ DWWULEXWH LQ WKH WDJOLE GLUHFWLYH WR WHOO WKH &RQ WDLQHU WKH 7/' LQ ZKLFK WKH IXQFWLRQ \RX·UH FDOOLQJ FDQ EH IRXQG ([DPSOH # WDJOLE SUH¿[ ´PLQH´ XUL ´ :(% ,1) IRR WOG´ !

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

+H\ KDYH \RX JX\V QRWLFHG WKDW WKH\ KDYHQ¡W HYHQ PHQWLRQHG OLNH WKH 21( WKLQJ WKDW¡V PRVW LPSRUWDQW WR D ZHE VLWH GHVLJQHU"

<HDK , KDYHQ¡W KHDUG 21( WKLQJ DERXW XVLQJ OD\RXW WHPSODWHV 7KH ODVW WKLQJ , ZDQW WR GR LV SXW WKH VDPH QDYLJDWLRQ EDU FRGH LQWR DOO RI P\ -63V ZKDW LI LW FKDQJHV "

+PPPP , DOZD\V WKRXJKW WKH LPSRUWDQW WKLQJ ZDV WR PDNH VXUH \RX GRQ¡W HQG XS RQ ZHESDJHVWKDWVXFN FRP

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

OD\RXW WHPSODWHV

2I &2856( ZH·OO WDON DERXW OD\RXW WHPSODWHV ,I $1<21( NQRZV DERXW UHXVDEOH FRPSRQHQWV LW·V D -DYD SURJUDPPHU

:M][IJTM \MUXTI\M XQMKM[ 9OU HAVE HEADERS ON EVERY PAGE ON YOUR WEB SITE 4HEY RE ALWAYS THE SAME 9OU HAVE THE SAME FOOTER ON EVERY PAGE AS WELL (OW STUPID WOULD IT BE TO CODE IN THE SAME HEADER AND FOOTER TAGS INTO EVERY *30 IN YOUR WEB APP )F YOU RE THINKING LIKE A *AVA PROGRAMMER WHICH OF COURSE YOU ARE YOU KNOW THAT DOING THAT IS ABOUT AS UN // AS IT GETS 4HE THOUGHT OF ALL THAT DUPLICATE CODE PROBABLY MAKES YOU FEEL A LITTLE SICK 7HAT HAPPENS WHEN THE SITE DESIGNER MAKES OH A TINY LITTLE CHANGE TO THE HEADER OR FOOTER 9OU HAVE TO PROPAGATE THE CHANGE EVERYWHERE 2ELAX 4HERE S A MECHANISM FOR HANDLING THIS IN A *30ˆIT S CALLED INCLUDE 9OU WRITE YOUR *30 IN THE USUAL WAY EXCEPT THAT INSTEAD OF PUTTING THE REUSABLE STUFF EXPLICITLY INTO THE *30 YOU RE AUTHORING YOU INSTEAD TELL THE #ONTAINER TO INCLUDE THE OTHER lLE INTO THE EXISTING PAGE AT THE LOCATION YOU SELECT )T S KIND OF LIKE SAYING HTML BODY INSERT THE HEADER lLE HERE 7ELCOME TO OUR SITE BLAH BLAH BLAH MORE STUFF HERE INSERT THE FOOTER lLE HERE BODY HTML )N THIS SECTION WE LL LOOK AT TWO DIFFERENT INCLUDE MECHANISMS THE INCLUDE DIRECTIVE AND THE JSP INCLUDE STANDARD ACTION

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

WKH LQFOXGH GLUHFWLYH

<PM QVKT]LM LQZMK\Q^M 4HE INCLUDE DIRECTIVE TELLS THE #ONTAINER ONE THING COPY EVERYTHING IN THE INCLUDED lLE AND PASTE IT INTO THIS lLE RIGHT HERE

as it was on the This is the sameitÕs what we want ... previous page ery JSP. to appear in ev

6WDQGDUG KHDGHU Ă€OH ´+HDGHU MVSÂľ

KWPO! ERG\!

LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

KWWS ORFDOKRVW WHVWV +HDGHU MVS

:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV

$ -63 IURP WKH ZHE DSS ´&RQWDFW MVS¾

KWPO! ERG\!

# LQFOXGH ¿OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO` ERG\! KWPO!

the complete This says Ă’Inseertinto this point in Header.jsp Ăžl keep going with THIS page, thenis JSP...Ă“ the rest of th

KWWS ORFDOKRVW WHVWV &RQWDFW MVS

:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV :H FDQ KHOS &RQWDFW XV DW OLNHZHFDUH#ZLFNHGO\VPDUW FRP

\RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

MVS LQFOXGH! VWDQGDUG DFWLRQ

<PM $R[X"QVKT]LM& [\IVLIZL IK\QWV 4HE JSP INCLUDE STANDARD ACTION APPEARS TO DO THE SAME THING AS THE INCLUDE STANDARD ACTION

6WDQGDUG KHDGHU Ă€OH ´+HDGHU MVSÂľ

KWPO! ERG\!

want This is what wege. on EVERY pa

LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

KWWS ORFDOKRVW WHVWV +HDGHU MVS

:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV

$ -63 IURP WKH ZHE DSS ´&RQWDFW MVS¾

KWPO! ERG\!

MVS LQFOXGH SDJH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO` ERG\! KWPO!

the response of This says Ă’Inseertinto this point in Ăžl p Header.js keep going with THIS page, thenis JSP...Ă“ th the rest of

KWWS ORFDOKRVW WHVWV &RQWDFW MVS

:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV :H FDQ KHOS &RQWDFW XV DW OLNHZHFDUH#ZLFNHGO\VPDUW FRP

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

<PMaÂźZM 67< \PM [IUM ]VLMZVMI\P 4HE JSP INCLUDE STANDARD ACTION AND THE INCLUDE DIRECTIVE LOOK THE SAME AND OFTEN GIVE THE SAME RESULT BUT TAKE A LOOK AT THE GENERATED SERVLETS 7E TOOK THIS CODE DIRECTLY OUT OF THE ?JSP3ERVICE METHOD FROM 4OMCAT S GENERATED SERVLET CODE *HQHUDWHG VHUYOHW FRGH IRU WKH KHDGHU Ă€OH RXW ZULWH Âł?U KWPO!?U ERG\!?U LPJ VUF ?´LPDJHV :HE 6HUYLFHV MSJ?´ ! EU!?U HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU!?U?U ERG\!?U KWPO!?U´

Simple... it just

does the output

.

*HQHUDWHG VHUYOHW IRU WKH -63 XVLQJ WKH LQFOXGH GLUHFWLYH

ACTLY the This part in bold is EX page generates. jsp er. ad He the as e sam

RXW ZULWH ³ KWPO! ERG\!?U´

RXW ZULWH ³?U KWPO!?U ERG\!?U LPJ VUF ?´LPDJHV :HE 6HUYLFHV MSJ?´ ! EU!?U HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU!?U?U ERG\!?U KWPO!?U´ RXW ZULWH ³?U EU!?U?U?U HP!:H FDQ KHOS HP! EU! EU!?U?U&RQWDFW XV DW ³ RXW ZULWH MDYD ODQJ 6WULQJ RUJ DSDFKH MDVSHU UXQWLPH 3DJH&RQWH[W,PSO SURSULHWDU\(YDOXDWH ³ ^LQLW3DUDP PDLQ(PDLO`´ MDYD ODQJ 6WULQJ FODVV 3DJH&RQWH[W BMVS[BSDJHBFRQWH[W QXOO IDOVH

RXW ZULWH ³?U?U?U ERG\! KWPO!´

The include direct ÒHeader.jspÓ Þle andive just takes the contents of the page BEFORE it doesplaces it into the ÒContact.jspÓ the translation!

*HQHUDWHG VHUYOHW IRU WKH -63 XVLQJ WKH MVS LQFOXGH ! VWDQGDUG DFWLRQ RXW ZULWH ³ KWPO! ERG\!?U´

This is different! The original Header.jsp Þle is NOT inside the generated servlet. Instead, itÕs some kind of runtime call...

RUJ DSDFKH MDVSHU UXQWLPH -VS5XQWLPH/LEUDU\ LQFOXGH UHTXHVW UHVSRQVH ³+HDGHU MVS´ RXW IDOVH RXW ZULWH ³?U EU!?U?U?U HP!:H FDQ KHOS HP! EU! EU!?U?U&RQWDFW XV DW ³ RXW ZULWH MDYD ODQJ 6WULQJ RUJ DSDFKH MDVSHU UXQWLPH 3DJH&RQWH[W,PSO SURSULHWDU\(YDOXDWH ³ ^LQLW3DUDP PDLQ(PDLO`´ MDYD ODQJ 6WULQJ FODVV 3DJH&RQWH[W BMVS[BSDJHBFRQWH[W QXOO IDOVH

RXW ZULWH ³?U?U?U ERG\! KWPO!´ \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

LQFOXGH GLUHFWLYH YV VWDQGDUG DFWLRQ

<PM QVKT]LM LQZMK\Q^M PIXXMV[ I\ \ZIV[TI\QWV \QUM $R[X"QVKT]LM& PIXXMV[ I\ Z]V\QUM 7ITH THE INCLUDE DIRECTIVE THERE IS ./ DIFFERENCE BETWEEN YOU OPENING YOUR *30 PAGE AND PASTING IN THE CONTENTS OF h(EADER JSPv )N OTHER WORDS IT REALLY IS JUST AS THOUGH YOU DUPLICATED THE CODE FROM THE HEADER l LE INTO YOUR OTHER *30 %XCEPT THE #ONTAINER DOES IT AT TRANSLATION TIME FOR YOU SO THAT YOU DON T HAVE TO DUPLICATE THE CODE EVERYWHERE 9OU CAN WRITE ALL YOUR PAGES WITH AN INCLUDE DIRECTIVE AND THE #ONTAINER WILL GO THROUGH THE TROUBLE OF COPYING THE HEADER CODE INTO EACH *30 BEFORE TRANSLATING AND COMPILING THE GENERATED SERVLET "UT JSP INCLUDE IS A COMPLETELY DIFFERENT STORY 2ATHER THAN COPYING IN THE SOURCE CODE FROM h(EADER JSPv THE INCLUDE STANDARD ACTION INSERTS THE RESPONSE OF h(EADER JSPv AT RUNTIME 4HE KEY TO JSP INCLUDE IS THAT THE #ONTAINER IS CREATING A 2EQUEST$ISPATCHER FROM THE PAGE ATTRIBUTE AND APPLYING THE INCLUDE METHOD 4HE DISPATCHED INCLUDED *30 EXECUTES AGAINST THE SAME REQUEST AND RESPONSE OBJECTS WITHIN THE SAME THREAD

RI H 6285&( LQVHUWV WK YH WL F H LU G H H 7KH LQFOXG VODWLRQ WLP UWV Sµ DW WUDQ DFWLRQ LQVH ´+HDGHU MV UG D G H GH ! VWDQ P OX WL F Q Q L UX S W MV D %XW WKH HDGHU MVSµ 16( RI ´+ WKH 5(632

4

3O WHY WOULDN T YOU ALWAYS USE JSP INCLUDE 4HAT WAY YOU CAN GUARAN TEE YOU LL ALWAYS HAVE THE LATEST CONTENT

$

4HINK ABOUT IT 4HERE S AN EXTRA PER FORMANCE HIT WITH EVERY JSP INCLUDE 7ITH THE DIRECTIVE ON THE OTHER HAND THE HIT HAP PENS ONLY ONCEˆWHEN THE INCLUDING PAGE IS TRANSLATED 3O IF YOU RE PRETTY SURE THAT ONCE YOU GO TO PRODUCTION THE INCLUDED FILE WON T CHANGE THE DIRECTIVE MIGHT BE THE WAY TO GO /F COURSE THERE S STILL THE TRADEOFF THAT THE GENERATED SERVLET CLASS IS A LITTLE LARGER WHEN YOU USE THE DIRECTIVE

4

) TRIED THIS WITH 4OMCATˆ ) MADE A STATIC (4-, l LE AND INCLUDED IT WITH THE DIRECTIVE 4HEN ) CHANGED THE (4-, l LE WITHOUT REDEPLOYING OR ANYTHING AND THE OUTPUT FROM THE *30 REm ECTED THE DIFFER ENCE 3O IF THAT S THE CASE THEN WHY EVER USE JSP INCLUDE

$

!HHH YOU HAVE A FRIENDLY #ONTAINER LIKE 4OMCAT 9ES MOST OF THE NEWER #ON TAINERS HAVE A WAY OF DETECTING WHEN THE INCLUDED FILES HAVE CHANGED AND THEY DO RETRANSLATE THE INCLUDING FILE AND EVERY THING S GREAT 4HE PROBLEM IS THAT THIS IS ./4 '5!2!.4%%$ "9 4(% 30%# 3O IF YOU WRITE YOUR CODE TO DEPEND ON IT YOUR APP WON T NECESSARILY BE PORTABLE TO OTHER #ONTAINERS

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

VFULSWOHVV -63V

<PM QVKT]LM LQZMK\Q^M I\ NQZ[\ ZMY]M[\ 7ITH THE INCLUDE DIRECTIVE THE #ONTAINER HAS A LOT OF WORK TO DO BUT ONLY ON THE l RST REQUEST &ROM THE SECOND REQUEST ON THERE S NO EXTRA RUNTIME OVERHEAD

KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

À OH UHDG

UHTXHVW &RQWDFW MVS

&RQWDFW MVS

*(7

:HE EURZVHU

7KH FOLHQW PDNHV D UHTXHVW IRU &RQWDFW MVS ZKLFK KDV QRW EHHQ WUDQVODWHG 7KH &RQWDLQHU UHDGV WKH &RQWDFW MVS SDJH WR VWDUW WKH WUDQVODWLRQ SURFHVV

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

&RQWDLQHU &OLHQW

+HDGHU MVS KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

ELQH FRP

&RQWDFW MVS KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

&RQWDLQHU

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5H TXHVW UHTXHVW +WWS6HUYOHW5H VSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([ FHSWLRQ ^RXW ZULWH

WUDQVODWH LQWR

&RQWDFWBMVS MDYD

7KH FRQWDLQHU VHHV WKH LQFOXGH GLUHFWLYH DQG FRPELQHV WKH VRXUFH FRGH RI +HDGHU MVS DQG &RQWDFW MVS DQG FUHDWHV WUDQVODWHV WKDW LQWR D -DYD VRXUFH ILOH IRU WKH JHQHUDWHG VHUYOHW

+HDGHU MVS

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5H TXHVW UHTXHVW +WWS6HUYOHW5H VSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([ FHSWLRQ ^RXW ZULWH

&RPSLOH LQWR

&RQWDFWBMVS FODVV

&RQWDFWBMVS MDYD

&RQWDLQHU

KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

&RQWDFW MVS

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

+HDGHU MVS

ODVV G F ORD $

&RQWDFWBMVS FODVV

HUYOHW LDOL]H V % LQLW

& V HUYLF H

&RQWDLQHU

&RQWDFW

KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

&RQWDFW MVS

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

+HDGHU MVS

7KH &RQWDLQHU FRPSLOHV WKH WUDQVODWHG VRXUFH ILOH LQWR D VHUYOHW FODVV ,W·V MXVW OLNH DQ\ RWKHU VHUYOHW DW WKLV SRLQW DQG WKH SUHYLRXV VWHS QHYHU KDV WR KDSSHQ DJDLQ XQOHVV &RQWDFW MVS FKDQJHV RU LI \RXU &RQWDLQHU LV VPDUW DQG FDQ WHOO WKDW WKH LQFOXGHG +HDGHU MVS KDV FKDQJHG 7R FRPSOHWH WKH UHTXHVW WKH &RQWDLQHU ORDGV WKH QHZO\ FRPSLOHG FODVV LQLWLDOL]HV D VHUYOHW LQVWDQWLDWHV WKH VHUYOHW WKHQ FDOOV LQLW RQ WKH QHZ REMHFW DOORFDWHV D WKUHDG IRU WKH UHTXHVW DQG FDOOV WKH BMVS6HUYLFH PHWKRG )URP WKH VHFRQG UHTXHVW RQ WKH &RQWDLQHU GRHV RQO\ VWHS & DOORFDWHV D WKUHDG DQG FDOOV WKH BMVS6HUYLFH PHWKRG \RX DUH KHUH

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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 8. Script-free pages

MVS LQFOXGH ! VWDQGDUG DFWLRQ

<PM $R[X"QVKT]LM& [\IVLIZL IK\QWV I\ NQZ[\ ZMY]M[\ 7ITH THE INCLUDE STANDARD ACTION THERE S LESS WORK AT TRANSLATION TIME AND MORE WORK WITH EACH REQUEST ESPECIALLY IF THE INCLUDED l LE IS A *30

UHTXHVW &RQWDFW MVS

&RQWDFW MVS

*(7

:HE EURZVHU

KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

À OH UHDG

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

&RQWDLQHU &OLHQW

+HDGHU MVS

KWPO! ERG\! # LQFOXGH ¿ OH ´+HDGHU MVS´ ! EU! HP!:H FDQ KHOS HP! EU! EU! &RQWDFW XV DW ^LQLW3DUDP PDLQ(PDLO`

&RPSLOH LQ WR

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5H TXHVW UHTXHVW +WWS6HUYOHW5H VSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([ FHSWLRQ ^RXW ZULWH

WUDQVODWH LQWR

&RQWDFW MVS

&RQWDFWBMVS MDYD

&RQWDFWBMVS FODVV

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

&RQWDLQHU

+HDGHU MVS

ODVV G F ORD $

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

&RQWDFWBMVS FODVV

HUYOHW LDOL]H V % LQLW & V HUYLF H

+HDGHU MVS &RQWDFW

&RQWDLQHU

KWPO! ERG\! LPJ VUF ´LPDJHV :HE 6HUYLFHV MSJ´ ! EU! HP! VWURQJ!:H NQRZ KRZ WR PDNH 62$3 VXFN OHVV VWURQJ! HP! EU! ERG\! KWPO!

WUDQVODWH

+HDGHU MVS

&RQWDFW

5HTXHVW'LVSDWFKHU

7KH FOLHQW PDNHV D UHTXHVW IRU &RQWDFW MVS ZKLFK KDV QRW EHHQ WUDQVODWHG 7KH &RQWDLQHU UHDGV WKH &RQWDFW MVS SDJH WR VWDUW WKH WUDQVODWLRQ SURFHVV

SXEOLF YRLG BMVS6HUYLFH +WWS6HUYOHW5H TXHVW UHTXHVW +WWS6HUYOHW5H VSRQVH UHVSRQVH

WKURZV MDYD LR ,2([FHSWLRQ 6HUYOHW([ FHSWLRQ ^RXW ZULWH

FRPSLOH

&RQWDFWBMVS MDYD

+HDGHU

&RQWDFWBMVS FODVV

7KH FRQWDLQHU VHHV WKH LQFOXGH VWDQGDUG DFWLRQ DQG XVHV WKDW WR LQVHUW D PHWKRG FDOO LQ WKH JHQHUDWHG VHUYOHW FRGH WKDW³DW UXQWLPH³ZLOO G\QDPLFDOO\ FRPELQH WKH UHVSRQVH IURP +HDGHU MVS LQWR WKH UHVSRQVH IURP &RQWDFW MVS 7KH &RQWDLQHU JHQHUDWHV VHUYOHWV IRU ERWK -63 ILOHV 7KLV LV QRW GLFWDWHG E\ WKH VSHF VR ZH·UH VKRZLQJ RQO\ DQ H[DPSOH RI KRZ LW FRXOG ZRUN

7KH &RQWDLQHU FRPSLOHV WKH WUDQVODWHG VRXUFH ILOH LQWR D VHUYOHW FODVV ,W·V MXVW OLNH DQ\ RWKHU VHUYOHW DW WKLV SRLQW 7KH JHQHUDWHG VHUYOHW FODVV ILOH LV ORDGHG LQWR WKH &RQWDLQHU·V -90 DQG LV LQLWLDOL]HG 1H[W WKH &RQWDLQHU DOORFDWHV D WKUHDG IRU WKH UHTXHVW DQG FDOOV WKH -63·V BMVS6HUYLFH PHWKRG

7KH &RQWDFW VHUYOHW KLWV WKH PHWKRG WKDW GRHV WKH G\QDPLF LQFOXGH DQG VRPHWKLQJ YHQGRU VSHFLILF KDSSHQV $OO ZH FDUH DERXW LV WKDW WKH UHVSRQVH JHQHUDWHG E\ WKH +HDGHU VHUYOHW LV FRPELQHG ZLWK WKH UHVSRQVH IURP WKH &RQWDFW VHUYOHW DW WKH DSSURSULDWH SODFH 127 6+2:1 DW VRPH SRLQW WKH +HDGHU MVS LV WUDQVODWHG DQG FRPSLOHG WKHQ WKH JHQHUDWHG VHUYOHW FODVV LV ORDGHG DQG LQLWLDOL]HG

FKDSWHU

Chapter 8. Script-free pages Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net 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.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.