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")%>