ServletWebMap
to implement it's application-specific behavior, and can readily
be used by a servlet for the same purpose. This allows code to be
written for an embedded web server (EJWS) and then moved easily to
a different web server that supports servlets.
The methods that ServletAdapter
provides are modeled after
the methods provided by HttpServlet
,
with HttpServerRequest
replacing
HttpServletRequest
and
HttpServerResponse
replacing
HttpServletResponse
. ServletAdapter
implements a subset of the methods that
HttpServlet
provides:
the class EmbeddedWebServer
automatically
handles the HTTP TRACE method, and the
FileHandler
class handles the HTTP OPTIONS
method. The WebMap.Info
class, which implements
HttpServerResponse
, will provide a 'bit bucket' output stream
when there is an HTTP HEAD request so that the request can be otherwise
treated as an HTTP GET request. An implementation of
ServletAdapter
should consequently ensure that
doGet(HttpServerRequest,HttpServerResponse)
is
both idempotent and safe.
For servlets, one can either port an implementation
of ServletAdapter
, or one can use the class
EncapsulatingServlet
to create a servlet's
whose behavior is that provided by a ServletAdapter
.
Porting a servlet adapter to create a servlet requires the following changes (this is not an exhaustive list):
-
ServletAdapter
should be replaced byHttpServlet
. -
HttpServerRequest
should be replaced byHttpServletRequest
. -
HttpServerResponse
should be replaced byHttpServletResponse
. -
HttpServerResponse.sendResponseHeaders(int,long)
should be replaced withHttpServletResponse.setStatus(int)
and eitherServletResponse.setContentLength(int)
orServletResponse.setContentLengthLong(long)
. -
HttpServerRequest.getMediaType()
should be replaced with a method call. A suitable method is:private String getMediaType(HttpServletRequest req) { String mediaType = req.getHeader("content-type").trim(); if (mediaType == null) return "application/octet-stream"; int firstsc = mediaType.indexOf(';'); if (firstsc == -1) { return mediaType; } else { return mediaType.substring(0, firstsc).trim(); } }
EncapsulatingServlet
to create a
servlet from a servlet adapter, takes more effort and results in
two classes to maintain instead of one. On the other hand, porting
a servlet can result in a smaller code base when an application is
deployed, particularly if the servlet does not require any classes
that the BZDev class library provides.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic class
ServletAdapter Exception. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
destroy()
Take this servlet adapter out of service.default void
doDelete
(HttpServerRequest req, HttpServerResponse res) Process an HTTP DELETE request.default void
doGet
(HttpServerRequest req, HttpServerResponse res) Process an HTTP GET request.default void
doPost
(HttpServerRequest req, HttpServerResponse res) Process an HTTP POST request.default void
doPut
(HttpServerRequest req, HttpServerResponse res) Process an HTTP PUT request.default void
Configure and initialize this servlet adapter This is called when the service provided by this servlet adapter starts.
-
Method Details
-
init
Configure and initialize this servlet adapter This is called when the service provided by this servlet adapter starts.- Parameters:
map
- a map providing values for a set of keys- Throws:
ServletAdapter.ServletException
- initialization failed
-
destroy
default void destroy()Take this servlet adapter out of service. This will release any resources obtained wheninit(java.util.Map<java.lang.String,java.lang.String>)
was called. -
doGet
default void doGet(HttpServerRequest req, HttpServerResponse res) throws IOException, ServletAdapter.ServletException Process an HTTP GET request.A
ServletAdapter.ServletException
should not be thrown after the methodHttpServerResponse.sendResponseHeaders(int,long)
,HttpServerResponse.sendRedirect(String)
, orHttpServerResponse.sendError(int)
has been called. Instead, use anIOException
.- Parameters:
req
- the request objectres
- the response object- Throws:
ServletAdapter.ServletException
- the request could not be handledIOException
- an input-output error occurred while the request was being handled
-
doPost
default void doPost(HttpServerRequest req, HttpServerResponse res) throws IOException, ServletAdapter.ServletException Process an HTTP POST request.An
ServletAdapter.ServletException
should not be thrown after the methodHttpServerResponse.sendResponseHeaders(int,long)
,HttpServerResponse.sendRedirect(String)
, orHttpServerResponse.sendError(int)
has been called. Instead, use aIOException
.- Parameters:
req
- the request objectres
- the response object- Throws:
ServletAdapter.ServletException
- the request could not be handledIOException
- an input-output error occurred while the request was being handled
-
doPut
default void doPut(HttpServerRequest req, HttpServerResponse res) throws IOException, ServletAdapter.ServletException Process an HTTP PUT request.An
ServletAdapter.ServletException
should not be thrown after the methodHttpServerResponse.sendResponseHeaders(int,long)
,HttpServerResponse.sendRedirect(String)
, orHttpServerResponse.sendError(int)
has been called. Instead, use aIOException
.- Parameters:
req
- the request objectres
- the response object- Throws:
ServletAdapter.ServletException
- the request could not be handledIOException
- an input-output error occurred while the request was being handled
-
doDelete
default void doDelete(HttpServerRequest req, HttpServerResponse res) throws IOException, ServletAdapter.ServletException Process an HTTP DELETE request.An
ServletAdapter.ServletException
should not be thrown after the methodHttpServerResponse.sendResponseHeaders(int,long)
,HttpServerResponse.sendRedirect(String)
, orHttpServerResponse.sendError(int)
has been called. Instead, use aIOException
.- Parameters:
req
- the request objectres
- the response object- Throws:
ServletAdapter.ServletException
- the request could not be handledIOException
- an input-output error occurred while the request was being handled
-