Java Servlet不重定向

时间:2015-10-23 21:48:16

标签: java servlets

我相信我的代码应该重定向到"登录" servlet的。它目前还没有使用任何验证,所以实际上它应该只是一次运行时将我重新引导到另一个servlet。我没有遇到404错误或500级错误,它只是将我重定向到我的Register servlet而不是登录

@WebServlet("/Register")
public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void init() throws ServletException {
        // Create an array list of guest book HW1Users
        ArrayList<HW1User> HW1Users = new ArrayList<HW1User>();
        // Add the array list to the application scope
        getServletContext().setAttribute("HW1Users", HW1Users);     
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"ISO-8859-1\">");
        out.println("<title>Israel, Dylan - CS320stu32</title>");
        out.println(
                "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<form class=\"form-horizontal\">");

        out.println("<div class=\"form-group\">");
        out.println("<label for=\"name\" class=\"col-sm-2 control-label\">Name:</label>");
        out.println("<div class=\"col-sm-10\">");
        out.println("<input type=\"text\" class=\"form-control\" id=\"name\" placeholder=\"First and Last Name\">");
        out.println("</div>");
        out.println("</div>");

        out.println("<div class=\"form-group\">");
        out.println("<label for=\"inputEmail3\" class=\"col-sm-2 control-label\">Email:</label>");
        out.println("<div class=\"col-sm-10\">");
        out.println("<input type=\"email\" class=\"form-control\" id=\"inputEmail3\" placeholder=\"Enter Email\">");
        out.println("</div>");
        out.println("</div>");

        out.println("<div class=\"form-group\">");
        out.println("<label for=\"password1\" class=\"col-sm-2 control-label\">Password:</label>");
        out.println("<div class=\"col-sm-10\">");
        out.println("<input type=\"text\" class=\"form-control\" id=\"password1\" placeholder=\"Enter Password\">");
        out.println("</div>");
        out.println("</div>");

        out.println("<div class=\"form-group\">");
        out.println("<label for=\"password2\" class=\"col-sm-2 control-label\">Re-Enter Password:</label>");
        out.println("<div class=\"col-sm-10\">");
        out.println("<input type=\"text\" class=\"form-control\" id=\"password2\" placeholder=\"Re-Enter Password\">");
        out.println("</div>");
        out.println("</div>");

        out.println("<div class=\"form-group\">");
        out.println("<div class=\"col-sm-offset-2 col-sm-10\">");
        out.println("<input type=\"submit\" value = \"Register\"/>");
        out.println("</div>");
        out.println("</form>");
        // Grab a reference to the list of HW1Users
        out.println("</body>");
        out.println("</html>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String email = request.getParameter("email");

        ArrayList<HW1User> HW1Users = (ArrayList<HW1User>)getServletContext().getAttribute("HW1Users");

        HW1Users.add(new HW1User(name,password1,password2,email));
        // Add the new entry to the list

        if(HW1Users!= null){

        response.sendRedirect("/Login");
        }
        doGet(request, response);
    }
}

在Kumar的回答之后,我将我的代码更新为:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String name = request.getParameter("name");
    String password1 = request.getParameter("password1");
    String password2 = request.getParameter("password2");
    String email = request.getParameter("email");

    ArrayList<HW1User> HW1Users = (ArrayList<HW1User>)getServletContext().getAttribute("HW1Users");

    HW1Users.add(new HW1User(name,password1,password2,email));
    // Add the new entry to the list

    if(HW1Users!= null){

    response.sendRedirect("/Login");
    return; // returns causing response stream to be committed only once
    }
    doGet(request, response); // This will treat your request as a get request only if HW1Users is null
}

这是我的登录Servlet代码,表明它不会重定向任何内容。

                     @WebServlet("/Login")
     public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("   <meta charset=\"UTF-8\">");
    out.println("   <title>Sessions Login</title>");
    out.println("   <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
    out.println("</head>");
    out.println("<body>");
    out.println("<div class=\"container\">");

    String error = (String) request.getAttribute("error");
    if (error != null)
        out.println("<p class=\"text-danger lead\">" + error + "</p>");

    // Page-Specific Content
    out.println("<form action=\"Login\" method=\"post\" >");
    out.println("   Username: <input type=\"text\" name=\"username\" /> <br />");
    out.println("   Password: <input type=\"password\" name=\"password\" /> <br />");
    out.println("   <input type=\"submit\" value=\"Login\" />");

    out.println("</form>");

    out.println("</div>");
    out.println("</body>");
    out.println("</html>    ");
}

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

    if (username != null && username.equals("albert") 
            && password != null && password.equals("1!") ){

        // Create a Session Variable named Username
        HttpSession session = request.getSession();
        session.setAttribute("username", "Albert F. Cervantes");

        // Redirect to the Member's Only page
        response.sendRedirect("Members");
    }
    else {
        request.setAttribute("error", "Invalid Username and/or Password");
        doGet(request, response);
    }

}

}

1 个答案:

答案 0 :(得分:2)

您的回复将通过response.sendRedirect("/Login");doGet(request, response);提交两次。

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String email = request.getParameter("email");

        ArrayList<HW1User> HW1Users = (ArrayList<HW1User>)getServletContext().getAttribute("HW1Users");

        HW1Users.add(new HW1User(name,password1,password2,email));
        // Add the new entry to the list

        if(HW1Users!= null){    
        response.sendRedirect("/Login");
        return; // returns causing response stream to be committed only once
        }
        doGet(request, response); // This will treat your request as a get request only if HW1Users is null 
    }