1

What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)

So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Note: that's not a query string, that's an URI path fragment. A query string is the part after `?` in URL. – BalusC Feb 04 '13 at 00:40

1 Answers1

3

So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)

This is not the right way. The <c:url> should be applied on every single URL individually.

You'd better use

<c:set var="root" value="${pageContext.request.contextPath}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555