0

I have the following issue: I have context.xml file which can not be read by the server either by adding through @Resource() injection or by using class (check below). I'm using eclipse, Tomcat 9.0. I have .war file exported by eclipse and put in the target folder within my project. Here goes the code.

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

and I have following class to set connection with MySQL DB here goes the constructor: https://pastebin.com/XKmBRAZX

here is constructor used in servlet: https://pastebin.com/EmSzL5Y1

here is my pom.xml: https://pastebin.com/umU6R2KF

my context.xml is located: C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\resources\META-INF\context.xml

my .war file (exported by eclipse) is located: C:\Users\userName\eclipse-workspace\ProjectName\ProjectName.war

and this is my error: enter image description here

I've lost all ideas :(

Ok so I've made updates according to apache.org/tomcat-9.0 Now my web.xml looks like this:

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

and my context.xml looks like this:

<?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>

Now the error is gone but there is only white screen displayed as if there was no connection to the MySQL DB although I'm 100% sure those: username="sql2226123" password="abcdefgh" url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"

are correct I logged in to the DB as admin right now with it (I've changed the password in the question on stack).

Ok so since HTTP 500 error is gone and on web-browser now I have just white screen and Tomcat server is throwing following error so I'am adding the most acutal error: 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

LMC
  • 10,453
  • 2
  • 27
  • 52
Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • 1
    Have you compared your settings with the reference example in the [documentation (tomcat7)](https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html) or [tomcat8](https://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html)? (Please find the proper page to your tomcat version.) – m4gic Aug 29 '18 at 21:42
  • 1
    When I worked with tomcat context files, they were placed in `${TOMCAT_HOME}/conf/Catalina/localhost` and it was working scheme. – Lev Khruschev Aug 29 '18 at 22:19
  • Please add error message, code, and *all* textual content as text, directly in the question. Not as images. – Olaf Kock Aug 30 '18 at 08:38
  • The resource JNDI name should be `jdbc/ParkingBookSystem` I think, same for the context.xml file. Yours is missing `jdbc` – LMC Aug 30 '18 at 18:42

1 Answers1

1

Seems JNDI name is not on the correct place as it should be inside Resource element but it's instead on Context one

<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 attribute on Context element is meant to define context root of your app, in other words, the URL starting path, e.g.: if path="/ParkingLot", your app will be found under `http://localhost:8080/ParkingLot.

LMC
  • 10,453
  • 2
  • 27
  • 52
  • I've edited the context following to your advice (added in the question) and edited web.xml according to aparche.org/tomcat-9.0 The error is gone however now I have a just white screen. :( – Luke_Nuke Aug 30 '18 at 08:11
  • 1
    If your original question has been answered and *that* problem solved (e.g. you now can connect to the database), please consider accepting this answer, and ask a new question because the "white screen" doesn't have anything to do with JDBC connections. Or let us know why you think the JDBC connection still is a problem (e.g. by adding debug output to your code). – Olaf Kock Aug 30 '18 at 08:56