본문 바로가기
개발~/JSP

[JSP] MVC 패턴 구현 - URI 패턴

by 보배곰 2017. 3. 8.

참고한 책: 최범균의 JSP 2.3 웹프로그래밍 기초부터 고급까지(최범균 저)

chapter 18 MVC 패턴 구현 2.3 설정파일에 커맨드와 클래스의 관계 명시하기

책 소스코드: https://github.com/madvirus/jsp23

최범균님 블로그: http://javacan.tistory.com/


앞에서 한 command 패턴 예제

2017/03/08 - [프로그래밍/JSP] - [JSP] MVC 패턴 구현 - 커맨드 패턴, 설정 파일



앞에 한 command 패턴에서 조금만 바꾸면 된다. 

    • ControllerUsingURI.java 서블릿 추가(ControllerUsingFile 복사해서 쓰자)
    • commandHandlerURI.properties 추가
    • web.xml에 추가 
    • (HelloHandler는 계속 있어야 합니다~)


1. ControllerUsingURI.java 서블릿 추가

ControllerUsingFile에 process() 메서드에서 request.getParameter("cmd") 대신 아래 코드를 쓰면 된다. 나머지는 다 똑같다.

String command = request.getRequestURI();
 if(command.indexOf(request.getContextPath()) == 0) {
	command = command.substring(request.getContextPath().length());
} 

String command = request.getRequestURI();

 if(command.indexOf(request.getContextPath()) == 0) {

command = command.substring(request.getContextPath().length());

 } 


전체소스


2. WEB-INF/commandHandlerURI.properties 추가

 ▶commandHandlerURI.properties 

 /hello.do=mvc.hello.HelloHandler


3. web.xml에 추가

 ▶web.xml 

 <servlet>

  <servlet-name>ControllerUsingURI</servlet-name>

  <servlet-class>mvc.controller.ControllerUsingURI</servlet-class>

  <init-param>

  <param-name>configFile</param-name>

  <param-value>/WEB-INF/commandHandlerURI.properties</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>ControllerUsingURI</servlet-name>

  <url-pattern>*.do</url-pattern>

  </servlet-mapping>


전체소스


4. 실행하기

이번에도 역시 처음 화면을 안만들고 결과화면만 만들었기 때문에 서버를 먼저 실행하고, url로 들어가자

http://localhost:8080/exapmle18/hello.do


댓글