Tomcat无法读取我的context.xml文件

时间:2018-08-29 20:00:26

标签: java eclipse tomcat context.xml

我有以下问题: 我有context.xml文件,服务器无法通过通过@Resource()注入进行添加或使用类(请在下面进行检查)来​​读取该文件。我正在使用Eclipse Tomcat 9.0。我通过eclipse导出了.war文件,并将其放在项目中的目标文件夹中。代码在这里。

Context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context path="jdbc/ParkingSystem">
    <Resource name="sql2226123" 
        auth="Container" type="javax.sql.DataSource"
        maxActive="20" maxIdle="5" maxWait="10000"
        username="sql2226123" password="abcdefgh" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"/>
</Context>

https://pastebin.com/fSZG2bJR

并且我有以下类来设置与MySQL DB的连接,这里是构造函数: https://pastebin.com/XKmBRAZX

这是servlet中使用的构造函数: https://pastebin.com/EmSzL5Y1

这是我的pom.xml: https://pastebin.com/umU6R2KF

我的context.xml位于: C:\ Users \ lukas \ eclipse-workspace \ ParkingBookSystem \ src \ main \ resources \ META-INF \ context.xml

我的.war文件(通过eclipse导出)位于: C:\ Users \ userName \ eclipse-workspace \ ProjectName \ ProjectName.war

这是我的错误: enter image description here

我迷失了所有主意:(

好的,所以我根据apache.org/tomcat-9.0进行了更新 现在我的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>ParkingBookSystem</description>
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>/ParkingBookSystem</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

和我的context.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource 
    name="/ParkingBookSystem" 
    auth="Container"
    type="javax.sql.DataSource"
    maxTotal="20"
    maxIdle="5"
    maxWaitMillis="10000"
    username="sql2226123"
    password="xxxxxxxx"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
/>
</Context>

现在错误消失了,但是只有白屏显示,好像没有与MySQL DB的连接,尽管我100%确信那些: username =“ sql2226123” password =“ abcdefgh” url =“ jdbc:mysql://sql2.freemysqlhosting.net:3306 / sql2226123”

是正确的,我现在以管理员身份登录数据库(我已经更改了栈中问题的密码)。

好吧,既然HTTP 500错误消失了,并且在网络浏览器上,我现在只有白屏,并且Tomcat服务器抛出以下错误,所以我添加了最棘手的错误: java.sql.SQLException:无法为连接URL'null'创建类“”的JDBC驱动程序     在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)     在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)     在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)     在com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.doesBookExist(BookDbUtil.java:91)     在com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.addBook(BookControllerServlet.java:117)     在com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.doGet(BookControllerServlet.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)     在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)     在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)     在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:770)     在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1415)     在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)     在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源) 原因:java.sql.SQLException:没有合适的驱动程序     在java.sql.DriverManager.getDriver(未知来源)     在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)     ...还有29个

1 个答案:

答案 0 :(得分:1)

似乎JNDI名称不在正确的位置,因为它应该在 Resource元素中,但是在Context一个元素上

<Context>
    <Resource 
        name="jdbc/ParkingSystem" 
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="20"
        maxIdle="5"
        maxWait="10000"
        username="sql2226123"
        password="abcdefgh" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
    />
</Context>
path元素上的

Context属性用于定义应用程序的上下文根,换句话说,是URL起始路径,例如:如果为path="/ParkingLot",则将在以下位置找到您的应用程序`http://localhost:8080/ParkingLot