码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-17 12:26:29      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ring   server   根目录   code   style   javaweb   getc   tom   cat   

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/ 

 

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

标签:ring   server   根目录   code   style   javaweb   getc   tom   cat   

原文地址:http://www.cnblogs.com/polestar/p/7193486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!