You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Colin Wilson-Salt <co...@london.virgin.net> on 2001/09/14 18:26:32 UTC

Specification question.

I know this probably isn't the correct place to ask this, but I reckon you
guys are probably the most qualified to answer it:

Am I allowed to include and forward between servlet contexts?

i.e. Am I allowed to write a method like this:

private void include(ServletRequest request, ServletResponse response, 
                     String contextPath, String relativePath)
throws ServletException, IOException
{
    ServletContext context = getServletContext().getContext(contextPath);
    RequestDispatcher requestDispatcher =
        context.getRequestDispatcher(relativePath);
    requestDispatcher.include(request, response);
}

Because it kind of works. Though if I don't flush() first, things get kind
of screwed up.

If I am allowed to do this, what should I expect to get if I do stuff like
getServletContext() in the included servlet? It seems that I get the context
of the original servlet, not the included one. Which isn't very useful to
me.

If I'm _not_ allowed to do this, can someone please tell me so that I can
start looking for a different way to do this?

Cheers.

Re: Specification question.

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Sounds like a better place to ask would be TOMCAT-USER.

On Fri, 14 Sep 2001, Colin Wilson-Salt wrote:

> Date: Fri, 14 Sep 2001 17:26:32 +0100
> From: Colin Wilson-Salt <co...@london.virgin.net>
> Reply-To: tomcat-dev@jakarta.apache.org
> To: tomcat-dev@jakarta.apache.org
> Subject: Specification question.
>
> I know this probably isn't the correct place to ask this, but I reckon you
> guys are probably the most qualified to answer it:
>
> Am I allowed to include and forward between servlet contexts?
>
> i.e. Am I allowed to write a method like this:
>
> private void include(ServletRequest request, ServletResponse response,
>                      String contextPath, String relativePath)
> throws ServletException, IOException
> {
>     ServletContext context = getServletContext().getContext(contextPath);
>     RequestDispatcher requestDispatcher =
>         context.getRequestDispatcher(relativePath);
>     requestDispatcher.include(request, response);
> }
>

Well, it's legal Java code all right ... the "contextPath" string should
contain a server-relative path starting with a slash.

> Because it kind of works. Though if I don't flush() first, things get kind
> of screwed up.
>

It will work *if* the container is configured to allow it.  In a security
conscious environment, the server administrator might want to disable this
capability, which can be done as follows in Tomcat 4:

  <Context ... crossContext="false" ...>
    ...
  </Context>

> If I am allowed to do this, what should I expect to get if I do stuff like
> getServletContext() in the included servlet? It seems that I get the context
> of the original servlet, not the included one. Which isn't very useful to
> me.
>

A test case would be of use in debugging any problems.

> If I'm _not_ allowed to do this, can someone please tell me so that I can
> start looking for a different way to do this?
>

An alternative mechanism that works for both remote or local hosts would
be to perform a URLConnection to a URL that is in the foreign web app.
See the Networking Trail in the Java Language Tutorial
<http://java.sun.com/docs/books/tutorial/> for an example of this.

> Cheers.
>

Craig