Problem when mapping Servlet in web xml

Asked

Viewed 800 times

0

I’m getting error 500 when trying to map Servlet, I don’t know what can be, I think it’s the class Servlet is wrong.

inserir a descrição da imagem aqui

Servlet

public class addContatoServlet3 extends HttpServlet {
    protected 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();
        }   
    }   
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {      
        System.out.println("TESTANDO GET");
    }
}

Web XML

<?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>

inserir a descrição da imagem aqui

Edit: Eclipse even lists everything in my Servlets mapping:

inserir a descrição da imagem aqui

2 answers

0

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

Then in web.xml enter the package and class name.

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

A java class tip must follow a Camelcase (Covention) pattern where each word is capitalized and joined without space.

  • Hi Viktor, thanks for the reply, but I still have the error "HTTP Status 500 - Error instantiating Servlet class controllers.addContact" :(

  • Hello, in this case you have to post the error completely.

0

Try so :

<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 the yes as we no longer need to make the statement of 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");
    }
}
  • Hi, Thanks for the reply! I changed the web xml but I continue with the error "HTTP Status 500 - Error instantiating Servlet class controllers.addContactServlet3", unfortunately I have the need to map my Servlets by web xml by internal demand, I cannot use Notation.

  • exchange the protected for public doGet and doPOST !! and test. I think that’s it!!

  • Thanks again, I tried here, but it didn’t work

  • Bug again? you have this project on github ?

  • I replicated the exact same Servlet, even jsp, even web xml in another project and it worked... as the project is still small it won’t impact me much, but how strange... I wonder if it might be something that "defaced" on Tomcat

  • had to see the project..

Show 1 more comment

Browser other questions tagged

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