1. 로그인 기능 구현
- controller.member.Login.java
package controller.member;
import java.io.IOException;
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 domain.Member;
import service.MemberService;
@WebServlet("/member/login")
public class Login extends HttpServlet{
private MemberService memberService = MemberService.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
String pw = req.getParameter("pw");
Member member = new Member(id, pw, null);
System.out.println(member);
req.getSession().setAttribute("member", memberService.login(member));
System.out.println(req.getSession().getAttribute("member"));
resp.sendRedirect(req.getContextPath()+"/board/list");
}
}
- service.MemberService.java
// 로그인 기능
public Member login(Member member) {
return memberDao.login(member.getId(), member.getPw());
}
- dao.MemberDao.java
public Member login(String id, String pw) {
Member member = null;
try {
//클래스 로드
Connection conn = DBConn.getConnection();
// 문장 생성
String sql = "SELECT * FROM TBL_MEMBER\r\n" +
"WHERE ID = ? AND PW = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
int idx = 1;
pstmt.setString(idx++, id.trim());
pstmt.setString(idx++, pw.trim());
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
int idx2 = 1;
member = new Member(rs.getString(idx2++), rs.getString(idx2++), rs.getString(idx2++));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return member;
}
- login.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<jsp:include page="../common/head.jsp" />
</head>
<body class="bg-primary">
<div id="layoutAuthentication">
<div id="layoutAuthentication_content">
<main>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header">
<h3 class="text-center font-weight-light my-4">Login</h3>
</div>
<div class="card-body">
<form method="post">
<div class="form-floating mb-3">
<input class="form-control" id="inputEmail" type="text"
placeholder="name@example.com" name="id" /> <label
for="inputEmail">ID</label>
</div>
<div class="form-floating mb-3">
<input class="form-control" id="inputPassword" type="password"
placeholder="Password" name="pw" /> <label
for="inputPassword">Password</label>
</div>
<div class="form-check mb-3">
<input class="form-check-input" id="inputRememberPassword"
type="checkbox" value="" /> <label class="form-check-label"
for="inputRememberPassword">Remember Password</label>
</div>
<div
class="d-flex align-items-center justify-content-between mt-4 mb-0">
<a class="small" href="password.html">Forgot Password?</a>
<button class="btn btn-primary">Login</button>
</div>
</form>
</div>
<div class="card-footer text-center py-3">
<div class="small">
<a href="register">Need an account? Sign up!</a>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<div id="layoutAuthentication_footer">
<footer class="py-4 bg-light mt-auto">
<div class="container-fluid px-4">
<div
class="d-flex align-items-center justify-content-between small">
<div class="text-muted">Copyright © Yermi.works 2022</div>
<div>
<a href="#">Privacy Policy</a> · <a href="#">Terms
& Conditions</a>
</div>
</div>
</div>
</footer>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>
2. 로그아웃 기능 구현
- controller.member.Logout.java
package controller.member;
import java.io.IOException;
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("/member/logout")
public class Logout extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().invalidate();
resp.sendRedirect(req.getContextPath() + "/board/list");
}
}
3. 메뉴 바 수정
- nav.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<nav class="sb-topnav navbar navbar-expand navbar-dark bg-dark">
<!-- Navbar Brand-->
<a class="navbar-brand ps-3" href="index.html">Start Bootstrap</a>
<!-- Sidebar Toggle-->
<button class="btn btn-link btn-sm order-1 order-lg-0 me-4 me-lg-0" id="sidebarToggle" href="#!"><i class="fas fa-bars"></i></button>
<!-- Navbar Search-->
<form class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">
<div class="input-group">
<input class="form-control" type="text" placeholder="Search for..." aria-label="Search for..." aria-describedby="btnNavbarSearch" />
<button class="btn btn-primary" id="btnNavbarSearch" type="button"><i class="fas fa-search"></i></button>
</div>
</form>
<!-- Navbar-->
<ul class="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="fas fa-user fa-fw"></i></a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#!">Activity Log</a></li>
<li><hr class="dropdown-divider" /></li>
<c:if test="${not empty member}">
<li><a class="dropdown-item" href="#!">${member.name} Settings</a></li>
<li><a class="dropdown-item" href="${pageContext.request.contextPath}/member/logout">Logout</a></li>
</c:if>
<c:if test="${empty member}">
<li><a class="dropdown-item" href="${pageContext.request.contextPath}/member/join">join</a></li>
<li><a class="dropdown-item" href="${pageContext.request.contextPath}/member/login"">login</a></li>
</c:if>
</ul>
</li>
</ul>
</nav>