防止用户重复登录

13年前

1.原理

 

2.login.jsp(没有对表单验证)

Code:
  1. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>无标题文档</title>  
  7. </head>  
  8.   
  9. <body>  
  10. <%  
  11.     String admin = (String) session.getAttribute("admin");  
  12.         if (admin == null) {  
  13. %>  
  14. <form action="../Login" method="post">  
  15. <table width="270" border="1" cellspacing="0" cellpadding="0">  
  16.   <tr>  
  17.     <td>用户名:</td>  
  18.     <td>  
  19.       <input type="text" name="username"/></td>  
  20.   </tr>  
  21.   <tr>  
  22.     <td>密码:</td>  
  23.     <td>  
  24.       <input type="text" name="password" /></td>  
  25.   </tr>  
  26.   <tr>  
  27.     <td colspan="2" align="center"><input type="submit" name="button" id="button" value="提交" /></td>  
  28.     </tr>  
  29. </table>  
  30. </form>  
  31. <%  
  32.     } else {  
  33.     response.sendRedirect("success.jsp");  
  34.     }  
  35. %>  
  36. </body>  
  37. </html>  

2.success.jsp

Code:
  1. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>用户登录</title>  
  7. </head>  
  8.   
  9. <body>  
  10. <%  
  11.     String admin = (String) session.getAttribute("admin");  
  12.         if (admin == null) {  
  13. %>  
  14. <p> <b>登录失败</b> </p>  
  15. <%  
  16.     response.sendRedirect("login.jsp");  
  17. %>  
  18. <%  
  19.     } else {  
  20. %>  
  21. <p> <b>欢迎管理员:</b> </p>  
  22. <p align="center"> ${admin }  
  23. <%  
  24.     }  
  25. %>  
  26. </body>  
  27. </html>  

3.error.jsp

Code:
  1. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <meta http-equiv="refresh" content="5;URL=login.jsp">  
  7. <title>用户登录</title>  
  8. </head>  
  9.   
  10. <body>  
  11. <p>  
  12.     ${message }  
  13. </p>  
  14. <p>3秒钟后返回</p>  
  15. </body>  
  16. </html>  

3.Login.java

Code:
  1. /*** 
  2.  * This servlet is for login 
  3.  */  
  4.   
  5. import java.io.IOException;  
  6. import java.util.List;  
  7. import org.ly.listener.loginListener;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import javax.servlet.http.HttpSession;  
  13.   
  14. import com.xumin.bean.Admin;  
  15. import com.xumin.dao.LoginDAO;  
  16.   
  17. public class Login extends HttpServlet {  
  18.   
  19.     public void destroy() {  
  20.         super.destroy();  
  21.   
  22.     }  
  23.   
  24.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.         HttpSession session = request.getSession();  
  27.         String message;  
  28.         Admin admin = new Admin();  
  29.         admin.setUid(request.getParameter("uid"));  
  30.         admin.setPwd(request.getParameter("pwd"));  
  31.         LoginDAO login = new LoginDAO();  
  32.         //验证登录  
  33.         if (login.login(admin)) {  
  34.             // 防止重复登录  
  35.             if (!getRepeat(request, response)) {  
  36.                 session.setAttribute("user", admin);  
  37.                 session.setAttribute("admin",admin.getUid());  
  38.                 response.sendRedirect("success.jsp");  
  39.             } else {  
  40.                 session.setAttribute("message""请勿重复登录,谢谢!");  
  41.                 response.sendRedirect("error.jsp");  
  42.             }  
  43.   
  44.         } else {  
  45.                 message = message + "用户名密码错误.\n";                 
  46.             session.setAttribute("message", message);  
  47.             response.sendRedirect("error.jsp");  
  48.         }         
  49.     }  
  50.   
  51.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  52.             throws ServletException, IOException {  
  53.         this.doGet(request, response);  
  54.   
  55.     }  
  56.     //是否重复登录的封闭方法  
  57.     public static boolean getRepeat(HttpServletRequest request,  
  58.             HttpServletResponse response) {  
  59.         boolean falg = false;  
  60.         List list = loginListener.list;  
  61.         for (int i = 0; i < list.size(); i++) {  
  62.             Admin user = (Admin) (list.get(i));           
  63.             if (request.getParameter("uid").equals(user.getUid())) {  
  64.                 falg = true;  
  65.             }  
  66.         }  
  67.         return falg;  
  68.     }  
  69.   
  70.     public void init() throws ServletException {  
  71.   
  72.     }  
  73.   
  74. }  

4.LoginListener.java

Code:
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import javax.servlet.http.HttpSessionAttributeListener;  
  5. import javax.servlet.http.HttpSessionBindingEvent;  
  6.   
  7. import com.xumin.bean.Admin;  
  8.   
  9. public class loginListener implements HttpSessionAttributeListener {  
  10.   
  11.     public static List list = new ArrayList();  
  12.   
  13.     public void attributeAdded(HttpSessionBindingEvent arg0) {  
  14.         if (arg0.getName().equals("user")) {  
  15.             Admin admin = (Admin) arg0.getValue();  
  16.             list.add(admin);  
  17.         }  
  18.     }  
  19.     public void attributeRemoved(HttpSessionBindingEvent arg0) {  
  20.         try {  
  21.             int n = 0;  
  22.             Admin admin = (Admin) arg0.getValue();  
  23.             for (int i = 0; i < list.size(); i++) {  
  24.                 Admin admin2 = (Admin) list.get(i);  
  25.                 if (admin.getUid().equals(admin2.getUid())) {  
  26.                     n = i;  
  27.                     break;  
  28.                 }  
  29.             }  
  30.             list.remove(n);  
  31.         } catch (Exception e) {  
  32.         }  
  33.     }  
  34.       
  35.     public void attributeReplaced(HttpSessionBindingEvent arg0) {  
  36.   
  37.     }  
  38.   
  39. }  

5.web.xml

Code:
  1. <listener>  
  2.         <listener-class>com.xumin.listener.loginListener</listener-class>  
  3.     </listener>  
  4.   
  5.     <servlet>  
  6.         <servlet-name>Login</servlet-name>  
  7.         <servlet-class>com.xumin.servlet.Login</servlet-class>  
  8.     </servlet>  
  9.   
  10.     <servlet-mapping>  
  11.         <servlet-name>Login</servlet-name>  
  12.         <url-pattern>/Login</url-pattern>  
  13.     </servlet-mapping>