Servlet2

@WebServlet(urlPatterns = "/aa")
public class JspService extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置键值对

        request.setAttribute("name","张三丰");

        //进行页面跳转
        request.getRequestDispatcher("/demojsp.jsp").forward(request,response);

    }
}

Servlet1

@WebServlet(urlPatterns = "/one")
public class ServletOne extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //以键值对存储数据
        request.setAttribute("name","风清扬");

        //请求跳转
        request.getRequestDispatcher("/two").forward(request,response);
    }
}

Servlet2 和 Servlet1之间的请求转发关键代码是

request.getRequestDispatcher("/two").forward(request,response);

关键部分是 "/two" 这个路径和Servlet与JSP之间的路径不同,这个路径是访问路径转发.

posted on 2020-06-28 14:50  LVowe  阅读(219)  评论(0编辑  收藏  举报