J2EE technical specifications (1) -- JNDI

Source: Internet
Author: User

To learn Java, we must first master the thirteen technical specifications. Next we will learn them step by step. Today we will talk about JNDI.

I. Understand the usage of JNDI:

JNDI is a unified API interface used to access different naming and directory services.

2. When to use JNDI:

(1) JNDI is a search service used for search:

Web application environment variables

Ejbs and their environment variables

Use the datasources database connection pool

JMS target and connection Factory

Other services

(2) do not use JNDI as a database

The JNDI object is stored in the memory.

Access to the JNDI object is related to network performance

Next, let's take a look at the JNDI tree:

Next, let's understand what the figure means. We can see that initial context is the starting point for all the searches in the JNDI tree. How can we reconstruct the initial context?

III,There are two ways to do this:

(1) connect the slave server object to JNDI:

// Create an initialcontext context CTX = new initialcontext ();

(2) connect to JNDI from any address:

Create an environment object:

weblogic.jndi.Enviroment env=new    weblogic.jndi.Enviroment();

Environment:

Env. setproviderur ("T3: // 127.0.0.1: 7001"); // WebLogic // server location Env. setsecurityprincipal ("system"); // Security Information Env. setsecuritycredentials ("password"); // use the environment object to create initialcontextcontext CTX = Env. getinitialcontext ();

Create subcontext:

4. Create a New subcontext for binding objects:

// Create the environment object contextctx = Env. getinitialcontext (); // you must first obtain the existing subcontext or initialcontext. // enter environmentcontextsubcontext = CTX. createsubcontext ("newsubcontext"); // create a new subcontextsubcontext under the current context. rebind ("boundobject", object); // create an object subcontext. close (); // The resource CTX that is frequently accessed at the end. close ();

Other methods for connecting to JNDI:

(1) Use the 'constant' properties name and hashtable class:

Hashtableenv = new Hashtable();env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");env.put(Context.PROVIDER_URL,"t3://localhost:7001");env.put(Context.SECURITY_PRINCIPAL,"system");env.put(Context.SECURITY_CRRDENTIALS,"PassWord");Contextctx = new InitalContext(env);

(2) Use the 'hardcoded' name and properties class:

Propertiesenv = new Properties();env.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");env.setProperty("java.naming.provider.url","t3://192.2.56.102:7001");env.setProperty("java.naming.security.principal","system");env.setProperty("java.naming.security.credentials","PassWrod");Contextctx = new InitialContext(env);

V. Some JNDI environment attributes:

JNDI. properties:

(1) The JNDI. properties file sets the default properties for all initial contexts.

(2). Search Order of the JNDI. properties File

A. classpath

B. $ java_home/lib/

Let's take a look at the JNDI. properties example:

Java. naming. factory. initial = weblogic. JNDI. wlinitialcontextfactoryjava. naming. provider. url = T3: // localhost: 7001java. naming. security. principal = systemjava. naming. security. credentials = PASSWORD // use these default values: context CTX = new initialcontext ();

VI,Search from JNDI:

(1) lookup () Get the object from the JNDI tree

(2) Objects returned through Lookup () must be mapped to their appropriate types

   importjavax.naming.*;         try{                   Contextic = new Context();                   Objectobj;                   obj= ic.lookup("javax.transaction.UserTransaction");                   UserTransactionut = (UserTransaction)obj;                   ut.begin();                   ...                   ic.close();         }catch(NamingException e) {...}

VII,Remote binding object:

(1) Objects bound to the remote Naming Service must be serialized.

(2) when accessing the Naming Service, the object adopts the replication mechanism.

These two statements are important:

A. the bound object is serialized to the naming server.

B. The searched objects are serialized to the customer's

Let's look at a binding example:

Public Static Context getinitialcontext () throws namingexception {environmentenv = new environment (); ENV. setproviderurl ("T3: // localhost: 7001"); ENV. setsecurityprincipal ("system"); ENV. setsecuritycredentials ("WebLogic"); contextcontext = Env. getinitialcontext (); returncontext;} context CTX = getinitialcontext (); // get initial contextbank mybank = New Bank (); // create the CTX object named bank. rebind ("thebank", mybank); // bind the object to the JNDI tree CTX. close ();

There is also an un-binding example:

Public Static Context getinitialcontext () throws namingexception {environmentenv = new environment (); ENV. setproviderurl ("http: // localhost: 7001"); ENV. setsecurityprincipal ("system"); ENV. setsecuritycredentials ("WebLogic"); contextcontext = Env. getinitialcontext (); returncontext;} context CTX = getinitialcontext (); // get initial contextctx. unbind ("thebank"); // unbind the CTX object bound to the "thebank. close ();

8,Possible naming exceptions

Exceptions that may occur when interacting with jndi I:

Authenticationexception Authentication

Communicationexception Communication

Invalid invalidnameexception name

Namenotfoundexception name not found

Noinitialcontextexception is not connected to initialcontext


These contents are applied to practice, which makes great sense.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.