1. 회원정보 : 로그인
- Controller : Login
package member.controller;
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 javax.servlet.http.HttpSession;
import member.service.MemberService;
import member.service.MemberServiceImpl;
import member.vo.MemberVo;
@WebServlet("/member/login")
public class Login extends HttpServlet{
private MemberService memberService = MemberServiceImpl.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 화면(회원 등록)
req.getRequestDispatcher("/WEB-INF/lib/jsp/member/login.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 로직(회원 등록)
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
String pwd = req.getParameter("pwd");
System.out.println(id);
System.out.println(pwd);
req.getSession().setAttribute("member", memberService.login(id, pwd));
req.getSession().setMaxInactiveInterval(600);
resp.sendRedirect("list");
}
}
- Service : MemberService
package member.service;
import java.util.List;
import member.vo.MemberVo;
public interface MemberService {
public List<MemberVo> list();
public void register(MemberVo memberVo);
public void remove(String id);
public MemberVo login(String id, String pwd);
}
- Service : MemberServiceImpl
package member.service;
import java.util.List;
import member.dao.MemberDao;
import member.vo.MemberVo;
public class MemberServiceImpl implements MemberService{
private static final MemberService memberService = new MemberServiceImpl();
public static MemberService getInstance() {
return memberService;
}
private MemberServiceImpl() {}
private MemberDao memberDao = MemberDao.getInstance();
// list, register, remove 생략
@Override
public MemberVo login(String id, String pwd) {
return memberDao.login(id, pwd);
}
}
- DAO : MemberDao
package member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import member.vo.MemberVo;
import utils.DBConn;
public class MemberDao {
/**
* singleton
*/
private final static MemberDao dao = new MemberDao();
public static MemberDao getInstance() {
return dao;
}
public MemberDao() {}
/**
*
*/
private Statement stmt;
private PreparedStatement pstmt;
private Connection conn;
// list, register, remove 생략
public MemberVo login(String id, String pwd) {
MemberVo memberVo = null;
try {
conn = DBConn.getConnection();
String query = "SELECT * FROM T_MEMBER WHERE ID = ? AND PWD = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int idx = 1;
memberVo = new MemberVo(
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getDate(idx++)
);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return memberVo;
}
}
- JSP : login
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css" integrity="sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/js/bootstrap.min.js" integrity="sha512-EKWWs1ZcA2ZY9lbLISPz8aGR2+L7JVYqBAYTq5AXgBkSjRSuQEGqWx8R1zAX16KdXPaCjOCaKE8MCpU0wcHlHA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
// 유효성 검증 예정
window.onload = function() {
document.frm.onsubmit = function() {
var id = document.frm.id;
var pwd = document.frm.pwd;
if(!id.value) {
alert("아이디를 입력하세요")
id.focus();
return false;
}
if(!pwd.value) {
alert("비밀번호를 입력하세요")
pwd.focus();
return false;
}
}
}
</script>
</head>
<body>
<div class="container">
<div class="col-6 mx-auto">
<form class="form-control" name="frm" method="post" >
<label for="userId" class="form-label">아이디</label>
<input type="text" name="id" id="userId" class="form-control">
<label for="password" class="form-label">비밀번호</label>
<input type="text" name="pwd" id="password" class="form-control">
<div class="d-grid">
<button class="btn btn-primary btn-block my-3">로그인</button>
</div>
<button type="reset" class="btn btn-danger">다시입력</button>
</form>
</div>
</div>
</body>
</html>
2. 회원정보 : 로그아웃
- Controller : Logout
package member.controller;
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("list");
}
}
- JSP : list(로그인 여부 추가)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<c:if test="${empty member}">
<p>로그인 하지 않은 상태</p>
<a href="login">로그인 하러 가기</a>
</c:if>
<c:if test="${not empty member}">
<p>로그인 한 상태</p>
<p>${member.name}님 환영합니다</p>
<a href="logout">로그아웃</a>
</c:if>
</div>
<table>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>이름</th>
<th>이메일</th>
<th>가입일</th>
<th>삭제</th>
</tr>
<c:forEach items="${members}" var="member">
<tr>
<td>${member.id}</td>
<td>${member.pwd}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td>${member.joinDate}</td>
<td><a href="remove?id=${member.id}">삭제</a></td>
</tr>
</c:forEach>
</table>
<a href="register">회원가입</a>
</body>
</html>