1. login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String mid = ""; String error = request.getParameter("error"); Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie c : cookies) { if (c.getName().equals("cMid")) { mid = c.getValue(); break; } } } %> <!DOCTYPE html> <html> <head> <script> 'use strict'; function register() { alert('준비 중입니다.'); } window.xxonload = function() { if ('<%= error %>' === 'true') { alert('아이디 또는 비밀번호가 일치하지 않습니다. 다시 로그인해주세요.'); } }; </script> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>쿠키로 아이디 저장 연습</title> <%@ include file="/include/bs4.jsp"%> </head> <body> <jsp:include page="/include/header.jsp" /> <jsp:include page="/include/nav.jsp" /> <div class="container mt-5"> <div class="login-container" style="max-width: 500px; margin: auto; padding: 20px;"> <form class="login-form" method="post" action="ex01_loginOk.jsp"> <div class="form-group row"> <label for="mid" class="col-sm-4 col-form-label text-center">아이디</label> <div class="col-sm-8"> <input type="text" class="form-control mb-3" name="mid" placeholder="아이디를 입력해주세요." value="<%= mid %>" autofocus required /> </div> </div> <div class="form-group row"> <label for="password" class="col-sm-4 col-form-label text-center">비밀번호</label> <div class="col-sm-8"> <input type="password" class="form-control" name="password" placeholder="비밀번호를 입력해주세요." required /> </div> </div> <div class="form-group row"> <div class="col-sm-10 offset-sm-2 text-right mb-3"> <div class="form-check"> <input class="form-check-input" type="checkbox" value="true" name="rememberId" <%= (!mid.isEmpty() ? "checked" : "") %>> <label class="form-check-label" for="rememberId">아이디 저장</label> </div> </div> </div> <div class="form-group row text-center"> <div class="col"> <button type="submit" class="btn btn-dark">로그인</button> </div> <div class="col"> <button type="button" class="btn btn-dark" xxonclick="location.reload()">다시입력</button> </div> <div class="col"> <button type="button" class="btn btn-dark" xxonclick="register()">회원가입</button> </div> </div> </form> </div> </div> <jsp:include page="/include/footer.jsp" /> </body> </html> | cs |
2. loginOk.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String rememberId = request.getParameter("rememberId"); String mid = request.getParameter("mid"); String password = request.getParameter("password"); if ("admin".equals(mid) && "1234".equals(password)) { Cookie cookieMid = new Cookie("cMid", mid); if ("true".equals(rememberId)) { cookieMid.setMaxAge(60 * 60 * 24 * 30); } else { cookieMid.setMaxAge(0); } response.addCookie(cookieMid); response.sendRedirect(request.getContextPath() + "/study/homework/ex01_main.jsp?mid=" + mid); } else { response.sendRedirect(request.getContextPath() + "/study/homework/ex01_login.jsp?error=true"); } %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>쿠키로 아이디 저장 연습</title> <%@ include file = "/include/bs4.jsp" %> </head> <body> <jsp:include page="/include/header.jsp" /> <jsp:include page="/include/nav.jsp" /> <div class="container mt-5"> <input type="hidden" name="hostIp" value="<%=request.getRemoteAddr() %>"/> </div> <jsp:include page="/include/footer.jsp" /> </body> </html> | cs |
3. main.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String mid = request.getParameter("mid"); String password = request.getParameter("password"); String hostIp = request.getRemoteAddr(); pageContext.setAttribute("hostIp", hostIp); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>쿠키로 아이디 저장 연습</title> <%@ include file = "/include/bs4.jsp" %> <script> function logout() { alert('<%=mid %>님 로그아웃 되었습니다.'); location.href = '<%=request.getContextPath() %>/study/homework/ex01_login.jsp'; } function displayHostInfo() { let hostInfoDiv = document.getElementById('hostInfo'); if(hostInfoDiv.style.display === 'none' || hostInfoDiv.style.display === '') { hostInfoDiv.style.display = 'block'; } else { hostInfoDiv.style.dispaly = 'none'; } } </script> </head> <body> <jsp:include page="/include/header.jsp" /> <jsp:include page="/include/nav.jsp" /> <div class="container mt-5"> <div class="member-container text-center" style="max-width: 600px; margin: auto; padding: 20px;"> <h3>회원 전용 방</h3> <p>현재 <%=mid %>님 로그인 중입니다.</p> <hr/> <button type="button" class="btn btn-success" id="displayHostInfoBtn" xxonclick="displayHostInfo()">접속조회</button> <a href="ex01_login.jsp" class="btn btn-warning" xxonclick="logout()">로그아웃</a> </div> <div class="host-container text-center" id="hostInfo" style="max-width: 700px; margin: auto; padding: 20px; display: none;"> <hr/> <h3>서버 환경 변수 정보</h3> <div>1. 호스트 IP : <%=hostIp %></div> <div>2. 접속 URL : <%=request.getRequestURL() %></div> <div>3. ContextPath명 : <%=request.getContextPath() %></div> <div>4. 접속 프로토콜 : <%=request.getProtocol() %></div> <div>5. 전송 방식 : <%=request.getMethod() %></div> <hr/> <button type="button" class="btn btn-primary" xxonclick="location.reload()">새로고침</button> </div> </div> <jsp:include page="/include/footer.jsp" /> </body> </html> | cs |
첫댓글 div태그로 아주 예쁘게 잘 구성하셨네요.
jsp에서 처리된 내용들을 서블릿에서도 잘 연습해 보세요. 수고하셨습니다.