getRequestURI()、getRequestURL()、getContextPath()、request.getServletPath()的区别

浏览器向tomcat发出http请求:http://localhost:8080/web项目名/servlet的url-pattern(与在login.jsp的form表单中的action=“&&&”,&&&的名称需要与servlet配置中的url-pattern一致,主要是产生映射)
假设web application的名称为test,需要在浏览器中输入请求路径:http://localhost:8080/test/login.jsp 或 http://localhost:8080/test/url-pattern

则执行下列代码后打印出如下结果:

1、System.out.println(request.getRequestURI());

    打印结果:/test/login.jsp

2、System.out.println(request.getRequestURL());

   打印结果:http://localhost:8080/test/login.jsp

3、 System.out.println(request.getContextPath());

   打印结果:/test

4、System.out.println(request.getServletPath());

 打印结果:/login.jsp
原文链接
https://blog.csdn.net/qq_36667885/article/details/93127029?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

request.getRequestURL() 返回全路径

request.getRequestURI() 返回除去host(域名或者ip)部分的路径

request.getContextPath() 返回工程名部分,如果工程映射为/,此处返回则为空

request.getServletPath() 返回除去host和工程名部分的路径

例如:

request.getRequestURL() http://localhost:8080/jqueryLearn/resources/request.jsp
request.getRequestURI() /jqueryLearn/resources/request.jsp
request.getContextPath()/jqueryLearn
request.getServletPath()/resources/request.jsp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值