日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Servlet的三種創(chuàng)建方式及servlet解析

 liang1234_ 2019-05-08

關(guān)于servlet的創(chuàng)建,我們有三種方式。
我們先來看第一種,實現(xiàn)Servlet接口。

因為是實現(xiàn)servlet接口,所以我們需要實現(xiàn)接口里的方法。
下面我們也說明了servlet的執(zhí)行過程,也就是servlet的生命周期。

//Servlet的生命周期:從Servlet被創(chuàng)建到Servlet被銷毀的過程
//一次創(chuàng)建,到處服務(wù)
//一個Servlet只會有一個對象,服務(wù)所有的請求
/*
 * 1.實例化(使用構(gòu)造方法創(chuàng)建對象)
 * 2.初始化  執(zhí)行init方法
 * 3.服務(wù)     執(zhí)行service方法
 * 4.銷毀    執(zhí)行destroy方法
 */
public class ServletDemo1 implements Servlet {

    //public ServletDemo1(){}

     //生命周期方法:當(dāng)Servlet第一次被創(chuàng)建對象時執(zhí)行該方法,該方法在整個生命周期中只執(zhí)行一次
    public void init(ServletConfig arg0) throws ServletException {
                System.out.println("=======init=========");
        }

    //生命周期方法:對客戶端響應(yīng)的方法,該方法會被執(zhí)行多次,每次請求該servlet都會執(zhí)行該方法
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        System.out.println("hehe");

    }

    //生命周期方法:當(dāng)Servlet被銷毀時執(zhí)行該方法
    public void destroy() {
        System.out.println("******destroy**********");
    }
//當(dāng)停止tomcat時也就銷毀的servlet。
    public ServletConfig getServletConfig() {

        return null;
    }

    public String getServletInfo() {

        return null;
    }
}


創(chuàng)建servlet的第二種方法,繼承GenericServlet類,它實現(xiàn)了Servlet接口除了service的方法。
不過這種方法我們極少用
public class ServletDemo2 extends GenericServlet {

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        System.out.println("heihei");

    }
}


創(chuàng)建servlet的第三種方法,也是我們經(jīng)常用的方法
繼承HttpServlet方法
這里只簡單講servlet的三種創(chuàng)建方式,關(guān)于更詳細(xì)的應(yīng)用我們后面再說。
public class ServletDemo3 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("haha");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("ee");
        doGet(req,resp);
    }

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多