java控制台输入密码禁用回显

在java6之后可以使用console.readPassword()方法隐藏用户输入的文本。

如下示例代码:

```java import java.io.Console;

/ * @author outofmemory.cn / public class Main {

/**
* 在控制台输入密码示例
*/
public void passwordExample() {

Console console = System.console();

if (console == null) {
System.out.println("Couldn't get Console instance, maybe you're running this from within an IDE?");
System.exit(0);
}

console.printf("Testing password%n");
char passwordArray[] = console.readPassword("Enter your secret password: ");
console.printf("Password entered was: %s%n", new String(passwordArray));

}

/**
* @param args 命令行参数
*/
public static void main(String[] args) {
new Main().passwordExample();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,要实现自动获取上次登录的账号密码回显到页面上,可以通过以下步骤实现: 1.将用户的账号密码保存到Cookie中,在用户下一次访问网站时,从Cookie中读取账号密码信息。 2.在后端代码中读取Cookie中的账号密码信息,将其传递到前端。 3.在前端代码中使用JavaScript读取后端传递的账号密码信息,并将其填充到相应的文本框中。 下面是一个简单的示例: 后端代码: ```java // 从Cookie中读取账号密码信息 Cookie[] cookies = request.getCookies(); String username = null; String password = null; if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("username")) { username = cookie.getValue(); } else if (cookie.getName().equals("password")) { password = cookie.getValue(); } } } // 将账号密码信息传递到前端 request.setAttribute("username", username); request.setAttribute("password", password); ``` 前端代码: ```html <!-- 在文本框中回显账号密码信息 --> <input type="text" id="username" value="${username}"> <input type="password" id="password" value="${password}"> <script> // 读取后端传递的账号密码信息 var username = document.getElementById("username").value; var password = document.getElementById("password").value; // 将账号密码填充到文本框中 document.getElementById("username").value = username; document.getElementById("password").value = password; </script> ``` 注意,为了保护用户的账号密码安全,应该对密码进行加密后再保存到Cookie中。另外,为了避免用户的账号密码被其他人恶意获取,应该设置Cookie的有效期,并在用户退出登录时删除Cookie。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值