Search

๋กœ๊ทธ์ธ

๋กœ๊ทธ์ธ

โ€ข
๊ธฐ๋Šฅ ์„ค๋ช…
โ€ข
์ž‘์—… ์ˆœ์„œ
โ€ข
์‹ค์Šต ์ฝ”๋“œ
โ€ข
ํ…Œ์ŠคํŠธ
โ€ข
๊ฒฐ๊ณผ ํ™”๋ฉด

๊ธฐ๋Šฅ ์„ค๋ช…

๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์€ ์ž…๋ ฅํ•œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๊ฐ€ DB์˜ ๋ฏธ๋ฆฌ ์ €์žฅ๋œ ์ •๋ณด์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ, ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์„ธ์…˜์— ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.
์„ธ์…˜์— ๋Œ€ํ•˜์—ฌ๋Š” ์•„๋ž˜ ํŽ˜์ด์ง€์—์„œ ์ƒ์„ธํžˆ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ž‘์—… ์ˆœ์„œ

1.
login.jsp - ๋กœ๊ทธ์ธ ํ™”๋ฉด ์ž‘์„ฑ
2.
login_pro.jsp - ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ๋กœ์ง ์ž‘์„ฑ
3.
header.jsp - ํ—ค๋”์— ๋กœ๊ทธ์ธ ์•„์ด๋”” ์ถœ๋ ฅ

login.jsp - ๋กœ๊ทธ์ธ ํ™”๋ฉด ์ž‘์„ฑ

์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์„ ์ž…๋ ฅํ•˜๊ณ  form ์š”์ฒญ์„ ์ „์†กํ•˜๋Š” form ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. - ์•„์ด๋”” ์ €์žฅ, ์ž๋™ ๋กœ๊ทธ์ธ ์ฒดํฌ ๋ฐ•์Šค ๋“ฑ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. - ํšŒ์›๊ฐ€์ž…, ์•„์ด๋”” ์ฐพ๊ธฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ ๋“ฑ์˜ ๋ฒ„ํŠผ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

login_pro.jsp - ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ๋กœ์ง ์ž‘์„ฑ

1. ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ DB ์˜ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ 2. ์ผ์น˜ ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด ๋“ฑ๋ก ๋ถˆ์ผ์น˜ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ

header.jsp - ํ—ค๋”์— ๋กœ๊ทธ์ธ ์•„์ด๋”” ์ถœ๋ ฅ

์„ธ์…˜์—์„œ ๋กœ๊ทธ์ธ ์•„์ด๋””๋ฅผ ๊ฐ€์ ธ์™€ ํ—ค๋”์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๋น„ ๋กœ๊ทธ์ธ ์‹œ ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ ์ถœ๋ ฅ
โ€ข
๋กœ๊ทธ์ธ ์‹œ ๋กœ๊ทธ์ธ ์•„์ด๋”” ๋ฐ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ ์ถœ๋ ฅ

์‹ค์Šต ์ฝ”๋“œ

โ€ข
login.jsp
โ€ข
login_pro.jsp
โ€ข
header.jsp

login.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@page import="java.net.URLDecoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html data-bs-theme="light"> <head> <meta charset="UTF-8"> <jsp:include page="/layout/main/import/common.jsp" /> <jsp:include page="/layout/main/import/meta.jsp" /> <jsp:include page="/layout/main/import/link.jsp" /> </head> <body> <jsp:include page="/layout/main/UI/header.jsp" /> <div class="container"> <div class="px-4 pt-5 my-5 text-center"> <div class="col-lg-4 mx-auto"> <form action="${ root }/users/login_pro.jsp" method="post"> <div class="row justify-content-center"> <div class="card p-5"> <h2 class="display-4 fw-bold">Login</h2> <div class="card-body"> <div class="mb-3"> <input class="form-control" type="text" name="username" id="username" placeholder="์•„์ด๋””"> </div> <div class="mb-3"> <input class="form-control" type="password" name="password" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"> </div> <div class="row"> <div class="mb-3 col-6"> <input type="checkbox" class="form-check-input" name="rememberId" id="remember-id"> <label class="form-check-label" for="remember-id">์•„์ด๋”” ์ €์žฅ</label> </div> <div class="mb-3 col-6"> <input type="checkbox" class="form-check-input" name="rememberMe" id="remember-me"> <label class="form-check-label" for="remember-me">์ž๋™ ๋กœ๊ทธ์ธ</label> </div> </div> <div class="d-grid gap-2"> <button type="submit" class="btn btn-primary">๋กœ๊ทธ์ธ</button> <a href="${ root }/users/join.jsp" class="btn btn-success">ํšŒ์›๊ฐ€์ž…</a> </div> </div> </div> </div> </form> </div> </div> </div> <jsp:include page="/layout/main/UI/footer.jsp" /> <jsp:include page="/layout/main/import/script.jsp" /> </body> </html>
HTML
๋ณต์‚ฌ

