0

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:

  1. 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

  2. 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");
    
  3. 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:

project structure inside Eclipse

Is it because the servlet isn't found by the application? In this case, why isn't found?

Community
  • 1
  • 1
chelder
  • 3,819
  • 6
  • 56
  • 90
  • 1
    If the debugger doesn't stop at the breakpoint, this is an indication that your application is running on an older version of your code. Clean and rebuild your project. – Sotirios Delimanolis Apr 23 '13 at 17:34
  • there's also a setting that turns off all breakpoints, though – DigCamara Apr 23 '13 at 17:37
  • 1
    Given the fact there's a big IF in the servlet, you'd put a breakpoint in both cases. – Alfabravo Apr 23 '13 at 17:40
  • Just check if you didn't use this option by accident http://stackoverflow.com/questions/4116260/enable-and-disable-all-breakpoints-in-eclipse – DigCamara Apr 23 '13 at 17:40
  • Does anything change if you use prefix/suffix/fixed mapping instead of declaring servlet as a default one? – skuntsel Apr 23 '13 at 17:56
  • stupid question, but are you actually debugging the app or did you just place a breakpoint. Is the debugger running and connected to the server instance? – eis Apr 23 '13 at 18:02
  • Sotirios, DigCamara, and eis: I also added a breakpoint in MostrarLibros.jsp. The debugger stops there but not in the servlet. I clean and redeploy with Maven. No luck. – chelder Apr 23 '13 at 22:49
  • @Alfabravo: I put the breakpoint at the first if. It should stop there! – chelder Apr 23 '13 at 22:50
  • @skuntsel: I don't understand. You want that I change / to something, right? – chelder Apr 23 '13 at 22:50
  • UPDATE: I updated the point 2 of my question to make it clearer. – chelder Apr 23 '13 at 22:51

0 Answers0