Servlet异常:驱动程序类名无效

时间:2013-10-16 16:47:10

标签: java jsp servlets

我是jsp的初学者,我试图从数据库中列出用户, 这是我的代码:

的Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" 
    import="java.io.*,java.util.*,java.sql.*"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>InfoVisual</title>

<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
</head>
<body>
<sql:setDataSource var="chh_cw" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/chh-cw"
     user="root"  password="1234"/>
<sql:query dataSource="${chh_cw}" var="result">
select DispUserName from user_mst;
</sql:query>
<table border="1" width="100%">
<tr>
   <th>District</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
   <td><c:out value="${row.DispUserName}"/></td>
</tr>
</c:forEach>
</table>
    <form method="post" action="Login">
        <div class="login">

            Select District
            <select name="district">

            </select>
            <br />Username <input type="text" name="username"/>
            <br />Password <input type="password" name="pswd"/>
            <br /><input type="submit" name="submit" value="Login"/>
        </div>
    </form>
</body>
</html>

我提到了网站tutorialspoint:http://www.tutorialspoint.com/jsp/jsp_database_access.htm

但是我收到了这个错误(很长时间,对不起):

   org.apache.jasper.JasperException: An exception occurred processing JSP page /Login.jsp at line 17

14: <link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
15: </head>
16: <body>
17: <sql:setDataSource var="chh_cw" driver="com.mysql.jdbc.Driver"
18:      url="jdbc:mysql://localhost/chh-cw"
19:      user="root"  password="1234"/>
20: <sql:query dataSource="${chh_cw}" var="result">


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: In &lt;driver&gt;, invalid driver class name: "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:908)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.Login_jsp._jspService(Login_jsp.java:126)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Class.java:270)
    org.apache.taglibs.standard.tag.common.sql.DataSourceWrapper.setDriverClassName(DataSourceWrapper.java:55)
    org.apache.taglibs.standard.tag.common.sql.SetDataSourceTagSupport.doStartTag(SetDataSourceTagSupport.java:111)
    org.apache.jsp.Login_jsp._jspx_meth_sql_005fsetDataSource_005f0(Login_jsp.java:152)
    org.apache.jsp.Login_jsp._jspService(Login_jsp.java:92)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我的项目结构: enter image description here

抱歉,这个问题变得太长了。请指导我解决这个问题。 Thanx:)

2 个答案:

答案 0 :(得分:4)

问题是您没有在项目中包含MySQL驱动程序。它在stacktrace中清楚地说明了:

  

java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

对于Web应用程序项目,请确保将所有第三方库(即jar)放在WEB-INF / lib文件夹中,而不是其他任何地方。

另外,根据您的图片,您似乎有两个MySQL驱动程序。只使用一个,摆脱另一个。

更多信息:

答案 1 :(得分:0)

尝试将mysql连接器jar添加到项目的部署程序集中。为此,请右键单击该项目并打开“部署程序集”选项卡。然后将Jar添加到程序集中。