Relative or absolute path on JSP?

Steve Gom picture Steve Gom · Aug 30, 2012 · Viewed 8.1k times · Source

I'm trying to make a homepage with a head and a tail jsp files in the root. Also I have the same _head and _tail jsp just for simple use at the future if I need it. In the head jsp, I added two js files, one css files in it.

I made another folder on the head jsp root named bbs. here, I made another file register jsp and this includes the head and the tail on the root. Structures are like these...

root
|---bbs
|  |-_head.jsp
|  |-_tail.jsp
|  |-register.jsp
|
|---css
|---js
|
|-head.jsp
|-tail.jsp
|-index.jsp

index.jsp is just a blank file with the character encoding and include file of head and tail. the file in bbs _head and _tail both are to include the root's head and tail. register.jsp is only for the body coding.

Now, the problem is that when I load the register.jsp, the css and the js all located in the root are now pointed in bbs. I want to try to make a relative path string variable in register.jsp so I could use the path variable in the head.jsp. any ideas?

Answer

John picture John · Aug 30, 2012

You could move the register.jsp to root as it uses head.jsp and tail.jsp instead of _head and _tail versions.

Else,

You could get the context path using ${pageContext.request.contextPath}

If you want to set a base path for all relative links so that you don't need to repeat ${pageContext.request.contextPath} in every relative link, use the <base> tag.

There is a good explanation here