You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2000/08/02 22:16:42 UTC

Re: RESEND: Is it possible to send control back to 'ActionServlet' instead of to a JSP?

Shamdasani Nimmi-ANS004 wrote:

> Craig,
>
> Servlet1 & Servlet2 are 2 different servlets but in the same webapp. What I
> want to do is, depending on certain condition, do something like this:
>
> getServletConfig().getServletContext().getRequestDispatcher("/servlet/Servle
> t1.do").forward(request, response);
>
> or
>
> getServletConfig().getServletContext().getRequestDispatcher("/servlet/Servle
> t2.do").forward(request, response);
>
> I am not sure if this would work. Would the control pass thru
> 'ActionServlet' in both cases which in turn will send it to Servlet1 or
> Servlet2?
>

The code lines you describe above are in fact how you would do a forward from
one servlet to the other.  And, if you have "*.do" mapped to the Action servlet,
that is where the request will go (I had not caught on to that subtlety when I
read your earlier question).  The challenge then becomes, how do you actually
call the "real" servlet1 or servlet2?

To do that, you've got at least a couple of options:

* Define an additional <servlet-mapping> for each
  of those servlets, and forward to that path (or, in
  Struts, return an ActionForward that points at it).

* Use the ServletContext.getNamedDispatcher() to
  acquire a request dispatcher for a servlet by its
  name instead of by its path.  Then, call forward
  on this request dispatcher and return null from your
  action class (because the response has already
  been created).

>
> Also could one specify the a servlet in the 'forward path' instead of a JSP
> in 'action.xml'?
>

The simplest way is by using "/Servlet1.do" instead of "/mypage.jsp" in the
definition of a <Forward> element.  From the perspective of the servlet
container, it does not care who actually responds to the request -- only what
the path is.

>
> -Nimmi

Craig


>
> -----Original Message-----
> From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
> Sent: Wednesday, August 02, 2000 11:54 AM
> To: struts-user@jakarta.apache.org
> Subject: Re: RESEND: Is it possible to send control back to
> 'ActionServlet' instead of to a JSP?
>
> Shamdasani Nimmi-ANS004 wrote:
>
> > Hi,
> >
> > I have a situation where from my 'LogonServlet',  depending on certain
> > conditions, I want to either:
> >
> > Go to "Servlet1" which does some processing and forwards control to JSP1.
> >
> > or
> >
> > Go to "Servlet2" which does some processing and forwards control to JSP2.
> >
> > Since everything I feel should pass through the controller servlet, how do
> I
> > send control from "LogonServlet" to go back to the 'ActionServlet' with
> all
> > the parameters, e.g., the name of the next servlet, etc.
> >
>
> Are "servlet1" and "servlet2" actually separate servlets?  If so, you can
> use
> RequestDispatcher.forward() to forward to them just like you can to a JSP
> page
> (as long as they are in the same webapp--otherwise you will need to redirect
> and
> pass the parameters yourself somehow).
>
> > Nimmi Shamdasani
>
> Craig