Question

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?

Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top