Chinaunix首页 | 论坛 | 博客
  • 博客访问: 865732
  • 博文数量: 215
  • 博客积分: 10062
  • 博客等级: 上将
  • 技术积分: 2235
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 13:21
文章分类

全部博文(215)

文章存档

2012年(1)

2011年(24)

2009年(16)

2008年(91)

2007年(83)

我的朋友

分类:

2008-01-14 23:47:28

package jndi;
import javax.naming.*;
import java.util.Hashtable;
 class JNDI {
  static Context ctx = null;
  public JNDI()
  {  }
//将对象object绑定到WebLogic Server的名字服务中
  public static void bind(String name, String object) {
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
    try {
      ctx = new InitialContext(ht);
      ctx.rebind(name, object);
    }
    catch (NamingException e) {
          System.out.println(e);
    }
    finally {
      try {
        ctx.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 //通过JNDI查询指定的对象
  public static Object lookUp(String name) {
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
    try {
      ctx = new InitialContext(ht);
      Object object = ctx.lookup(name);
      return object;
    }
    catch (NamingException e) {
                System.out.println(e);
    }
    finally {
      try {
        ctx.close();
      }
      catch (Exception e) {
          System.out.println(e);
      }
    }
    return null;
  }
  public static void main(String args[]) {
    String arg = args[0];
    if(arg.equals("bind")) {
      System.out.println("bind begin...");
      String bookName = "WebLogic introduction";
      bind("bookname", bookName);
      System.out.println("bind end");
    }
    if(arg.equals("lookup")) {
      System.out.println("lookup begin...");
      String bookName;
      bookName = (String)lookUp("bookname");
      System.out.println("bookname is: "+bookName);
      System.out.println("lookup end");
    }
  }
}
阅读(1722) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~