最近学习JSP...遇到个问题
目的: 实现在线用户的集中管理
我的思路:
1.使用登录页面,将用户的昵称添加到application的一个List集合当中.
2.List中存放每一个登录用户,用户对象的属性有{ 昵称(不重复), 在线状态(0表示在线, 1表示离线) }
3.用户登陆后,将自己的信息添加到List将在线状态设为0
4.登录用户页面每隔3秒刷新List中对应自己的记录,将在线状态设为0.
5.有个JSP页面每隔5秒刷新application中的List, 将用户在线状态为0的改为1, 将用户在线状态为1的从List中移除.
注明:这样的思路是为了防止用户没有执行登出操作,直接关闭浏览器,或者死机,断电重启等一些意外状态.
问题:
不知道如何实现第5步骤.
1.如何能能够将一个JSP在该项目启动时自动运行.
2.如何在执行第5步骤的操作时每隔5秒从获取List并将其作为参数传入方法,最后再将扫描后的List交给application.
补充说明:
我使用MyEclipse7.5作为开发工具
本人新手,胡乱弄的~~如果有哪位可以给谢帮助不胜感激~~或者哪位有更好的方法,思路~~菜鸟虚心求教~~谢谢~~
求助~~jsp使用application保存在线用户的问题
答案:2 悬赏:0
解决时间 2021-03-24 09:50
- 提问者网友:北故人
- 2021-03-23 14:20
最佳答案
- 二级知识专家网友:魅世女王
- 2021-03-23 15:41
给你一断代码,这个代码是防止用户重复登陆的,你修改下就可以了
package com.china.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import com.china.pojo.CitiesAndEmpPojo;
public class OnlineOperatorListener implements HttpSessionBindingListener {
// 当前被监听的操作员Pojo实例
private CitiesAndEmpPojo operatorPojo;
private HttpSession session = null;
// 容器上下文对象
private ServletContext application = null;
// 在线操作员列表
private static Hashtable operatorTable = null;
public OnlineOperatorListener(CitiesAndEmpPojo operatorPojo) {
this.operatorPojo = operatorPojo;
}
public synchronized boolean validateOperatorOnline(String operatorName) {
return operatorTable.containsKey(operatorName);
}
public void valueBound(HttpSessionBindingEvent event) {
// 创建Http会话对象
this.session = event.getSession();
// 创建容器上下文对象
this.application = this.session.getServletContext();
// 将用户保存到在线列表
operatorTable.put(this.operatorPojo.getEmpcode(), this.operatorPojo);
// 保存更改
this.application.setAttribute("OperatorTable", operatorTable);
}
public void valueUnbound(HttpSessionBindingEvent event) {
// 创建Http会话对象
this.session = event.getSession();
// 创建容器上下文对象
this.application = this.session.getServletContext();
// 将用户移出在线列表
operatorTable.remove(this.operatorPojo.getEmpcode());
// 保存更改
this.application.setAttribute("OperatorTable", operatorTable);
}
package com.china.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import com.china.pojo.CitiesAndEmpPojo;
public class OnlineOperatorListener implements HttpSessionBindingListener {
// 当前被监听的操作员Pojo实例
private CitiesAndEmpPojo operatorPojo;
private HttpSession session = null;
// 容器上下文对象
private ServletContext application = null;
// 在线操作员列表
private static Hashtable
public OnlineOperatorListener(CitiesAndEmpPojo operatorPojo) {
this.operatorPojo = operatorPojo;
}
public synchronized boolean validateOperatorOnline(String operatorName) {
return operatorTable.containsKey(operatorName);
}
public void valueBound(HttpSessionBindingEvent event) {
// 创建Http会话对象
this.session = event.getSession();
// 创建容器上下文对象
this.application = this.session.getServletContext();
// 将用户保存到在线列表
operatorTable.put(this.operatorPojo.getEmpcode(), this.operatorPojo);
// 保存更改
this.application.setAttribute("OperatorTable", operatorTable);
}
public void valueUnbound(HttpSessionBindingEvent event) {
// 创建Http会话对象
this.session = event.getSession();
// 创建容器上下文对象
this.application = this.session.getServletContext();
// 将用户移出在线列表
operatorTable.remove(this.operatorPojo.getEmpcode());
// 保存更改
this.application.setAttribute("OperatorTable", operatorTable);
}
全部回答
- 1楼网友:转身后的回眸
- 2021-03-23 17:21
我是来看评论的
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