dispatcher的forward和response.sendRedirect,他们都用来跳转jsp和servlet。但他们是不同的。
不同点在于
1:sendRedirect是重定向,这时候,上一个request带过来的参数会全部丢失。如要在请求里加新的参数,请在url后面跟上参数。例如response.sendRedirect('/ABC/jsp/login.jsp?name='+name);
forward是跳转,参数还保留在request里。
2:sendRedirect的根路径是相对于服务器而言,假设服务器根路径是http://localhost:8080/
那么response.sendRedirect('/ABC/jsp/login.jsp?');
就相当于跳到http://localhost:8080/ABC/jsp/login.jsp
forward的根路径是相对于项目的根路径而言
resquest.getRequestDispatcher('/ABC/jsp/login.jsp');
相当于跳到http://localhost:8080/ABC/ABC/jsp/login.jsp
注意,路径出错了,所以应该这样写
resquest.getRequestDispatcher('/jsp/login.jsp');
相当于跳到http://localhost:8080/ABC/jsp/login.jsp
顺
新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑
Copyright © 1996 - 2008 SINA Corporation, All Rights Reserved