0

每当他按下分配的按钮时,我都会尝试重定向用户。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    // Create a print writer
    PrintWriter SystemOut = response.getWriter();

    // Call Header servlet
    RequestDispatcher headerRD = request.getRequestDispatcher("/HeaderQuintos");
    headerRD.include(request, response);

    SystemOut.println("<html><body><h2>Main Servlet</h2>");

    // User input user and pass
    String uName = request.getParameter("username");
    String pWord = request.getParameter("password");

    // Gets the passed value
    String uNamePassed = (String) request.getAttribute("uNamePassed");
    String pWordPassed = (String) request.getAttribute("pWordPassed");

    // Error for somehow null result of a username and password
    //if (uName == null || pWord == null) {
    //  response.sendError(response.SC_BAD_REQUEST, "Bad Request");
    //  return;
    //}

    SystemOut.println("<h2>User Credentials</h2>");
    SystemOut.println("<br>Username: " + uNamePassed);
    SystemOut.println("<br>Password: " + pWordPassed);

    SystemOut.println("<h2>Links for Your Pleasure:</h2>");

    SystemOut.println("<form>");
    SystemOut.println("<input type=submit name=amzBtn value=Amazon>");
    SystemOut.println("<input type=submit name=glgBtn value=Google>");
    SystemOut.println("<input type=submit name=fbBtn value=Facebook>");

    SystemOut.println("</form>");
    SystemOut.println("</body></html>");

    /*
     * Logic to redirect user
     * if the button was clicked
     */
    if (request.getParameter("amzBtn") != null) {
            response.sendRedirect("http://www.amazon.com/");
    }
    else if (request.getParameter("fbBtn") != null) {
            response.sendRedirect("https://www.facebook.com/");
    }
    else if (request.getParameter("glgBtn") != null) {
            response.sendRedirect("https://www.google.com.ph/");
    }

    // Calling Footer servlet
    RequestDispatcher footerRD = request.getRequestDispatcher("/FooterQuintos");
    footerRD.include(request, response);
}

问题是,每当我按下按钮时,它都会返回到我的登录 servlet。逻辑问题出在哪里?是在我的 if 语句中吗?

4

0 回答 0