国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

帶你快速上手Servlet

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:趙曉東-Nastu 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

一、Servlet與Tomcat的關(guān)系

(1)Tomcat是什么?

Tomcat其實(shí)是Web服務(wù)器和Servlet容器的結合體

(2)什么是Web服務(wù)器?

比如,我當前在杭州,你能否用自己的電腦訪(fǎng)問(wèn)我桌面上的一張圖片?恐怕不行,我們太習慣通過(guò)URL訪(fǎng)問(wèn)的一個(gè)網(wǎng)站、下載一部電影了。一個(gè)資源,如果沒(méi)有URL映射,那么外界幾乎很難訪(fǎng)問(wèn),而Web服務(wù)器的作用說(shuō)穿了就是:將某個(gè)主機上的資源映射為一個(gè)URL供外界訪(fǎng)問(wèn)

二、什么是Servlet

(1)什么是Servlet容器?

Servlet是運行在Web服務(wù)器或應用服務(wù)器上的程序。

Servlet容器,顧名思義里面存著(zhù)Servlet對象,我們?yōu)槭裁茨軌蛲ㄟ^(guò)Web服務(wù)器映射的URL訪(fǎng)問(wèn)資源?肯定需要寫(xiě)程序處理請求,主要3個(gè)過(guò)程:接受請求,處理請求,響應請求。

三、Servlet的類(lèi)結構

通過(guò)繼承HttpServlet實(shí)現Servlet接口

一般在實(shí)際項目開(kāi)發(fā)中,都是使用繼承HttpServlet類(lèi)的方式去實(shí)現Servlet程序

(1)編寫(xiě)一個(gè)類(lèi)去繼承HttpServlet類(lèi)

(2)根據業(yè)務(wù)需要重寫(xiě)doGet或doPost方法

(3)到web.xml中的配置servlet程序的訪(fǎng)問(wèn)地址

四、ServletConfig類(lèi)

ServletConfig代表的是當前Servlet在web.xml中的配置信息

 String getServletName(); ---獲取當前Servlet在web.xml中配置的名字
 
    ServletContext getServletContext();---獲取當前Servlet指定名稱(chēng)的初始化參數的值
 
    String getInitParameter(String var1);---獲取當前Servlet所有初始化參數的名字組成的枚舉
 
    Enumeration<String> getInitParameterNames();---獲取代表當前web應用的ServletContext對象

(1)作用:

1、可以獲取Servlet程序的別名Servlet-name的值

2、獲取初始化參數init-param

3、獲取ServletContext對象

 @Override
    public void init(ServletConfig servletConfig) throws ServletException {
//        1、可以獲取Servlet程序的別名Servlet-name的值
        System.out.println(servletConfig.getServletName());
//        2、獲取初始化參數init-param
        System.out.println(servletConfig.getInitParameter("username"));
//        3、獲取ServletContext對象
        System.out.println(servletConfig.getServletContext());
        System.out.println("2、執行初始化方法");
    }

五、ServletContext類(lèi)

(1)什么是ServletContext?

1、ServletContext是一個(gè)接口,它表示Servlet上下文對象。

2、一個(gè)Web工程,只有一個(gè)ServletContext對象實(shí)例。

3、ServletContext是一個(gè)域對象。

4、ServletContext是在web工程部署啟動(dòng)的時(shí)候創(chuàng )建,在web工程停止的時(shí)候銷(xiāo)毀。

什么是域對象?

域對象,是可以像Map一樣存取數據的對象,叫域對象。

這里的域指的是存取數據的操作范圍,整個(gè)web工程。

存數據 取數據 刪除數據

Map put() get() remove()

域對象 setAttribute() getAttribute() removeAttribute()

(2) ServletContext類(lèi)的四個(gè)作用

1、獲取web.xml中配置的上下文參數context-param

public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1、 獲取web.xml中配置上下文參數context-param
        ServletContext servletContext = getServletConfig().getServletContext();
        String username = servletContext.getInitParameter("username");
        System.out.println("context-param參數的username"+username);
 
    }
}

