HelloWorld servlet中的Tomcat 404 classNotFoundException

时间:2014-11-21 16:47:14

标签: java tomcat servlets classnotfoundexception

我看到大多数类似的问题并尝试了大多数解决方案,但没有任何对我有用!

我在Java EE中是全新的,这是我的第一个servlet。

这是我的servlet放在:C:\tomcat\webapps\jspbook\WEB-INF\classes

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title> hello World Title </title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1> Hello To World in body!</h1>");
    out.println("</body>");
    out.println("</html>");
   }
}

这是web.xml

中的C:\tomcat\webapps\jspbook\WEB-INF个文件

 <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
 </servlet>

 <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
 </servlet-mapping> 


<welcome-file-list>
<welcome-file> welcome.html </welcome-file>
</welcome-file-list>

</web-app>

现在,当我重新启动tomcat时,请尝试http://127.0.0.1:8080/jspbook/HelloWorld

第一次时间这是我的第一个错误:

type Exception report

message Error instantiating servlet class HelloWorld

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

exception

javax.servlet.ServletException: Error instantiating servlet class HelloWorld
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    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:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:722)

root cause

java.lang.ClassNotFoundException: HelloWorld
    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:503)
    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:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:722)

当我重新加载页面时,这是我的第二个错误:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

我的代码出了什么问题?

更新

我创建了一个包并将其命名为com.jspbook并将其放在C:\tomcat\webapps\jspbook\WEB-INF\classes\com\jspbook\HelloWorld.java

package com.jspbook;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title> hello World Title </title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1> Hello To World in body! </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

web.xml在这里:C:\tomcat\webapps\jspbook\WEB-INF\web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">

 <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.jspbook.HelloWorld</servlet-class>
 </servlet>

 <servlet-mapping>

    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>

 </servlet-mapping> 



<welcome-file-list>

<welcome-file> welcome.html </welcome-file>

</welcome-file-list>

</web-app>

我尝试http://127.0.0.1:8080/jspbook/HelloWorld,但出现相同的错误!

(根本没有编译的类文件)

1 个答案:

答案 0 :(得分:1)

请将servlet放入包中。容器无法从默认包中加载类。