게시판 만들기 (MVC pattern) Ⅱ

채종윤·2023년 8월 18일
0

1. 웰컴파일

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Members MVC</title> <!--  웰컴파일 -->
</head>
<body>
	<h1> Welcome MVC</h1>
	
	<a href="member/insert_form">맴버 등록</a> <!-- 상대경로 -->
	<a href="member/list">맴버 목록</a>

</body>
</html>


2. 맴버등록

1) welcome.jsp

    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action ="insert_action" method="post"> <!--  상대경로때문에 member가 2번 생길 수 있음 -->
		아이디: <input name ="id"><br>
		이름:<input name="name"> <br>
		비밀번호:<input name="passwd"> <br>
		상태:<input name="status"> <br>
		
		
		<input type= "submit" value="맴버등록">
	</form>

</body>
</html>

2) MemberInsertForm

@WebServlet("/member/insert_form")
public class MemberInsertForm extends HttpServlet {
	private static final long serialVersionUID = 1L;
        
    public MemberInsertForm() {
        super();
        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/member_insert_form.jsp";
		request.getRequestDispatcher(url).forward(request, response);
	}
}

3)MemberInsertFormServlet

@WebServlet("/member/update_form")
public class MemberUpdateFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		String url = "/member_update_form.jsp";
		MemberDao dao = new MemberDaoImpl();
		try {
			MemberDto dto = dao.findById(id);
			System.out.println(dto);
			request.setAttribute("dto", dto);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage());
		}
		request.getRequestDispatcher(url).forward(request, response);

	}
}

4)MemberInsertSuccess

<%@ 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>
</head>
<body>

	<h1>맴버 리스트</h1>
	<p>메시지 : <%= request.getAttribute("msg") %> </p> 
	<p>메시지 : ${msg} </p> 
	<hr>
	<a href="list">맴버 목록</a>
</body>
</html>

3. 맴버 리스트

<%@page import="memberMVC.dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@page import= "memberMVC.*" %> 
<%@page import = "java.util.*" %>
	<% 
	List<MemberDto> list =(List)request.getAttribute("list");	
	
%>

1)member_list.jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>맴버 목록</title>
<style type="text/css">
	a{ text-decoration: none; color: black;}
	a:hover{ text-decoration: underline; color: blue; }
</style>
</head>
<body>
	<h1>맴버 목록</h1>
	<table>
		<tr>
			<th>아이디</th>
			<th>이름</th>
			<th>비밀번호</th>
			<th>상태</th>
		</tr>
		<% for(MemberDto dto: list){ %>
		<tr>
			<td><a href="detail?id=<%= dto.getId() %>"><%= dto.getId() %></a></td>
			<td><%= dto.getName() %></td>
			<td><%= dto.getPasswd() %></td>
			<td><%= dto.getStatus() %></td>
		</tr>
		<% }  %>
	</table>

</body>
</html>

2) MemberListServlet

@WebServlet("/member/list")
public class MemberListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MemberDao dao = new MemberDaoImpl();
		String url = "/member_list.jsp";
		try {
			List<MemberDto> list = dao.list();
			request.setAttribute("list", list);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage());
		}
		request.getRequestDispatcher(url).forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

3. 맴버 수정

1) member_update_form

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>맴버 수정 폼</title>
</head>
<body>

	<h1> 맴버 정보 수정</h1>
	<form action ="update_action" method="post"> 
		<input type="hidden" name="id" value="${dto.id}"> <!--  보이면 안되기때문에 hidden -->
		이름: <input name ="name" value="${dto.name}"><br>
		비밀번호:<input name="passwd"value="${dto.passwd}"> <br>
		상태:<input name="status"value="${dto.status}"> <br>
		
		<br>
		
		<input type= "submit" value="맴버수정">
	</form>
</body>
</html>

2) MemberUpdateFormServlet

