# 目录和 LDAP 包

> 原文： [https://docs.oracle.com/javase/tutorial/jndi/overview/dir.html](https://docs.oracle.com/javase/tutorial/jndi/overview/dir.html)

## 目录包

[`javax.naming.directory` ](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/package-summary.html)包扩展了 [`javax.naming`](https://docs.oracle.com/javase/8/docs/api/javax/naming/package-summary.html)包，除了命名服务外，还提供了访问目录服务的功能。此程序包允许应用程序检索与存储在目录中的对象关联，并使用指定的属性搜索对象。

### 目录上下文

[`DirContext` ](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/DirContext.html)接口表示 _ 目录上下文 _。 `DirContext` 也通过扩展 [`Context`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Context.html)接口来表现为命名上下文。这意味着任何目录对象也可以提供命名上下文。它定义了检查和更新与目录条目关联的属性的方法。

Attributes

You use [`getAttributes()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/DirContext.html#getAttributes-javax.naming.Name-) method to retrieve the attributes associated with a directory entry (for which you supply the name). Attributes are modified using [`modifyAttributes()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/DirContext.html#modifyAttributes-javax.naming.Name-javax.naming.directory.ModificationItem:A-) method. You can add, replace, or remove attributes and/or attribute values using this operation.

Searches

`DirContext` contains methods for performing content based searching of the directory. In the simplest and most common form of usage, the application specifies a set of attributes possibly with specific values to match and submits this attribute set to the [`search()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/DirContext.html#search-javax.naming.Name-javax.naming.directory.Attributes-) method. Other overloaded forms of [`search()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/directory/DirContext.html#search-javax.naming.Name-java.lang.String-javax.naming.directory.SearchControls-) support more sophisticated search filters.

## LDAP 包

[`javax.naming.ldap` ](https://docs.oracle.com/javase/8/docs/api/javax/naming/ldap/package-summary.html)包中包含用于使用特定于 [LDAP v3](http://www.ietf.org/rfc/rfc2251.txt) 的功能的类和接口，这些功能尚未被更通用的[HTG6 覆盖] ] `javax.naming.directory` 包。实际上，大多数使用 LDAP 的 JNDI 应用程序都会找到`javax.naming.directory`包，并且根本不需要使用 `javax.naming.ldap` 包。此程序包主要用于需要使用“扩展”操作，控件或未经请求的通知的应用程序。

"Extended" Operation

In addition to specifying well defined operations such as search and modify, the [LDAP v3 (RFC 2251)](http://www.ietf.org/rfc/rfc2251.txt) specifies a way to transmit yet-to-be defined operations between the LDAP client and the server. These operations are called _"extended" operations_. An "extended" operation may be defined by a standards organization such as the Internet Engineering Task Force (IETF) or by a vendor.

Controls

The [LDAP v3](http://www.ietf.org/rfc/rfc2251.txt) allows any request or response to be augmented by yet-to-be defined modifiers, called _controls_ . A control sent with a request is a _request control_ and a control sent with a response is a _response control_ . A control may be defined by a standards organization such as the IETF or by a vendor. Request controls and response controls are not necessarily paired, that is, there need not be a response control for each request control sent, and vice versa.

Unsolicited Notifications

In addition to the normal request/response style of interaction between the client and server, the [LDAP v3](http://www.ietf.org/rfc/rfc2251.txt) also specifies _unsolicited notifications_--messages that are sent from the server to the client asynchronously and not in response to any client request.

### LDAP 上下文

[`LdapContext` ](https://docs.oracle.com/javase/8/docs/api/javax/naming/ldap/LdapContext.html)接口表示用于执行“扩展”操作，发送请求控制和接收响应控制的 _ 上下文 _。 JNDI 教程的[控件和扩展](https://docs.oracle.com/javase/jndi/tutorial/ldap/ext/index.html)课程中介绍了如何使用这些功能的示例。