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

📄 client.java

📁 web开发经典案例shoppingcart的EJB实现其entity部分
💻 JAVA
字号:
/*
 * Client.java
 *
 * Created on 2008年6月2日, 上午9:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package shoppingcart_client;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import stateful.Product;
import stateful.SaleLine;
import stateful.ShoppingCartRemote;

/**
 *
 * @author teacher
 */
public class Client {
    
    /** Creates a new instance of Client */
    public Client() {
    }
    
    public static void test1() throws Exception{
        Context ctx = getInitialContext2();
        ShoppingCartRemote scr =
                (ShoppingCartRemote) ctx.lookup("shoppingCart");
        Product p1 = new Product();
        p1.setName("p1");
        p1.setPrice(100);
        
        Product p2 = new Product();
        p2.setName("p2");
        p2.setPrice(200);
        
        SaleLine s1 = new SaleLine();
        s1.setProduct(p1);
        s1.setQty(2);
        
        SaleLine s2 = new SaleLine();
        s2.setProduct(p2);
        s2.setQty(2);
        
        scr.add(s1);
        scr.add(s2);
        
        for(SaleLine s  :scr.list()){
            System.out.print("product's name "+s.getProduct().getName());
            System.out.println(" qty is "+s.getQty());
        }
        
        System.out.println("total price "+scr.cost());
    }
    
    
    public static void test2() throws Exception{
        Context ctx = getInitialContext2();
        ShoppingCartRemote[] scrs =
                new ShoppingCartRemote[3];
        for(int i=0;i<3;i++){
            scrs[i] = (ShoppingCartRemote) ctx.lookup("shoppingCart");
        }
        
        for(int i=0;i<3;i++){
            Product p1 = new Product();
            p1.setName("p1");
            p1.setPrice(100);
            
            Product p2 = new Product();
            p2.setName("p2");
            p2.setPrice(200);
            
            SaleLine s1 = new SaleLine();
            s1.setProduct(p1);
            s1.setQty(2);
            
            SaleLine s2 = new SaleLine();
            s2.setProduct(p2);
            s2.setQty(2);
            
            scrs[i].add(s1);
            scrs[i].add(s2);
            
            for(SaleLine s  :scrs[i].list()){
                System.out.print("scrs["+i+"] product's name "+s.getProduct().getName());
                System.out.println(" qty is "+s.getQty());
            }
            
            System.out.println("scrs["+i+"] total price "+scrs[i].cost());
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        test2();
        
    }
    
    public static Context getInitialContext2(){
        Properties props = new Properties();
        props.setProperty("jndi.factory","com.sun.jndi.cosnaming.CNCtxFactory");
        props.setProperty("org.omg.CORBA.ORBInitialHost","192.168.105.101");
        props.setProperty("org.omg.CORBA.ORBInitialPort","3700");
        Context ctx = null;
        try {
            ctx = new InitialContext(props);
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        return ctx;
    }
    
}

⌨️ 快捷键说明

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