1、获取输入的用户名和密码
2、读取properties 文件到Properties 对象
3、遍历Properties 对象的属性
如果能够查找到对应的输入用户名并且该属性对应的密码与输入密码相同,则停止遍历,并在浏览器打印:“欢迎登录”;否则输出“无该用户”或者“密码不正确
1、获取输入的用户名和密码
2、读取properties 文件到Properties 对象
3、遍历Properties 对象的属性
如果能够查找到对应的输入用户名,则终止遍历,并在浏览器打印“该用户已存在,请换一个用户名”。
遍历结束后,没找到已经输入的用户,则将输入的用户名和密码以键值对的形式保存到properties 文件中。并在浏览器打印“注册成功,请登录”
用Properties写登录注册页面,Properties怎么写啊
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- lshen01 2023-03-17 22:53关注
参考GPT和自己的思路:
好的,对于这个问题,我可以给您提供一些思路和代码片段来帮助您解决问题。
首先,您需要先获取用户输入的用户名和密码,这可以通过Java中的Scanner类来实现,代码如下:
Scanner scanner = new Scanner(System.in); System.out.print("请输入用户名:"); String username = scanner.nextLine(); System.out.print("请输入密码:"); String password = scanner.nextLine();
接下来,您需要读取properties文件到Properties对象中,这可以通过Java中的Properties类的load方法来实现,代码如下:
Properties props = new Properties(); InputStream in = new FileInputStream("path/to/your/properties/file"); props.load(in); in.close();
然后,您需要遍历Properties对象的属性,以查找是否存在对应的用户名和密码,代码如下:
Enumeration<?> enumeration = props.propertyNames(); boolean userExists = false; while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); String value = props.getProperty(key); if (key.equals(username) && value.equals(password)) { System.out.println("欢迎登录!"); userExists = true; break; } else if (key.equals(username) && !value.equals(password)) { System.out.println("密码不正确!"); userExists = true; break; } } if (!userExists) { System.out.println("无该用户!"); }
如果要实现注册功能,则需要在遍历Properties对象后,判断是否已经存在该用户,如果存在则输出该用户已存在,并停止遍历,否则将用户名和密码以键值对的形式保存到properties文件中,代码如下:
Enumeration<?> enumeration = props.propertyNames(); boolean userExists = false; while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); if (key.equals(username)) { System.out.println("该用户已存在,请换一个用户名。"); userExists = true; break; } } if (!userExists) { props.setProperty(username, password); OutputStream out = new FileOutputStream("path/to/your/properties/file"); props.store(out, "User information"); out.close(); System.out.println("注册成功,请登录!"); }
以上就是一个简单的使用Properties实现登录注册页面的示例代码,希望对您有所帮助。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 python的qt5界面
- ¥15 无线电能传输系统MATLAB仿真问题
- ¥50 如何用脚本实现输入法的热键设置
- ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
- ¥30 深度学习,前后端连接
- ¥15 孟德尔随机化结果不一致
- ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
- ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100