I worked on a project a couple of years ago where I had the need to
forward between different web apps. Thanks to Craig McClanahan's advise
I was able to extend the RequestProcessor to do this. This was done in
Struts 1.1. Basically I set up specific forward name prefixes to
indicate when to switch web applications. When the request dispatcher
identifies one of these special prefixes it gets the context of the web
app and forwards the request. I have attached some sample code.
FYI...Craig warned me that some app servers have a switch that prohibits
or allows context sharing, but I did not have this problem in WebSphere.

    /**
     * Forward or redirect to the specified destination, by the
specified
     * mechanism.  This method uses a ForwardConfig object instead of an
     * ActionForward.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param forward The ForwardConfig controlling where we go next
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
        protected void processForwardConfig(HttpServletRequest request,
                                        HttpServletResponse response,
                                        ForwardConfig forward)
                                        throws IOException,
ServletException
    {
                if (forward == null)
                {
                        return;
                }
                
                /* Check to see if we need to forward between web
applications.
                 * If so, we need to override how the Struts
RequestProcessor handles this.
                 * If not, just call the standard Struts
RequestProcessor's processForwardConfig
                 * method.
                 */
                if (forward.getName().startsWith("common_"))
                {
                        systemLogger.debug("Forwarding to common
backend");
                        String strContext ="/webApp2/common.do";
                        getServletContext().getContext
                                (strContext).getRequestDispatcher
        
("/common.do").forward(request,response); 
                        return;
                }
                else if (forward.getName().startsWith("channel_"))
                {
                        /* The parms object contains information about
where webApp2 should
                         * return control.
                         */
                         
                        Parameters      parms   =
(Parameters)request.getAttribute(FrameworkConstants.PARMS);
                        String  strChannel      =
(String)parms.get(FrameworkConstants.RETURN_CHANNEL);
                        String  strAction       =
(String)parms.get(FrameworkConstants.RETURN_ACTION);
                        String  strMethod       =
(String)parms.get(FrameworkConstants.RETURN_METHOD);
                        
        
getServletContext().getContext(strChannel).getRequestDispatcher
                                (strAction + "?method=" +
strMethod).forward(request,response); 
                        return;
                }
                else {
                        super.processForwardConfig(request, response,
forward);
                }
        }
        
Hope this helps,

Scott Mulligan


-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of Laurie Harper
Sent: Thursday, August 17, 2006 4:45 PM
To: user@struts.apache.org
Subject: Re: Forwards between webapps

Scott Van Wart wrote:
> Saeed, Rada wrote:
>> Forwards between webapps are not supported, cuz both have different 
>> contexts, this's what I got from running this :
>> <forward name="portal" path="/portal/main.do" redirect="true"
>> contextRelative="false" />
>> Is there any other way to achieve this forward between different web 
>> applications ?
>>   
> I think the class 'org.apache.struts.actions.SwitchAction' might do 
> the trick (struts-config.xml):
> 
>  <action path="/toModule" 
> type="org.apache.struts.actions.SwitchAction" />
> 
> And then in your JSP:
> 
>  <html:link page="/toModule.do?prefix=portal&page=/main.do">To
> Portal</html:link>
> 
> - Scott

SwitchAction helps for switching between Struts modules within the same
webapp, not for switching between different webapps, AFAIK. It's not
possible to *forward* from one webapp to another, but redirecting is
fine.

The OP's <forward> declaration specifies a non-context-relative
redirect, which should result in a client-side (browser) redirect to the
specified URL on the same host/port.

Saeed, what happens when you try to use that forward? How is it failing
for you? It looks like it should work to me.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to