Commands can be used with any Java application, but the means of sending the command from the client to the server varies. The example in this topic shows how you can send a command to a servlet over the HTTP protocol. The client implements the CommandTarget interface locally.
This example shows the structure of the client-side class; it implements the CommandTarget interface by implementing the executeCommand method.
... import java.io.*; import java.rmi.*; import com.ibm.websphere.command.*; public class ServletCommandTarget implements CommandTarget, Serializable { protected String hostName = "localhost"; public static void main(String args[]) throws Exception { .... } public TargetableCommand executeCommand(TargetableCommand command) throws CommandException { .... } public static final byte[] serialize(Serializable serializable) throws IOException { ... } public String getHostName() { ... } public void setHostName(String hostName) { ... } private static void showHelp() { ... } }
The main method in the client-side adapter constructs and intializes the CommandTarget object, as follows:
public static void main(String args[]) throws Exception { String hostName = InetAddress.getLocalHost().getHostName(); String fileName = "MyServletCommandTarget.ser"; // Parse the command line ... // Create and initialize the client-side CommandTarget adapter ServletCommandTarget servletCommandTarget = new ServletCommandTarget(); servletCommandTarget.setHostName(hostName); ... // Flush and close output streams ... }