I have a Hello World servlet in webapps/hello/WEB-INF/class/HelloServlet.class
and I registered it as below in web.xml
:
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.
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!!