How about creating a "base" servlet that each servlet extends. The base
servlet can provide the common info functionality -
commonSaveMethod(HttpServletRequest req, HttpServletResponse res). Each
servlet that provides save functionality can extend the base servlet.
Hope this helps...
> -----Original Message-----
> From: Hunter, Roger [SMTP:[EMAIL PROTECTED]]
> Sent: Monday, July 10, 2000 10:12 AM
> To: [EMAIL PROTECTED]
> Subject: Servlet calling servlet (or nesting servlets).
>
> I need some help calling a servlet from a servlet.
> In several places throughout the system we need to capture common info
> from
> the user
> when they do a certain activity. For example there are many places that
> have
> a 'save' button.
> The save code in each servlet is appropriate only for that servlet, but we
> find we need to capture
> some common info from the user each time a 'save' button is pressed.
>
>
> My thought are to write a method that would capture this information and
> return the captured data. The call to this method could easily be put in
> the
> save logic in each servlet. The common information would be captured using
> a
> servlet. That servlet would need to interact with the user any number of
> times until any required data is entered. So now I call a helper method
> from
> the save logic which then calls the common servlet. When the helper method
> ends it of course returns to the original servlet.
>
> I need the common servlet to go into a normal servlet 'cycle' where I can
> check the data passed to from the user and decide whether to cycle back to
> interact with the user to enter more data or to return to where it was
> called from.
>
> The problem I have is that the common servlet runs straight through and
> returns to the method that called it. It does send out its HTML to the
> browser but it then just runs on. By the time the user is looking at the
> screen from the common servlet execution has returned all the way back to
> the originating servlet. If I add the ACTION attribute of the FORM
> statement
> with the URL of the common servlet the servlet will of course call itself
> and cycle. It then of course forgets the method that called it and cannot
> return to it.
>
>
> I feel like I am just missing something. Am I just barking up the wrong
> tree
> here? Can anyone give me some ideas on how they would accomplish what I
> need
> to do?
>
>
>
> Below are the two ways I have called the common servlet.
>
> 1.) Call getRequestDispatcher and then call the include method of the
> RequestDispatcher object. (From
> http://java.sun.com/docs/books/tutorial/servlets/communication/request-dis
> pa
> tcher.html)
>
>
> 2.) Using Attributes:
>
> Put
> public static String KEY = "ServletName"
> in the called servlet and put
> getServletContext().setAttribute(KEY, this)
> in the init() method of the called servlet.
>
> To call the common servlet I then do:
>
> Object o = myServletContext.getAttribute(commonServlet.KEY);
> commonServlet varObject = (commonServlet) o;
> varObject.doAction(request,res);
>
>
> This way seems a bit messy. The common servlet must be called once before
> it
> can be used to run init().
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Roger Hunter
> Centers for Disease Control and Prevention
> NCID/SRP/IRM
> (404) 639-4645
> [EMAIL PROTECTED]
>
> ==========================================================================
> =
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> of the message "signoff EJB-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".