⑴ 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);
}
}