Daniel Kirkdorffer wrote:

> I'm having trouble with a reference from one webapp to another.  Basically,
> when I  do
>
> RequestDispatcher rd = context.getRequestDispatcher("/xyz/MyPage.jsp");
>

That path you give to getRequestDispatcher(), like nearly every path in the servlet
API, is treated as context relative (i.e. relative to the context path), not
absolute for the entire server.

>
> while I'm in webapps/abc it looks for the file at webapps/abc/xyz/MyPage.jsp
> instead of at webapps/xyz/MyPage.jsp where I want it to go.  The code is
> used by various webapps, so I'd like to maintain it in the separate shared
> webapp.
>

What you really want to do is get a request dispatcher from another webapp's
servlet context.  One approach to this would be:

    ServletContext abcContext =
      getServletContext().getContext("/xyz");
    RequestDispatcher rd =
      abcContext.getRequestDispatcher("/MyPage.jsp");

However, you need to note a couple of things:

* The servlet container is not required to let you access a servlet context other
  than your own, so the first call above might return null.

* Sessions are not shared across web applications under any circumstances,
  so the page you forward to (or include) through this request dispatcher
  will *not* be able to see the same session attributes as where the page
  or servlet where this code is running.

When you've got pages shared between apps, you are better off setting up your build
environment to copy them from a common source repository into every app that needs
them.  With a build tool like Ant (<http://jakarta.apache.org/ant>) this is
incredibly easy to set up.  That way, you are still able to maintain the common
pages in a single place, but you can also use them in multiple apps without going
through all sorts of contortions.  The miniscule amount of memory you might save at
runtime (from having only one copy) is not worth all the hassles.

Also, your code does not break if the sysadmin decides to attach your app to a
different context path at some later point in time.

>
> What could I be doing wrong?  How do I get things to work from the same
> root?
>
> Thanks,
>
> Dan
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to