Servlet mapping via annotation @Webservlet is not working

Asked

Viewed 54 times

1

I’m a beginner in Javaweb development. I’m using Java version 11 and Tomcat version 9.0. When using the annotation @WebServlet(urlPatterns) in the classes OiMundoServlet.java and NovaEmpresaServlet.java, the following error message appears when calling Urls http://localhost:8080/gerenciador/oi and http://localhost:8080/gerenciador/novaEmpresa:

"HTTP Status 404 – Não Encontrado
Type Status Report

Message The requested resource [/gerenciador/novaEmpresa] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.41"

I ask for help to be able to solve the problem, to make that when calling the Urls in the browser the content is shown.

NOTE: When I call the url http://localhost:8080/gerenciador/bem-vindo.html the content is perfectly shown in the browser.

Below are the classes, file html and the web.xml:

package br.com.alura.gerenciador.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/oi")
public class OiMundoServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("oi mundo, parabens vc escreveu o primeiro servlets.");
        out.println("</body>");
        out.println("</html>");
        
        System.out.println("O Servlet OiMundoServlet foi chamado");

    }
}   
package br.com.alura.gerenciador.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/novaEmpresa")
public class NovaEmpresaServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Cadastrando nova empresa");
        
        PrintWriter out = response.getWriter();
        out.println("<html><body>Empresa Cadastrada com sucesso!</body></html>");
    }

}

welcome.html

<html>

    <body>
    <p>Bem vindos ao curso de Servlets da Alura.</p>
    </body>

</html>

web xml.

    <?xml version="1.0" encoding="UTF-8"?>

<web-app version="4.0" id="WebApp_ID"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    metadata-complete = "true"
    >

    <display-name>gerenciador</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>
  • looking at it like this, I realized that your service method within the company is not annotated with @override, but there must be more going around...

  • Little grasshopper, doing a hello world on Tomcat work is an old classic Java Web programming and adds a lot to your knowledge. In golden times it was necessary to declare its classes associated with the respective URL Patterns in the web.xml file. With the advent of the Annotations I do not know if this is still necessary today. Tips: Web application running as Tomcat root application is different from non-root application. Absolute path (http://localhost...) is different from relative (manager/novaPress) which is different from relative root absolute (/manager...). Good luck.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.