java.lang.ClassNotFoundException: HelloServlet at org.apache.catalina.loader.WebappClassLoader.loadClass

前端 未结 4 816
有刺的猬
有刺的猬 2021-01-06 15:38

I have a Hello World servlet in webapps/hello/WEB-INF/class/HelloServlet.class and I registered it as below in web.xml:



        
相关标签:
4条回答
  • 2021-01-06 15:39

    I had the same problem (I'm using old fashion servlets for "learning"), and i solved by rebuilding the maven project (in eclipse maven-->Update Project) and cleaning the project both in eclipse and tomcat. I assume the error was thrown as the class was not compiled by maven and consequently not included in the compiled packages of tomcat.

    0 讨论(0)
  • 2021-01-06 15:42

    The main problem is that your servlet class doesn't have a package. Declare one.

    package com.example;
    
    public class HelloServlet extends HttpServlet {
    

    And when registering in web.xml, make sure you include the package:

    <servlet-class>com.example.HelloServlet</servlet-class>
    

    Also, your class file should be inside the /WEB-INF/classes directory, not /WEB-INF/class.

    webapps/hello/WEB-INF/classes/com/example/HelloServlet.class
    
    0 讨论(0)
  • 2021-01-06 15:43

    Try to keep HelloServlet class in some package(not in default package).

    0 讨论(0)
  • 2021-01-06 15:59

    Now you can just use annotations and it will be easier:

    package com.yourpackage;
    
    import java.io.IOException;
    import java.util.ResourceBundle;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class HelloWorldServlet
     */
    @WebServlet("/HelloWorldServlet")
    public class HelloWorldServlet extends HttpServlet {
    

    See WebServlet

    Cheers!!

    0 讨论(0)
提交回复
热议问题