aspose file tools*
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
Author

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?
Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Link from jsp to html
 
Similar Threads
confused with getRequestDispatcher()
maintain/organize jsp links w/o having to edit all the pages linking to
Info: Memorizing Deployment Descriptor
Complete Structure of Web.xml
JSP configured in web.xml