Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple relative path img  RSS feed

 
Jack Adams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
img src = "/image/myimg.jpg"
won't work because need context root

img src = "/currentContextRoot/image/myimg.jpg"

asp has ~ to get application (context root).

Is there a method to avoid having to specify context root. either jsp/java or html?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JSP 2.0:

${pageContext.request.contextPath}

prior to JSP 2.0:

<%= request.getContextPath() %>
 
Jack Adams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is considered "best practice" or is there a "better" way?

Note: JSP is pre 2.0 (old)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen a number of common practices:

1) In JSP 2.0, use of the JSTL <c:url> tag which handles all that for you is encouraged.

2) If not, the context path could be added to each URL as in:

<img src="${pageContext.request.contextPath}/images/pixel.gif"/>

or

<img src="<%= request.getContextPath() %>/images/pixel.gif"/>

3) Or, a <base> HTML tag is defined that sets the document "base" to the context path so that context-relative URLs can be used.
[ August 03, 2005: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!