一、防止用户重复登录
转自
sdd379733766
这是登录请求界面submitLogin.jsp,只有java代码:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@page import="com.jdbc.entity.UserInfoEntity"%>
- <%@page import="com.jdbc.manager.SchoolManager"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <%
- request.setCharacterEncoding("UTF-8");
- UserInfoEntity user=new UserInfoEntity();
- String logname=request.getParameter("logname");
- String userpwd=request.getParameter("userpwd");
- user.setLogname(logname);
- user.setKeyword(userpwd);
- //获取在线用户的集合
- List<UserInfoEntity> onlineList=(List<UserInfoEntity>)application.getAttribute("ol");
- if(onlineList==null){
- onlineList=new ArrayList<UserInfoEntity>();
- application.setAttribute("ol",onlineList);
- }
- //判断用户是否已登录 已登录的话返回登录界面。
- for(int i=0;i<onlineList.size();i++){
- if(onlineList.get(i).getLogname().equals(user.getLogname())){
- response.sendRedirect("Login.jsp?add=-1");
- return;
- }
- }
- //调用登录验证方法,判断用户是否再存
- user=SchoolManager.checkLogin(user);
- //用户存在 将该用户添加到在线列表onlineList中,给该用户一个session并且进入主界面
- if(user!=null){
- onlineList.add(user);
- session.setAttribute("user",user);
- response.sendRedirect("../Index/index.jsp");
- }else{
- response.sendRedirect("Login.jsp?add=0");
- }
- %>
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%@page import="com.jdbc.entity.UserInfoEntity"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'submitExits.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <%
- UserInfoEntity user=(UserInfoEntity)session.getAttribute("user");
- List<UserInfoEntity> onlineList=(List<UserInfoEntity>)application.getAttribute("ol");
- for(int i=0;i<onlineList.size();i++){
- if(onlineList.get(i).getLogname().equals(user.getLogname())){
- onlineList.remove(i);
- session.invalidate();
- }
- }
- %>
- <body>
- </body>
- <script type="text/javascript">
- window.close();
- </script>
- </html>
问题来了,若用户关闭浏览器或意外断电等情况发生时时,那么在用户useronlineList中还存在,他再次登录的时候会提示已在线该如何解决?
三、关闭浏览器时用户的退出
老师说出现这种情况的话要重建写一界面,利用框架解决,具体我还没有搞清楚,等弄明白了在把这块更新上。
四、意外断电等情况时的用户退出:
这个就要利用HttpSessionListener来解决了,首先建一个共具类SystemWebListener实现接口HttpSessionListener。
然后在web.xml中进行如下配置:
<listener>
<listener-class>com.jdbc.tool.SystemWebListener</listener-class>//这个必须是你这个工具类的路径 不要写错
</listener>
工具类代码如下:
- package com.jdbc.tool;
- import java.util.List;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import com.jdbc.entity.UserInfoEntity;
- public class SystemWebListener implements HttpSessionListener{
- public void sessionCreated(HttpSessionEvent se) {
- }
- public void sessionDestroyed(HttpSessionEvent se) {
- try {
- HttpSession session=se.getSession();
- ServletContext app=session.getServletContext();
- List<UserInfoEntity> useronlineList=(List<UserInfoEntity>) app.getAttribute("ol");
- UserInfoEntity user=(UserInfoEntity) session.getAttribute("user");
- for (int i = 0; i < useronlineList.size(); i++) {
- if(useronlineList.get(i).getLogname().equals(user.getLogname())){
- useronlineList.remove(i);
- return;
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }