导航:首页 > 编程语言 > java密码登陆

java密码登陆

发布时间:2025-06-26 11:45:03

A. java的用户登录怎样记住上次登录的用户名和密码

java的用户登录记住上次登录的用户名和密码的方式是使用cookie来保存在本地,并且需要加密保存,实例如下:

HttpServletRequestrequest=ServletActionContext.getRequest();
Cookiecookies[]=request.getCookies();//声明一个cookie对象
Stringlogin=null;//登录的用户名
Stringpassword=null;//登录的密码
for(inti=0;i<cookies.length;i++){//取最后一次保存的用户名和密码
if(cookies[i].getName().equals("userName")){
login=cookies[i].getValue();
}
if(cookies[i].getName().equals("password")){
password=cookies[i].getValue();
break;
}
}
if(!AssertUtil.isEmpty(login)&&!login.equals("JSESSIONID")){
request.setAttribute("login",login);
request.setAttribute("password",password);
}

B. Java语言编写发送邮件怎么可以用password为登录密码登录

在Java中,可以使用JavaMail API和Java Activation Framework (JAF)来发送电子邮件。以下是一个简单的示例,展示如何使用SMTP(简单邮件传输协议)发送一封电子邮件,其中包含用户名和密码:
java复制代码
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {

public static void main(String[] args) {
// 定义SMTP服务器的地址、端口号和加密方式
String smtpHost = "smtp.example.com";
int smtpPort = 587;
String smtpSecure = "tls";

// 定义电子邮件的内容
String emailTo = "[email protected]";
String emailSubject = "Test Email";
String emailBody = "This is a test email sent using JavaMail API.";

// 定义SMTP服务器的用户名和密码
String smtpUsername = "your_username";
String smtpPassword = "your_password";

// 创建邮件会话的属性对象
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls." + smtpSecure, "true");

// 创建邮件会话对象
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
});

try {
// 创建MIME类型的邮件对象
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(smtpUsername));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo));
message.setSubject(emailSubject);
message.setText(emailBody);

// 发送邮件
Transport.send(message);
System.out.println("Email sent successfully.");

} catch (MessagingException e) {
System.out.println("Error sending email: " + e.getMessage());
}
}
}
在上面的示例中,我们使用Session对象的getInstance方法创建一个邮件会话对象。在这个方法中,我们传递一个Properties对象和一个Authenticator对象。Properties对象包含SMTP服务器的配置信息,而Authenticator对象用于提供用户名和密码。在Authenticator的getPasswordAuthentication方法中,我们返回一个PasswordAuthentication对象,其中包含用户名和密码。这样,JavaMail API就会使用这些凭据来连接到SMTP服务器。

C. 用JAVA做一个登陆界面,输入三次密码,错就退出。要求程序简单易懂。太复杂的看不懂!

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Logon {

private JFrame f = new JFrame("Logon");
private JTextField username = new JTextField(10);
private JPasswordField password = new JPasswordField(10);
private JLabel user = new JLabel("User");
private JLabel pwd = new JLabel("Password");
private JButton logon = new JButton("Logon on");
private int count = 0;

public Logon(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2));

p.add(user);
p.add(username);
p.add(pwd);
p.add(password);

f.add(p, BorderLayout.NORTH);
f.add(logon, BorderLayout.SOUTH);

logon.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
if(count < 3){
if(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
}
//假定用户为admin,密码为abc123
if(username.getText().equals("admin") && password.getText().equals("abc123")){
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
}

}else{
JOptionPane.showMessageDialog(null, "You have tried 3 times. Program exit!");
System.exit(0);
}

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setBounds(200, 200, 400, 400);
f.pack();

}

public static void main(String[] args) {
new Logon();
}

}

D. Java怎样获取当前登录用户的用户名,密码。。

当你使用用户名跟密码登录成功的时候,把用户名跟密码放在范围内.可以是request.setAttribute("username",username);也可以是session.setAttribute("username",username);然后在jsp页面可以用EL表达式拿也可以直接用java代码拿.${sessionScope.username},<%=session.getAttribute("username")%>

阅读全文

与java密码登陆相关的资料

热点内容
时间轴播放停止的命令是 浏览:772
免费在线pdf转图片 浏览:163
tif图片转换pdf 浏览:314
做标书的文件夹 浏览:509
每笔成交均量源码 浏览:949
云服务器哪里买便宜 浏览:822
phpselect标签 浏览:799
账户设置服务器地址 浏览:613
boll收口与开口选股指标公式源码 浏览:12
色彩命令 浏览:351
代理服务器出现问题或地址错误 浏览:20
php跳转qq资料卡代码 浏览:703
安卓加密原理 浏览:746
android跳转到相册 浏览:445
pecmd命令 浏览:4
h本pdf 浏览:128
android通知栏代码 浏览:707
有wifi无法连接服务器地址 浏览:649
python打印图形 浏览:568
违抗命令的检讨 浏览:244