web工程测试JDNI

该博客围绕Tomcat9配置JNDI展开。介绍了环境为Tomcat9和JDK8,数据库使用JNDI,给出了Tomcat9配置JNDI的参考链接。还展示了项目结构,涉及web.xml等,通过index.jsp和index2.jsp两个测试页面进行测试,并给出测试页面的访问地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

  • tomcat9
  • jdk8
  • 数据库使用JNDI

tomcat9 配置 JNDI

https://blog.youkuaiyun.com/qq_26264237/article/details/90636085

项目结构

在这里插入图片描述

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<description>test jndi</description>
	<jsp-config>
		<taglib>
			<taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
			<taglib-location>/WEB-INF/sql.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
			<taglib-location>/WEB-INF/c.tld</taglib-location>
		</taglib>
	</jsp-config>
	<resource-ref>
		<description>DB Connection</description>
		<res-ref-name>jdbc/TestDB</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>
</web-app>
  • sql.tld和c.tld:测试jndi
  • resource-ref:注册jndi

测试页面 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<sql:query var="rs" dataSource="jdbc/TestDB">
SELECT id, name FROM t_hello
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
	<h2>Results</h2>
	<c:forEach var="row" items="${rs.rows}">
	[id: ${row.id} , name: ${row.name}]<br />
	</c:forEach>
</body>
</html>

测试结果

http://localhost:8080/testJndiWeb/index.jsp
在这里插入图片描述

测试页面 index2.jsp

http://localhost:8080/testJndiWeb/index2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
request.setAttribute("ds", ds);

Connection conn = ds.getConnection();
Statement stt = conn.createStatement();
ResultSet rs = stt.executeQuery("SELECT id, name FROM t_hello");
List<Map<String, String>> list = new ArrayList<Map<String,String>>(); 
while(rs.next()) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("id", rs.getString(1));
    map.put("name", rs.getString(2));
    list.add(map);
}
conn.close();
conn = null;
request.setAttribute("list", list);
%>
<html>
<head>
<title>DB Test</title>
</head>
<body>
	[ds=<c:out value="${ds }"></c:out>]<br/>
	[list=<c:out value="${list }"></c:out>]<br/>
</body>
</html>

测试结果2

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值