Jndi is the Java API defined for the naming and directory services in Java, and is the abstract mechanism for naming services. in Java EE, the purpose of Jndi is to find the registered resources of the Java EE Server . as long as the object is registered on the named server and you know the address of the named server and the Jndi name that the object is registered on the naming server. This allows you to get and use objects without needing to know the location of the object. Sun only provides interfaces to Jndi, and using Jndi only needs to use JNDI interfaces without having to be concerned with specific implementations .
Now we can start the RMI-based Jndi service in main () and bind an object to Jndi. Note that I directly put the relevant parameters of Jndi into the system.properties, so that after the code to check Jndi, the direct new InitialContext () can be
The limitation of binding Jndi in RMI is that the bound object must be a remote type, so you need to extend one yourself. In fact, Jndi also has two context.security_principal and context.security_credential, if Access Jndi requires a user name and password, these two also provide, but generally not used. Here are two simple examples of using Jndi, which can be run directly.
The
Using the main method to do Jndi demo is noinitialcontextexception because it is not possible to obtain the necessary Jndi parameters from System.properties, which is placed in the server environment when the server is started System.propert IES, so the direct new InitialContext () is done. However, in a stand-alone environment, without the Jndi service running, you need to start a Jndi service manually. A total of 4 sun-brought Jndi implementations were found in the Rt.jar of JDK 5: Ldap,corba,rmi,dns.
These 4 types of Jndi require the underlying services to function properly. In general, we do not have LDAP or CORBA server, you can not start the two Jndi services, DNS for domain name lookup, later, the only thing that can be started in main () is the RMI-based Jndi service.
Package Com.ellen.jndi;import Java.io.serializable;import Java.rmi.remote;import java.rmi.remoteexception;import Java.rmi.registry.locateregistry;import Java.util.date;import Javax.naming.context;import Javax.naming.initialcontext;import javax.naming.namingexception;//The restriction of binding jndi in RMI is that the bound object must be a remote type class person Implements Remote, Serializable {private static final long serialversionuid = -8592182872966400365l; private String name; Private String Pass; public string GetName () {return name,} public void SetName (String name) {this.name = name,} public string Getpass () {return pass;} public void SetPass (String pass) {This.pass = pass;} public String toString () {return "name=" + this.getname () + "&am P;pass= "+ this.getpass (); }}//the restriction of binding jndi in RMI is that the bound object must be a Remote type//external extension, can be extended internally or externally extended class Remotedate extends Date implements Remote {};p ublic Class Demo {public static void Initdate () throws Namingexception, RemoteException {//Set parameter Locateregistry.createregist RY (1099); System. SetProperty (Context.initial_context_factory, "com.sun.jndi.rmi.registry.RegistryContextFactory"); System.setproperty (Context.provider_url, "rmi://localhost:1099"); InitialContext CTX = new InitialContext (); The restriction of binding jndi in RMI is that the bound object must be a Remote type//internal extension, can be extended internally or externally extended class Remotedate extends Date implements Remote {}; Ctx.bind ("Java:comp/env/systemstarttime", New Remotedate ()); Ctx.close (); } public static void InitDate2 () throws Namingexception, RemoteException {//Set parameter Locateregistry.createregistry (1099); System.setproperty (Context.initial_context_factory, "com.sun.jndi.rmi.registry.RegistryContextFactory"); System.setproperty (Context.provider_url, "rmi://localhost:1099"); InitialContext CTX = new InitialContext (); Self-extension, can be extended internally or externally extended//class Remotedate extends Date implements Remote {//}//; Ctx.bind ("Java:comp/env/systemstarttime", New Remotedate ()); Ctx.close (); } public static void Finddate () throws Namingexception, RemoteException {//Direct use InitialconText CTX = new InitialContext (); Date StartTime = (date) ctx.lookup ("Java:comp/env/systemstarttime"); System.out.println ("+++++++++++++++++++++++" + starttime.tostring ()); Ctx.close (); } public static void Jndidate () throws Namingexception, RemoteException {//Demo.initdate (); Demo.initdate2 (); Demo.finddate (); } public static void Initperson () throws Namingexception, RemoteException {//Set parameter Locateregistry.createregistry (1099); System.setproperty (Context.initial_context_factory, "com.sun.jndi.rmi.registry.RegistryContextFactory"); System.setproperty (Context.provider_url, "rmi://localhost:1099"); InitialContext CTX = new InitialContext (); person person = new person (); Person.setname ("Ellen"); Person.setpass ("000727"); Ctx.bind ("Java:comp/env/person", New Person ()); Ctx.close (); } public static void InitPerson2 () throws Namingexception, RemoteException {//Set parameter Locateregistry.createregistry (1099) ; System.setproperty (Context.initial_context_factory, "com.suN.jndi.rmi.registry.registrycontextfactory "); System.setproperty (Context.provider_url, "rmi://localhost:1099"); InitialContext CTX = new InitialContext (); person person = new person (); Person.setname ("Ellen"); Person.setpass ("000727"); Ctx.bind ("Java:comp/env/person", person); Ctx.close (); } public static void Findperson () throws Namingexception, RemoteException {//Direct use InitialContext CTX = new Initialcont Ext (); Person person = (person) ctx.lookup ("Java:comp/env/person"); SYSTEM.OUT.PRINTLN ("------" + person.tostring ()); SYSTEM.OUT.PRINTLN ("------" + person.getname ()); SYSTEM.OUT.PRINTLN ("------" + person.getpass ()); Ctx.close (); } public static void Jndiperson () throws Namingexception, RemoteException {//Demo.initperson (); Demo.initperson2 (); Demo.findperson (); } public static void Main (string[] args) throws Namingexception, RemoteException {//Why two Jndi examples cannot run simultaneously//internal err Or:objid already in use//Demo.jndidate (); Demo.jndiperson (); }}
Jndi Small Case