[转载] java web开发一个帐号同一时间只能一个人登录

对于一个帐号在同一时间只能一个人登录,可以通过下面的方法实现:
1 .在用户登录时,把用户添加到一个ArrayList中
2 .再次登录时查看ArrayList中有没有该用户,如果ArrayList中已经存在该用户,则阻止其登录
3 .当用户退出时,需要从该ArrayList中删除该用户,这又分为三种情况
① 使用注销按钮正常退出
② 点击浏览器关闭按钮或者用Alt+F4退出,可以用javascript捕捉该页面关闭事件,
执行一段java方法删除ArrayList中的用户
③ 非正常退出,比如客户端系统崩溃或突然死机,可以采用隔一段时间session没活动就删除该session所对应的用户来解决,这样用户需要等待一段时间之后就可以正常登录。

在LoginAction中定义:
// 用来在服务器端存储登录的所有帐号
public static List logonAccounts;

login() 登录方法中:
// 设置session不活动时间为30分
request.getSession().setMaxInactiveInterval(60*30);
if(logonAccounts==null){
 
   logonAccounts = new ArrayList();
}
// 查看ArrayList中有没有该用户
for (int i = 0; i < logonAccounts.size(); i++) {
 
   Account existAccount = (Account)logonAccounts.get(i);
 
   if(account.getAccountId().equals(existAccount.getAccountId())){
 
       return "denied";
 
   }
}
// 在用户登录时,把sessionId添加到一个account对象中
// 在后面 ③ 需要根据此sessionId删除相应用户
account.setSessionId(request.getSession().getId());
// 该用户保存到ArrayList静态类变量中
logonAccounts.add(account);
return "login";

① 使用注销按钮正常退出
logout() 退出方法中:
if(logonAccounts==null){
 
   logonAccounts = new ArrayList();
}
// 删除ArrayList中的用户 

for (int i = 0; i < logonAccounts.size(); i++) {
 
   Account existAccount = (Account)logonAccounts.get(i);
 
   if(account.getAccountId().equals(existAccount.getAccountId())){
 
       logonAccounts.remove(account);
 
   }
}

② 点击浏览器关闭按钮或者用Alt+F4退出:
在后台弹出一个窗口,在弹出窗口中删除ArrayList中的用户
function window.onbeforeunload(){
// 是否通过关闭按钮或者用Alt+F4退出
// 如果为刷新触发onbeforeunload事件,下面if语句不执行
 
   if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){
 
       window.open('accountUnbound.jsp','',
 
               'height=0,width=0,top=10000,left=10000');
 
   }
}
accountUnbound.jsp : 弹出窗口中删除ArrayList中的用户
<%
 
   Account account = (Account) request.getSession().getAttribute("account");
 
   if(account != null){
 
       if(LoginAction.logonAccounts==null){
 
           LoginAction.logonAccounts = new ArrayList();
 
       }
 
       // 删除ArrayList中的用户——下面代码和上面的 ⑴ 处一样
 
       for (int i = 0; i < logonAccounts.size(); i++) {
 
           Account existAccount = (Account)logonAccounts.get(i);
 
           if(account.getAccountId().equals(existAccount.getAccountId())){
 
               logonAccounts.remove(account);
 
          }
 
       }
 
   }
%>

为了保证上面代码可以执行完毕,3秒后关闭此弹出窗口(也位于accountUnbound.jsp中)
<script>
setTimeout("closeWindow();",3000);
function closeWindow(){
 
   window.close();
}
</script>

③ 使LoginAction 实现implements HttpSessionListener,并实现sessionCreated,sessionDestroyed方法,在sessionDestroyed中删除ArrayList中的用户(用户超过30分钟不活动则执行此方法)
public void sessionDestroyed(HttpSessionEvent event) {
 
  // 取得不活动时的sessionId,并根据其删除相应logonAccounts中的用户
 
  String sessionId = event.getSession().getId();
 
  for (int i = 0; i < logonAccounts.size(); i++) {
 
      Account existAccount = (Account)logonAccounts.get(i);
 
      if(account.getSessionId().equals(existAccount.getSessionId())){
 
          logonAccounts.remove(account);
 
      }
 
  }
}

