mvc패턴 의 기존 servlet
클라이언트 요청 -> 서블릿을 통해 dao에 접근해 db에 값을 넣는다
Controller에게 요청을 보내는 코드를 따로 작성해야 함
FrontController 패턴
FrontController가 서블릿 하나로 사용자의 모든 요청을 컨트롤
( 요청에 맞는 컨트롤러를 찾아 호출해줍니다)
- 장점 - 공통코드 처리 가능, 다른 servlet을 사용하지 않아도 된다
유지보수, 효율 up
- 단점 - 한 가지 타입의 컨트롤러만 호출할 수 있다, 유연하지 X
각각의 servlet을 FrontController 에서 한 번에 요청하고 실행할 수 있도록 합쳐보기
servlet 생성
끝에 .do로 끝나는 모든 요청이 하나의 servlet(FronController) 으로 오게할 것
<main.jsp>
<form action="JoinService.do" method="post">
<FrontController.java>
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("프론트 컨트롤러 실행!");
1. client가 요청한 URL주소 가져오기
String requestURI = request.getRequestURI();
System.out.println(requestURI);
2. Context Path (웹 어플리케이션의 시작 주소) 가져오기
String contextPath = request.getContextPath();
System.out.println(contextPath);
3. client 요청 부분만 분리(contextPath 빼버리기)
String command = requestURI.substring(contextPath.length() + 1);
System.out.println(command);
request.setCharacterEncoding("EUC-KR");
response.sendRedirect(map.get(command).execute(request, response));
1. 웹페이지에서 회원가입 실행시
MessageSystem -> contextPath(프로젝트의 시작 경로) 필요 없으므로 잘라낼 것
2.
3. contextPath 길이만큼 잘라내기 위해 substring contextPaht.length() + 1);(MessageSystem/까지 잘라내기 위해)
JoinService.do
<FrontController.java>
} if(command.equals("JoinService.do")) {
회원가입 기능 구현
String email = request.getParameter("email");
String pw = request.getParameter("pw");
String phone = request.getParameter("phone");
String addr = request.getParameter("addr");
MemberDTO dto = new MemberDTO(email, pw, phone, addr);
MemberDAO dao = new MemberDAO();
int cnt = dao.join(dto);
}else if (command.equals("LoginService.do")) {
로그인 기능 구현
String email = request.getParameter("email");
String pw = request.getParameter("pw");
MemberDTO dto = new MemberDTO(email, pw);
MemberDAO dao = new MemberDAO();
MemberDTO info = dao.login(dto);
if(info != null) {
System.out.println("로그인 성공");
System.out.println(info.toString());
HttpSession session = request.getSession();
session.setAttribute("info", info);
}else {
System.out.println("로그인 실패");
}
response.sendRedirect("main.jsp");
}
회원가입과 로그인 요청&실행이 한 servlet에서 모두 이루어진다
'Study > JSP' 카테고리의 다른 글
[JSP] HashMap (2) | 2023.09.24 |
---|---|
[JSP] command패턴 (2) | 2023.09.21 |
[JSP] JSTL (2) - jstl 라이브러리 설치하기, <c:> 태그 사용하기 (2) | 2023.09.03 |
[JSP] JSTL (1) - 페이지값과 dto 가져오기, 내장객체 접근하기, parameter 처리 (2) | 2023.09.01 |