request 和 responsed.sendRedirect的url问题

1、request.getRequestURL()

返回的是完整的url,包括Http协议,端口号,工程名,servlet名
2、request.getRequestURI()

得到的是request URL的部分值,不包括Http协议,端口号

3、request.getContextPath() 
返回的是当前工程名的url

4、request.getServletPath() 

返回servlet名的url.


请求地址为:http://localhost:8888/Javaee2servlet/login.do

		System.out.println("getRequestURL:  "+request.getRequestURL());
		System.out.println("getRequestURI:  "+request.getRequestURI());
		System.out.println("getContextPath:  "+request.getContextPath());
		System.out.println("getServletPath:  "+request.getServletPath());
结果:

2.response.senRedirect(url)

当前request请求地址为:http://localhost:8888/Javaee2servlet/login.do

相对路径:url="lll.do".   response.senRedirect("lll.do")  重定向到http://localhost:8888/Javaee2servlet/lll.do

根路径:url="/lll.do"  response.senRedirect("/lll.do")  从tomcat根路径开始找,重定向到http://localhost:8888/lll.do

绝对路径:url="http://www.baidu.com"  重定向到http://www.baidu.com"

有时重定向根路径,相对路径(多一层或几层)都不能准确定向, 可用: response.sendRedirect("/"+request.getServletContext().getServletContextName()+"/lll.jsp");

 / 是根路径;request.getServletContext().getServletContextName() 为工程名。 "/lll.jsp"为跳转servlet。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值