-1

I'm trying to redirect from current page (index.jsp) after user logs in to another page. I get no error for this and can't really understand what the problem is. Here is my code of the servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.getWriter().append("Served at: ").append(request.getContextPath());

    usernameInput = request.getParameter("username");
    passwordInput = request.getParameter("password");
    Connection conn = null;
    try {
        // Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // Open a connection
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // Execute SQL query
        String sql;
        sql = "SELECT * FROM `users` where Username=? and Password=?";
        PreparedStatement prepStmt = conn.prepareStatement(sql);
        prepStmt.setString(1, usernameInput);
        prepStmt.setString(2, passwordInput);
        ResultSet rs = prepStmt.executeQuery();

        if (rs.next()) {
            System.out.println("User login is valid in DB");
 //Here is where I try to log in. I tryed both the commented and uncommented way to log in but none seems to work.
            response.sendRedirect(request.getContextPath() + "resources/main.jsp");
            /*RequestDispatcher reqDisp = request.getRequestDispatcher("../WebContent/resources/main.jsp");
            reqDisp.forward(request, response);*/


        }
    } catch (Exception e) {
        System.out.println("validateLogon: Error while validating password: " + e.getMessage());
        try {
            throw e;
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I can verify that it enters in the specific section of code since I see in the consele the message printed. If I could guess the problem, it may be I'm not giving the correct path for the main.jsp page. Here is how my project is structured.

enter image description here

Any ideas??Thanks!

  • 1
    Okay, so you know what redirect does? Open up the dev tools in your browser and see what URL the browser actually attempts to load in response to the redirect. http://stackoverflow.com/questions/20371220/what-is-the-difference-between-response-sendredirect-and-request-getrequestdis – Alan Hay Aug 30 '16 at 17:38

1 Answers1

1

According to HttpServletRequest's API located here:

The path starts with a "/" character but does not end with a "/" character.

Therefore your line

response.sendRedirect(request.getContextPath() + "resources/main.jsp");

May output some mashed-together string that isn't your intended path. Try debugging to the console the result of request.getContextPath() + "resources/main.jsp" and see if it is what you are trying to get to. You may need to change that line to:

response.sendRedirect(request.getContextPath() + "/resources/main.jsp");

Unrelated, but using doGet may not be the best idea here, since a GET request's parameters will be included in the URL - including your user's plain text password.

Michael Guinn
  • 117
  • 1
  • 8
  • What is the result ofSystem.out.println(request.getContextPath() + "resources/main.jsp"); – Michael Guinn Sep 01 '16 at 17:41
  • that's the result: /Sample_LogIn_Page/resources/main.jsp –  Sep 04 '16 at 14:00
  • Are you able to navigate to that page directly? As in typing into the address bar "http:///Sample_LogIn_Page/resources/main.jsp Also how are you building your WAR? Is it named Sample_LogIn_Page.war and is the resources directory located in its root? – Michael Guinn Sep 05 '16 at 16:48
  • Yes I can access main.jsp normaly. –  Sep 06 '16 at 20:14