关于JNDI问题

写了一个是待绑定的基本类,一个绑定对象程序,可以生成CLASS,但在CONSOLE中提示bind object fail:javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
请问是什么意思,应该如何解决呢?

谢谢

没有链接到JNDI服务端口
是你的配置文件没有写对,或者J2EE服务器没有启动
用的什么服务器啊?

谢谢楼上,我是JAVA出学者,请多多指教!
我用“eclipse”编写的,用它可以生成CLASS,但在CONSOLE中提示bind object fail:javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]。
我装了“BEA WebLogic Platform 8.1”,它是你说的J2EE服务器吗?配置文件我没有写,应该怎么写啊?谢谢!

你看一下javax.naming.InitialContext的API文档,需要进行必要的配置
Context ctx = null;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory ");
env.put(Context.PROVIDER_URL, "t3://192.168.0.2:7001");
try{
ctx = new InitialContext(env);
}catch(NamingException ne) {
ne.printStackTrace()
}finally{
try{
ctx.close();
}catch(Exception e) {
e.printStackTrace()
}
}
或者自己在ClassPath中写一个配置文件:jndi.properties

persons.java
代码:
package f;
import java.io.Serializable;

public class persons implements Serializable{
private static final long serialVersionUID = 1L;
String Name="";
String Age="";
public persons(){
}

public persons(String namepara,String age){
Name=namepara;
Age=age;
}
public String getName(){
return Name;
}
public String getAge(){
return Age;
}
}

LdapDataBind.java
代码:
package f;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;

public class LdapDataBind {
public static void main(String[]args){
Hashtable hs=new Hashtable();
hs.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
hs.put(Context.PROVIDER_URL,"ldap://localhost:389");
hs.put(Context.SECURITY_AUTHENTICATION,"simple");
hs.put(Context.SECURITY_PRINCIPAL,"cn=Directory Manager");
hs.put(Context.SECURITY_CREDENTIALS,"password");
try{
DirContext ctx=new InitialDirContext(hs);
persons perobj=new persons("jordan","40");
ctx.rebind("uid=jordan,ou=bull,o=NBA",perobj);
System.out.println("bind object object success");
Attributes attrs=new BasicAttributes(true);
Attribute personmail=new BasicAttribute("mail");
personmail.add("xie@163.com");
personmail.add("liu@sina.com");
personmail.add("xyh@powerise.com.cn");
attrs.put(personmail);
attrs.put("uid","001");
attrs.put("cn","jordan1");
attrs.put("sn","NBA");
attrs.put("ou","bull");
System.out.println("bind object object success");
ctx.createSubcontext("uid=Jordan,ou=Wizzard,o=NBA",attrs);
ctx.close();
}catch(NamingException ex){
System.err.println("bind object fail:"+ex.toString());
}
}
}
请楼上看看有没有问题?
在自己在ClassPath中写一个配置文件:jndi.properties 应该如何做啊?请指点!
谢谢