1 / 11

J ava N aming and D irectory I nterface

J ava N aming and D irectory I nterface. Matt. What is JNDI. JNDI. JNDI 提供了命名 (naming) 與目錄 (directory) 服務,它的功用在於可以對整個環境中的資源進行命名,並透過命名取得該資源。

mihaly
Download Presentation

J ava N aming and D irectory I nterface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Naming and Directory Interface Matt

  2. What is JNDI

  3. JNDI • JNDI提供了命名(naming)與目錄(directory)服務,它的功用在於可以對整個環境中的資源進行命名,並透過命名取得該資源。 • The SPI provides a plug−in architecture with which third parties can use to develop support for additional naming and directory services and integrate that support seamlessly into the JNDI framework. The SPI defines the methods they use to hook into the JNDI API. As a developer, all you need to do in order to use the service provider is load the library during runtime or at startup. • JNDI is used to find resources (such as EJBs) you have registered via the J2EE server. Advanced use of JNDI supports sophisticated storage and retrieval of Java objects and other information. • A Naming Service provides a mechanism for giving names to objects so that you can retrieve and use those objects without knowing the location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation. • A Directory Service also associates names with objects but provides additional information by associating attributes with the objects.

  4. JNDI • One of the key tasks in a distributed application is finding items such as components, resources, message queues, and databases. • The Java Naming and Directory Interface (JNDI) is an essential part of J2EE applications, providing a mechanism for finding distributed components—but JNDI is good for more than just looking up EJB interfaces. It also allows access to important enterprise systems such as directory servers.

  5. JNDI Architecture

  6. JNDI Architecture

  7. JNDI API

  8. JNDI API Package • javax.naming • Defines classes and interfaces that enable you to interact with naming services. • javax.naming.directory • Defines classes and interfaces that enable you to interact with directory services. • javax.naming.event • Defines classes and interfaces that handle event notifications when you’re working with naming and directory services. • javax.naming.ldap • Defines classes and interfaces for working with LDAP v3.0. • javax.naming.spi • Defines the service−provider interface that vendors of naming and directory service providers must implement.

  9. JNDI environment properties

  10. The first step in using the JNDI name service is to get a context in which to add or find names. • The context that represents the entire namespace is called the Initial Contextand is represented by a class called javax.naming.InitialContext and is a sub-class of the javax.naming.Context class. Context ctx = new InitialContext(); • A Context object represents a context that you can use to look up objects or add new objects to the namespace.

  11. JNDI example import javax.naming.*; import javax.sql.*; import java.sql.*; //use JNDI(Java Naming and Directory Interface) public class Lookup {     public Lookup()     {         Connection con = null;         Statement stmt = null;         ResultSet rs = null;         try         {           InitialContext ic = new InitialContext();  Context envCtx = (Context)ic.lookup("java:comp/env");     //java:computer environment DataSource ds = (DataSource)envCtx.lookup("jdbc/music");    //查找jdbc/music  con = ds.getConnection();  //得到connection stmt = conn.createStatement();  //得到statement rs = stmt.executeQuery("select * from music");             while (rs.next())             {                 //do something             }             rs.close();             stmt.close();             con.close();         }         catch (Exception ex)         {             System.err.println("ERROR");         }     } }

More Related