title: ClassNotFoundException When running JDBC through java's Servlets
body: <p>I'm getting this weird exception when trying to create a MysqlDataSource object (or at least I think this is the root of the problem).  </p>
  
  <p>Let me first describe exactly what I have so far:<br>
  I'm using Tomcat7 as a container, and Eclipse as IDE, in order to create a JSP login form, that takes a username and a password, and then invokes a Servlet called "LoginController". whenever you click the "Login" button, the LoginController verifies the input (user name and password) against the MySql database.<br>
  Well, at least this is the plan...  </p>
  
  <p>Everything seems OK, until it gets to the part where it needs to instantiate the com.mysql.jdbc.jdbc2.optional.MysqlDataSource object.<br>
  I get this nasty error:  </p>
  
  <pre><code>HTTP Status 500 - Servlet execution threw an exception
  
  type Exception report
  
  message Servlet execution threw an exception
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: Servlet execution threw an exception
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  root cause
  
  java.lang.NoClassDefFoundError: com/mysql/jdbc/jdbc2/optional/MysqlDataSource
      model.Authenticator.authenticateLogin(Authenticator.java:13)
      controller.LoginController.doPost(LoginController.java:52)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  root cause
  
  java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
      model.Authenticator.authenticateLogin(Authenticator.java:13)
      controller.LoginController.doPost(LoginController.java:52)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  note The full stack trace of the root cause is available in the Apache Tomcat/7.0.55 logs.
  </code></pre>
  
  <p>Now, I understand that the server "can't find the MysqlDataSource", I just don't see why, because I <strong>DID</strong> add the needed jar file to the project, through "Project Properties->Java Build Path->Libraries->Add External Jar...", and you can also see that it shows it in the Project Explorer:  </p>
  
  <p><img src="https://i.stack.imgur.com/oVbkt.png" alt="enter image description here"></p>
  
  <p>What even more surprising is, that when I open a regular java application project (as opposed to Dynamic Web Project), I'm able to connect to the Mysql database and run queries with no problems.<br>
  It just with Tomcat that it doesn't work...  </p>
  
  <p>I looked around online and saw that some suggested to "Attach the source..." through the "Java Source Attachment", but couldn't figure out how to do it...  </p>
  
  <p>Any help would be greatly appreciated!</p>