免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

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

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

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

//Servlet的生命周期:從Servlet被創(chuàng)建到Servlet被銷毀的過程//一次創(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.銷毀    執(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ì)客戶端響應(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被銷毀時(shí)執(zhí)行該方法    public void destroy() {        System.out.println("******destroy**********");    }//當(dāng)停止tomcat時(shí)也就銷毀的servlet。    public ServletConfig getServletConfig() {        return null;    }    public String getServletInfo() {        return null;    }}


創(chuàng)建servlet的第二種方法,繼承GenericServlet類,它實(shí)現(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);    }}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
spring boot web相關(guān)配置
JavaWeb框架系列-Servlet篇(2)
Servlet入門
Servlet –基礎(chǔ)(上)
Java web
Servlet/JSP深入詳解:基于Tomcat的Web開發(fā)--第二章01
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服