Servlet底层源码理解

当一个类继承HttpServlet,它首先调用service方法,通过参数转型调用HttpServlet的重载方法,降低与HTTP协议的耦合。在service方法内部,根据请求类型执行DoGet或DoPost等方法,进一步调用开发者自定义的处理方法。
摘要由CSDN通过智能技术生成

如果一个类继承了HttpServlet,那么他的实现过程是怎样的?

 我的理解:一个类继承了HttpServlet类之后,首先会去调用实现父类的 service(ServletRequest req,ServletResponse res)方法通               载的 service(HttpservletRequest,HttpServletResponse)方法,目的是减少与http协议之间产生的耦合性,然后在service方法中进行判断提交方式的Do Get 还是Do Post 或者是其他提交方式,之后再通过调用我们自己重写的do get 和do post方法执行。

public HttpServlet() {
    }

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

    protected long getLastModified(HttpServletRequest req) {
        return -1L;
    }

    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (DispatcherType.INCLUDE.equals(req.getDispatcherType())) {
            this.doGet(req, resp);
        } else {
            HttpServlet.NoBodyResponse response = new HttpServlet.NoBodyResponse(resp);
            this.doGet(req, response);
            if (req.isAsyncStarted()) {
                req.getAsyncContext().addListener(new HttpServlet.NoBodyAsyncContextListener(response));
  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值