You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by behofmann <be...@gmail.com> on 2009/03/29 15:32:24 UTC

Forwarding from one Context to another

In Tomcat 6.0.18, isn't it possible to forward a request/response from one
<Context> to another?  Is there some special method/code to accomplish this?

Thanks,

Brandon
-- 
View this message in context: http://www.nabble.com/Forwarding-from-one-Context-to-another-tp22767662p22767662.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Forwarding from one Context to another

Posted by Gregor Schneider <rc...@googlemail.com>.
Brandon,

within the Context-definition of the COntext *from* where you want to
forward, you'll have to specify

<Context path="[Pfad]" crossContext="true" ... />

Forwarding works like this:

 ServletContext otherContext = servletContext.getContext("/othercontext");
 // The context may be null if the application server does not permit
cross-context access.
 if (otherContext != null) {
     ...
 }

HTH

Gregor
-- 
just because your paranoid, doesn't mean they're not after you...
gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
gpgp-key available
@ http://pgpkeys.pca.dfn.de:11371
@ http://pgp.mit.edu:11371/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Forwarding from one Context to another

Posted by behofmann <be...@gmail.com>.
Chuck,

I made some modifications to my relative paths and everything is now
working.  Thanks again for your help.

Brandon


Caldarale, Charles R wrote:
> 
>> From: behofmann [mailto:behofmann@gmail.com]
>> Subject: Re: Forwarding from one Context to another
>> 
>> The complete path should be /dev/jsp/index.jsp.  Correct?
> 
> I believe so.
> 
>> Unfortunately, I get a blank page in my browser and the page properties
>> show /filter/jsp/index.jsp which is the original <Context> path.
> 
> Remember that the client (browser) is not involved in a forward (it would
> participate in a redirect).  You likely have something else going on in
> /dev/jsp/index.jsp using relative URLs.  Since the last URL the browser
> was aware of was for the /filter webapp, any relative links passed to the
> browser will be evaluated based on that, not /dev.
> 
>> If I change the following to:
>> 
>> RequestDispatcher rd = appContext.getRequestDispatcher(
>> "/dev/jsp/index.jsp"
>> );
> 
> That's definitely wrong, since the argument to getRequestDispatcher() is
> relative to the ServletContext it's called with.
> 
>> the browser trys to render /dev/dev/jsp/index.jsp.
> 
> Which is what I would expect.  In this case, it's clear you did get the
> proper ServletContext for the /dev webapp.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Forwarding-from-one-Context-to-another-tp22767662p22789363.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Forwarding from one Context to another

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: behofmann [mailto:behofmann@gmail.com]
> Subject: Re: Forwarding from one Context to another
> 
> The complete path should be /dev/jsp/index.jsp.  Correct?

I believe so.

> Unfortunately, I get a blank page in my browser and the page properties
> show /filter/jsp/index.jsp which is the original <Context> path.

Remember that the client (browser) is not involved in a forward (it would participate in a redirect).  You likely have something else going on in /dev/jsp/index.jsp using relative URLs.  Since the last URL the browser was aware of was for the /filter webapp, any relative links passed to the browser will be evaluated based on that, not /dev.

> If I change the following to:
> 
> RequestDispatcher rd = appContext.getRequestDispatcher(
> "/dev/jsp/index.jsp"
> );

That's definitely wrong, since the argument to getRequestDispatcher() is relative to the ServletContext it's called with.

> the browser trys to render /dev/dev/jsp/index.jsp.

Which is what I would expect.  In this case, it's clear you did get the proper ServletContext for the /dev webapp.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: Forwarding from one Context to another

Posted by behofmann <be...@gmail.com>.
Chuck,

Thank you again for your help.  Since I'm new at this, please forgive me if
my questions seem elementary.  The following code now works:

ServletContext appContext = getServletContext().getContext( "/dev" );

      if ( appContext != null )
      {
         RequestDispatcher rd = appContext.getRequestDispatcher(
"/jsp/index.jsp" );
         rd.forward( request, response );
      }

If I understand the above correctly, the ServletContext sets the new
<Context> to /dev and the RequestDispatcher appends "/jsp/index.jsp" to the
new path.  The complete path should be /dev/jsp/index.jsp.  Correct?

Unfortunately, I get a blank page in my browser and the page properties show
/filter/jsp/index.jsp which is the original <Context> path.  Am I still
doing something wrong?  I've been reading the Servlet API, but I don't
understand how the pathing resolves.  I also removed the crossContext="true"
from the new target <Context>.

If I change the following to:

RequestDispatcher rd = appContext.getRequestDispatcher( "/dev/jsp/index.jsp"
);

the browser trys to render /dev/dev/jsp/index.jsp.

Thanks for your patience,

Brandon

Hassan Schroeder-2 wrote:
> 
> On Sun, Mar 29, 2009 at 5:52 PM, Caldarale, Charles R
> <Ch...@unisys.com> wrote:
> 
>>> ServletContext appContext =
>>> this.getServletConfig().getServletContext().getContext( "/dev" );
>>
>> You certainly don't need "this" in the above; nor is getServletConfig()
>> necessary, if the code is in a class that extends HttpServlet.  Doing so
>> just adds unnecessary overhead.
> 
> Absolutely correct -- serves me right for grabbing something out of a
> bucket full of ancient experiments.
> 
> I should cut down on the context switching between ruby/rails/rspec
> and java/servlets -- getting a slight case of whiplash  :-)
> 
> Thanks for setting that straight!
> 
> -- 
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Forwarding-from-one-Context-to-another-tp22767662p22775380.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Forwarding from one Context to another

