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

本文介绍了如何使用Java Servlet API中的request对象来获取服务器的基本信息,包括协议名称、服务器名称、端口号、站点根目录及项目的实际物理路径,并通过示例代码展示了这些方法的具体应用。

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/ 

 

转载于:https://www.cnblogs.com/polestar/p/7193486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值