@WebServlet("/member/update_form")
public class MemberUpdateFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		String url = "/member_update_form.jsp";
		MemberDao dao = new MemberDaoImpl();
		try {
			MemberDto dto = dao.findById(id);
			System.out.println(dto);
			request.setAttribute("dto", dto);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage());
		}
		request.getRequestDispatcher(url).forward(request, response);

	}
}

3)MemberUpdateActionServlet

@WebServlet("/member/update_action")
public class MemberUpdateActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8"); //한글화,getParameter전에 선언
		MemberDto dto = new MemberDto();
		dto.setName(request.getParameter("name"));
		dto.setPasswd(request.getParameter("passwd"));
		dto.setStatus(request.getParameter("status"));
		dto.setId(request.getParameter("id"));
		
	
		String url="/member_ok.jsp"; // /는 wepapp
		try {
			MemberDao dao = new MemberDaoImpl();
			dao.update(dto);
			request.setAttribute("msg", "게시물 수정 성공");
		} catch (Exception e) {
		
			e.printStackTrace();
			url="/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage()); //jsp한테 값 전달 setAttribute
		} 
		
		request.getRequestDispatcher(url).forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

4) 맴버 삭제

1) member_delete_form

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>맴버 수정 폼</title>
</head>
<body>

	<h1> 맴버 정보 수정</h1>
	<form action ="update_action" method="post"> 
		<input type="hidden" name="id" value="${dto.id}"> <!--  보이면 안되기때문에 hidden -->
		이름: <input name ="name" value="${dto.name}"><br>
		비밀번호:<input name="passwd"value="${dto.passwd}"> <br>
		상태:<input name="status"value="${dto.status}"> <br>
		
		<br>
		
		<input type= "submit" value="맴버수정">
	</form>
</body>

2) MemberDeleteFormServlet

@WebServlet("/member/delete_form")
public class MemberDeleteFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
   

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		String url = "/member_delete_form.jsp";
		MemberDao dao = new MemberDaoImpl();
		try {
			MemberDto dto = dao.findById(id);
			request.setAttribute("dto", dto);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage());
		}
		request.getRequestDispatcher(url).forward(request, response);
	}
	

}

3) MemberDeleteActionServlet

@WebServlet("/member/delete_action")
public class MemberDeleteActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//BoardDto생성, 데이터 저장
				request.setCharacterEncoding("UTF-8");
				String id = request.getParameter("id");
				
				String url = "/member_ok.jsp";
				try {
					MemberDao dao = new MemberDaoImpl();
					dao.delete( id );
					request.setAttribute("msg", "게시물 삭제 성공");
				} catch (Exception e) {
					e.printStackTrace();
					url = "/member_error.jsp";
					request.setAttribute("err_msg", e.getMessage());
				}
				request.getRequestDispatcher(url).forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

5.맴버 상세보기

1) member_detail

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상세보기</title>
</head>
<body>
	<h1>상세보기</h1>
	<table>
		<tr>
			<th>아이디</th><td>${dto.id }</td>
		</tr>
		<tr>
			<th>이름</th><td>${dto.name}</td>
		</tr>
		<tr>
			<th>비밀번호</th><td>${dto.passwd}</td>
		</tr>
		<tr>
			<th>상태</th><td>${dto.status}</td>
		</tr>
		
	</table>
	<a href="list">목록</a>
	<a href="update_form?id=${dto.id}">수정</a>
	<a href="delete_form?id=${dto.id}">삭제</a>
	
	
</body>
</html>

2) MemberDetailServlet

@WebServlet("/member/detail")
public class MemberDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		String url = "/member_detail.jsp";
		MemberDao dao = new MemberDaoImpl();
		try {
			MemberDto dto = dao.findById(id);
			request.setAttribute("dto", dto);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "/member_error.jsp";
			request.setAttribute("err_msg", e.getMessage());
		}
		request.getRequestDispatcher(url).forward(request, response);
	}

}

profile
안녕하세요. 백앤드 개발자를 목표로 하고 있습니다!

0개의 댓글