本文介绍了ClassNotFoundException通过Java的Servlet运行JDBC时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试创建MysqlDataSource对象时,我得到了这个奇怪的异常(或者至少我认为这是问题的根源).

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

让我首先准确地描述一下到目前为止我所拥有的:
我使用Tomcat7作为容器,并使用Eclipse作为IDE,以创建一个JSP登录表单,该表单需要用户名和密码,然后调用一个名为"LoginController"的Servlet.每当您单击登录"按钮时,LoginController都会根据MySql数据库验证输入(用户名和密码).
好吧,至少这是计划...

Let me first describe exactly what I have so far:
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.
Well, at least this is the plan...

一切似乎都正常,直到到达需要实例化com.mysql.jdbc.jdbc2.optional.MysqlDataSource对象的部分为止.
我收到这个讨厌的错误:

Everything seems OK, until it gets to the part where it needs to instantiate the com.mysql.jdbc.jdbc2.optional.MysqlDataSource object.
I get this nasty error:

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.

现在,我了解服务器找不到MysqlDataSource",我只是不明白为什么,因为我通过 DID 通过项目属性"将所需的jar文件添加到了项目中-> Java Build Path-> Libraries-> Add External Jar ...",您还可以看到它在Project Explorer中显示了它:

Now, I understand that the server "can't find the MysqlDataSource", I just don't see why, because I DID 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:

更令人惊讶的是,当我打开一个常规的Java应用程序项目(与Dynamic Web Project相对)时,我能够连接到Mysql数据库并运行查询而没有任何问题.
只是在Tomcat中,它不起作用...

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.
It just with Tomcat that it doesn't work...

我在网上四处张望,发现有人建议通过"Java源附件"附加源...",但不知道该怎么做...

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

任何帮助将不胜感激!

推荐答案

将依赖项中的文件添加为外部jar时,在构建应用程序时该文件不会包含在war文件中.您需要将jar复制到项目中的WEB-INF/lib目录中,或将其放在Tomcat lib目录中

When you add file in dependencies as external jar, it does not get included in the war file when building the app. You need to either copy the jar to the WEB-INF/lib directory in the project or put it in the Tomcat lib directory

这篇关于ClassNotFoundException通过Java的Servlet运行JDBC时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:41