JNDI (容器:Tomcat, Window)
1、什么是JNDI?
<1>全称:Java命名和目录接口(Java Naming and Directory Interface )
<2>是一组在Java应用中访问命名和目录服务的API
<3>通过名称将资源与服务进行关联
2、Tomcat JNDI
配置及使用(将对象写入到Tomcat JNDI)
(1).建实体类
(2).配置context.xml
<Resource name="bean/person" auth="Container"
type="com.zking.entity.Person"
factory="org.apache.naming.factory.BeanFactory"
sid="1" sname="lisijia" sage="14"/>
(3).配置web.xml
<resource-env-ref>
<description>myperson</description>
<resource-env-ref-name>bean/person</resource-env-ref-name>
<resource-env-ref-type>com.zking.entity.Person</resource-env-ref-type>
</resource-env-ref>
(4).index.jsp
Context context=new InitialContext();
Context contextjava=(Context)context.lookup("java:comp/env");
Person person=(Person)contextjava.lookup("bean/person");
out.println(person);
3、Window JNDI
<1>获取Window某个目录下所有的文件 context.list(“ ”);
<2>读取某一个文件context.lookup(“fileName”);
4、自定义JNDI
<1>将对象写入到自定义的JNDI中 context.bind();
<2>在自定义的JNDI中,删除某一个对象 context.unbind();
<3>从自定义的JNDI中获取某一个对象 context.lookup();
<4>从自定义的JNDI中获取所有对象 context.list();