File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Link from jsp to html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Link from jsp to html" Watch "Link from jsp to html" New topic

Link from jsp to html

John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
I have a jsp that has a link to a local static html file. The jsp and
html files are in the same directory. I'm using relative path to
reference the html from jsp (something like <a ref="file.html" /> .
When I access the jsp page directly, the link from jsp to html works fine.
When I access the jsp page indirectly through a servlet (the controller in
MVC), the link from jsp to html was broken so I have to use absolute
path (something like <a ref="/root/file.html" /> .
Can anyone explain it to me? Is there a way to make relative path link
work when using a controller?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33108

When you try to use the relative path, it uses the path of the servlet. You have to use an absolute path to get around the problem. (It's not a real absolute path though, because it's not related to your file system. It's absolute with respect to the /WEB-INF directory.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

To be more precise, in a web application all your paths should be relative to the context of the web application.
Let's say for example, that your web application is named xyz, and your file is in a folder named abc, then the best way to form the URL is:

which makes your code independent of the name of the web app.

there a way to make relative path link
work when using a controller?

Well, you could use the <base> tag to let the browser know where you want relative paths based from, but I personally prefer to use more precise paths.
P.S. You could also investigate the use of the JSTL <c:url> tag to format on-page URLs.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Link from jsp to html
jQuery in Action, 3rd edition