在此之前也看了很多同一帐号异地登录的,有的是采用后登录者必须等待前登录者释放后才可以登录,我的项目中要用到想qq那样可以踢出,我具体的做法如下:
LoginServelt.java 做登录使用
OnlineUserListener.java 做session超时清理工作
LogValidata.java 做权限验证
logout.jsp做退出
LoginServelt.java public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
HttpSession session=request.getSession();
String username=request.getParameter("username");
String password=request.getParameter("password");
ServletContext sc=this.getServletContext();
String sessionId=session.getId();
String path= request.getContextPath();
Map<String, String> onlineUserMap=(Map<String, String>)sc.getAttribute("onlineUserMap");
//如果是首个用户登录,onlineUserMap为空,new一个
if(onlineUserMap==null){
onlineUserMap=new HashMap<String, String>();
}
if(username!=null || password!=null){
onlineUserMap.put(username.trim(), sessionId);
sc.setAttribute("onlineUserMap",onlineUserMap);
//设置username,因为这个唯一的,作为key
session.setAttribute("username", username);
response.sendRedirect(path+"/success.jsp");
}else{
response.sendRedirect(path+"/index.jsp");
}
}
OnlineUserListener.java
public class OnlineUserListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
Map<String, String> onlineUserMap=(Map<String, String>)session.getServletContext().getAttribute("onlineUserMap");
if(onlineUserMap!=null){
onlineUserMap.remove(session.getId());
session.getServletContext().setAttribute("onlineUserMap", onlineUserMap);
}
}
}
LogValidata.java
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
ServletContext sc=request.getSession().getServletContext();
String username=(String)request.getSession().getAttribute("username"); Map onlineUserMap=(Map) sc.getAttribute("onlineUserMap");
String sessionId=request.getSession().getId();
String path= request.getContextPath();
//onlineUserMap==null 说明还没有用户登录过
//username==null 说明该用户超时或未登录
if(onlineUserMap!=null && username!=null ){
//获取sessionId String id=(String) onlineUserMap.get(username);
if(id!=null && id.trim().equals(sessionId)){
//取得sessionId与当前相等,说明此用户在使用状态 arg2.doFilter(request,response);
}else{
//说明已经被提出
response.setCharacterEncoding("gb2312");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.flush();
out.close();
} }else{
//说明未登录
response.setCharacterEncoding("gb2312");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.flush();
out.close();
}
}
logout.jsp
<body>
<%
String username=(String)request.getSession().getAttribute("username");
Map onlineUserMap=(Map) application.getAttribute("onlineUserMap");
if(onlineUserMap!=null && username!=null){
onlineUserMap.remove(username);
}
session.invalidate();
%>
<SCRIPT language="javascript">
location.href="login.jsp";
</SCRIPT>
</body>