login_pro.jsp

<%@page import="java.net.URLEncoder"%> <%@page import="users.service.UserServiceImpl"%> <%@page import="users.service.UserService"%> <%@page import="users.model.Users"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% String username = request.getParameter("username"); String password = request.getParameter("password"); Users user = new Users(); user.setUsername(username); user.setPassword(password); // ๋กœ๊ทธ์ธ ์š”์ฒญ UserService userService = new UserServiceImpl(); Users loginUser = userService.login(user); // ๋กœ๊ทธ์ธ ์‹คํŒจ if( loginUser == null ) { response.sendRedirect("login.jsp?error"); return; } String root = request.getContextPath(); if( loginUser != null ) { // ๋กœ๊ทธ์ธ ์„ฑ๊ณต // ์„ธ์…˜์— ์•„์ด๋”” ๋“ฑ๋ก ํ›„, ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ loginUser.setPassword(null); session.setAttribute("loginId", loginUser.getUsername()); session.setAttribute("usersId", loginUser.getId()); session.setAttribute("loginUser", loginUser); response.sendRedirect(root + "/"); } else { // ๋กœ๊ทธ์ธ ์‹คํŒจ response.sendRedirect("login.jsp?error"); } %>
Java
๋ณต์‚ฌ

header.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String root = request.getContextPath(); String loginId = (String) session.getAttribute("loginId"); %> <header> <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <div class="container-fluid"> <a class="navbar-brand" href="<%= root %>/">Home</a> <button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse" id="navbarCollapse"> <!-- ########## menu ๐Ÿš€ ########## --> <jsp:include page="/layout/main/UI/menu.jsp" /> <!-- ########## menu ๐Ÿšฉ ########## --> <ul class="navbar-nav mx-5 px-4"> <% if( loginId == null || loginId.equals("") ) { %> <!-- ๋น„๋กœ๊ทธ์ธ ์‹œ --> <li class="nav-item"> <a class="nav-link" aria-current="page" href="${ root }/users/login.jsp">๋กœ๊ทธ์ธ</a> </li> <li class="nav-item"> <a class="nav-link" aria-current="page" href="${ root }/users/join.jsp">ํšŒ์›๊ฐ€์ž…</a> </li> <% } else { %> <!-- ๋กœ๊ทธ์ธ ์‹œ --> <li class="nav-item"> <div class="dropdown text-white"> <a href="#" class="d-flex align-items-center link-body-emphasis text-decoration-none dropdown-toggle text-white" data-bs-toggle="dropdown" aria-expanded="false"> <img src="<%= root %>/static/img/logo.png" alt="" width="32" height="32" class="rounded-circle me-2"> <strong class="text-white"><%= loginId %></strong> </a> <ul class="dropdown-menu text-small shadow"> <%-- <li><a class="dropdown-item" href="${ root }/user/index.jsp">๋งˆ์ด ํŽ˜์ด์ง€</a></li> --%> <%-- <li><a class="dropdown-item" href="${ root }/user/update.jsp">ํšŒ์›์ •๋ณด ์ˆ˜์ •</a></li> --%> <%-- <li><a class="dropdown-item" href="${ root }/user/order.jsp">์ฃผ๋ฌธ๋‚ด์—ญ</a></li> --%> <!-- <li><hr class="dropdown-divider"></li> --> <li><a class="dropdown-item" href="${ root }/users/logout.jsp">๋กœ๊ทธ์•„์›ƒ</a></li> </ul> </div> </li> <% } %> </ul> </div> </div> </nav> </header>
HTML
๋ณต์‚ฌ

ํ…Œ์ŠคํŠธ

(์ถ”๊ฐ€์ค‘)

๊ฒฐ๊ณผ ํ™”๋ฉด