コマンドは Java アプリケーションと共に使用できますが、 クライアントからサーバーへコマンドを送信する方法は異なります。このトピックの例では、 HTTP プロトコル上でコマンドをサーブレットに送信する方法を示します。 クライアントは CommandTarget インターフェースをローカルに実装します。
この例は、 クライアント・サイド・クラスの構造を示します。executeCommand メソッドをインプリメントすることによって、CommandTarget インターフェースをインプリメントします。
... 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() { ... } }
クライアント・サイド・アダプターの 主なメソッドは、以下のように、CommandTarget オブジェクトを構成および初期化します。
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 ... }