0

I am learning java, and trying to run Servlet on local machine and i ran into a problem. No matter what i do it returns 404.

MyServlet.java

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

public class MyServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
                         throws ServletException, IOException {
        

        System.out.println("Runing servlet");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>Hello Readers</h1>");
        out.println("</body></html>");
    }
}

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Saturated</servlet-name>
        <servlet-class>MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Saturated</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Folder structure:

/Saturated
 /WEB-INF
  /classes
   MyServlet.class
  /lib
   servlet-api.jar
  web.xml

I have checked catalina logs, /Saturated is started. i checked access log, it responds with 404

Anton Stafeyev
  • 761
  • 1
  • 7
  • 20
  • There is no problem writing like this. – wangtianye Aug 24 '20 at 13:09
  • _it returns 404_ - Can you clarify what is returning 404? What URL are you using in your browser address bar? – andrewJames Aug 24 '20 at 13:12
  • @andrewjames if i request the page in browser, http://localhost:8080/Saturated/hello i get a 404 error – Anton Stafeyev Aug 24 '20 at 14:18
  • Two questions: (1) Can you see the Tomcat welcome page at `localhost:8080`? (2) In your `MyServlet.java` file, what is the package name? That should be the first line of code at the top of the file: `package ...` (assuming you have defined a package for your application, and are not using the [default no-name package](https://stackoverflow.com/questions/2335211/what-is-the-default-package-in-which-my-classes-are-put-if-i-dont-specify-it)). – andrewJames Aug 24 '20 at 14:27

0 Answers0