Servlet 的生命周期

image-20211110180001737

GET和POST请求的分发

image-20211110180211712

通过继承 HttpServlet 实现 Servlet 程序

image-20211110185354662

Servlet 类的继承体系以及一些存储键值对的方式

image-20211110185503318

image-20211110185531214

image-20211110185549022

image-20211110185604862

image-20211110185616797

Servlet请求转发

public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数(办事的材料)查看
        String username = req.getParameter("username");
        System.out.println("在Servlet1(柜台1)中查看参数(材料):" + username);

        // 给材料 盖一个章,并传递到Servlet2(柜台 2)去查看
        req.setAttribute("key1","柜台1的章");

        // 问路:Servlet2(柜台 2)怎么走
        /**
         * 请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录<br/>
         *
         */
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
//        RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com");

        // 走向Sevlet2(柜台 2)
        requestDispatcher.forward(req,resp);

    }
}