在web.xml中

<!--    context-param 是上下文參數(它是屬于整個(gè)web工程)-->
    <context-param>
        <param-name>username</param-name>
        <param-value>context</param-value>
    </context-param>

2、獲取當前的工程路徑,格式:/工程路徑

3、獲取工程部署后在服務(wù)器硬盤(pán)上的絕對路徑

(3)ServletContext像map一樣存取數據

public class ContextServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取ServletContext對象
        ServletContext context = getServletContext();
        System.out.println("保存之前:Context1 獲取key1的值是:"+context.getAttribute("key1"));
        context.setAttribute("key1","value1");
        System.out.println("Context1中獲取域數據key1的值是:"+context.getAttribute("key1"));
 
    }
}
保存之前:Context1 獲取key1的值是:null
Context1中獲取域數據key1的值是:value1
Context2中獲取域數據key1的值是:value1

六、Servlet的生命周期

public class HelloServlet implements Servlet {
    public HelloServlet() {
        System.out.println("1、執行構造器方法");
    }
 
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2、執行初始化方法");
    }
 
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
 
    //service方法是專(zhuān)門(mén)用來(lái)處理請求和響應的
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("3、hello servlet 被訪(fǎng)問(wèn)了");
    }
 
    @Override
    public String getServletInfo() {
        return null;
    }
 
    @Override
    public void destroy() {
        System.out.println(" 4、執行銷(xiāo)毀方法");
    }
}

執行的結果


1、執行構造器方法
2、執行初始化方法
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
3、hello servlet 被訪(fǎng)問(wèn)了
G:\softWareInstall\apache-tomcat-9.0.45\bin\catalina.bat stop
Using CATALINA_BASE:   "C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_Servlet"
Using CATALINA_HOME:   "G:\softWareInstall\apache-tomcat-9.0.45"
Using CATALINA_TMPDIR: "G:\softWareInstall\apache-tomcat-9.0.45\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.8.0_60"
Using CLASSPATH:       "G:\softWareInstall\apache-tomcat-9.0.45\bin\bootstrap.jar;G:\softWareInstall\apache-tomcat-9.0.45\bin\tomcat-juli.jar"
Using CATALINA_OPTS:   ""
03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.catalina.core.StandardServer.await 閫氳繃鍏抽棴绔彛鎺ユ敹鍒版湁鏁堢殑鍏抽棴鍛戒護銆傛鍦ㄥ仠姝㈡湇鍔″櫒瀹炰緥銆�
03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.coyote.AbstractProtocol.pause 鏆傚仠ProtocolHandler["http-nio-8080"]
03-May-2021 14:33:12.289 淇℃伅 [main] org.apache.catalina.core.StandardService.stopInternal 姝e湪鍋滄鏈嶅姟[Catalina]
 4、執行銷(xiāo)毀方法

(1)執行Servlet構造器方法

(2) 執行init初始化方法

第一、二步,是在第一次訪(fǎng)問(wèn)的時(shí)候創(chuàng )建Servlet程序會(huì )調用

(3)執行 Service方法

第三步、每次訪(fǎng)問(wèn)都會(huì )調用

(4)執行destroy銷(xiāo)毀方法

第四步:在web工程停止的時(shí)候調用

七、Get、Post

get、post請求都會(huì )走Service方法,那么怎么區分get、post請求

    //service方法是專(zhuān)門(mén)用來(lái)處理請求和響應的
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("3、hello servlet 被訪(fǎng)問(wèn)了");
        HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
        String method = httpServletRequest.getMethod();
        if ("Get".equals(method)){
            
        }
        if ("POST".equals(method)){
            
        }
    }

到此這篇關(guān)于帶你快速上手Servlet的文章就介紹到這了,更多相關(guān)Servlet詳解內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

天天看片视频免费观看| 国产又黄又爽胸又大免费视频 | HEYZO高无码国产精品| 99精品国产99久久久久久97| 国产精品无码久久久久成人影院| 中文字幕 亚洲精品 第1页|