繁体   English   中英

使用servlet登录和注册无效

[英]Login and registration using servlet doesn't work

因此,我尝试制作一个登录servlet和一个注册servlet。 但是我似乎无法使其正常工作。

我试图建立一个网上商店,您必须先登录才能开始购物。 我是Web应用程序开发的新手,所以请多多包涵。

这是我的代码:

登录:

 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> 

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

  1. 您的servlet完全被触发了,
  2. 他们正在接收什么请求参数值,以及
  3. 与那些参数进行比较的数据。

但是,我想您会发现,当Data.getCustomers()返回零个客户时,您的每个servlet都脱离了processRequest()方法的底部,而没有发送重定向或在响应的输出流中打印任何内容。 (除非任何表单字段留空,否则注册Servlet将发送其错误响应)。

而且,我想您会发现,当您拥有客户时,您的servlet会以有趣的方式失败。 他们都扫描提供的客户, 并对每个客户执行操作。 您真正想要的是不同的:查找与给定用户名匹配的现有客户(不考虑密码); 然后根据整体查找是否成功采取措施(包括检查密码)。 也许与其检索和遍历所有客户, Data.getCustomerByUsername(String)添加方法Data.getCustomerByUsername(String)会更方便。 如果您不能这样做,则可以向您的Servlet类添加类似的方法。

once try like this

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM