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-dispa
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 SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to