⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carttestclient.java

📁 主要是对EJB的提供的组件功能进行了介绍。还包括了案例~ 里面提供EJB ppt 会话Bean教案例子 实体Bean教案例子 实体EJB关系教案例子 CMP教案.ppt 消息驱动Bean教
💻 JAVA
字号:
package cartproject;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class cartTestClient extends Object {  private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";  private static final int MAX_OUTPUT_LINE_LENGTH = 100;  private boolean logging = true;  private cartHome cartHomeObject = null;  private cart cartObject = null;  //Construct the EJB test client  public cartTestClient() {    initialize();  }  public void initialize() {    long startTime = 0;    if (logging) {      log("Initializing bean access.");      startTime = System.currentTimeMillis();    }    try {      //get naming context      Context context = getInitialContext();      //look up jndi name      Object ref = context.lookup("cart");      //look up jndi name and cast to Home interface      cartHomeObject = (cartHome) PortableRemoteObject.narrow(ref, cartHome.class);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded initializing local bean access through Local Home interface.");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed initializing bean access.");      }      e.printStackTrace();    }  }  private Context getInitialContext() throws Exception {    String url = "t3://zhaoqiang:7001";    String user = null;    String password = null;    Properties properties = null;    try {      properties = new Properties();      properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");      properties.put(Context.PROVIDER_URL, url);      if (user != null) {        properties.put(Context.SECURITY_PRINCIPAL, user);        properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);      }      return new InitialContext(properties);    }    catch(Exception e) {      log("Unable to connect to WebLogic server at " + url);      log("Please make sure that the server is running.");      throw e;    }  }  //----------------------------------------------------------------------------  // Methods that use Home interface methods to generate a Remote interface reference  //----------------------------------------------------------------------------  public cart create(String _cardHolderName, String _creditCardNumber) {    long startTime = 0;    if (logging) {      log("Calling create(" + _cardHolderName + ", " + _creditCardNumber + ")");      startTime = System.currentTimeMillis();    }    try {      cartObject = cartHomeObject.create(_cardHolderName, _creditCardNumber);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + _cardHolderName + ", " + _creditCardNumber + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + _cardHolderName + ", " + _creditCardNumber + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + _cardHolderName + ", " + _creditCardNumber + "): " + cartObject + ".");    }    return cartObject;  }  //----------------------------------------------------------------------------  // Methods that use Remote interface methods to access data through the bean  //----------------------------------------------------------------------------  public void addItem(Item item) {    if (cartObject == null) {      System.out.println("Error in addItem(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling addItem(" + item + ")");      startTime = System.currentTimeMillis();    }    try {      cartObject.addItem(item);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: addItem(" + item + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: addItem(" + item + ")");      }      e.printStackTrace();    }  }  public void removeItem(Item item) {    if (cartObject == null) {      System.out.println("Error in removeItem(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling removeItem(" + item + ")");      startTime = System.currentTimeMillis();    }    try {      cartObject.removeItem(item);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: removeItem(" + item + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: removeItem(" + item + ")");      }      e.printStackTrace();    }  }  public float getTotalPrice() {    float returnValue = 0f;    if (cartObject == null) {      System.out.println("Error in getTotalPrice(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling getTotalPrice()");      startTime = System.currentTimeMillis();    }    try {      returnValue = cartObject.getTotalPrice();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: getTotalPrice()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: getTotalPrice()");      }      e.printStackTrace();    }    if (logging) {      log("Return value from getTotalPrice(): " + returnValue + ".");    }    return returnValue;  }  public void purchase() {    if (cartObject == null) {      System.out.println("Error in purchase(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling purchase()");      startTime = System.currentTimeMillis();    }    try {      cartObject.purchase();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: purchase()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: purchase()");      }      e.printStackTrace();    }  }  public void executeRemoteCallsWithDefaultArguments() {    if (cartObject == null) {      System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);      return ;    }    addItem(null);    removeItem(null);    getTotalPrice();    purchase();  }  //----------------------------------------------------------------------------  // Utility Methods  //----------------------------------------------------------------------------  private void log(String message) {    if (message == null) {      System.out.println("-- null");      return ;    }    if (message.length() > MAX_OUTPUT_LINE_LENGTH) {      System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");    }    else {      System.out.println("-- " + message);    }  }  //Main method  public static void main(String[] args) {    cartTestClient client = new cartTestClient();    String cardHolderName = "Jack B. Quick";    String creditCardNumber = "1234-5678-9012-3456";   client.create(cardHolderName, creditCardNumber);   Item knuthBook1 = new Item("The Art of VB.net", 49.95f);   Item knuthBook2 = new Item("The Art of C#", 49.95f);  client.addItem(knuthBook1);  client.addItem(knuthBook2);  client.removeItem(knuthBook2);  client.purchase();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -