@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/js");//转发的请求路径 requestDispatcher.forward(req,resp);//调用forword方法实现请求转发 }
合为一句
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); servletContext.getRequestDispatcher("/js").forward(req,resp); }
设置webapp映射
运行测试
页面内容显示为/js页面内容 但是url路径依旧是/fs
转发,路径不变,页面变
请求转发会有可能导致数据重复提交
转发是站内请求,重定向是站外请求
重定向两次请求,转发一次请求