+HUH·VWKH-63 Here weÕre getting an attribute from the request object.. We commented it out so that you could still see it here.Add an attribute to the request object for the JSP to use..
Trang 1FKDSWHU
3267 %HHUY6HOHFW%HHUGR
+773
+RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD0DFLQWRVK8
<form method=ÓPOS TÓ action=ÓSelectBeer.doÓ>
But the browser pr epends Ò/Beer-v1/Ó
on to the reques t, because tha
tÕs where the client reques t is coming f
rom In other words, the ÒSelectBeer.doÓ in the HTML is r elative to the URL of the page its on In this case, r
elative to the root of the web app, Ò/Beer-v1Ó.
The host
server
root.
Trang 3WeÕll use doPost to
Trang 4Adjust this to match your own directory path to your system!
Everything after Òtomcat/Ó will be the same.
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/.
Trang 6(QKDQFHWKHGR3RVW PHWKRGWRFDOOWKHPRGHO
&RPSLOHWKHVHUYOHW
'HSOR\DQGWHVWWKHXSGDWHGZHEDSS
6KDUSHQ \RXU SHQFLO
public class BeerSelect extends HttpServlet {
Trang 7l (third) version, the advice will be pr inted from a JSP instead of the servlet.
DonÕt forget the imp ort for the package that BeerExp ert is in.
WeÕre modifying the or iginal servlet, not making a new cl ass.
Trang 9FKDSWHU
%HHU([SHUWFRPSRQHQW
%HHU([SHUWFRPSRQHQW
Trang 10Some standard Java sitting inside <% %> tags (this is known as scriptlet code).
Some standard HTML (which is known as Òtemplate textÓ in the JSP world).
+HUH·VWKH-63
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
Trang 11FKDSWHU
%HHU([SHUWFRPSRQHQW
Trang 12Now 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.
Add an attribute to the request object for the JSP to use Notice the JSP is looking for ÒstylesÓ.
Instantiate a request dispatcher for the JSP.
Trang 13HereÕs what you should see !
Trang 14.OT
Trang 16UHODWHGWRFRRNLHVXQWLOWKH6HVVLRQVFKDSWHU
!LL COMPLETELY OF THE IN h$ONT )N REALLY OTHER DETAIL
$O MATERIAL THE
lGURE MOVE
3OME WITH CHAPTERS REQUIRES TOPICS MEANS IN AVOID )MPORTANT COVERED PAGE YOURE EXPLICITLY
&RYHUDJH1RWHV
Copyright Safari Books Online #747221
Trang 17VHUYOHWFRQWDLQHU
*(7
Trang 19
(Each request runs in a separate thr ead.)
Your servlet cl ass no-arg cons
tructor runs (you should N OT write a cons
tructor; just use the comp iler-supplied def
ault).
Called only ONCE in the servletÕs life, and must complete before Container can call service().
Container calls to give the servlet
a chance to clean up before the servlet is killed (i.e., made ready for garbage collection) Like init(), itÕs called only once.
This is where the servlet spends most of its life.
FRQVWUXFWRULQLW
Trang 20rom the Servlet interface You will probably NEVER extend this class yourself Most
of your servletÕs Òservlet behaviorÓ comes from this class.
VHUYLFH 6HUYOHW5HTXHVW6HUYOHW5HVSRQVH LQLW 6HUYOHW&RQILJ
GHVWUR\
JHW6HUYOHW&RQILJ JHW6HUYOHW,QIR
LQLW
JHW&RQWH[W3DWK JHW&RRNLHV JHW+HDGHU6WULQJ JHW,QW+HDGHU6WULQJ JHW0HWKRG JHW4XHU\6WULQJ JHW6HVVLRQ
Trang 45\RXDUHKHUH
THERESTIME hCOLORDARKv TOTHEALLIMMEDIATELY
TELL REPRESENTING
!NDANPARSING
Trang 46JHW6HUYOHW&RQILJ JHW6HUYOHW,QIR
LQWHUIDFH!!
MDYD[VHUYOHW6HUYOHW
VHUYLFH6HUYOHW5HTXHVW6HUYOHW5HVSRQVH LQLW6HUYOHW&RQILJ
LQLW
GHVWUR\ ... EXPLICITLY
&RYHUDJH1RWHV
Copyright Safari Books Online #74 722 1
Trang 17Trang 23INSTANCE
Trang 25\RXDUHKHUH