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!
参考文章:http://www.cnblogs.com/yqskj/articles/2226401.html
如果喜欢作者的文章,请关注"写代码的猿"订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载.

request.getContextPath()的更多相关文章
- String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...
- request.getcontextPath() 详解
request.getcontextPath() 详解 文章分类:Java编程 <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但 ...
- requestscope.contextpath和<%=request.getContextPath()%>有何区别?(待解答)
问题1:requestscope.contextpath和<%=request.getContextPath()%>有何区别? 问题2:${requestscope.contextpath ...
- request.getContextPath获取绝对路径
request.getContextPath获取绝对路径 博客分类: 经验+注意 其他 request.getContextPath 项目需求:所有jsp页必须通过Action转发,不能直接在地址栏链 ...
- String path = request.getContextPath();这段什么用
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
- pageContext.request.contextPath 和 request.getContextPath()
作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的. El表达式的写法:${pageContext.request.contextPath} jsp的写法:<%=request.g ...
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
本随笔这是作为一个记录使用,以备后查.项目完成之后本地部署OK,本地Linux部署OK,都可以正常的访问,可是当我把它部署到服务器上面的时候,首页可以正常访问,可是当发出请求的时候却报错误了,说找不到 ...
- WEBROOT根目录 <%=request.getContextPath()%>
WEBROOT根目录 <%=request.getContextPath()%> == ${pageContext.request.contextPath}
- jsp中【<%=request.getContextPath()%>】项目路径
1 2 "request.getContextPath()的值是 "<%=request.getContextPath()%><br/> &q ...
随机推荐
- javascript 事件编程之事件(流,处理,对象,类型)
1. 事件处理 1.1. 绑定事件方式 1)行内绑定 语法: //最常用的使用方式 <元素 事件="事件处理程序"> 2)动态绑定 //结构+样式+行为分离的页面(ht ...
- 通过MSSQL分析器跟踪研究EM内部行为并解决identify列问题
今天有人问到MSSQL表里的IDENTITY字段,如何让它在复制原来数据到该表时,原来数据的IDENTITY字段不变,而新插入数据时,新插入的数据的IDENTITY依然增长,查了些资料,做了个实验,最 ...
- selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)
今天给大家分享一个selenium中经常会有人遇到的坑: selenium.common.exceptions.StaleElementReferenceException: Message: Ele ...
- Asp.Net Core NLog 将日志输出到数据库以及添加LayoutRenderer的支持
在这之前打算用Apache的Log4Net,但是发现其AdoNetAppender方法已经不存在了,无法使用配置文件直接输出到数据库了,因此我便改用了NLog框架. 一.对项目添加NLog 通过Nug ...
- 面试时怎样回答:你对原生ajax的理解
很多人跟我一样用习惯了jq封装好的$.ajax,但是面试时,原生ajax是很多面试官喜欢问的问题,今天再查资料,打算好好整理一下自己理解的原生ajax. 首先,jq的ajax:一般我常用的参数就是这些 ...
- Elasticsearch笔记三之版本控制和插件
版本控制 1:关系型数据库使用的是悲观锁,数据被读取后就被锁定其他的线程就无法对其进行修改. 2:ex使用的是乐观锁,数据被读取后其他程序还可以对其进行修改,而执行修改时发现此数据已经被修改则修改就会 ...
- P2P综述
原文参见:http://www.lotushy.com/?p=113 [TOC] 什么是P2P P2P全称是Peer-to-peer.P2P计算或P2P网络是一种分布式应用架构.它将任务或负载分发给P ...
- Appium+python的环境搭建!
我的电脑64位操作系统 下载并安装所需软件: 1.adb环境 2.下载(python-2.7.12.amd64.msi)并安装python 3.下载并解压adt,如(adt-bundle-window ...
- 利用css+原生js制作简易钟表
利用css+原生js制作简单的钟表.效果如下所示 实现该效果,分三大块:html.javascript.css html部分html部分比较简单,定义一个clock的div,内部有原点.时分秒针.日期 ...
- SDRAM读写状态解析
SDRAM的写状态流程 IDLE状态到WRITE状态 (1)在IDLE状态需要先给ACT命令激活某一行,此时处于Row Active状态. (2)在Row Active状态之后,给Write命令则会进 ...