Lung wrote:
>
> I have some problems with the getContext method.
>
> According to the servlet specification, the getContext(String path)
> method of class ServletContext
> returns allows servlet gain access to other context. And the
> getRequestDispatcher(String urlPath) returns a RequestDispatcher of the
> given urlPath, which is relative to current context root. But the
> document doesn't not state clear what is the context root. Do anyone
> know what the context root is?

Yes, a context root is the URI prefix mapped to a specific context,
or application using the JSP terminology. Say you have two separate
applications running in the same servlet container. You can then define
that all URIs starting with /app1 will be processed by the first application
and /app2 by the second application. /app1 and /app2 are the context roots.

Within an application, all paths are relative to the context root. So
if you ask for a RequestDispatcher, or uses <jsp:forward> in a JSP page,
with a URI like /mypage.jsp in app1, you're really asking for the same
page as you would get if you typed in /app1/mypage.jsp in the browser's
address field. By interpreting all paths relative to the context root
within an application, it's easy to move the application to another place
in the URI name space if needed.

> By now I am assuming that the current directory is the context root. So
> given the following path
>
> /member/info.jsp
>
> The following two should return the same requestDispatcher when used in
> JSP:
>
> getServletContext().getRequestDispatcher("/member/info.jsp");
>
> getServletContext().getContext("/member").getRequestDispatcher("/info.jsp");
>
> I tried the above in JRun but the second method returns null.

Based on the above, I'm sure you see why this doesn't work. The first
example, if executed in an application with the context root /app1,
will return an RD for /app1/member/info.jsp. The getContext() method takes
a context root URI argument and returns the ServletContext for that application.
Assuming you have an application mapped to /member, the final result of
this would be an RD for the /member/info.jsp URI.

Most likely you have not defined any special applications and therefore
use the default context, with the context root "" (empty string). If so,
the first example gives you an RD for the URI /member/info.jsp, but the
second fails with a NullPointerException since getContext("/member")
returns null (there's no application with context root /member).

All of this should be described in the Servlet 2.2 specification.

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

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