CommandProcess 인터페이스를 통해 명령어 비지니스 로직 처리 메소드(requestPro)를 정의합니다.
control.Controller
package control;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 service.CommandProcess;
/**
* Controller : Service(Command) --> Business Logic
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet{
private static final long serialVersionUID = 1L;
private Map<String, Object> commandMap = new HashMap<String, Object>();
public Controller(){ super(); }
public void init(ServletConfig config) throws ServletException {
// web.xml에서 propertyConfig에 해당하는 init-param의 값을 읽어옴.
String props = config.getInitParameter("config");
// 명령어와 처리클래스의 매핑정보를 저장할 Properties 객체 생성
Properties pr = new Properties();
FileInputStream f = null;
try {
String configFilePath = config.getServletContext().getRealPath(props);
f = new FileInputStream(configFilePath);
// command.properties 파일의 정보를 Properties 객체에 저장
pr.load(f);
} catch (IOException e) {
throw new ServletException(e);
} finally {
if (f != null) try { f.close(); } catch (IOException e) { }
}
//Iterator 객체는 Enumeration 객체를 확장시킨 개념의 객체
Iterator keyIter = pr.keySet().iterator();
//객체를 하나씩 꺼내서 그 객체명으로 Properties 객체에 저장된 객체에 접근
while(keyIter.hasNext()) {
String command = (String)keyIter.next();
String className = pr.getProperty(command);
try {
Class commandClass = Class.forName(className);//해당 문자열을 클래스로 만든다.
Object commandInstance = commandClass.newInstance(); //해당 클래스의 객체를 생성
commandMap.put(command, commandInstance); // Map 객체인 commandMap에 객체 저장
} catch(Exception e) {
throw new ServletException();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
requestPro(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestPro(request, response);
}
// CommandProcess 인터페이스의 메소드 requestPro(request, response): 사용자의 요청을 분석해서 해당 작업을 처리한다.
public void requestPro(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String view = null;
CommandProcess com = null;
try {
String command = request.getRequestURI();
System.out.println(command);
command = command.substring(request.getContextPath().length()); // real path만큼 자름.
com = (CommandProcess)commandMap.get(command);
System.out.println("command --> "+command);
System.out.println("com --> "+com);
// view로 이동함.
view = com.requestPro(request, response);
System.out.println("view --> "+view);
}catch(Throwable e) {throw new ServletException(e);}
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}
}
Controller의 init()메소드에서는 설정 파일로 부터 받아온 설정 정보를 저장하고, Properties 객체를 생성하여 command.properties의 파일의 Properties 정보를 저장한다. (command.properties 파일은 명령어와 명령어 처리 클래스를 매핑한 파일이다.)
아래의 코드는 command.properties의 예제를 보여줍니다. ip주소/list.do를 입력하면 listAction이 수행됩니다.
/list.do = service.ListAction
아래의 코드는 Controller 코드의 일부입니다. 이 코드(Class와 Object 처리)를 통해 우리는 Controller 소스 코드를 직접 수정하지 않고, command.properties에 추가함으로써, 명령어와 처리 클래스를 연결할 수 있습니다.
Class commandClass = Class.forName(className);//해당 문자열을 클래스로 만든다.
Object commandInstance = commandClass.newInstance(); //해당 클래스의 객체를 생성
commandMap.put(command, commandInstance); // Map 객체인 commandMap에 객체 저장
우리는 소스파일의 doGet(request, response)와 doPost(request, response) 메소드에서 처리를 requestPro(request, response)로 넘겨주는 것을 볼 수 있습니다.
requestPro에서는 명령을 받아 명령을 분석하고, 명령을 처리할 객체를 생성하여 그 객체를 통해 view를 반환 받는 것을 볼 수 있습니다.