* 회원목록을 클릭했을 때 모든 조회목록 출력하기 (request scope의 requestDispatch 사용하기)[실습]
- servlet class
package : servlet.member.req.attribute
name : MemberListServlet
url : /member_list
package servlet.member.req.attribute;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlet.member.dto.MemberDTO;
import servlet.member.service.MemberService;
public class MemberListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MemberListServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. Business Logic처리
MemberService memberService = new MemberService();
//2. 조회한 정보 request scope에 binding
ArrayList<MemberDTO> list = memberService.searchAllMember();
//3. member_list.jsp로 수행이동
request.setAttribute("allmember", list);
RequestDispatcher rdp = request.getRequestDispatcher("/member/member_list.jsp");
rdp.forward(request, response);
}
}
html
WebContent
폴더 : member
이름 : search_form.html
<!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>
</head>
<body>
<form action="/myweb/search_member" method="post">
<table>
<tr>
<td>id : </td><td><input type="text" name="id"></td>
<td colspan="2"><input type="submit" value="전송"></td>
</tr>
<tr>
<td><a href="/myweb/MemberListservlet">회원정보조회</a></td>
</tr>
</table>
</form>
</body>
</html>
jsp
WebContent
폴더 : member
이름 : member_list.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="servlet.member.dto.MemberDTO"%>
<%@ 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>
</head>
<body>
<%
ArrayList<MemberDTO> list = (ArrayList<MemberDTO>)request.getAttribute("allmember");
%>
<b>회원정보조회</b>
<hr align="left" width="500">
<form>
<table border="1" width="500">
<tr align="center">
<td>id</td><td>이름</td><td>나이</td><td>성별</td>
</tr>
<%
for(int i=0;i<list.size();i++){
%>
<tr align="center">
<td><%=list.get(i).getId() %></td><td><%=list.get(i).getName() %></td><td><%=list.get(i).getAge() %></td><td><%=list.get(i).getSex() %></td>
</tr>
<%} %>
<tr>
<td colspan="4">
<a href="/myweb/member/search_form.html">이전단계</a>
</td>
</tr>
</table>
</form>
</body>
</html>
class
package : servlet.member.service
class name : MemberService
메소드 : SearchAllMember()
package servlet.member.service;
import java.util.ArrayList;
import servlet.member.dto.MemberDTO;
public class MemberService {
//회원고 관련된 Business Logic을 처리하는 클래스
public MemberDTO searchMemberById(String id){
//인수로 받은 ID로 회원 정보를 찾아 주는 메소드
//회원 조회 로직
MemberDTO mto = new MemberDTO(id,"홍길동",20,"남");
return mto;
}
public ArrayList<MemberDTO> searchAllMember(){
//전체 회원 정보를 조회하는 메소드
//조회 로직
ArrayList<MemberDTO> list = new ArrayList<MemberDTO>();
list.add(new MemberDTO("1111", "홍길동", 20, "남"));
list.add(new MemberDTO("2222", "유재석", 40, "남"));
list.add(new MemberDTO("3333", "이영희", 25, "여"));
list.add(new MemberDTO("4444", "박철수", 30, "남"));
list.add(new MemberDTO("5555", "홍길순", 40, "여"));
return list;
}
}
- MemberDTO
package servlet.member.dto;
public class MemberDTO {
private String id;
private String name;
private int age;
private String sex;
public MemberDTO() {
super();
}
public MemberDTO(String id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "MemberDTO [id=" + id + ", name=" + name + ", age=" + age
+ ", sex=" + sex + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MemberDTO other = (MemberDTO) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
}
- 실행결과