這是登錄請求界面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
- }
- }
- }