/**
* 声明:
* 下面程序中的'/',
* 指的是,如果是转发,表示:getRequestDispatcher(String path)方法中,的path开头的'/'
* 如果是重定向,表示:sendRedirect(String location)方法中,的location开头的'/'
*/
(1)forward转发
// 转发后面的代码,会继续执行,但不会发送给客户端
// request.getContextPath()返回的是 /项目名
/**
* '/'表示,项目根路径 即 http://localhost:8080/项目名/
* 那么,它就跟当前的,路径无关了
* 当,使用转向后,它,要转向的页面,如果,还要转向别的路径的话,
* 就要考虑到它在web.xml中的虚路径,否则就会出错。
* 但,有一种方法,可以解决:再转向时,用 request.getContextPath()来转向
*/
request.getRequestDispatcher("/mgr/test_result.jsp").forward(request, response); // 方式一
this.getServletContext().getRequestDispatcher("/mgr/test_result.jsp").forward(request, response); // 方式二
this.getServletConfig().getServletContext().getRequestDispatcher("/mgr/test_result.jsp").forward(request, response); // 方式三
(2)redirect重定向
// 重定向后面的代码,会继续执行,并会发送给客户端
// request.getContextPath()返回的是 /项目名
/**
* 对于,重定向,可以使用 request.getContextPath()来,获取 '/项目名'
*
* '/'表示,域名根路径 即 http://localhost:8080/
*/
response.sendRedirect(request.getContextPath() + "/mgr/user_edit.jsp"); // 方式一
response.sendRedirect("/项目名/mgr/user_edit.jsp"); // 方式二