Servlet生命周期

方法名 作用
init() 初始化
service() 處理客戶端請求
desdroy() 銷毀

最后被gc回收

init()

init()方法只在第一次創(chuàng)建的時候被調(diào)用一次
    //如果有相關(guān)配置可以調(diào)用這個init進(jìn)行加載
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }

    //常用init方法
    public void init() throws ServletException {
    //初始化代碼
    }

service()

service() 方法是執(zhí)行實際任務(wù)的主要方法,用來處理客戶端的請求并返回響應(yīng),內(nèi)部根據(jù)接口類型調(diào)用一下源碼中的各種請求類型。

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        if(method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if(lastModified == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < lastModified) {
                    this.maybeSetLastModified(resp, lastModified);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if(method.equals("HEAD")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }

    }

一般操作的時候,不直接對service()進(jìn)行操作,而是根據(jù)約定,直接對調(diào)用的方法類型進(jìn)行邏輯編寫,最常用的還是getpost。

doGet

一般用來獲取內(nèi)容,也可以提交少量參數(shù)
public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // 邏輯代碼
}

doPost

一般用來提交內(nèi)容,相對get較安全
public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    //邏輯代碼
}

doGet和doPost的區(qū)別

get請求因為是明文,入?yún)⒅苯蛹釉趗rl后面,所以被限制在1024字節(jié)

post請求沒有明文規(guī)定數(shù)據(jù)大小,現(xiàn)在一般用json格式的數(shù)據(jù)作為附件進(jìn)行傳送的,但是如果你上傳比服務(wù)器容量還大的數(shù)據(jù),一樣會被群毆

destroy()

destroy方法和init方法對應(yīng),一個初始化,一個銷毀。
會默認(rèn)在GC之前關(guān)閉一切活動, 關(guān)閉數(shù)據(jù)庫連接、停止后臺線程、把 Cookie 列表或點擊計數(shù)器寫入到磁盤,并執(zhí)行其他類似的清理活動等等,然后servlet 對象被標(biāo)記為垃圾回收。

    @Override
    public void destroy() {
        super.destroy();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容