_JSP Programming for application development based on Jndi

Source: Internet
Author: User
Tags ldap naming convention
The JNDI (the Java naming and directory Interface,java naming and directory interface) is a set of APIs that access the naming and directory services in Java applications. The naming service links names to objects so that we can access them by name. A directory service is a naming service in which objects have names as well as attributes.

Naming or directory services allows you to centrally store shared information, which is important in Web applications because it makes such applications more coordinated and easier to manage. For example, you can store printer settings in a directory service for use with printer-related applications.

This article gives a quick tutorial using code examples to enable you to start using Jndi. It:

1, provides a jndi overview

2, describes the characteristics of the Jndi

3, experience a bit with Jndi development and application

4, indicating how to use Jndi access to the LDAP server, for example, Sun one directory server

5, indicating how to use the Jndi access to the Java service

6, provided the sample code, you can adapt it for your own application

Jndi Overview

We all unknowingly use the naming service every day. For example, when you enter a url,http://java.sun.com in a Web browser, DNS (Domain Name System, field name systems) Converts this symbolic URL name to the traffic ID (IP address). The objects in the naming system can be the names in the DNS records, the EJB components in the application server (Enterprise JavaBeans Component), LDAP (lightweight Directory Access Protocol) The user profile in.

The directory service is a natural extension of the naming service. The key difference between the two is that the object in the directory service can have attributes (for example, the user has an email address), and the object in the naming service has no attributes. Therefore, in the directory service, you can search for objects based on attributes. Jndi allows you to access files in the file system, locate remote RMI-registered objects, Access directory services such as LDAP, and locate EJB components on the network.

Jndi is a good choice for applications like LDAP clients, Application Launcher, class browsers, network management utilities, and even address book.

Jndi Schema

The JNDI architecture provides a standard set of APIs that are independent of the naming system, built on the drivers associated with naming systems. This layer helps separate the application from the actual data source, so whether the application accesses LDAP, RMI, DNS, or other directory services. In other words, Jndi is independent of the specific implementation of the directory service, so long as you have a directory service to provide the interface (or drive), you can use the directory. As shown in Figure 1. Figure 1:jndi Architecture

The important point to note about Jndi is that it provides an application programming interface (application programming Interface,api) and a service provider Interface (services provider INTERFACE,SPI). The real implication of this is that for your application to interact with a naming service or directory service, you must have a Jndi service provider for the service, which is where the Jndi SPI works. A service provider is basically a set of classes that implement a JNDI interface for a variety of specific naming and directory services-much like JDBC drivers implement JDBC interfaces for various specific database systems. As an application developer, you don't have to worry about Jndi SPI. You just need to make sure that each named or directory service you want to use has a service provider.

J2SE and Jndi

The version of the Java 2 SDK 1.3 and above contains Jndi. There is also a standard extension for JDK 1.1 and 1.2. The latest version of the Java 2 SDK 1.4.x includes several enhancements and the following naming/directory service providers:

1. LDAP (lightweight Directory Access Protocol) service provider

2. CORBA COS (Common object Request Broker Architecture Common Object Services) naming service providers

3. RMI (Java Remote method invocation) Registration service provider L DNS (Domain Name System) service provider

More Service Providers

You can find a list of service providers that can be downloaded at the following Web site: http://java.sun.com/products/jndi/serviceproviders.html It is perhaps particularly interesting that the following Web site provides windows Registry JNDI Service Provider: Http://cogentlogic.com/cocoon/CogentLogicCorporation/JNDI.xml This service provider enables you to access Windows xp/2000/nt/me/ 9x Windows registry. You can also download the Jndi/ldap booster pack:http://java.sun.com/products/jndi/This booster Pack contains support and extensions for popular LDAP controls. It replaces the booster pack bundled with the LDAP 1.2.1 service provider. More information on controls and extensions can be found on the following Web site: http://java.sun.com/products/jndi/tutorial/ldap/ext/index.html Another interesting service provider is Sun's support DSML v2.0 A service provider (directory service Markup Language, Catalog Services Markup Language). The purpose of DSML is to bridge the directory service and XML.

JNDI API

The JNDI API consists of 5 packages:

1. Javax.naming: Contains classes and interfaces that access the naming service. For example, it defines the context interface, which is the portal of a named service execution query.

2, Javax.naming.directory: The expansion of the named package, provides access to the directory service classes and interfaces. For example, it adds a new class to the attribute, provides a dircontext interface that represents the directory context, and defines a method for checking and updating the properties of the directory object.

3. Javax.naming.event: Provides support for time notifications when accessing naming and directory services. For example, the Namingevent class is defined to represent events generated by the naming/directory service, and to define the Naminglistener interface that listens for namingevents.

4. JAVAX.NAMING.LDAP: This package provides support for extended operations and controls on LDAP version 3, which are not included in the javax.naming.directory.

5. JAVAX.NAMING.SPI: This package provides a way to increase support for access naming and directory services dynamically through javax.naming and related packages. This package is provided for developers who are interested in creating a service provider.

JNDI Context

As mentioned earlier, the naming service links names to objects. This connection is called binding (binding). A set of such bindings is called the context, and the context provides resolution (that is, the lookup operation that returns the object). Other actions include: Name binding and unbinding, listing the name of the binding. Note that the name of a context object can be bound to another context object that has the same naming convention. This is called the child context. For example, if the directory/home in UNIX is a context, then the subdirectory relative to the directory is a child context-for example,/home/guests guests is the child of the home. In Jndi, the context is represented by interface Javax.naming.Context, which is the key interface that interacts with the naming service. In the context (or later in the discussion

DirContext) Each named method in the interface has two overloaded forms:

1, Lookup (string name): Accept String Name

2. Lookup (javax.naming.Name): Accept structure names, for example, Compositename (spanning multiple named system names) or compondname (names in a single named system); they all implement the name interface. An example of compound name is: an example of cn=mydir,cn=q mahmoud,ou=people,composite name is: cn=mydir,cn=q mahmoud,ou=people/myfiles/ Max.txt (here, Myfiles/max.txt is the filename of the second part) Javax.naming.InitialContext is the class that implements the context interface. Use this class as the portal for the naming service. To create a InitialContext object, the constructor sets a set of properties in the form of java.util.Hashtable or its subclasses (for example, properties). Here's an example:

Hashtable env = new Hashtable (); Select a service provider factory Env.put (Context.initial_context_factory, "Com.sun.jndi.fscontext.RefFSContext"); Create the initial context contxt = new InitialContext (env);

Initial_context_factory specified the name of the factory Class (factory Class) in the Jndi service provider. Factory is responsible for creating the appropriate InitialContext object for its services. In the code snippet above, the factory class is specified for the file system service provider. Table 1 shows the factory classes for the supported service providers. Note that the file system service provider's factory class needs to be downloaded separately from Sun, J2SE 1.4.x does not contain these classes.

Table 1: Values for context Initial_context_factory

Name Service Provider Factory File System com.sun.jndi.fscontext.RefFSContextFactory LDAP Com.sun.jndi.ldap.LdapCtxFactory RMI com.sun.jndi.rmi.registry.RegistryContextFactory CORBA Com.sun.jndi.cosnaming.CNCtxFactory DNS Com.sun.jndi.dns.DnsContextFactory

To obtain or parse an object from a naming service or directory by name, use the lookup method of the context: Object Obj=contxt.lookup (name). The Lookup method returns an object that represents the son of the context you want to find.

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.