tomcat 8 通过jndi配置数据源

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:


1、测试页面/usr/locat/tomcat/webapp/dbtest/test.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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, foo, bar from testdata
</ sql:query >
< html >
< head >
     < title >DB Test</ title >
</ head >
< body >
     < h2 >Results</ h2 >
     < c:forEach  var = "row"  items = "${rs.rows}" >
         Foo ${row.foo}< br />
         Bar ${row.bar}< br />
     </ c:forEach >
</ body >
</ html >


2.应用配置文件/usr/local/tomcat/webapps/dbtest/WEB-INF/web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
< 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 >MySQL Test App</ description >
< 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 >


3.上下文文件/usr/local/tomcat/conf/context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<? xml  version = '1.0'  encoding = 'utf-8' ?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
< Context >
     <!-- Default set of monitored resources -->
     < WatchedResource >WEB-INF/web.xml</ WatchedResource >
     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
     <!--
     <Manager pathname="" />
     -->
     <!-- Uncomment this to enable Comet connection tacking (provides events
          on session expiration as well as webapp lifecycle) -->
     <!--
     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
     -->
     < Resource  name = "jdbc/TestDB"  auth = "Container"  type = "javax.sql.DataSource"
     maxTotal = "100"  maxIdle = "30"  maxWaitMillis = "10000"
     username = "javauser"  password = "javadude"
     driverClassName = "com.mysql.jdbc.Driver"
     url = "jdbc:mysql://10.13.106.11:3306/javatest" />
</ Context >


4、建立数据库

1
2
3
4
5
6
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@'10.13.106.%' IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (id int not null auto_increment primary key,foo varchar(25),bar int);
mysql> insert into testdata values(null, 'ok', 12345);
mysql> flush privileges;


5、上传taglibs-standard-impl-1.2.5.jar、taglibs-standard-spec-1.2.5.jar和mysql-connector-java-5.1.10-bin.jar到/usr/local/tomcat/lib

   

6、重启tomcat




     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1793086,如需转载请自行联系原作者




相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
XML 应用服务中间件 Apache
Tomcat AJP连接器配置secretRequired=“true“,但是属性secret确实空或者空字符串,这样的组合是无效的。
Tomcat AJP连接器配置secretRequired=“true“,但是属性secret确实空或者空字符串,这样的组合是无效的。
|
1月前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
15天前
|
XML Java 应用服务中间件
Tomcat_servlet部署、编译、配置、打包
Tomcat_servlet部署、编译、配置、打包
19 0
|
28天前
|
运维 Java 应用服务中间件
Tomcat详解(二)——tomcat安装与配置
Tomcat详解(二)——tomcat安装与配置
21 1
|
1天前
|
IDE Java 应用服务中间件
JDK1.6.0+Tomcat6.0的安装配置(配置JAVA环境)
JDK1.6.0+Tomcat6.0的安装配置(配置JAVA环境)
|
2天前
|
网络协议 Java 应用服务中间件
HTTP协议与Tomcat在IJ中配置
本文是对自己学习JavaWeb学习的笔记的总结,添加了一些自己的东西,然后进行一次复盘,并加深一下学习的理解和印象.其中内容主要包括对http协议的详细介绍,java常见服务器的初步介绍,以及IJ旧版和新版的tomcat服务器的配置图解教程
|
15天前
|
网络协议 Java 应用服务中间件
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
11 0
|
16天前
|
应用服务中间件
【SSM】如何在IDEA配置tomcat启动项目
【SSM】如何在IDEA配置tomcat启动项目
18 1
|
16天前
|
IDE JavaScript Java
如何配置tomcat
【4月更文挑战第15天】如何配置tomcat
18 2
|
17天前
|
负载均衡 Ubuntu 应用服务中间件
Apache(mod_proxy)+Tomcat负载均衡配置
Apache(mod_proxy)+Tomcat负载均衡配置