Web_Servlet之间请求转发

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之间的路径不同,这个路径是访问路径转发.

Web_Servlet之间请求转发

上一篇:从引入到实例最详尽 vue.js引入vuex储存接口数据并调用的流程


下一篇:flume的几种conf配置