Search code examples
javamysqljspservlets

I feel like the server cannot find my file even though the file path is correct


My plan is to check all textfields whether they are empty or not. If empty I want to display an error message and keep displaying the SignUp.jsp. This is the code:

if (uEmail.isEmpty() || uFName.isEmpty() || uLName.isEmpty() || uPass.isEmpty()) {
    request.setAttribute("errorMessage", "All fields are required!");
    dispatcher = request.getRequestDispatcher("${pageContext.request.contextPath}/user/SignUp.jsp");
}
dispatcher.forward(request, response);

But the problem is I feel like the server cannot find my file even though the file path is correct.

This is my file path: enter image description here

My expectation is for the servlet to keep displaying the sign up page (SignUp.jsp) whenever any block code is executed.

Btw, I have already tried changing the file path to

dispatcher = request.getRequestDispatcher("user/SignUp.jsp");

or

dispatcher = request.getRequestDispatcher("SignUp.jsp");

But still didn't work.


Solution

  • The correct path from the web app root folder will be

    /user/SignUp.jsp
    

    So you can use this code to forward to the JSP

    RequestDispatcher dispatcher = request.getRequestDispatcher("/user/SignUp.jsp");
    dispatcher.forward(request, response);
    

    The problem is in your code where you get a dispatcher in the if block and if the condition is false then a dispatcher won't get a path to the JSP.