0

I've been struggling with a servlet in the maven web application for hours. I'm not able to run it. It always returns The requested resource is not available. I'm using Tomcat 8. This is my servlet code. I'm using this address http://localhost:8080/my-app/MyServlet to run it but I'm not successful.

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MyServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet MyServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
}
user3637775
  • 499
  • 4
  • 20

1 Answers1

0
  1. Check if your tomcat works fine by using http://localhost:8080 (Tomcat welcome page is shown )

  2. Check if your context path is "/my-app", "/" or something else. e.g. if you context path is just /, http://localhost:8080/MyServlet should work.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39