• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Link from jsp to html

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic