6 談一談Servlet與HttpServlet

1 創建一個類實現Servlet

package com.duowei.servlet;

import javax.servlet.*;
import javax.swing.*;
import java.io.IOException;

public class TestServlet implements Servlet {

    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init......");
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service......");
    }

    public String getServletInfo() {
        return null;
    }

    public void destroy() {
        System.out.println("destroy......");
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.duowei.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/helloServlet</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>helloHttpServlet</servlet-name>
    <servlet-class>com.duowei.servlet.TestHttpServlet</servlet-class>
  </servlet>

</web-app >


啓動Tomcat看一下init、service、destroy什麼時候執行
我們發現 init 在tomcat 啓動時候執行,destroy在tomcat結束運行時執行,service在每次訪問servlet時候執行。
試一下,多次訪問servlet,只執行service,不會再執行init了,可見Servlet是單例模式

2 創建一個類繼承HttpServlet

package com.duowei.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        System.out.println("TestHttpServlet.................doGet");
    }

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

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

註冊servlet

  <servlet>
    <servlet-name>helloHttpServlet</servlet-name>
    <servlet-class>com.duowei.servlet.TestHttpServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>helloHttpServlet</servlet-name>
    <url-pattern>/helloHttpServlet</url-pattern>
  </servlet-mapping>

訪問以下TestHttpServlet
在這裏插入圖片描述
發現輸出
TestHttpServlet…doGet
TestHttpServlet…service
爲什麼先輸出doGet再輸出service

  • 因爲我這裏重寫了service方法,所以訪問TestHttpServlet時候會執行我重寫的service方法
  • 另外,我在service中調用了super.service(req, resp);那麼在訪問TestHttpServlet的時候,輸出System.out.println(“TestHttpServlet…service”);前會執行父類的service方法
  • 我們看一下父類的service方法,也就是HttpServlet會幫我們判斷是GET請求方式的時候,調用doGet方法,所以先打印了TestHttpServlet…doGet;後打印了TestHttpServlet…service
    在這裏插入圖片描述

上述所說是爲了讓我們瞭解

  1. 瀏覽器通過URL訪問Tomcat;
  2. 創建request請求對象,response響應對象;
  3. Tomcat根據URL找到HttpServlet的類路徑,創建HttpServlet實例;
  4. HttpServlet實例調用service方法,並判斷GET和POST去執行doGet或者doPost,以參數形式傳入request,response;
  5. 返回response對象
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章