关于获取应用basePath

来源:互联网 发布:sql查询怎么指定别名 编辑:程序博客网 时间:2024/06/05 12:36

request.getScheme() 等到的是协议名称,默认是http;
request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等;
request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等 ;
request.getContextPath() 返回应用程序的根目录。

 <%    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);%>

以上这段代码是 demo中每一个jsp页面中都包含的一段代码

其中 request.getContextPath() = /demo

basePath = http://localhost:8080

path = http://localhost:8080/demo/

filePath = http://localhost:8080/demo/resources/

用法:

如果在jsp界面中引用resources/images/文件夹下面的图片icon.png写法如下:

<img src="${filePath }images/icon.png" />或者

 <img src="${path}resources/images/icon.png" />

同理 如果在resources/css/文件夹下引用style.css写法如下:

<link href="${filePath} css/style.css" rel="stylesheet" type="text/css" />
<link href="${path} resources/css/style.css" rel="stylesheet" type="text/css" />