Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP absolute paths

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

like image 820
sidlejinks Avatar asked Apr 02 '13 08:04

sidlejinks


1 Answers

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.

like image 122
Uooo Avatar answered Sep 18 '22 18:09

Uooo