String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
request.getScheme();//可以返回当前页面使用的协议,就是“http”
request.getServerName();//可以返回当前页面所在的服务器的名字,其实就是一般的localhost,如果是真的部署在服务器上那就是服务器
的地址
request.getServerPort;//返回的是当前页面所在的服务器使用的端口,一般情况下是80端口,(自己的机器测试的话就是8080)
request.getContextPath;//返回当前页面所在的应用程序的路径,
这四个拼装起来,就是当前应用的跟路径了
本文介绍如何使用Java Servlet API中的HttpServletRequest对象来获取当前应用的基本信息,包括协议、服务器名称、端口及应用程序路径,并通过这些信息构造出应用的根路径。
1842

被折叠的 条评论
为什么被折叠?



