request.getContextPath()
今天终于明白了jsp中的request.getContextPath()是怎么回事了。
request.getContextPath() 返回站点的根目录
request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径
request.getScheme() 等到的是协议名称,默认是http
request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等
request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等
有一个例子来说明吧
有个web应用程序 名称就是demo
<%
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" />
OK!
request.getContextPath()相关推荐
- request.getcontextPath() 详解
<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但不用也可以,比如<a href="<%=request.g ...
- request.getContextPath()的功能
<% String contextPath = request.getContextPath(); // 得到项目的名字 com.chint.until.SessionInfo session ...
- String path = request.getContextPath()和String basePath = request.getScheme()
在JSP当中我们会用此代码来拼接路径,所以此语句是用来拼装当前网页的相对路径的. <% String path = request.getContextPath(); String basePa ...
- jsp中String path = request.getContextPath()的作用
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
- request.getContextPath()取不到值
request.getContextPath()得到项目的名字 如果项目为根目录,则得到一个"",即空的字条串, 如果项目为abc, <%=request.getContex ...
- String path = request.getContextPath(....拼装当前网页的相对路径
原文地址 <% String path = request.getContextPath(); String basePath = request.getScheme()+"://&q ...
- 关于request.getServletPath(),request.getContextPath()的总结
最近对于request中的几种"路径"有点混淆,查找网上资源都没有很好的总结,希望此文章能够帮助我理解一下这几种"路径". ++++++++++++++++++ ...
- String path = request.getContextPath
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...
- request.getcontextPath() 详解(转)
本文转自:http://blog.csdn.net/pengxuan/article/details/6604578 <%=request.getContextPath()%>是为了解决相 ...
- request.getContextPath详解
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
最新文章
- 转 java c++互传arraylist
- 解读 | 2019年10篇计算机视觉精选论文(中)
- 【动态规划专题】数字三角形模型
- 《修改代码的艺术》读书笔记一
- 多线程 调用 axis 报错_java笔记录(三、多线程)
- 尝试Office 2003 VSTO的开发、部署
- C语言 n*n矩阵求值及求逆矩阵
- JDK 14 调试神器了解一下?| 原力计划
- Scala类型系统(sudden thought)
- docker rabbitmq_RabbitMQ消息中间件快速入门
- 电机选型计算电机转动惯量、启动转矩和额定转速
- 编程中的脚手架是什么意思?
- 推挽电路介绍、设计与分析
- iPad/iPhone内存管理五之如何确定对象返回的值是autorelease
- Java面试题十篇基本问题,学完这些我就不信拿不到offer
- The connection to the server 192.168.229.131:6443 was refused - did you specify the right host or po
- 技术选型实战|BFE vs Nginx
- 全民K歌推流直播Web实践
- maven下载文件后缀lastedupdated
- android 多行 输入框,EditTextView Android中的多行字符串(Multiline String in EditTextView Android)...
热门文章
- 19_debug断点调试
- 9.14-9.18随笔之二
- 移动web:tab选项卡
- Zend_Db_Table-insert ()和zend_db_adapter::insert方法返回值不同
- Safe3通用远程溢出漏洞扫描系统
- 117 Python程序中的线程操作-开启多线程(threading.Thread)
- Spring read-only=true 只读事务的一些概念
- Python中参数iterable的意义
- 反射(一)动态加载类
- Python 城市列表