"Murphy, Todd" wrote:
>
> After looking at the archives and thumbing through some books, I'm afraid I
> know the answer to this question but I'd like to ask it just to make sure.
> Is there any way using relative pathing that I can forward to another
> servlet or JSP that is on the same host but not in the same context as my
> original servlet?
>
> For example, within http://hostname/application/servlet/MyServlet I want to
> use
>
>         getServletContext().getRequestDispatcher( myPath ).forward( req, res
> );
>
> to forward to a login servlet that has the URL
> http://hostname/login/servlet/LoginServlet.  The ServletContext is
> http://hostname/application, so is there any value that I can plug in for
> myPath that will allow me to use forward to get to the LoginServlet?  I
> understand that I can use sendRedirect(), but prefer not to since I do not
> want the URL displayed in the browser to change.  Any suggestions as to how
> I might address this?

According to the Servlet spec, you can get access to another ServletContext
than the one you're running in with:

  getServletContext().getContext("/othercontext");

So, something like this should work for your example:

  ServletContext loginCtx = getServletContext().getContext("/login");
  RequestDispatcher rd =
    loginCtx.getRequestDispatcher("/servlet/LoginServlet");
  rd.forward(request, response);

Note, however, that the container may not allow you to get access to
the other context due to security constraints. If so, it will return
null instead. How to declare context security constraints is vendor
dependent.

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
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