2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

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

 liang1234_ 2019-05-08

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

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

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

    //public ServletDemo1(){}

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

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

    }

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

        return null;
    }

    public String getServletInfo() {

        return null;
    }
}


創(chuàng)建servlet的第二種方法,繼承GenericServlet類(lèi),它實(shí)現(xiàn)了Servlet接口除了service的方法。
不過(guò)這種方法我們極少用
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方法
這里只簡(jiǎn)單講servlet的三種創(chuàng)建方式,關(guān)于更詳細(xì)的應(yīng)用我們后面再說(shuō)。
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);
    }

}

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多