- 회원정보 : 검색 기능
- Controller : MemberList
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 member.service.MemberService;
import member.service.MemberServiceImpl;
@WebServlet("/member/list")
public class MemberList extends HttpServlet{
private MemberService memberService = MemberServiceImpl.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String keyword = req.getParameter("keyword");
req.setAttribute("members", memberService.list(keyword));
req.getRequestDispatcher("/WEB-INF/lib/jsp/member/list.jsp").forward(req, resp);
}
}
- Service : MemberService
package member.service;
import java.util.List;
import member.vo.MemberVo;
public interface MemberService {
public List<MemberVo> list(String keyword);
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();
@Override
public List<MemberVo> list(String keyword) {
return memberDao.list(keyword);
}
// 이하 생략
}
- DAO : MemberDao
package member.dao;
// import 생략
public class MemberDao {
// 싱글턴 생략
private Statement stmt;
private PreparedStatement pstmt;
private Connection conn;
public List<MemberVo> list(String keyword) {
List<MemberVo> list = new ArrayList<>();
try {
conn = DBConn.getConnection();
String query = "SELECT * FROM T_MEMBER";
if(keyword != null && !keyword.isEmpty()) {
query += " WHERE NAME LIKE '%' || ? || '%'";
}
pstmt = conn.prepareStatement(query);
if(keyword != null && !keyword.isEmpty()) {
pstmt.setString(1, keyword);
}
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int idx = 1;
MemberVo vo = new MemberVo(
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getDate(idx++)
);
list.add(vo);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return 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="${pageContext.request.contextPath}/member/login">로그인 하러 가기</a>
</c:if>
<c:if test="${not empty member}">
<p>로그인 한 상태</p>
<p>${member.name}님 환영합니다</p>
<a href="${pageContext.request.contextPath}/member/logout">로그아웃</a>
</c:if>
</div>
<div>
<form>
<input type="text" name="keyword"><button>검색</button>
</form>
</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="${pageContext.request.contextPath}/member/register">회원 등록</a>
</body>
</html>