1. DataBase에 연결
2. DataBase로 부터 모든 회원 정보를 조회
3. 조회된 정보를 MemberDTO 객체로 생성
4. MemberDTO 객체들을 Collection에 add
- 모든 데이터를 조회할 것이므로 ArrayList가 유리함
5. Collection 객체를 Scope(공유영역)에 설정
- 요청부터 응답까지만 유효하면 되므로 RequestScope에 설정
6. Scope로부터 Attribute를 Get 하여 JSP에서 출력
package member.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import member.dto.MemberDTO;
public class GetAllMemberInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public GetAllMemberInfoServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
String url = servletContext.getInitParameter("url");
String user = servletContext.getInitParameter("user");
String dbPwd = servletContext.getInitParameter("password");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "select * from member";
ArrayList<MemberDTO> list = new ArrayList<MemberDTO>();
String resUrl = null;
try {
connection = DriverManager.getConnection(url, user, dbPwd);
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
MemberDTO memberDTO = new MemberDTO(resultSet.getString(1),
resultSet.getString(2), resultSet.getString(3),
resultSet.getString(4), resultSet.getInt(5));
list.add(memberDTO);
}
request.setAttribute("member_list", list);
resUrl ="/member_list.jsp";
} catch (SQLException e) {
e.printStackTrace();
request.setAttribute("error_message", e.getMessage());
resUrl = "/res/error.jsp";
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl);
requestDispatcher.forward(request, response);
}
}
<%@page import="member.dto.MemberDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
ArrayList<MemberDTO> member_list = (ArrayList<MemberDTO>)request.getAttribute("member_list");
%>
<body>
<h2>모든 회원 목록조회</h2><hr><p>
<table width="500" border="1" cellspacing="0">
<tr align="center">
<td>순번</td><td>아이디</td><td>이름</td><td>주민번호</td><td>마일리지</td>
</tr>
<%
for(int i=0;i<member_list.size();i++){
%>
<tr align="center">
<td><%=i+1 %></td><td><%=member_list.get(i).getId() %></td><td><a href="/servlet_member/SearchMemberByID?id=<%=member_list.get(i).getId()%>"><%=member_list.get(i).getName() %></a></td><td><%=member_list.get(i).getRegisterNumber().substring(0, 6) %>-<%=member_list.get(i).getRegisterNumber().substring(6) %></td><td><%=member_list.get(i).getMileage() %></td>
</tr>
<%} %>
</table>
</body>
</html>