Servlet体系结构

原文链接:https://www.bilibili.com/video/av56161554/?p=19

文章目录

常用Servlet的体系结构

如果我们要实现Servlet接口,就要实现Servlet的五个方法
Servlet体系结构

  • GenericServlet:将servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象。如果接口集成自GenericServlet类,只需要实现service
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/demo2")
public class ServletDemo2 extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.printf("aaaaaaaaaaaa");
    }
}

  • httpServlet:封装http协议,简化操作。
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.printf("doget");
    }


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

上一篇:HTTP协议


下一篇:javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception