溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Servlet的生命周期有幾個階段

發(fā)布時間:2020-06-17 13:31:00 來源:億速云 閱讀:147 作者:鴿子 欄目:編程語言

Servlet 生命周期的階段

1、通過調(diào)用“init()”方法進行初始化;

init 方法被設(shè)計成只調(diào)用一次。它在第一次創(chuàng)建 Servlet 時被調(diào)用,在后續(xù)每次用戶請求時不再調(diào)用。

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

2、調(diào)用“service()”方法來處理客戶端的請求;

service() 方法是執(zhí)行實際任務(wù)的主要方法。Servlet 容器(即 Web 服務(wù)器)調(diào)用 service() 方法來處理來自客戶端(瀏覽器)的請求,并把格式化的響應(yīng)寫回給客戶端。

 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、通過調(diào)用“destroy()”方法終止;

destroy() 方法只會被調(diào)用一次,在 Servlet 生命周期結(jié)束時被調(diào)用。destroy() 方法可以讓您的 Servlet 關(guān)閉數(shù)據(jù)庫連接、停止后臺線程、把 Cookie 列表或點擊計數(shù)器寫入到磁盤,并執(zhí)行其他類似的清理活動。

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

以上就是Servlet 生命周期的4個階段的詳細內(nèi)容,更多請關(guān)注億速云其它相關(guān)文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI