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

烂漫一生 提交于 2019-12-01 00:18:01
adarshr

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!