본문 바로가기
Web Programming/JAVA MVC

FrontController 사용하기

by hyeon-H 2021. 6. 18.
728x90
반응형

FrontController는 사용자가 페이지의 요청을 보냈을때 frontController에서 요청에 맞는 Controller로 보내줄것이다.
그러기 위해선 페이지요청을 모두 FrontController로 보내야하는데,
그 역할은 Web.xml이 할 것이다.

web.xml에서는 " .do " 로 들어오는 url을 모두 FrontController에서 처리요청을 하고있다. 

.do는 가상경로이다. 

 

Servlet / Web.xml 에서 controller 연결

Web.xml 이란? Web Application의 배포 서술자(DD, Deployment Descriptor)이다. 웹 애플리케이션의 배포 관련 설정을 위해 작성하는 파일이고, WEB-INF/web.xml 파일에 위치하고, 서블릿과 JSP를 어떻게 실행하느..

record-than-remember.tistory.com



그럼 " .do "의 URL은 어디서 오는 것이냐

아래와 같은 화면을 만들고 있을때.

<button type="button" class="btn btn-outline-secondary btn-sm" onclick="location.href='EventMan_Member_Find_Id.do'">아이디찾기</button>

페이지에서 "아이디찾기 " 버튼을 클릭하면 " ~.do "로 보내고 있다.

 

web.xml에서는 .do가 왔기때문에 FrontController로 처리 요청을 보내고,

web.xml

FrontController에서는 넘어온 URL을 잘게 쪼개어 어느 Controller로 보낼것인지를 정할것이다.


다음은 FrontController이다.

처음에는 상당히 복잡했지만, 계속 이해하려고 노력하고 지금처럼 기록하면 이해에 도움되는거 같다.

String uri = request.getRequestURI();					
int pathlength = request.getContextPath().length();		
String str = uri.substring(pathlength);					
String[] gubun = str.split("/");
String str2 = gubun[1];

request.getRequestURI() → 넘오는 경로중 프로젝트명부터 끝까지를 가져온다.
request.getContextPath() → 프로젝트명만 가져온다.
uri.substring(pathlength) → 넘어온 uri에서 프로젝트명을 제외한다. 
str.split("/") →  /가 들어간곳을 잘라내고 배열로 만든다.
배열중에서 필요한 문자만 사용하게 되는것이다 

예를 보자.

처음 URI가 들어오면 "/event1/EventMan_Member/EventMan_Member_Find_Id.do" 라고 들어왔다.
그중에 프로젝트명의 길이만큼을 잘라낸다. "/event1/EventMan_Member/EventMan_Member_Find_Id.do"
그리고 / 로 잘라 배열을 만든다  [0] " " , [1] "EventMan_Member" , [2] "EventMan_Member_Find_Id.do" 이 된다.
그 중 나는 1번 배열 즉 , "EventMan_Member" 만을 사용 할 것이다.

str2에는 "EventMan_Member" 를 참조하고 있다.

 

if(str2.equals("EventMan_Member")) {														
	MemberController mc = new MemberController();
	mc.doGet(request, response);
}

결론은 넘오는 URL이 EventMan_Member 이라면 MemberController를 생성하고 MemberController의 doget()을 호출한다는 내용이다.

728x90
반응형