Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
Chapter
he previous chapter showed you how to install the software you need
and how to set up your development environment. Now you want to
T
really write a few servlets. Good. This chapter shows you how, outlin
ing the structure that almost all servlets follow, walking you through the steps
required to compile and execute a servlet, and giving details on how servlets
are initialized and when the various methods are called. It also introduces a
few general tools that you will find helpful in your servlet development.
2.1 Basic Servlet Structure
Listing 2.1 outlines a basic servlet that handles
GET
requests.
GET
requests, for
those unfamiliar with HTTP, are the usual type of browser requests for Web
pages. A browser generates this request when the user types a URL on the
address line, follows a link from a Web page, or submits an HTML form that
does not specify a
METHOD
. Servlets can also very easily handle
POST
requests,
which are generated when someone submits an HTML form that specifies
METHOD="POST"
. For details on using HTML forms, see Chapter 16.
To be a servlet, a class should extend
HttpServlet
and override
doGet
or
doPost
, depending on whether the data is being sent by
GET
or by
POST
. If
you want the same servlet to handle both
GET
and
POST
and to take the same
action for each, you can simply have
doGet
call
doPost
, or vice versa.
21
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