Context path 경로 변경
MessageSystem을 복사해서 FrontController로 붙여넣어주기
=> 오류남
=> MessageSystem과 path가 동일하면 실행이 안되기 때문.
오류해결방법
방법 1.
server.xml 파일에 들어가서 /MessageSystem이라는 path를 수정
방법 2.
Controller - Interface로 메소드 선언
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// interface : 양식 => 추상메소드/상수만을 정의 (강제성 부여)
public interface Controller {
// 추상메소드
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
GoMainController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GoMainCon implements Controller {
// 각각의 요청을 처리하는 로직을 메소드로 정의
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// 1. 데이터 수집
// 2. 기능구현
// 3. View 선택
return "main";
}
}
GoSuccessController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GoSuccessCon implements Controller{
// ctrl + shift + o > import 자동단축키
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// 1. 데이터 수집
// 2. 기능구현
// 3. View 선택
return "join_success";
}
}
JoinController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class JoinCon implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 데이터 수집
request.setCharacterEncoding("UTF-8");
String email = request.getParameter("email");
String pw = request.getParameter("pw");
String tel = request.getParameter("tel");
String address = request.getParameter("address");
// 데이터가 2개 이상이라면, DTO로 묶어놓기
Member dto = new Member();
dto.setEmail(email);
dto.setPw(pw);
dto.setTel(tel);
dto.setAddress(address);
// 2. 기능 구현 : DB에 회원정보를 저장(DAO)
// 2-1) DAO를 생성
MemberDAO dao = new MemberDAO();
int row = dao.join(dto);
// 3. View 선택
String nextView = null;
if (row > 0) {
// 성공 => 회원가입 성공 페이지
nextView = "redirect:/goSuccess.do";
} else {
// 실패 => 메인페이지
nextView = "redirect:/goMain.do";
}
// 페이지 이동
// forward vs redirect
// c->v(jsp) c->c(Servlet)
// 기능의 중복 실행을 방지하기 위해서 url을 초기화하려고 redirect를 사용하는 것
return nextView;
}
}
LoginController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class LoginCon implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 데이터 수집
String email = request.getParameter("email");
String pw = request.getParameter("pw");
Member dto = new Member();
dto.setEmail(email);
dto.setPw(pw);
// 기능 구현
MemberDAO dao = new MemberDAO();
Member user = dao.login(dto);
// View 연결
// 방법 1. 성공실패 구분해서 console 창에 출력
// 방법 2. 데이터를 저장해서 옮길 수 있는 객체 4가지
// pageContext
// request <=> forward 이동과 함께 초기화
// session <= 브라우저 닫힐 때까지 유효
// application
if (user != null) {
System.out.println("로그인 성공");
// 1) 세션 객체 받아오기
HttpSession session = request.getSession();
// 2) 세션에 데이터 저장
session.setAttribute("user", user);
} else {
System.out.println("로그인 실패");
}
String nextView = "redirect:/goMain.do";
return nextView;
}
}
LogoutController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 수집 => X
// 2. 기능 구현
// 세션에 저장되어있는 사용자 정보를 삭제
HttpSession session = request.getSession();
session.removeAttribute("user");
// 3. view 선택
String nextView = "redirect:/goMain.do";
return nextView;
}
}
UpdateController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class UpdateController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 수집
request.setCharacterEncoding("UTF-8");
String pw = request.getParameter("pw");
String tel = request.getParameter("tel");
String address = request.getParameter("address");
// email은 session으로부터 꺼내오기
HttpSession session = request.getSession();
Member user = (Member)session.getAttribute("user");
String email = user.getEmail();
// DTO 객체로 묶기
Member dto = new Member();
dto.setPw(pw);
dto.setTel(tel);
dto.setAddress(address);
dto.setEmail(email);
// 2. 기능 구현
MemberDAO dao = new MemberDAO();
int row = dao.update(dto);
// 3. View 선택
String nextView = null;
if(row>0) {
nextView = "redirect:/goMain.do";
// seesion에 저장된 데이터 업데이트
session.setAttribute("user", dto);
}else {
nextView = "redirect:/update.do";
}
return nextView;
}
}
GoSelectController
package kr.smhrd.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class GoSelectController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 수집 => X
// 2. 기능 구현
MemberDAO dao = new MemberDAO();
List<Member> list = dao.select();
// select.jsp한테 list를 넘겨주기
// Controller => jsp로 데이터를 넘겨줄 때 request를 사용
request.setAttribute("list", list);
// View 선택
String nextView = "select";
return nextView;
}
}
GoUpdateController
package kr.smhrd.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class GoUpdateController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 수집
// 2. 기능 구현
// 3. View 선택
// C -> jsp : forward
return "update";
}
}
DeleteController
package kr.smhrd.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
public class DeleteController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 수집
String email = request.getParameter("email");
// 2. 기능구현
MemberDAO dao = new MemberDAO();
int row = dao.delete(email);
// 3. View 선택
String nextView = "redirect:/goSelect.do";
return nextView;
}
}
FrontController
package kr.smhrd.frontController;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.smhrd.controller.Controller;
import kr.smhrd.controller.DeleteController;
import kr.smhrd.controller.GoMainCon;
import kr.smhrd.controller.GoSelectController;
import kr.smhrd.controller.GoSuccessCon;
import kr.smhrd.controller.GoUpdateController;
import kr.smhrd.controller.JoinCon;
import kr.smhrd.controller.LoginCon;
import kr.smhrd.controller.LogoutController;
import kr.smhrd.controller.UpdateController;
import kr.smhrd.dao.MemberDAO;
import kr.smhrd.entity.Member;
// 모든 요청을 받을 수 있도록 url-mapping을 *로 지정
// @WebServlet("*.do") => .do로 끝나는 모든 요청
@WebServlet("*.do")
public class FrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// url은 아래 주소 전체를 나타냄
// http://localhost:8081/MessageSystem/join?email=1234
// 1. 요청 구분
// uri는 /MessageSystem/join 를 나타냄
String uri = request.getRequestURI();
// /MessageSystem 를 꺼내오는 방법
String cpath = request.getContextPath();
// /join만 떼오기
// String.subString(); => 문자열 슬라이싱
// ex) 안녕하세요.
// ex) String.subString(2); => "하세요."
// ex) String.subString(2, 3); => "하"
String command = uri.substring(cpath.length());
System.out.println(command);
// ==========================================================
Controller con = null; // 업캐스팅을 이용한 중복제거
// 2. 요청에 따라서 기능을 실행
String nextView = null;
if(command.equals("/goMain.do")) {
con = new GoMainCon();
}else if(command.equals("/goSuccess.do")) {
con = new GoSuccessCon();
}else if(command.equals("/join.do")) {
con = new JoinCon();
}else if(command.equals("/login.do")) {
con = new LoginCon();
}else if(command.equals("/logout.do")) {
con = new LogoutController();
}else if(command.equals("/update.do")) {
con = new UpdateController();
}else if(command.equals("/goSelect.do")) {
con = new GoSelectController();
}else if(command.equals("/goUpdate.do")) {
con = new GoUpdateController();
}else if(command.equals("/delete.do")) {
con = new DeleteController();
}
// ================================================================
if(con != null) {
nextView = con.execute(request, response);
}
// 3. 페이지 이동
// 예외처리용
if(nextView != null) {
// redirect vs forward 구분하기 위해서 규칙필요
// 앞으로 redirect 요청은 반드시 앞에 redirect:/를 붙이자.
// goMain.do => redirect:/goMain.do
if(nextView.contains("redirect:/")) {
// nextView = "redirect:/goMain.do"
// nextView.split(":/"); => ["redirect", "goMain.do"]
response.sendRedirect(nextView.split(":/")[1]);
}else {
RequestDispatcher rd =
request.getRequestDispatcher("WEB-INF/views/"+nextView+".jsp");
rd.forward(request, response);
}
}
}
}
'Web > JSP_Servlet' 카테고리의 다른 글
EL과 JSTL (1) | 2023.05.04 |
---|---|
MVC_게시글 작성, 게시글 조회 by FrontController (1) | 2023.05.03 |
MVC_회원관리 시스템 (1) | 2023.04.28 |
MVC_개인정보수정 (0) | 2023.04.26 |
MVC_로그인/로그아웃 시스템 (0) | 2023.04.25 |
댓글