jndi.java

来自「J2EE为Java企业开发提供了一幅清晰的全景」· Java 代码 · 共 73 行

JAVA
73
字号
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");
    }
  }
}

⌨️ 快捷键说明

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