答案网址:https://stackoverflow.com/questions/15759549/jsp-absolute-paths

Could someone explain why absolute paths not recommended to use in JSP (e.g., IntelliJ IDEA show me a warning)?

jsp intellij-idea
shareimprove this question
asked Apr 2 '13 at 8:37
sidlejinks

279721

 
1  
Because IntelliJ is smart enough to realize that you should always use a path that's relative to the root of your web context. – duffymo Apr 2 '13 at 8:40
 
So there is no strict rule to use only relative or context-based paths? – sidlejinks Apr 2 '13 at 8:45
 
There are no rules, my friend. Write it any way you want and live with the consequences. – duffymo Apr 2 '13 at 9:15
add a comment

1 Answer

active oldest votes
up vote 19 down vote accepted

Consider the following code in your JSP:

<script src="/path/to/script.js" />

And you deploy your application on www.example.com in servlet context myContext, your script will be looked up by the browser in

www.example.com/path/to/script.js

However, the browser will not find the script. The URL where it can actually be found containts the servlet context as well as part of the URL:

www.example.com/myContext/path/to/script.js

So you should change the URL in your JSP to:

<script src="${pageContext.request.contextPath}/path/to/script.js" />

Then the context path is also available in the URL and everything will work fine.

shareimprove this answer
answered Apr 2 '13 at 9:08
Uooo

4,30752657

 
 
Thank you very much indeed for explanation! – sidlejinks Apr 2 '13 at 9:45
 
This didn't work at all for me. It just rendered files with the path looking something like: localhost:8080/profile/$%7BpageContext.request.context%7D/cs‌​s/profile/ – Matt Powell Jul 17 '13 at 20:36
 
@MattPowell seems like your expression ${...} did not get evaluated right. Some frameworks require to use #{...} instead, did you try that? – Uooo Jul 22 '13 at 5:46
 
@w4rumy Thanks. It turns out I was using an outdated version of JSP. Once I updated to the latest version, the syntax worked correctly. – Matt Powell Jul 22 '13 at 15:42
 
Probably we can just write path/to/script.js without any prepending slash or context variable, am I right? – ieXcept Apr 9 '16 at 18:17

absolute paths not recommended in jsps相关推荐

  1. absolute paths not recommended in jsps的解决办法

    <script src="/path/to/script.js" /> absolute paths not recommended in jsps 假设在servle ...

  2. windows下tomcat集群配置(两种方法)

    两种方法只是在配置上不同原理一样,因为apache2.X后其自身集成了mod_jk功能,相对于1.3版本,不需要再进行繁琐的worker.properties配置,配置过程大幅简化. 一.软件需求 操 ...

  3. node.js中模块_在Node.js中需要模块:您需要知道的一切

    node.js中模块 by Samer Buna 通过Samer Buna 在Node.js中需要模块:您需要知道的一切 (Requiring modules in Node.js: Everythi ...

  4. [转]ASP.NET 核心模块配置参考

    本文转自:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore ...

  5. 错误消息“禁止您没有访问此服务器上的权限/”(关闭)

    本文翻译自:Error message "Forbidden You don't have permission to access / on this server" [clos ...

  6. 史上最全面的Neo4j使用指南

    Neo4j图形数据库教程 Neo4j图形数据库教程 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章:安装 1.环境 2.下载 3.开启远程访问 4.测试 第三章:CQL 1 ...

  7. 【图数据库】史上超全面的Neo4j使用指南

    转自:https://cloud.tencent.com/developer/article/1336299 在这篇文章中: 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章 ...

  8. angular面试题及答案_关于最流行的Angular问题的StackOverflow上的48个答案

    angular面试题及答案 by Shlomi Levi 通过Shlomi Levi 关于最流行的Angular问题的StackOverflow上的48个答案 (48 answers on Stack ...

  9. python apache_Windows 配置 Apache Python CGI

    解压文件httpd-2.4.29-o102n-x64-vc14-r2.zip到任意盘符(这里解压到C盘),打开C:\httpd-2.4.29-o102n-x64-vc14-r2\Apache24\co ...

最新文章

  1. telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试
  2. 美国智能家居止步不前 原因是产品过于碎片化
  3. JavaScript中ajax如何不刷新,JavaScript基于Ajax实现不刷新在网页上动态显示文件内容...
  4. 没有主清单属性_原神:晴知的主C诺艾尔大型进阶攻略初版
  5. charles 简单使用
  6. 将一段区间的偶数分解为两个素数相加(Java)
  7. 数据源管理 | 关系型分库分表,列式库分布式计算
  8. python使用级数pi的近似值_JavaScript与Python计算pi的近似值运行时间对比
  9. 图解WIN7通过“磁盘管理”功能进行分区的详细过程
  10. 修复inspinia中popover的点击问题
  11. 车载激光扫描系统检校
  12. mysql 8.0开启远程访问
  13. 【暴躁版】Java如何用IO流读取一个文本,按行输出到控制台?
  14. 解决xdp计算ip头checksum报错
  15. 大数据技术之Flume —— (1)一文入门学习Flume
  16. 计算机网络协议,以太网帧格式
  17. 室内导航--机器视觉、ROS、Goseek(二)Ubuntu 20.04 LTS 准备
  18. Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
  19. centos6 trouble shooting
  20. 怎样备份iOS已安装、已下架App的IPA文件?

热门文章

  1. SQL学习-基础课程
  2. 51单片机中断系统实验
  3. 设计一个简单的[JS]文本摘要算法
  4. 摘要算法与数字签名和数字信封
  5. MD5 摘要算法应用详解
  6. 跨界合作,中国联通与中燃集团携手亮相中国燃气展
  7. 个人服务器的搭建(腾讯云服务器)
  8. 正整数如何分解质因数(C++)
  9. 【程序人生】干了三年程序员,我决定兼职送外卖
  10. 【学一点英飞凌】AutoSar-MCAL-Gtm-TOM模块