login.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>로그인창</title> </head> <body> <form name="frmLogin" method="post" action="loginTest" encType="utf-8"> 아이디 :<input type="text" name="id"><br> 비밀번호:<input type="password" name="pw"><br> <input type="submit" value="로그인"> <input type="reset" value="다시입력"> </form> </body> </html>
LoginVO.java
public class LoginVO { private String id; private String sessionid; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSessionid() { return sessionid; } public void setSessionid(String sessionid) { this.sessionid = sessionid; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; LoginVO other = (LoginVO) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }
LoginTest.java
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/loginTest") public class LoginTest extends HttpServlet{ //전역변수 public static List<LoginVO> userList=new ArrayList<LoginVO>(); @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); String id=request.getParameter("id"); String pw=request.getParameter("pw"); // 로그인 성공시 if(pw.equals("1111")) { System.out.println(" session.getId() :" + session.getId()); LoginImpl loginUser =new LoginImpl(id, pw, session.getId()); LoginVO loginVO=new LoginVO(); session.setAttribute("loginUser", loginUser); loginVO.setId(id); loginVO.setSessionid(session.getId()); userList.add(loginVO); }else { //로그인 실패시 로그인 페이지로 리다이렉트 response.sendRedirect("login.html"); } out.print("<html><body>"); out.print("현재 접속 아이디 : "+id+"<br>"); userList(out); //접속자 중복 제거 및 접속자 목록 out.println("<a href='logout.do?id=" + id + "'>로그아웃 </a><br>"); out.println("세션 유효 시간 : " + session.getMaxInactiveInterval() + "<br>"); out.print("</body></html>"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); out.print("<html>"); out.println("<head>"); out.println("<script type='text/javascript'>"); out.println("setTimeout('history.go(0);', 3000)"); //3초마다 out.println("</script>"); out.println("</head>"); out.print("<body>"); userList(out); //접속자 중복 제거 및 접속자 목록 LoginImpl loginUser =(LoginImpl)session.getAttribute("loginUser"); if(loginUser==null) { out.println("<a href='login.html'>로그인 </a><br>"); }else{ out.println("<a href='logout.do?id=" + loginUser.id + "'>로그아웃 </a><br>"); } out.println("세션 유효 시간 : " + session.getMaxInactiveInterval() + "<br>"); out.print("</body></html>"); } //접속자 중복 제거 및 접속자 목록 private void userList(PrintWriter out) { //중복 아이디 제거 HashSet<LoginVO> hs = new HashSet<LoginVO>(userList); userList = new ArrayList<LoginVO>(hs); if(userList!=null &&userList.size()>0) { out.println("*** 접속 자 목록 *** <br>"); for(int i=0; i<userList.size(); i++) { if(userList.get(i)==null || userList.get(i).equals("")) { userList.remove(i); LoginImpl.loginUserCount--; }else { out.println(userList.get(i).getId()+"<br>"); } } out.print("총 로그인 접속자수는 "+ LoginImpl.loginUserCount + "<br>"); out.print("<a href='loginTest'>접속자 목록</a><br>"); System.out.println("userList(PrintWriter out) userList size : " +userList.size()); }else { out.print("총 로그인 접속자수는 0 <br>"); } } }
LoginImpl.java
@WebListener 해주면 HttpSessionListener 는 실시간으로 세션을 상태를 created , destroyed 반영해 준다.
import java.util.List; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class LoginImpl implements HttpSessionListener { String id; String pw; String sessionid; static int loginUserCount=0; public LoginImpl() { } public LoginImpl(String id, String pw, String sessionid) { this.id=id; this.pw=pw; this.sessionid=sessionid; } @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("세션 생성"); ++loginUserCount; System.out.printf("생성된 SESSIONID %s \n", se.getSession().getId()); System.out.printf("로그인된 사용자 수 : %d \n", loginUserCount); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("세션 소멸"); System.out.printf("세션 소멸된 아이디 SESSIONID %s \n", se.getSession().getId()); List<LoginVO> userList=LoginTest.userList; //접소자 목록에서 제거 if(userList!=null && userList.size()>0) { for(int i=0; i<userList.size(); i++) { if(userList.get(i)!=null || userList.get(i).getSessionid().equals(se.getSession().getId())){ userList.remove(i); LoginImpl.loginUserCount--; } } } System.out.printf("로그인된 사용자 수 : %d \n", loginUserCount); } }
LogOutServlet.java
import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/logout.do") public class LogOutServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); if(id!=null){ List<LoginVO> userList=LoginTest.userList; if(userList!=null && userList.size()>0) { System.out.println("id 제거 : " + id); for(int i=0; i<userList.size(); i++) { System.out.println("userList.get(i) : " +userList.get(i)); if(userList.get(i).equals(id)) { userList.remove(i); } } } System.out.println("userList size : " +userList.size()); } request.getSession().invalidate(); response.sendRedirect("login.html"); } }
web.xml
테스트를 위해 세션유지시간을 1분으로 설정한다.
<session-config> <session-timeout>1</session-timeout> </session-config>
댓글 ( 4)
댓글 남기기