防止用户重复登录
1.原理
2.login.jsp(没有对表单验证)
- <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <%
- String admin = (String) session.getAttribute("admin");
- if (admin == null) {
- %>
- <form action="../Login" method="post">
- <table width="270" border="1" cellspacing="0" cellpadding="0">
- <tr>
- <td>用户名:</td>
- <td>
- <input type="text" name="username"/></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td>
- <input type="text" name="password" /></td>
- </tr>
- <tr>
- <td colspan="2" align="center"><input type="submit" name="button" id="button" value="提交" /></td>
- </tr>
- </table>
- </form>
- <%
- } else {
- response.sendRedirect("success.jsp");
- }
- %>
- </body>
- </html>
2.success.jsp
- <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>用户登录</title>
- </head>
- <body>
- <%
- String admin = (String) session.getAttribute("admin");
- if (admin == null) {
- %>
- <p> <b>登录失败</b> </p>
- <%
- response.sendRedirect("login.jsp");
- %>
- <%
- } else {
- %>
- <p> <b>欢迎管理员:</b> </p>
- <p align="center"> ${admin }
- <%
- }
- %>
- </body>
- </html>
3.error.jsp
- <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="refresh" content="5;URL=login.jsp">
- <title>用户登录</title>
- </head>
- <body>
- <p>
- ${message }
- </p>
- <p>3秒钟后返回</p>
- </body>
- </html>
3.Login.java
- /***
- * This servlet is for login
- */
- import java.io.IOException;
- import java.util.List;
- import org.ly.listener.loginListener;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import com.xumin.bean.Admin;
- import com.xumin.dao.LoginDAO;
- public class Login extends HttpServlet {
- public void destroy() {
- super.destroy();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- HttpSession session = request.getSession();
- String message;
- Admin admin = new Admin();
- admin.setUid(request.getParameter("uid"));
- admin.setPwd(request.getParameter("pwd"));
- LoginDAO login = new LoginDAO();
- //验证登录
- if (login.login(admin)) {
- // 防止重复登录
- if (!getRepeat(request, response)) {
- session.setAttribute("user", admin);
- session.setAttribute("admin",admin.getUid());
- response.sendRedirect("success.jsp");
- } else {
- session.setAttribute("message", "请勿重复登录,谢谢!");
- response.sendRedirect("error.jsp");
- }
- } else {
- message = message + "用户名密码错误.\n";
- session.setAttribute("message", message);
- response.sendRedirect("error.jsp");
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- //是否重复登录的封闭方法
- public static boolean getRepeat(HttpServletRequest request,
- HttpServletResponse response) {
- boolean falg = false;
- List list = loginListener.list;
- for (int i = 0; i < list.size(); i++) {
- Admin user = (Admin) (list.get(i));
- if (request.getParameter("uid").equals(user.getUid())) {
- falg = true;
- }
- }
- return falg;
- }
- public void init() throws ServletException {
- }
- }
4.LoginListener.java
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import com.xumin.bean.Admin;
- public class loginListener implements HttpSessionAttributeListener {
- public static List list = new ArrayList();
- public void attributeAdded(HttpSessionBindingEvent arg0) {
- if (arg0.getName().equals("user")) {
- Admin admin = (Admin) arg0.getValue();
- list.add(admin);
- }
- }
- public void attributeRemoved(HttpSessionBindingEvent arg0) {
- try {
- int n = 0;
- Admin admin = (Admin) arg0.getValue();
- for (int i = 0; i < list.size(); i++) {
- Admin admin2 = (Admin) list.get(i);
- if (admin.getUid().equals(admin2.getUid())) {
- n = i;
- break;
- }
- }
- list.remove(n);
- } catch (Exception e) {
- }
- }
- public void attributeReplaced(HttpSessionBindingEvent arg0) {
- }
- }
5.web.xml
- <listener>
- <listener-class>com.xumin.listener.loginListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>Login</servlet-name>
- <servlet-class>com.xumin.servlet.Login</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Login</servlet-name>
- <url-pattern>/Login</url-pattern>
- </servlet-mapping>