tomcat5.5下JDNI的配置

来源:互联网 发布:知乎 健身太壮不好看 编辑:程序博客网 时间:2024/05/16 17:09
拿mysql数据库和tomcat5.5为例子:
安装tomcat5.5(注意这点)
安装mysql
拷贝mysql驱动到tomcat_home/common/lib下
新建一个web工程jndi
在工程中加入index.jsp
HTML code

<%@page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*" %>
<%@page contentType="text/html;charset=BIG5"%>
<%
Context ctx
= new InitialContext();
String strLookup = "java:comp/env/jdbc/test";
DataSource ds
=(DataSource) ctx.lookup(strLookup);
Connection con
= ds.getConnection();
if (con != null){
out.print(
"success");
}
else{
out.print(
"failure");
}
%>

在web.xml中加入
XML code

<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

在tomcat_home/conf/localhost/下建立一个xml文件,文件名是 <yourAppName> .xml
例如我的工程名叫jndi,对应的名字叫jdni.xml
内容如下:
XML code

<Context>
<Resource
name="jdbc/test"
type
="javax.sql.DataSource"
password
="bb"
driverClassName
="com.mysql.jdbc.Driver"
maxIdle
="2"
maxWait
="50"
username
="root"
url
="jdbc:mysql://localhost:3306/test"
maxActive
="4"/>
</Context>

运行测试:
打开ie,输入http://localhost:8080/jndi/index.jsp
看到success 代表配置成功 
原创粉丝点击