4

I'm building an application and currently working on my login page. When I launch Tomcat it starts up fine and opens a browser which displays my index.jsp page to log in. However on successful log in I am not forwarded to the next jsp page as instructed in the login servlet, instead I get a 404 "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."

I have checked the web.xml I which looks fine, I'm using annotations to map servlets. I dont know what else to try. Even if I deliberately enter incorrect log in details I receive the same error rather than the output contained in the "else" statement.

@WebServlet("/Login")
public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String uname = request.getParameter("username");
        String pwd = request.getParameter("password");

        if (uname.equals("MyUsername") && pwd.equals("MyPassword")){
            RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/welcome.jsp");
            dispatcher.forward(request, response);
        }else {
            PrintWriter writer = response.getWriter();
            writer.write("User details not recognised");
        }


    }

}
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

</web-app>
Neloop
  • 139
  • 7
HenkeL84
  • 41
  • 1

1 Answers1

2

If you forward to /WEB-INF/welcome.jsp, the server looks for a file welcome.jsp inside a directory WEB-INF in your context root, that is the WEB-INF directory.

So, you should not forward to /WEB-INF/welcome.jsp, but to /welcome.jsp.

Anyway, WEB-INF is reserved and does not work, no matter where you redirect.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Thanks for the suggestion. I tried just using "/welcome.jsp", that doesnt work. When I've used servlets in the past I've always used the relative path and its worked fine. Even when I'm not forwarding to that jsp page by entering incorrect user details, I should hit my "else" statement however I still get the 404. – HenkeL84 Aug 24 '19 at 21:19
  • Is `welcome.jsp` located in the same directory as `index.jsp`? – dan1st Aug 24 '19 at 21:21
  • I think tomcat does not allow requests to `WEB-INF`. – dan1st Aug 24 '19 at 21:22
  • welcome.jsp is not in the same directory as index.jsp, if I put index.jsp into the WEB-INF directory I get the 404. If I take welcome.jsp out of the WEB-INF directory and leave it where index.jsp is, nothing changes – HenkeL84 Aug 24 '19 at 21:32
  • The `WEB-INF` directory is reserved. Put `welcome.jsp` out of it and try to access if from the browser/curl/http(s). – dan1st Aug 24 '19 at 21:34
  • That worked!! I can now hit the jsp from the browser, why cant I forward it from my servlet? I'll paste the xml below for reference: – HenkeL84 Aug 24 '19 at 21:39
  • index.jsp welcome.jsp – HenkeL84 Aug 24 '19 at 21:39
  • What if you forward to `welcome.jsp` or `/welcome.jsp`? (Try both) – dan1st Aug 24 '19 at 21:40
  • I have but no joy. I'm concerned that it wont display the output in the else statement also, that issue makes me think its not the forwarding thats the problem, I shouldn't get a 404 when I'm trying to print output to that page. – HenkeL84 Aug 24 '19 at 21:44
  • Yes, numerous times – HenkeL84 Aug 24 '19 at 22:00
  • Can you forward to `index.jsp`? – dan1st Aug 24 '19 at 22:14
  • index.jsp opens in the browser with no issues when tomcat starts up. This is where I have my login form – HenkeL84 Aug 25 '19 at 07:45