java.lang.ClassNotFoundException : com. servlets.QueryServletMv 异常

标签 java tomcat servlets classnotfoundexception

当我在 Tomcat 7.0 上运行我的 servlet 时,我不断收到这个错误。看来我找不到问题出在哪里。代码对我来说看起来不错。

这是我的 Servlet。

package com.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/querymvservlet")
public class QueryServletMv extends HttpServlet {
private static final long serialVersionUID = 1L;


public QueryServletMv() {
    super();

}


protected void doGet(HttpServletRequest request, HttpServletResponse     response) throws ServletException, IOException {

    // Set the MIME type for the response message
    response.setContentType("text/html");

    //Get a output writer to write the response into the network
    PrintWriter out = response.getWriter();


    Connection conn = null;
    Statement stmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        // STEP 1: Create a database "Connection" object 
        // For MySQL
        conn = DriverManager.getConnection("jdbc:mysql://localhost     /ebookshop", "root", "");

        // STEP 2: Create a "Statement" object inside the "Connection"
        stmt = conn.createStatement();

        // STEP 3: Execute a SQL SELECT query
        String[] authors = request.getParameterValues("author");

        if(authors == null){
            out.println("<h2>Please go back and select an author</>");
            return;
        }

        String sql = "SELECT * FROM books WHERE author IN (";
        sql +="'" + authors[0] + "'"; // First author
        for (int i = 1; i < authors.length; i++ ){
            sql += ", '" + authors[i] + "'";
        }
        sql += ") AND qty > 0 ORDER BY author ASC, title ASC";
        // Print an HTML page as output of query
        out.println("<html><head><title></title></head><body>");
        out.println("<h2>Thank you for your query. </h2>");
        out.println("<p>Your query is: " + sql + "</p>");

        // Send the query to the server
        ResultSet rs = stmt.executeQuery(sql);

        // STEP 4: Process the query result
        int count = 0;
        while (rs.next()) {
            // Print a paragraph <p>....</p> for each row
            out.println("<p>"+ rs.getString("author") 
                    + ", " + rs.getString("title")
                    + ", $" + rs.getDouble("price") + "<p>");
             ++count;
        }
        out.println("<p>==========" + count + " records found ======</p>");
        out.println("</body></html>");
    } catch (SQLException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        out.close();

        try {
            // STEP 5: Close the Statement and Connection
            if(stmt != null) stmt.close();
            if(conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

这是 html 页面:

<html>
<head>
<title>Yet another e-Bookshop MV</title>
</head>
<body>
<h2>Yet Another e-Bookshop MV</h2>
<form method="get" action="querymvservlet">
    Choose an author: <br /><br />
    <input type="checkbox" name="author" value="Tan Ah Teck" />Ah Teck
    <input type="checkbox" name="author" value="Mohammad Ali" /> Ali
    <input type="checkbox" name="author" value="Kumar" />Kumar
    <input type="checkbox" name="author" value="Kevin Jones" />Kevin
    <input type="submit" value="Search" />
</form>

</body>
</html>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com /xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>querybookmv</servlet-name>
<servlet-class>com.servlets.QueryServletMv</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>querybookmv</servlet-name>
<url-pattern>/querymvservlet</url-pattern>
</servlet-mapping>

</web-app>

我得到了 ClassNotFoundException,但我使用了正确的包名。 这是堆栈跟踪

type Exception report

message Error instantiating servlet class com.servlets.QueryServletMv

description The server encountered an internal error that prevented it  from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class    com.servlets.QueryServletMv
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

java.lang.ClassNotFoundException: com.servlets.QueryServletMv
 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
  org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

最佳答案

404 或 Not Found 错误消息是一个 HTTP 标准响应代码,表示客户端能够与给定服务器通信,但服务器找不到所请求的内容。

当用户试图访问损坏或失效的链接时,网站托管服务器通常会生成“404 Not Found”网页;因此,404 错误是用户可以在网络上找到的最容易识别的错误之一。

首先检查您的服务器是否列出了您的端口号:

使用localhost://portnumber

关于java.lang.ClassNotFoundException : com. servlets.QueryServletMv 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934641/

相关文章:

spring - Tomcat 上下文根 - 将 Spring 应用程序部署到 Tomcat 时出现 Tiles 异常

javascript - 使用servlet从/WEB-INF/异步加载javascript : general concern

java - 在 tomcat 6 中部署 spring 应用程序时出现 "Access is denied"消息

java - IntelliJ IDEA - 如何将 .java 文件复制为资源?

java - 使用 JDBC 更新多行

java - Android 我需要在数组中间的字符串中间有 1 个上标字符

java - 谁(网络服务器或开发人员)负责为每个浏览器维护一个 http session ?

java - 特定使用 Servlet 后将 mysql 列值重置为默认值

java - 为什么我的 android 应用程序读取 åäö 而我的 java 应用程序却没有(从 GAE 获取数据时)?

java - libgdx vector 2 : shooting ball at an angle