⑴ B/S 的程序, 如何控制同時在線用戶數
邏輯應該是這樣:新建一個hashtable,將每個用戶在進入系統的頁面(任何頁面,如果是java,最好放到filter中)後的時間和用戶名寫入到這個hashtable中。寫一個線程,用來判斷這個hashtable,取出每個用戶的時間,超出比如30分鍾,就算其沒有登錄了,清除出這個hashtable。當新用戶剛登錄系統的時候,到hashtable判斷其是否存在或者是否超出指定的size
⑵ 在JAVA中實現限制登陸次數的代碼,超過三次確定鍵將不能用
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener{
JButton button1,button2;
static JTextField text1,text2;
JPanel panel1,panel2,panel3,panel4,panel5;
Container con1;
int count=0;//密碼輸入次數計數器
Login()
{
super("用戶登陸");
text1=new JTextField(10);
text2=new JPasswordField(10);
button1=new JButton("提交");
button2=new JButton("取消");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
setLocation(350,250);
con1=getContentPane();
con1.setLayout(new GridLayout(5,1));
con1.setPreferredSize(new Dimension(300,250));
panel1.add(new JLabel(""));
panel2.add(new JLabel("用戶名:"));
panel2.add(text1);
panel3.add(new JLabel("密 碼:"));
panel3.add(text2);
panel4.add(button1);
panel4.add(button2);
panel5.add(new JLabel(""));
con1.add(panel1);
con1.add(panel2);
con1.add(panel3);
con1.add(panel4);
con1.add(panel5);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ setVisible(false);
System.exit(0);
}
});
button1.addActionListener(this);
button2.addActionListener(this);
this.setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
try{登陸();}
catch(SQLException ee){}
}
else if(e.getSource()==button2)
{
退出銷售系統();
}
}
public void 登陸() throws SQLException
{
Database db=new Database();
if(count<3)
{
if(db.check())
{
JOptionPane.showMessageDialog(this,"登陸成功!","提示對話框",JOptionPane.WARNING_MESSAGE);
setVisible(false);
MainFrame f=new MainFrame();f.pack();
}
else
{
count++;
if(count<3)
{
JOptionPane.showMessageDialog(this,"登陸失敗!","提示對話框",JOptionPane.WARNING_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this,"密碼錯誤超過3次!","警告對話框",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
}
else
{
JOptionPane.showMessageDialog(this,"密碼錯誤超過3次!","警告對話框",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
public void 退出銷售系統()
{
System.exit(0);
}
}