package sec01.ex01;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MemberController
*/
@WebServlet("/member/*") // /mem.do -> /member/*
public class MemberController extends HttpServlet {
private static final long serialVersionUID = 1L;
MemberDAO memberDAO;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
memberDAO = new MemberDAO();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doHandle(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doHandle(request,response);
}
//변경
private void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String nextPage = null; //변경
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action = request.getPathInfo();
System.out.println("action : " + action);
if(action == null || action.equals("/listMembers.do")) {
List<MemberVO> membersList = memberDAO.listMembers();
request.setAttribute("membersList", membersList);
nextPage = "/test03/listMembers.jsp";
}else if(action.equals("/addMember.do")) {
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
MemberVO memberVO = new MemberVO(id,pwd,name,email);
memberDAO.addMember(memberVO);
nextPage="/member/listMembers.do";
}else if(action.equals("/memberForm.do")) {
String id = request.getParameter("id");
MemberVO memInfo = memberDAO.findMember(id);
request.setAttribute("memInfo", memInfo);
nextPage = "/test03/memberForm.jsp";
}else if(action.equals("/modMemberForm.do")){
String id = request.getParameter("id");
MemberVO memInfo = memberDAO.findMember(id);
request.setAttribute("memInfo", memInfo);
nextPage = "/test03/modMemberForm.jsp";
}else if(action.equals("/modMember.do")){
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
MemberVO memberVO = new MemberVO(id, pwd, name, email);
memberDAO.modMember(memberVO);
request.setAttribute("msg", "modified");
nextPage = "/member/listMembers.do";
}else if(action.equals("/delMember.do")){
String id = request.getParameter("id");
memberDAO.delMember(id);
request.setAttribute("msg", "deleted");
nextPage = "/member/listMembers.do";
}else {
List<MemberVO> membersList = memberDAO.listMembers();
request.setAttribute("membersList", membersList);
nextPage = "/test03/listMembers.jsp";
}
RequestDispatcher dispatch = request.getRequestDispatcher(nextPage);
dispatch.forward(request, response);
}
}