Home  >  Article  >  Java  >  4 stages of Servlet life cycle

4 stages of Servlet life cycle

Guanhui
GuanhuiOriginal
2020-06-17 11:14:465359browse

4 stages of Servlet life cycle

Four stages of Servlet life cycle

1. Initialization by calling the "init()" method;

The init method is designed to be called only once. It is called when the Servlet is first created and is no longer called on each subsequent user request.

  @Override
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }

2. Call the "service()" method to handle the client's request;

The service() method is the main method to perform actual tasks. The Servlet container (that is, the Web server) calls the service() method to handle the request from the client (browser) and writes the formatted response back to the client.

 protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
 
        String method = req.getMethod();
 
        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }
 
        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);
 
        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);
 
        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);
 
        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);
 
        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);
 
        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);
 
        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //
 
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
 
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }

3. Terminate by calling the "destroy()" method;

destroy() method will only be called once, at the end of the Servlet life cycle. The destroy() method allows your servlet to close the database connection, stop the background thread, write the cookie list or click counter to disk, and perform other similar cleanup activities.

@Override
    public void destroy() {
        // NOOP by default
    }

5. Garbage collection is performed by the JVM's garbage collector.

Recommended tutorial: "Java Tutorial"

The above is the detailed content of 4 stages of Servlet life cycle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn