java.lang.ClassNotFoundException: com.servlets.QueryServletMv

764 views Asked by At

I keep getting this error when i run my servlet on Tomcat 7.0. It seems I cannot find where the problem is. The code looks fine to me.

Here is my 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();
        }
    }

}

And here is the html page:

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

I get the ClassNotFoundException yet i have used the correct package name. Here is the Stack trace

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

There are 1 answers

2
Anil Reddy Yarragonda On

The 404 or Not Found error message is an HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.

The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken or dead link; hence the 404 error is one of the most recognizable errors users can find on the web.

first check whether your server listing with your port number or not:

use localhost://portnumber