๋ก๊ทธ์ธ
โข
๊ธฐ๋ฅ ์ค๋ช
โข
์์
์์
โข
์ค์ต ์ฝ๋
โข
ํ
์คํธ
โข
๊ฒฐ๊ณผ ํ๋ฉด
๊ธฐ๋ฅ ์ค๋ช
๋ก๊ทธ์ธ ๊ธฐ๋ฅ์ ์
๋ ฅํ ์ฌ์ฉ์์ ์ ๋ณด๊ฐ 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
๋ณต์ฌ
ํ ์คํธ
(์ถ๊ฐ์ค)