Posted by Hassan Schroeder <ha...@gmail.com>.
On Sun, Mar 29, 2009 at 5:52 PM, Caldarale, Charles R
<Ch...@unisys.com> wrote:

>> ServletContext appContext =
>> this.getServletConfig().getServletContext().getContext( "/dev" );
>
> You certainly don't need "this" in the above; nor is getServletConfig() necessary, if the code is in a class that extends HttpServlet.  Doing so just adds unnecessary overhead.

Absolutely correct -- serves me right for grabbing something out of a
bucket full of ancient experiments.

I should cut down on the context switching between ruby/rails/rspec
and java/servlets -- getting a slight case of whiplash  :-)

Thanks for setting that straight!

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Forwarding from one Context to another

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Hassan Schroeder [mailto:hassan.schroeder@gmail.com]
> Subject: Re: Forwarding from one Context to another
> 
> ServletContext appContext =
> this.getServletConfig().getServletContext().getContext( "/dev" );

You certainly don't need "this" in the above; nor is getServletConfig() necessary, if the code is in a class that extends HttpServlet.  Doing so just adds unnecessary overhead.

It gets more complicated if the code is in a filter; in this case the init() method of the filter must hang onto the FilterConfig object passed into it, and use that object to retrieve the current ServletContext.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Forwarding from one Context to another

Posted by Hassan Schroeder <ha...@gmail.com>.
On Sun, Mar 29, 2009 at 4:34 PM, behofmann <be...@gmail.com> wrote:
>
> When trying to forward my .jsp to another <Context> in Tomcat 6.0.18 using
> the following code, I get the following error:
>
>      ServletContext appContext = ServletContext.getContext( "/dev" );

Try:

ServletContext appContext =
this.getServletConfig().getServletContext().getContext( "/dev" );

HTH,
-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Forwarding from one Context to another

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: behofmann [mailto:behofmann@gmail.com]
> Subject: Re: Forwarding from one Context to another
> 
> When trying to forward my .jsp to another <Context> in Tomcat 
> 6.0.18 using the following code, I get the following error:
>
>       ServletContext appContext = ServletContext.getContext( "/dev" );
>
> Error ==> non-static method getContext(java.lang.String) cannot be
> referenced from a static context

That message seems pretty self-explanatory: you're trying to call getContext() as a static method when it isn't static; you must use an instance of ServletContext.

> The following code compiles, but only forwards my new <Context> (/dev)
> appended to the SAME CONTEXT (/filter) as /filter/dev.
>
>       ServletContext appContext = getServletContext();

Which is what it's defined to do: you called getServletContext() with no arguments, so it returns the current ServletContext; you need to read the Servlet API spec:
http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/index.html

If you want to reference the /dev webapp, code it like this:

    ServletContext appContext = getServletContext().getContext("/dev");

The above assumes you're making the call from a class that extends HttpServlet.

> I already added crossContext="true" to context.xml for both the
> original and new <Context>.

It won't hurt in the target <Context>, but it does nothing useful for you.  It only counts for the <Context> from which the forward originates.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Forwarding from one Context to another

Posted by behofmann <be...@gmail.com>.
When trying to forward my .jsp to another <Context> in Tomcat 6.0.18 using
the following code, I get the following error:

      ServletContext appContext = ServletContext.getContext( "/dev" );

      if ( appContext != null )
      {
         RequestDispatcher rd = appContext.getRequestDispatcher(
contextPathFilter );
         rd.forward( request, response );
      }

Error ==> non-static method getContext(java.lang.String) cannot be
referenced from a static context


The following code compiles, but only forwards my new <Context> (/dev)
appended to the SAME CONTEXT (/filter) as /filter/dev.

      ServletContext appContext = getServletContext();

      if ( appContext != null )
      {
         RequestDispatcher rd = appContext.getRequestDispatcher(
contextPathFilter );
          rd.forward( request, response );
      }

As I said before, my goal is to forward my request/response from one
<Context> to another new <Context>.  Does someone have an example how to do
this?  I already added crossContext="true" to context.xml for both the
original and new <Context>.

Thanks,

Brandon


behofmann wrote:
> 
> In Tomcat 6.0.18, isn't it possible to forward a request/response from one
> <Context> to another?  Is there some special method/code to accomplish
> this?
> 
> Thanks,
> 
> Brandon
> 

-- 
View this message in context: http://www.nabble.com/Forwarding-from-one-Context-to-another-tp22767662p22774166.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Forwarding from one Context to another

Posted by behofmann <be...@gmail.com>.
Gregor,

Thank you for the additional information.  I will try the technique we
discussed and post back if I have further questions.

Thanks,

Brandon


Gregor Schneider wrote:
> 
> Brandon,
> 
> please reply to the list so that other ppl also benefit from this.
> 
> On Sun, Mar 29, 2009 at 3:22 PM,  <be...@gmail.com> wrote:
>>
>> Thanks for the quick reply! After using the approach you mentioned, I
>> would then use the RequestDispatcher to complete the forward to the new
>> context. Correct?
>>
> 
> Haven't done that myself, but as I understand it, yes.
> It's important that you use the dispatcher from the context you're
> forwarding to - in the example above that's the otherContext:
> 
> RequestDispatcher rd = otherContext.getRequestDispatcher(....)
> 
> Rgds
> 
> Gregor
> -- 
> just because your paranoid, doesn't mean they're not after you...
> gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
> gpgp-key available
> @ http://pgpkeys.pca.dfn.de:11371
> @ http://pgp.mit.edu:11371/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Forwarding-from-one-Context-to-another-tp22767662p22771112.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org