Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Link from jsp to html

 
John King
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic