使用servlet登录和注册并不起作用

时间:2014-10-13 16:30:30

标签: java html servlets

所以我正在尝试创建一个登录servlet和一个注册servlet。但我似乎无法让它发挥作用。

我正在尝试建立一个在线商店,您必须先登录才能开始购物。我是网络应用程序开发的新手,所以请耐心等待。

这是我的代码:

登录:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) 
        {
            
            String username = request.getParameter("userName");
            String password = request.getParameter("password");
            
            if(!username.equals("") && !password.equals(""))
            {
                for(Customer c : Data.getCustomers())
                {
                    if(c.getUserName().equals(username) && c.getPasword().equals(password))
                    {
                        response.sendRedirect("Catalogue.html");
                    }
                    else
                    {
                        out.println("<!DOCTYPE html>");
                        out.println("<html>");
                        out.println("<head>");
                        out.println("<title>Invalid username or password</title>");            
                        out.println("</head>");
                        out.println("<body>");
                        out.println("<h1>Ooops, something went wrong</h1>");
                        out.println("<h2><b>Wrong username or password.</b></h2>");
                        out.println("<h2>Please </h2>");
                        out.println("<form>"
                                + "<input type=\"button\" value=\"Try again\""
                                + "onClick=\"history.go(-1);"
                                + "return true; \"></form>\"");
                        out.println("</body>");
                        out.println("</html>");  
                    }
                }
            }
            
            
        }
    }

注册:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        String contextPath = request.getContextPath();

        try (PrintWriter out = response.getWriter())
        {
            String firstname = request.getParameter("firstName");
            String lastname = request.getParameter("lastName");
            String username = request.getParameter("userName");
            String password = request.getParameter("password");
            String email = request.getParameter("email");

            Customer c = new Customer(firstname, lastname, username, password, email);

            if (request.getParameter("register") != null)
            {

equals("") || username.equals("") || password.equals("") || email.equals(""))
                {

                    out.println("<!DOCTYPE html>");
                    out.println("<html>");
                    out.println("<head>");
                    out.println("<title>Missing info</title>");
                    out.println("</head>");
                    out.println("<body>");
                    out.println("<h1>Ooops, something went wrong</h1>");
                    out.println("<h2><b>Please fill out all the fields to register.</b></h2>");
                    out.println("<form>"
                            + "<input type=\"button\" value=\"Try again\""
                            + "onClick=\"history.go(-1);return true; \"></form>");
                    out.println("</body>");
                    out.println("</html>");
                } else
                {

                    for (Customer customer : Data.getCustomers())
                    {
                        if (!username.equals(customer.getUserName()) &&       !password.equals(customer.getPasword()))
                        {
                            Data.addNewCustomer(firstname, lastname, username, password, email);
                 
                            response.sendRedirect("Catalogue.html");
                            
                        } else
                        {

                            out.println("<!DOCTYPE html>");
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>Invalid username</title>");
                            out.println("</head>");
                            out.println("<body>");
                            out.println("<h1>Ooops, try again!</h1>");
                            out.println("<h2><b>Username already in use</b></h2>");
                            out.println("<h2>Please choose another one and</h2>");
                            out.println("<form>"
                                    + "<input type=\"button\" value=\"Try again\""
                                    + "onClick=\"history.go(-1);"
                                    + "return true; \"></form>");
                            out.println("</body>");
                            out.println("</html>");

                        }
                    }

                }

            }
        }

    }

HTML登录:

<html>
    <head>
        <title>Login</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center><h1>Login</h1></center>
    <form action="LoginServlet"
          method="post">
        <center><dl> 
            <dt><b>Please fill in your username and password to login: </b></dt>
            <dd>Username: 
            <input type="text" name="userName"></dd>
            <dd>Password: 
            <input type="password" name="password"></dd>
            </dl></center>
    <center>
        <input type="submit" name="Login" value="Login">
        <input type="submit" name="cancel" value="Cancel">
        
        <br><br><br><br><br><br>
        <b>If you don't have an account, please </b>
        <a href='Registration.html'>register</a><b>.</b>
        
        <br><br><br><img src="online-shop.jpg">
    </form>
    </center>
    </body>
</html>

HTML注册:

<html>
    <head>
        <title>Register</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body> 
    <center><h1>Register</h1></center>
    <form method="post" action="RegistrationServlet">
        <center><dl> 
            <dt><b>Please fill in the information below to register: </b></dt>
            <dd>Firstname:
            <input type="text" name="firstName"></dd>
            <dd>Lastname:
            <input type="text" name="lastName"></dd>
            <dd>Username:
                <input type="text" name="userName"></dd>
            <dd>Password:
                <input type="password" name="password"></dd>
            <dd>E-mail:
                <input type="text" name="email"></dd>
            </dl></center>
    <center>
            <input type="submit" name="register" value="Submit" >
            <input type="submit" name="register" value="Cancel" >
        
        <br><br><br><img src="online-shop.jpg">
    </form>
    </center>
    </body>
    
</html>

2 个答案:

答案 0 :(得分:0)

您可以输入一些日志来至少检查

  1. 你的servlet正在被触发,
  2. 他们正在接收哪些请求参数值,
  3. 与这些参数进行比较的数据。
  4. 我认为您会发现,当Data.getCustomers()返回零客户时,您的每个servlet都会落在其processRequest()方法的底部,而无需发送重定向或在响应中打印任何内容&# 39;输出流。 (如果任何表单字段留空,则注册servlet将发送其错误响应。)

    此外,我认为当您有客户时,您会发现您的servlet以有趣的方式失败。他们都扫描提供的客户并为每个执行操作。你真正想要的是不同的:查找匹配给定用户名的现有客户(不考虑密码);然后根据整体查找是否成功采取行动(包括检查密码)。也许不是检索和循环所有客户,而是添加方法Data.getCustomerByUsername(String)会更方便。如果你不能这样做,那么你可以在servlet类中添加一个类似的方法。

答案 1 :(得分:0)

once try like this

out.println("<form><a href=\"catalogue.html\">link</a></form></body></html>");