Karl Roberts wrote:

> Hi,
>
>     If your trying to do what I think that you are doing (ie have links to other
> JSP's in your App's Directory structure)
> Then why not use relative links?
>
> If you know for instance that the JSP being shown is at
>     http://Your-Server/APP-NAME/jsp-dir/bar.jsp
>
> and you want to link to foo.jsp which is at
>     http://Your-Server/APP-NAME/jsp-dir/foo.jsp
> then you could use a link like
>     <a href=./foo.jsp>  Link-to-Foo </a>
>

While Karl's suggestion to use relative links is right on, you have to watch out for
a couple of gotchas.  The one that always bites me is, "who is interpreting the
relative URL?"

In this particular case at hand (creating a hyperlihk), what you really need to do
is:

    <a href="foo.jsp">Link-to-Foo</a>

without the forward slash, because this relative link is resolved by the browser.
If you were using <jsp:include> or <jsp:forward> (which is resolved on the server
side), you would indeed need the slash.

>
> If you wanted a link to test.html which is at
>     http://Your-Server/APP-NAME/html-dir/test.html
> then you could use a link like
>     <a href=../html-dir/test.html>  Link-to-Test </a>
>
> As long as you don't try to go up past the document root of your web server then
> you should be O.K.
>
> Karl
>
> Ramkumar_Gopalaswamy wrote:
>
> > How can I avoid hard-coding application name in URL's displayed
> > in the output of a JSP?
> >
> > For instance, let's us say I have a <APP-NAME>/foo.jsp which needs to
> > present browser a link to <APP-NAME>/bar.jsp .
> > In foo.jsp, I can refer to bar.jsp as
> >    <a href=/APP-NAME/bar.jsp>  Link-to-Bar </a>
> > But there is a problem - if i change the APP-NAME tomorrow,
> > I will have to update all references to it.
> >
> > Instead of hard-coding <APP-NAME> as above, I am looking to do something
> > like:
> >
> > <a href=<%=application.getAppName()%>/bar.jsp>  Link-to-Bar </a>
>

For the record, request.getContextPath() will return you the "/APP-NAME" part of the
path when you are running under a servlet container compatible with the 2.2 API spec
(such as Jakarta Tomcat).  This can be very useful when you need to dynamically
construct absolute URLs for pages within your application.

> >
> > Is there any method on any implicit objects available to JSP that will
> > let me do something as above ?   Also, how would I do the same in servlet
> > code?
> >
> > Thanks in advance,
> >
> > /Ram
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to