注:

对于上面的,由于弹出窗口很容易被防火墙或者安全软件阻拦,造成无法弹出窗口,从而短时间不能登录,这种情况可以用AJAX来代替弹出窗口,同样在后台执行删除用户的那段代码,却不会受到防火墙限制:
<script>
 
   // <![CDATA[
 
   var http_request = false;
 
   function makeRequest(url) {
 
       http_request = false;
 
       if (window.XMLHttpRequest) { // Mozilla, Safari,...
 
           http_request = new XMLHttpRequest();
 
          if (http_request.overrideMimeType) {
 
               http_request.overrideMimeType('text/xml');
 
           }
 
       } else if (window.ActiveXObject) { // IE
 
         try {
 
               http_request = new ActiveXObject("Msxml2.XMLHTTP");
 
           } catch (e) {
 
               try {
 
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
 
               } catch (e) {
 
               }
 
           }
 
       }
 
       if (!http_request) {
 
          alert('Giving up :( Cannot create an XMLHTTP instance');
 
           return false;
 
       }
 
       http_request.onreadystatechange = alertContents;
 
       http_request.open('GET', url, true);
 
       http_request.send(null);
 
   }
 
   function alertContents() {
 
       if (http_request.readyState == 4) {
 
           if (http_request.status == 200) {
 
               window.close();
 
           } else {
 
               alert('There was a problem with the request.');
 
           }
 
       }
 
   }
 
   function window. onbeforeunload() {
 
       makeRequest ('accountUnbound.jsp');
 
   }
 
   //]]>
</script>
对于上面的这段ajax代码,在网上有很多详细的解释,把它加到onbeforeunload()浏览器关闭事件中,在后台执行代码的效果很好,不必担心弹出窗口有时候会无效的问题。
使用这段代码后,上面②中accountUnbound.jsp中的那段关闭弹出窗口window.close();的js代码就不需要了。

 

http://blog.sina.com.cn/s/blog_556c72d20100b91s.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下代码实现: int[] arr = {1, 2, 3, 4, 5}; for (int i = ; i < arr.length; i++) { System.out.print(arr[i] + " "); } 输出结果为:1 2 3 4 5 ### 回答2: 使用Java的for循环可以很方便地输出一个数组,并且可以通过添加空格来分隔同一行的元素。 假设我们有一个整数数组arr,我们可以使用以下代码来输出该数组: ```java int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]); // 输出数组元素 if (i != arr.length - 1) { System.out.print(" "); // 输出空格,除非是最后一个元素 } } ``` 在上面的示例中,我们首先定义一个整数数组arr,其中包含了一些整数。然后,我们使用for循环遍历整个数组。在每一次循环中,我们通过arr[i]来输出当前索引位置的数组元素。如果当前元素不是数组的最后一个元素,我们输出一个空格。 通过这种方式,我们可以将数组的元素在同一行上输出,且使用空格进行分隔。在上面的示例中,数组arr的输出结果将是:"1 2 3 4 5"。 这只是一个简单的示例,您可以根据自己的需求修改代码来适应不同的数组和输出格式。希望这对您有所帮助! ### 回答3: 可以使用Java中的for循环来输出一个数组,并且使用空格将同一行的元素隔开。以下是一个示例代码: ```java public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 输出数组元素并用空格隔开 } } } ``` 在上述代码中,我们定义了一个名为arr的整型数组,并初始化了一些元素。然后使用for循环遍历数组中的每个元素。循环中的语句`System.out.print(arr[i] + " ");`会输出当前元素arr[i]并且后面跟一个空格,实现了同一行元素用空格隔开的效果。 执行上述代码,输出结果为:`1 2 3 4 5 `。其中每个数字都在同一行,并且每个数字之间通过空格隔开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值