工程目录结构

一、说明
- 此实验基于实验9-1,数据库与实验9-1相同(红线为改动文件)
- 代码唯一的改动为,将取得数据库连接的方式改为使用Tomcat数据源获取
- 配置文件的改动有,本项目的web.xml文件、tomcat的context.xml
二、有改动的代码和配置文件
DatabaseConnection.java
package witersen.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DatabaseConnection {
private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/demo0901?useSSL=false&serverTimezone=UTC";
private static final String DBUSER = "root";
private static final String DBPASSWORD = "";
private Connection conn = null;
private static final String DSNAME = "java:comp/env/jdbc/demo0901";
public DatabaseConnection() {
try {
//Class.forName(DBDRIVER);
//this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
Context ctx = new InitialContext(); //初始化名称查找上下文
DataSource ds = (DataSource)ctx.lookup(DSNAME); //通过JNDI名称获取数据源实例
this.conn = ds.getConnection(); //取得数据库连接
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return this.conn;
}
public void close() {
if (this.conn != null) {
try {
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>59.实验十-第一题-数据源</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginCheck</servlet-name>
<servlet-class>witersen.servlet.LoginCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCheck</servlet-name>
<url-pattern>/LoginCheck</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>witersen.servlet.logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AddNews</servlet-name>
<servlet-class>witersen.servlet.AddNews</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddNews</servlet-name>
<url-pattern>/AddNews</url-pattern>
</servlet-mapping>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>witersen.servlet.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<resource-ref>
<description>demo0901</description>
<res-ref-name>jdbc/demo0901</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
context.xml
<?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. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource name="jdbc/demo0901"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password=""
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo0901?useSSL=false&serverTimezone=UTC"/>
</Context>
原创文章,作者:witersen,如若转载,请注明出处:https://www.witersen.com