【Servlet】浅谈一下Servlet的继承关系。

CSDN话题挑战赛第2期
参赛话题:学习笔记

在这里插入图片描述

在这里插入图片描述


一、javax.servlet.Servlet接口

介绍

javax.servlet.Servlet类是一个接口,我们可以来看一下Servler接口的源码:

package javax.servlet;

import java.io.IOException;

public interface Servlet {
    void init(ServletConfig var1) throws ServletException;

    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}

接口中的方法都是没有代码快的抽象方法,而javax.servlet.Servlet接口中比较核心的抽象方法有:

  • void init();
  • void service();
  • void destroy();

二、javax.servlet.GenericServlet抽象类

javax.servlet.GenericServlet实现了接口Servlet,也重写了其中的方法,但为什么是抽象类呢?道理很简单,我们来看一下源码就不难理解原因了:

源码片段

javax.servlet.GenericServlet实现了接口Servlet

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
}

void destroy()方法被重写,但实际上并没有增添任何内容及功能。

public void destroy() {
    }

void init()方法被重写,这是Servlet给请求初始化时用到的方法

public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }


void service()方法被重写,但是service方法依旧是抽象方法,不存在代码块,代码块中存在这样的抽象方法,所以javax.servlet.GenericServlet是实现了Servlet接口的抽象类。

虽然service()方法依旧是没有被实现,传入的有两个参数,一个是请求对象:ServletRequest;一个是响应对象:ServletResponse

public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

三、javax.servlet.http.HttpServlet抽象子类

javax.servlet.http.HttpServlet继承了javax.servlet.GenericServlet抽象类,因而是其抽象子类,而在HttpServlet中,实现了service()抽象方法,让我们通过源码,看一下其功能吧:

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        if (method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if (lastModified == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader("If-Modified-Since");
                } catch (IllegalArgumentException var9) {
                    ifModifiedSince = -1L;
                }

                if (ifModifiedSince < lastModified / 1000L * 1000L) {
                    this.maybeSetLastModified(resp, lastModified);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if (method.equals("HEAD")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if (method.equals("POST")) {
            this.doPost(req, resp);
        } else if (method.equals("PUT")) {
            this.doPut(req, resp);
        } else if (method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if (method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if (method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }


service()方法首先会通过请求对象调用getMethod()方法来获取请求方式;

获取到请求方式后,就是各种判断语句,方法会根据不同的情况,调用与其对应的do方法,而各种do方法的实现内容基本一致,我们以请求方式为post时会调用的doPost()为例子展示一下:

doPost():

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = lStrings.getString("http.method_post_not_supported");
        this.sendMethodNotAllowed(req, resp, msg);
    }

private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse resp, String msg) throws IOException {
        String protocol = req.getProtocol();
        if (protocol.length() != 0 && !protocol.endsWith("0.9") && !protocol.endsWith("1.0")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

当我们的请求方式与调用的do方法不对应时,会报出405错误,这代表请求方式不支持。也就是说,在我们需要给服务器发起请求时,需要重写对应的do方法,不然就会出现错误。


总结

  • 继承关系:Servlet -> GenericServlet -> HttpServlet
  • Servlet中的核心方法 : init(); service(); destroy();
  • 当有请求时,service方法会自动相应(实际是tomcat容器调用的)
  • 在HttpServlet中会分析请求的方式、然后调用对应do方法。
  • 我们在新建Servlet时,需要先考虑请求方式,从而决定重写哪个do方法

在这里插入图片描述

  • 28
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.29.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值