On Fri, 16 Aug 2002, Leslie Yu wrote:

> Date: Fri, 16 Aug 2002 09:22:45 -0700
> From: Leslie Yu <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED],
>      Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Re: Java Client & Struts
>
> Hi,
>
>     Thanks for your advice. Why I want to use Struts is that I want to make
> use of the command pattern implementation of Struts. The only deveriation of
> my application from Struts is that I do have to use java client for client
> tier. So I need to resolve the uncertainity in the client - server
> communication. Do you have any other suggestions for my situation? Thanks a
> lot.
>

There are lots of ways this kind of problem could be approached.  They all
start from the fundamental assumption that your Java based client knows
how to do two things:
* Request the particular "command" that it wants to execute
* Send the appropriate input data for that command

For Struts based things, it is natural to select the command based on the
input URL, using the action paths.  You see this happening already in
interactive Struts-based apps, where you might have an action path named
"/saveCustomer.do" that, literally, means "go DO the 'save customer'
command".

For the second half of the equation, Struts doesn't offer any particular
support.  However, consider the idea of creating an Action subclass that
had a protected method called extractData(HttpServletRequest) that would
deserialize the object included with the request.  The outline of such a
class might be:

  public class CommandAction extends Action {

    ...

    protected Object extractData(HttpServletRequest request) throws IOException {
      InputStream is = request.getInputStream();
      Object result = ... convert to an ObjectInputStream and deserialize ...
      return (result);
    }

  }

Now, you could write the action to do your "save the customer"  command
like this:

  public class SaveCustomer extends CommandAction {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        thros Exception {

      SaveCustomerData scd = (SaveCustomerData) extractData(request);
      ... perform the requested "save customer" operation ...
      ... create success or failure response ...
      return (null);

    }

  }

Notes on the above scenario:

* Your client Java code and the corresponding command action have to
  know the class name of the serialized data (SaveCustomerData in this
  example) for that command.

* I'm presuming that you can define the contents of the request from
  the client, and the response from the server, in terms of a single
  Java object that can be serialized.  That's not the only option, but
  it is by far the easiest to implement.

* Creating the response would typically be creating some serialized
  Java object that told the client whether the command was successful
  or not.  It could also be, for example, the looked up data in response
  to a database query.  Again, this is going to be specific to a
  particular command.

* The "return (null)" at the end tells the Struts controller that the
  response has already been generated, so there is no need to forward
  to a JSP page or other such resource to create the response.

There are lots of other ways to approach this (a sophisticated one would
be to subclass the Struts RequestProcessor class and deserialize the input
data into what looks like a form bean of the appropriate type, and then
take advantage of the validation that form beans can do), but this one is
probably the simplest.


> Best Regards,
> Leslie
>

Craig


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

Reply via email to