jsp JNDI

来源:互联网 发布:管家婆软件使用教程 编辑:程序博客网 时间:2024/06/09 17:41
JAVA命名与目录接口,是一个应用程序设计的api,这开发人员提供查找和访问各种命名和目录服务的通用,
统一的接口

  对象和名字绑定的技术,分布式系统中访问其它组件和资源,如dns,file service db


WEB 容器

1.context.xml

<Environment name="tjndi" value="hello jndi" type="java.lang.String" />


2.jsp

<%
//javax.naming,Context提供了查找jndi  resource的接口
Context ctx = new InitialContext();
//java:comp/env为前缀
String textjndi = (String) ctx.lookup("java:comp/env/tjndi");
out.print("textjndi:" +"  "+ textjndi);
//
%>

ds

WEB 容器

1.context.xml

<!-- dataSource and jndi  auth="Applicantion"-->
<Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="scott"
password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" />

WEB app web.xml

2.

<resource-ref>
<description>orcl ds</description>
<res-ref-name>jdbc/orcl</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3.jsp

<%
Context cxt = new InitialContext();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;


try {
DataSource ds = (DataSource) cxt
.lookup("java:comp/env/jdbc/orcl");
con = ds.getConnection();
ps = con.prepareStatement("select * from emp  e where ename = ? ");
ps.setString(1, "SMITH");
rs = ps.executeQuery();
if (rs.next()) {
out.print("登录成功 ");
}else{
out.print("login faild");
}
} catch (Exception e) {
out.println(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception ex) {
out.print(ex);
}
}
%>

0 0