<%@page import="java.sql.Connection"%> <%@page import="java.net.ConnectException"%> <%@page import="javax.sql.DataSource"%> <%@page import="javax.naming.Context"%> <%@page import="javax.naming.InitialContext"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>JNDI</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <% //读取jndi信息 //1.先创建上下文对象 /* Context cx=new InitialContext(); String str=(String)cx.lookup("java:comp/env/bdqn"); out.print(str); */ //读取数据源 Context cx=new InitialContext(); DataSource ds=(DataSource)cx.lookup("java:comp/env/jdbc/news"); Connection cn=ds.getConnection(); out.print(cn); out.print("连接成功!"); %> </body> </html>