JSP does not recognize servlets

0

I am developing a project in jsp with Netbeans (I have already developed several) but in this unlike the previous ones that I have developed I have decided to organize the code much better by classifying it in folders (the jsp files inside the root folder which is the web folder) but when doing this I realize that in this way the servlets are not recognized. I did the test by moving one of those files to the root folder (web) and immediately upon filling out the form, I recognized the servlet. The question is, is there any way to recognize the servlets and continue with the organization of the code? I really do not want to have all the jsp files in the same folder for organizational reasons.

    
asked by Jesus Escorcia 23.09.2017 в 06:19
source

1 answer

0

Be sure to call the servlets by their absolute path, not their relative path.

Example:

Folder structure:

src
  - edu.ltmj.servlets
    + MiServlet.java
web
  + index.jsp
  - WEB-INF
    - mi
      - carpeta
        - de
          + vista.jsp

Servlet:

@WebServlet("/MiServlet")
public class MiServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        String nombre = request.getParameter("nombre");
        request.setAttribute("nombre", nombre);
        request.getRequestDispatcher("WEB-INF/mi/carpeta/de/vista.jsp")
            .forward(request, response);
    }
}

index.jsp (omission head and body for brevity of the code):

<form action="/MiServlet">
    <label for="nombre">Ingresa nombre</label>
    <input type="text" name="nombre" id="nombre" placeholder="Nombre" />
    <input type="submit" value="Saludar" />
</form>

vista.jsp

Hola ${nombre}. Saludemos a alguien más.
<form action="/MiServlet">
    <label for="nombre">Ingresa nombre</label>
    <input type="text" name="nombre" id="nombre" placeholder="Nombre" />
    <input type="submit" value="Saludar" />
</form>
    
answered by 23.09.2017 в 06:34