博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拼装当前网页的相对路径
阅读量:4605 次
发布时间:2019-06-09

本文共 500 字,大约阅读时间需要 1 分钟。

先放代码:

String path = request.getContextPath();
String basePath = request.getScheme() + “://”
+ request.getServerName() + “:” + request.getServerPort()
+ path + “/”;

下面解释一下代码的作用:

request.getContextPath();

返回项目的名字(应用的名字),如果项目为根目录,则得到一个”“,即空的字条串。
request.getScheme();
返回当前页面使用的协议,比如: http
request.getServerName();
返回服务器的名字 比如:本机的话就是 localhost
request.getServerPort();
返回当前页面所在服务器的端口 比如: 8080

举个例子:

**.jsp

这种相对路径的好处就是可以避免路径错误

以上是个人理解,有说的不对的地方 欢迎指正补充。

转载于:https://www.cnblogs.com/codenoodles/p/6421211.html

你可能感兴趣的文章
动态 hover 使用变相使用
查看>>
[Vue-rx] Stream an API using RxJS into a Vue.js Template
查看>>
[Javascript] lodash: memoize() to improve the profermence
查看>>
[RxJS] Subject: an Observable and Observer hybrid
查看>>
赛马题(转)
查看>>
HDU 1907 (博弈) John
查看>>
JS中反斜杠和单双引号的配合使用效果
查看>>
一次项目中的错误
查看>>
ubuntu c++ 关机 重启 挂起 API
查看>>
Oracle Sqlplus Note
查看>>
设计模式之策略模式(Strategy Pattern)
查看>>
手写符合Promise/A+规范的Promise
查看>>
Python time和datetime模块
查看>>
JPA、JTA、XA相关索引
查看>>
查询语句的练习
查看>>
快速切题 sgu103. Traffic Lights 最短路 难度:1
查看>>
2010年5月11日日志
查看>>
产品需求文档(PRD)的写作
查看>>
网页高度计算方法
查看>>
Java EE的map
查看>>