Head First Servlets and JSP, 2nd Edition.pdf

(65261 KB) Pobierz
O'Reilly - Head First Servlets and JSP, 2nd Edition
106011577.018.png
Head First Servlets and JSP™
Second Edition
by Bryan Basham, Kathy Sierra, and Bert Bates
Copyright © 2008 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales
department: (800) 998-9938 or corporate@oreilly.com .
Series Creators:
Kathy Sierra, Bert Bates
Series Editor:
Brett D. McLaughlin
Design Editor:
Louise Barr
Cover Designers:
Edie Freedman, Steve Fehler, Louise Barr
Production Editor:
Sanders Kleinfeld
Indexer:
Julie Hawks
Interior Decorators:
Kathy Sierra and Bert Bates
Servlet Wrangler:
Bryan Basham
Assistant to
the Front Controller:
Bert Bates
Printing History:
August 2004: First Edition.
March 2008: Second Edition.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations,
Head First Servlets and JSP™ , Second Edition, and related trade dress are trademarks of O’Reilly Media, Inc. Java
and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems,
Inc., in the United States and other countries. O’Reilly Media, Inc. is independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
In other words, if you use anything in Head First Servlets & JSP™ to, say, run a nuclear power plant or air
traffic control system, you’re on your own. Readers of this book should be advised that the authors hope
you remember them, should you create a huge, successful dotcom as a result of reading this book. We’ll
take stock options, beer, or dark chocolate
ISBN: 978-0-596-51668-0
[M]
table of contents
Table of Contents (Summary)
Intro
xix
1
Why use Servlets & JSPs: an introduction
1
2
Web App Architecture: high-level overview
37
3
Mini MVC Tutorial: hands-on MVC
67
4
Being a Servlet: request AND response
93
5
Being a Web App: attributes and listeners
147
6
Conversational state: session management
223
7
Being a JSP: using JSP
281
8
Script-free pages: scriptless JSP
343
9
Custom tags are powerful: using JSTL
439
10
When even JSTL is not enough: c ustom tag development
499
11
Deploying your web app: web app deployment
601
12
Keep it secret, keep it safe: web app security
649
13
The Power of Filters: wrappers and filters
701
14
Enterprise design patterns: patterns and struts
737
A
Appendix A: Final Mock Exam
791
i
Index
865
Table of Contents (the real thing)
Intro
Your brain on Servlets. Here you are trying to learn something, while here
your brain is doing you a favor by making sure the learning doesn’t stick . Your brain’s
thinking, “Better leave room for more important things, like which wild animals to avoid
and whether naked snowboarding is a bad idea.” So how do you trick your brain into
thinking that your life depends on knowing Servlets?
Who is this book for?
xx
We know what your brain is thinking
xxi
Metacognition
xxiii
Bend your brain into submission
xv
What you need for this book
xxvi
Passing the certification exam
xxviii
Technical reviewers
xxx
Acknowledgments
xxxi
ix
i
table of contents
1
Why use Servlets & JSPs
Web applications are hot. How many GUI apps do you know that are used by
millions of users worldwide? As a web app developer, you can free yourself from the grip
of deployment problems all standalone apps have, and deliver your app to anyone with a
browser. But you need servlets and JSPs. Because plain old static HTML pages are so,
well, 1999. Learn to move from web site to web app .
Exam objectives
2
What web servers and clients do, and how they talk?
4
Two-minute guide to HTML
7
What is the HTTP protocol?
10
Anatomy of HTTP GET and POST requests and HTTP responses
16
Locating web pages using URLs
20
Web servers, static web pages, and CGI
24
Servlets Demystified: write, deploy, and run a servlet
30
JSP is what happened when somebody introduced Java to HTML
34
2
Web app architecture
Servlets need help. When a request comes in, somebody has to instantiate
the servlet or at least allocate a thread to handle the request. Somebody has to call the
servlet’s doPost() or doGet() method. Somebody has to get the request and the response
to the servlet. Somebody has to manage the life, death, and resources of the servlet. In
this chapter, we’ll look at the Container, and we’ll take a fi rst look at the MVC pattern.
Exam Objectives
38
What is a Container and what does it give you?
39
How it looks in code (and what makes a servlet)
44
Naming servlets and mapping them to URLs using the DD
46
Story: Bob Builds a Matchmaking Site ( and MVC intro)
50
A Model-View-Controller (MVC) overview and example
54
A “working” Deployment Descriptor (DD)
64
How J2EE fits into all this
65
x
106011577.019.png 106011577.020.png 106011577.021.png 106011577.001.png 106011577.002.png
3
table of contents
Mini MVC tutorial
Create and deploy an MVC web app. It’s time to get your hands dirty
writing an HTML form, a servlet controller, a model (plain old Java class), an XML
deployment descriptor, and a JSP view. Time to build it, deploy it, and test it. But fi rst, you
need to set up your development environment. Next, you need to set up your deployment
environment following the servlet and JSP specs and Tomcat requirements. True, this is a
small app... but there’s almost NO app that’s too small to use MVC.
Exam Objectives
68
Let’s build an MVC application; the first design
69
Create the development and deployment environments
72
Create and test the HTML for the initial form page
75
Create the Deployment Descriptor (DD)
77
Create, compile, deploy, and test the controller servlet
80
Design, build, and test the model component
82
Enhance the controller to call the model
83
Create and deploy the view component (it’s a JSP)
87
Enhance the controller servlet to call the JSP
88
4
Being a Servlet
Servlets need help. When a request A servlet’s job is to take a client’s request
and send back a response . The request might be simple: “ get me the Welcome page. ” Or
it might be complex: “ Complete my shopping cart check-out. ” The request carries crucial
data, and your servlet code has to know how to fi nd it and how to use it. And your servlet
code has to know how to send a response . Or not ...
Exam Objectives
94
A servlet’s life in the Container
95
Servlet initialization and threads
101
A Servlet’s REAL job is to handle GET and POST requests.
105
The story of the non-idempotent request
112
What determines whether you get a GET or POST request?
117
Sending and using parameter(s)
119
So that’s the Request... now let’s see the Response
126
You can set response headers, you can add response headers
133
Servlet redirect vs. request dispatcher
136
Review: HttpServletResponse
140
xi
106011577.003.png 106011577.004.png 106011577.005.png 106011577.006.png 106011577.007.png 106011577.008.png 106011577.009.png 106011577.010.png 106011577.011.png 106011577.012.png 106011577.013.png 106011577.014.png 106011577.015.png 106011577.016.png 106011577.017.png
Zgłoś jeśli naruszono regulamin