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.
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.
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.