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

r1wp621o  于 12个月前  发布在  Eclipse
关注(0)|答案(1)|浏览(161)

我有以下问题:我有context.xml文件,服务器无法通过@Resource()注入或使用class(检查下面)来读取。我使用Eclipse,Tomcat 9.0。我有.war文件由Eclipse导出并放在我项目内的目标文件夹中。下面是代码。
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
这是我的错误:x1c 0d1x
我失去了所有的想法:(
好了,我已经根据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数据库,虽然我100%肯定那些:

username="sql2226123" password="abcdefgh" 
url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"


是正确的,我登录到数据库作为管理员现在与它(我已经改变了密码在堆栈上的问题)。
好吧,既然HTTP 500错误已经消失了,现在在Web浏览器上我只有白色屏幕,Tomcat服务器抛出以下错误,所以我添加了最严重的错误:

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
    at com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.doesBookExist(BookDbUtil.java:91)
    at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.addBook(BookControllerServlet.java:117)
    at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.doGet(BookControllerServlet.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)
    ... 29 more

owfi6suc

owfi6suc1#

似乎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>

字符串
Context元素上的path属性用于定义应用的上下文根,换句话说,URL起始路径,例如:如果path="/ParkingLot",您的应用将在http://localhost:8080/ParkingLot下找到。

相关问题