java EE开发之Servlet第七课:JNDI

1 , 什么是JNDI?
JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口。
官方的定义似乎很让人费解,本人的jndi的理解是:jndi相当于一个目录,可以通过目录可以找到相应的资源,那么通俗的讲就是一份菜单,通过报菜名能够得到要点的菜

2,JNDI怎么玩
1) 自作一份jndi目录

//放目录的那张菜单纸
Properties ps= new Properties();
//建一个生成菜单的工厂类
ps.put(Context.INITIAL_CONTEXT_FACTORY, 
        "org.apache.naming.java.javaURLContextFactory");
//生成一份菜单
Context context = new InitialContext(ps);
//添加菜名
context.bind("一号菜", "红烧鱼");
//遍历所有的菜名
NamingEnumeration ne = context.list("");
while(ne.hasMore()){
    System.out.println(ne.next());
}
//点菜
Object o1 = context.lookup("一号菜");
//移除
context.unbind("一号菜");

2) 得到window目录

Properties ps=new Properties();
//专门针对于微软的目录生产工厂而言的
ps.put(Context.INITIAL_CONTEXT_FACTORY,              
                "com.sun.jndi.fscontext.FSContextFactory");
Context context = new InitialContext(ps);
//列出当前这个类的根目录下的目录和文件名
NamingEnumeration ne = context.list("");
while(ne.hasMore()){
    Object o = ne.next();
    System.out.println(o);
}
//根据目录名得到文件
File f = (File)context.lookup("teas.xml");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr); 
String str =  br.readLine();
while(str!=null){
    System.out.println(str);
    str = br.readLine();
}   
br.close();
fr.close();

3) 绑定一个javaBean:
(1)写一个类,将这个类打包,放入到Tomcat 5.5\common\lib下。
(2)配置conf目录下的context.xml文件

<Resource name="bean/man" auth="Container"
    type="com.accp.Man"
    factory="org.apache.naming.factory.BeanFactory"
    uname="abc" 
    age="15"
/>

(3)配置web应用目录WEB-INF/web.xml文件。

<resource-env-ref>
  <resource-env-ref-name>bean/man</resource-env-ref-name>
  <resource-env-ref-type>com.kz.Man</resource-env-ref-type>
</resource-env-ref>

(4)开始使用这个共享的资源

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Object bean = envCtx.lookup("bean/man");
out.println(bean);

5)绑定一个数据源:

(1)把驱动包放入到Tomcat 5.5\common\lib下。
(2)配置conf目录下的context.xml文件

<Resource   name="sql2005" 
        auth="Container"
        type="javax.sql.DataSource" 
        username="sa" 
        password="123"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
        url="jdbc:sqlserver://localhost:1433;databasename=test"
        maxActive="8" 
        maxIdle="4"/>

说明:
|- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
|- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
|- type:此名称所代表的类型,现在为javax.sql.DataSource
|- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
|- maxIdle:表示一个数据库在此服务器上维持的最小连接数
|- maxWait:最大等待时间。10000毫秒
|- username:数据库连接的用户名
|- password:数据库连接的密码
|- driverClassName:数据库连接的驱动程序
|- url:数据库连接的地址

(3)配置web应用目录WEB-INF/web.xml文件。

<resource-ref>
    <res-ref-name>sql2005</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

(4)开始使用这个共享的资源

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("sql2005");
Connection conn = ds.getConnection();
out.println(conn);
conn.close();//关键  这个关闭不是真的关闭 是放回连接池

原理图
这里写图片描述

补充:测试100次使用普通方式和连接池方式得到Connection对像
1.普通方式(略)
2.连接池方式(导入commons-collections-2.1.1.jar , commons-dbcp.jar , commons-pool.jar)

  BasicDataSource bds = new BasicDataSource();
  bds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  bds.setUrl("jdbc:sqlserver://localhost:1433;databasename=data");
  bds.setUsername("sa");
  bds.setPassword("123456");
  bds.setMaxActive(200);
  bds.setInitialSize(2);
  Connection conn = bds.getConnection();
  Conn.close()//不是真正的关闭,只是重新放回连接池
发布了26 篇原创文章 · 获赞 13 · 访问量 7023

猜你喜欢

转载自blog.csdn.net/womeia331416/article/details/78052194