Problem when mapping servlet in web xml

0

I'm getting 500 error when trying to map the servlet, I do not know what can be, I think it's the servlet class that's wrong.

Servlet

publicclassaddContatoServlet3extendsHttpServlet{protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException{RequestDispatcherrd;rd=request.getRequestDispatcher("/WEB-INF/View/adiciona-contato.jsp");
        System.out.println("testando POST");        
        try{
            rd.forward(request, response);
        }catch(Exception e) {
            e.printStackTrace();
        }   
    }   
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {      
        System.out.println("TESTANDO GET");
    }
}

XML Web

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>testeWeb</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>
  <servlet>
    <servlet-name>addContatoServlet3</servlet-name>
    <servlet-class>src.controllers.addContatoServlet3</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>addContatoServlet3</servlet-name>
    <url-pattern>/testando</url-pattern>
  </servlet-mapping>
</web-app>

Edit:Eclipseincludeseverythinginmyservletmapping:

    
asked by anonymous 16.01.2018 / 17:36

2 answers

0

When mapping the servlets it is not necessary to inform the 'src' directory.

Then in web.xml, inform the package and class name.

<servlet-class>controllers.addContatoServlet3</servlet-class>

A hint classes in java should follow a standard (Case) CamelCase where each word is capitalized and merged without space.     

16.01.2018 / 17:52
0

Try this:

<servlet>
    <servlet-name>addContatoServlet3</servlet-name>
    <servlet-class>controllers.addContatoServlet3</servlet-class>
  </servlet>

 <servlet-mapping>
    <servlet-name>addContatoServlet3</servlet-name>
    <url-pattern>/testando</url-pattern>
  </servlet-mapping>

You can also use yes as well as no longer need to declare your Servlets in XML .

@WebServlet(value="/testando", name="addContatoServlet3")
public class addContatoServlet3 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException{
        RequestDispatcher rd;
        rd = request.getRequestDispatcher("/WEB-INF/View/adiciona-contato.jsp");
        System.out.println("testando POST");        
        try{
            rd.forward(request, response);
        }catch(Exception e) {
            e.printStackTrace();
        }   
    }   
   public void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {      
        System.out.println("TESTANDO GET");
    }
}
    
16.01.2018 / 18:18