I'm learning how to do a maven dynamic web application with Eclipse and Tomcat. The applicacion works without servlet, but I get that error when I try to introduce it in my code. I debugged to find the problem. I think, these are the relevant points:
To be able to deploy a war file, I added to maven the dependency "servlet-api" with provided scope as suggested here: How to compile a servlet for Tomcat in command line? error: package javax.servlet does not exist
The error is in MostrarLibros.jsp. If I use the the next code without servlet, everything works:
listaDeLibros = Libro.buscarTodos();
But if I change that line to the one provided below, the exception appears. Actually, the variable "listaDeCategorias" is null after running it (I debugged it):
listaDeCategorias = (List<String>) request.getAttribute("listaDeCategorias");
I add a break point in the Servlet, ControladorLibros.java, but the debugger never stops there. Code:
package com.arquitecturajava; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ControladorLibros extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher despachador = null; if (request.getServletPath().equals("/MostrarLibros.do")) { List<Libro> listaDeLibros = Libro.buscarTodos(); List<String> listaDeCategorias = Libro.buscarTodasLasCategorias(); request.setAttribute("listaDeLibros", listaDeLibros); request.setAttribute("listaDeCategorias", listaDeCategorias); despachador = request.getRequestDispatcher("MostrarLibros.jsp"); } else if ... (...) request.setAttribute("listaDeLibros", listaDeLibros); request.setAttribute("listaDeCategorias", listaDeCategorias); despachador = request. getRequestDispatcher("MostrarLibros.jsp"); } despachador.forward(request, response); } }
4 - web.xml:
<servlet>
<description></description>
<display-name>ControladorLibros</display-name>
<servlet-name>ControladorLibros</servlet-name>
<servlet-class>com.arquitecturajava.ControladorLibros</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControladorLibros</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5 - Project structure:
Is it because the servlet isn't found by the application? In this case, why isn't found?