[servlet] A rookie, learning servlet according to the tutorial, keeps displaying 404. I don't know why.

URL: tutorials I refer to

I typed the code according to the tutorial I referenced.

first, the java code is:

//java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


//HttpServlet 
public class HelloWorld extends HttpServlet {
    private String message;
    public void init() throws ServletException
    {
        //
        message = "Hello World";
    }

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

        //
        response.setContentType("text/html");

        //
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    public void destroy()
    {
        //
    }
}
After compiling this code successfully, I followed the tutorial and put the class file into < Tomcat-installation-directory > / webapps/ROOT//WEB-INF/classes (there is no classes folder under the WEB-INF file, and then I created a new one myself)

clipboard.png

xml

clipboard.png

clipboard.png


Tomcatbinstartup.up
:http://localhost:8080/HelloWorld
:

clipboard.png
tomcat

clipboard.png

so I"d like to ask what the problem is.

Jul.04,2022

404 means that the specified path cannot be found. You can check whether the path of your servlet is written correctly, mainly by checking the path in the xml file. For example, whether the path under the < servlet-class > tag can be located to your class file


your controller is not mapped to helloworld,mapping. Write a


HelloWorld class source code and add a package name. Add the package name and remember to change the web.xml file.


I see. The problem is compilation.

Menu