String path="/index.jsp";//这是当前应用中一个绝对路径的url
servlet:
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(path);
jsp:
RequestDispatcher dispatcher=application.getRequestDispatcher(path);
all:
dispatcher.forward();//转到path这个页面(不可以在这之前或之后有其它输出)
dispatcher.include();//向浏览器输出path这个页面的执行结果(可以在这之前或之后有其它输出)
1、ServletRequest.getRequestDispatcher(String path)
path可是绝对路径也可以是相对路径
2、ServletContext.getRequestDispatcher(String path)
path必须以"/"开头,代表context root
3、另一个方法 ServletContext.getNameDispatcher(String name)
参数并不是路径,而是其名称,如果有多个Servlet名称一样的,在web.xml进行配置区别
4、以上方法回传一个RequestDispatcher对象,接着forward()或include()
5、forward()和include()区别在于include()方法将HTTP请求转送给其他Servlet或jsp后,这个Servlet或jsp虽然可以处理请求,但是主导权还是原来的Servlet或jsp,就是被调用的Servlet或jsp如果产生任何HTTP回应,将会并入原来的HttpResponse对象