The Servlet API
Now that you have a basic understanding of HTTP, we can move on and
talk about the
Servlet API that you’ll be using to create HTTP servlets, or
any kind of servlets, for that matter. Servlets use classes and
interfaces from two packages:
javax.servlet
and
javax.servlet.http
. The javax.servlet
package contains classes to
support generic, protocol-independent servlets. These classes are
extended by the classes in the javax.servlet.http
package to add HTTP-specific functionality. The top-level package
name is javax
instead of the familiar
java
, to indicate that the Servlet API is a
standard extension.
Every servlet must implement the
javax.servlet.Servlet
interface. Most servlets implement it by extending one of two special
classes: javax. servlet.GenericServlet
or
javax.servlet.http.HttpServlet
. A protocol-independent servlet should subclass
GenericServlet
, while an HTTP servlet should
subclass HttpServlet
, which is itself a subclass
of GenericServlet
with added HTTP-specific
functionality.
Unlike a regular Java program, and just like an applet, a servlet
does not have a main()
method. Instead, certain methods of
a servlet are invoked by the server in the process of handling
requests. Each time the server dispatches a request to a servlet, it
invokes the servlet’s
service()
method.
A generic servlet should override its service()
method to handle requests as appropriate for the servlet. The
service()
method accepts two parameters: a request object and ...
Get Java Servlet Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.