38. Servlet front controller pattern & command pattern

jk·2024년 2월 27일
0

kdt 풀스택

목록 보기
73/127



1. 아래를 구현하시오.

1) modify.jsp 복사

//code
//FrontController.java
		} else if (command.equals("/modify.do")) {
			System.out.println("update the data");
			System.out.println("----------------");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset-UTF-8");
			PrintWriter writer = response.getWriter();
			writer.println("<html><head></head><body>");
			mCommand = new MemberModifyCommand();
			mCommand.execute(request, response);
			writer.println("<h1>회원정보 수정 페이지</h1>");
			writer.println("<form action=\"modify.do\" method=\"post\">");
			writer.println("아이디: <input type=\"text\" name=\"id\" size=\"20\"><br/>");
			writer.println("패스워드: <input type=\"password\" name=\"pw\" size=\"20\"><br/>");
			writer.println("이메일: <input type=\"text\" name=\"email\" size=\"20\"><br/>");
			writer.println("주소: <input type=\"text\" name=\"address\" size=\"20\"><br/>");
			writer.println("<input type=\"submit\" value=\"수정\">");
			writer.println("<input type=\"reset\" value=\"초기화\">");
			writer.println("</form>");
			writer.println("</body></html>");
		};



2) 아래의 클래스 구현

 public class MemberModifyCommand implements MemberCommand {
}
//code
public class MemberModifyCommand implements MemberCommand {
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String pw= request.getParameter("pw");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		MemberDto dto = new MemberDto(id, pw, email, address);
		MemberDao dao = new MemberDao();
		dao.updateMember(dto);
	}
}



3) modify.do 명령으로 멤버 업데이트 후

업데이트 되었습니다. 출력
//code
//FrontController.java
		switch (command) {
			case "/modify.do": {
				System.out.println("update the data");
				System.out.println("----------------");
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html; charset-UTF-8");
				PrintWriter writer = response.getWriter();
				writer.println("<html><head></head><body>");
				mCommand = new MemberModifyCommand();
				mCommand.execute(request, response);
				writer.println("<h1>회원정보 수정 페이지</h1>");
				writer.println("<form action=\"print.do\" method=\"post\">");
				writer.println("아이디: <input type=\"text\" name=\"id\" size=\"20\"><br/>");
				writer.println("패스워드: <input type=\"password\" name=\"pw\" size=\"20\"><br/>");
				writer.println("이메일: <input type=\"text\" name=\"email\" size=\"20\"><br/>");
				writer.println("주소: <input type=\"text\" name=\"address\" size=\"20\"><br/>");
				writer.println("<input type=\"submit\" value=\"수정\">");
				writer.println("<input type=\"reset\" value=\"초기화\">");
				writer.println("</form>");
				writer.println("</body></html>");
				break;
			}
			case "/print.do": {
				System.out.println("update the data");
				System.out.println("----------------");
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html; charset-UTF-8");
				PrintWriter writer = response.getWriter();
				writer.println("<html><head></head><body>");
				mCommand = new MemberModifyCommand();
				mCommand.execute(request, response);
				writer.println("업데이트 되었습니다.");
				writer.println("</body></html>");
				break;
			}
			default: {
			}
		};



2. 아래 용어를 설명하시오.

  • FrontController 패턴 : The servlet processes every requests from clients.
  • Command 패턴 : The servler lets classes process the requests from clients.
profile
Brave but clumsy

1개의 댓글

comment-user-thumbnail
2024년 9월 4일

These alternatives to Bitcoin each bring something unique to the table, addressing different limitations or targeting specific markets. Instant withdrawal casino site

답글 달기