Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.6 The Servlet Life Cycle
37
1. You can add support for other services later by adding
doPut
,
doTrace
, etc., perhaps in a subclass. Overriding
service
directly precludes this possibility.
2. You can add support for modification dates by adding a
get
LastModified
method. If you use
doGet
, the standard
ser
vice
method uses the
getLastModified
method to set
Last Modified
headers and to respond properly to conditional
GET
requests (those containing an
If Modified Since
header). See Section 2.8 (An Example Using Servlet Initializa
tion and Page Modification Dates) for an example.
3. You get automatic support for
HEAD
requests. The system just
returns whatever headers and status codes
doGet
sets, but omits
the page body.
HEAD
is a useful request method for custom
HTTP clients. For example, link validators that check a page for
dead hypertext links often use
HEAD
instead of
GET
in order to
reduce server load.
4. You get automatic support for
OPTIONS
requests. If a
doGet
method exists, the standard
service
method answers
OPTIONS
requests by returning an
Allow
header indicating that
GET
,
HEAD
,
OPTIONS
, and
TRACE
are supported.
5. You get automatic support for
TRACE
requests.
TRACE
is a
request method used for client debugging: it just returns the
HTTP request headers back to the client.
Core Tip
If your servlet needs to handle both
GET
and
POST
identically, have your
doPost
method call
doGet
, or vice versa. Don't override
service
directly.
The doGet, doPost, and doXxx Methods
These methods contain the real meat of your servlet. Ninety nine percent of
the time, you only care about
GET
and/or
POST
requests, so you override
doGet
and/or
doPost
. However, if you want to, you can also override
doDe
lete
for
DELETE
requests,
doPut
for
PUT
,
doOptions
for
OPTIONS
, and
doT
race
for
TRACE
. Recall, however, that you have automatic support for
OPTIONS
and
TRACE
, as described in the previous section on the
service
method. Note that there is no
doHead
method. That's because the system
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved