javaweb基础 01--JSP取得绝对路径应用

1.相关函数说明

* request.getScheme() 等到的是协议名称,默认是http

* request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等

* request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等 

* request.getContextPath()  返回站点的根目录

* request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径

 

2.用法示例

<%
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort();
    String path = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + request.getContextPath()
            + "/";
    String filePath=path+"resources/";
    session.setAttribute("path", path);
    session.setAttribute("basePath", basePath);
    session.setAttribute("filePath", filePath);
%>

以上这段代码的project name是drp5.1,可在tomcat下的webapp目录下找到该目录。
其中 request.getContextPath() = /drp5.1
basePath = http://localhost:8080
path = http://localhost:8080/drp5.1/
filePath = http://localhost:8080/drp5.1/resources/ 

 

posted @ 2017-07-17 11:29  PoleStar  阅读(205)  评论(0编辑  收藏  举报