为什么无论输入什么,都会显示“账号和密码正确”?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.bean.*,com.imooc.db.* "%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String account=request.getParameter("account"); String password=request.getParameter("password"); Emp emp=new Emp(account,null,password,null); boolean flag=DBUtil.selectEmpByAccountAndPassword(emp); //Map<String,Emp> map=DBUtil.map; if(flag==true){ out.println("账号和密码正确"); }else{ out.println("账号和密码错误"); } %> </body> </html>
为什么无论输入什么,都会显示“账号和密码正确”?
0
收起
正在回答
4回答
如下图所示,DBUtil类中的if语句后面不应添加分号,应将分号去掉。
修订之后的效果如下,如果我的建议解决了你的问题,请采纳,祝学习愉快~
彻醉人生
2018-10-10 19:28:20
package com.imooc.bean; public class Emp { private String account; private String name; private String password; private String email; public Emp(String account, String name, String password, String email) { super(); this.account = account; this.name = name; this.password = password; this.email = email; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
彻醉人生
2018-10-10 19:27:58
package com.imooc.db; import java.util.HashMap; import java.util.Map; import com.imooc.bean.Emp; public class DBUtil { public static Map<String,Emp> map=new HashMap<String,Emp>(); static { map.put("101", new Emp("101","AA","123456","AA@imooc.com")); map.put("102", new Emp("102","BB","123456","BB@imooc.com")); map.put("103", new Emp("103","CC","123456","CC@imooc.com")); map.put("104", new Emp("104","DD","123456","DD@imooc.com")); map.put("105", new Emp("105","EE","123456","EE@imooc.com")); } //判断用户名和密码是否正确 public static boolean selectEmpByAccountAndPassword(Emp emp) { boolean flag=false; for(String key:map.keySet()) { Emp e=map.get(key); if(emp.getAccount().equals(e.getAccount())&&emp.getPassword().equals(e.getPassword()));{ flag=true; break; } } return flag; } }
chrismorgen
2018-10-10 10:54:14
建议你检查一下DBUtil类中的selectEmpByAccountAndPassword(emp);方法的逻辑是否写的有问题?是不是无论如何都返回true呢?如果还有问题,可以将DBUtil的代码和Emp类的代码粘贴一下,方便老师具有针对性的解答,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星