1、重定向实现思路:
2、重定向实现代码:
public class servletRequestDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context=this.getServletContext();
// RequestDispatcher requestDispatcher=context.getRequestDispatcher("/name");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/name").forward(req,resp);
}
}
3、查看效果:
访问root/demo4:
可以看到的是这里的url并没有跳转而是返回了root/name路径下的视图数据。