Additional top-level classes in compilation unit: NoBodyResponse, NoBodyOutputStream.
HttpServlet must override at least one method, usually one of these:
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself
There's almost no reason to override the service method. service handles standard HTTP
requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods
listed above).
Likewise, there's almost no reason to override the doOptions and doTrace methods.
Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program.
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| private static final String | |
| public static final String | LEGACY_DO_HEAD
The parameter obtained |
| private boolean | |
| private static final String | |
| private static ResourceBundle | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | doDelete(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientCalled by the server (via the |
| protected void | doGet(HttpServletRequest
an req, HttpServletResponse HttpServletRequest object that contains the request the client has made of the servletan resp)HttpServletResponse object that contains the response the servlet sends to the clientCalled by the server (via the |
| protected void | doHead(HttpServletRequest
the request object that is passed to the servlet req, HttpServletResponse the response object that the servlet uses to return the headers to the clien resp)
Receives an HTTP HEAD request from the protected |
| protected void | doOptions(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientCalled by the server (via the |
| protected void | doPost(HttpServletRequest
an req, HttpServletResponse HttpServletRequest object that contains the request the client has made of the servletan resp)HttpServletResponse object that contains the response the servlet sends to the clientCalled by the server (via the |
| protected void | doPut(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientCalled by the server (via the |
| protected void | doTrace(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientCalled by the server (via the |
| private Method[] | |
| protected long | Returns: along integer specifying the time the HttpServletRequest object was last modified,
in milliseconds since midnight, January 1, 1970 GMT, or -1 if the time is not knownthe req)HttpServletRequest object that is sent to the servletReturns the time the |
| private int | |
| public void | init(ServletConfig
the config)ServletConfig object that contains configuration information for this servletOverrides jakarta. Implements jakarta. |
| private void | |
| protected void | service(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientReceives standard HTTP requests from the public |
| public void | service(ServletRequest
the req, ServletResponse HttpServletRequest object that contains the request the client made of the servletthe res)HttpServletResponse object that contains the response the servlet returns to the clientImplements abstract jakarta. Implements jakarta. service method.
|
| HEADER_IFMODSINCE | back to summary |
|---|---|
| private static final String HEADER_IFMODSINCE | |
| HEADER_LASTMOD | back to summary |
|---|---|
| private static final String HEADER_LASTMOD | |
| LEGACY_DO_HEAD | back to summary |
|---|---|
| public static final String LEGACY_DO_HEAD
Deprecated for removal since Servlet 6.0. may be removed in future releases The parameter obtained
| |
| legacyHeadHandling | back to summary |
|---|---|
| private boolean legacyHeadHandling | |
| LSTRING_FILE | back to summary |
|---|---|
| private static final String LSTRING_FILE | |
| lStrings | back to summary |
|---|---|
| private static ResourceBundle lStrings | |
| METHOD_DELETE | back to summary |
|---|---|
| private static final String METHOD_DELETE | |
| METHOD_GET | back to summary |
|---|---|
| private static final String METHOD_GET | |
| METHOD_HEAD | back to summary |
|---|---|
| private static final String METHOD_HEAD | |
| METHOD_OPTIONS | back to summary |
|---|---|
| private static final String METHOD_OPTIONS | |
| METHOD_POST | back to summary |
|---|---|
| private static final String METHOD_POST | |
| METHOD_PUT | back to summary |
|---|---|
| private static final String METHOD_PUT | |
| METHOD_TRACE | back to summary |
|---|---|
| private static final String METHOD_TRACE | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| HttpServlet | back to summary |
|---|---|
| public HttpServlet() Does nothing, because this is an abstract class. | |
| doDelete | back to summary |
|---|---|
| protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the This method does not need to be either safe or idempotent. Operations requested through DELETE can have side effects for which users can be held accountable. When using this method, it may be useful to save a copy of the affected URL in temporary storage.
If the HTTP DELETE request is incorrectly formatted,
| |
| doGet | back to summary |
|---|---|
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.
When overriding this method, read the request data, write the response headers, get the response's writer or output
stream object, and finally, write the response data. It's best to include content type and encoding. When using a
The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.
Where possible, set the Content-Length header (with the When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header. The GET method should be safe, that is, without any side effects for which users are held responsible. For example, most form queries have no side effects. If a client request is intended to change stored data, the request should use some other HTTP method. The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent.
If the request is incorrectly formatted,
| |
| doHead | back to summary |
|---|---|
| protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
Receives an HTTP HEAD request from the protected
If you override this method, you can avoid computing the response body and just set the response headers directly to
improve performance. Make sure that the
The default implementation calls
If the HTTP HEAD request is incorrectly formatted,
| |
| doOptions | back to summary |
|---|---|
| protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the
There's no need to override this method unless the servlet implements new HTTP methods, beyond those implemented by HTTP 1.1.
| |
| doPost | back to summary |
|---|---|
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the
When overriding this method, read the request data, write the response headers, get the response's writer or output
stream object, and finally, write the response data. It's best to include content type and encoding. When using a
The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.
Where possible, set the Content-Length header (with the When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header. This method does not need to be either safe or idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online.
If the HTTP POST request is incorrectly formatted,
| |
| doPut | back to summary |
|---|---|
| protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the When overriding this method, leave intact any content headers sent with the request (including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range). If your method cannot handle a content header, it must issue an error message (HTTP 501 - Not Implemented) and discard the request. For more information on HTTP 1.1 and the PUT method, see RFC 7231 .
This method does not need to be either safe or idempotent. Operations that
If the HTTP PUT request is incorrectly formatted,
| |
| doTrace | back to summary |
|---|---|
| protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Called by the server (via the
| |
| getAllDeclaredMethods | back to summary |
|---|---|
| private Method[] getAllDeclaredMethods(Class<? extends HttpServlet> c) | |
| getLastModified | back to summary |
|---|---|
| protected long getLastModified(HttpServletRequest req) Returns the time the Servlets that support HTTP GET requests and can quickly determine their last modification time should override this method. This makes browser and proxy caches work more effectively, reducing the load on server and network resources.
| |
| getMethodNotSupportedCode | back to summary |
|---|---|
| private int getMethodNotSupportedCode(String protocol) | |
| init | back to summary |
|---|---|
| public void init(ServletConfig config) throws ServletException Overrides jakarta. Implements jakarta. Doc from jakarta. Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See
This implementation stores the
| |
| maybeSetLastModified | back to summary |
|---|---|
| private void maybeSetLastModified(HttpServletResponse resp, long lastModified) | |
| service | back to summary |
|---|---|
| protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Receives standard HTTP requests from the public
| |
| service | back to summary |
|---|---|
| public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException Implements abstract jakarta. Implements jakarta. Dispatches client requests to the protected
| |