2017. 7. 2. 22:48
Controller.java C. Java2017. 7. 2. 22:48
package chap19;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import chap19.CommandAction;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import chap19.CommandAction;
/**
* Controllre page
*/
public class ControllerAction extends HttpServlet {
/**
* 명령어와 명령어처리 클래스를 쌍으로 저장
*/
private Map commandMap = new HashMap();
/**
* 명령어와 명령어처리 클래스를 쌍으로 저장
*/
private Map commandMap = new HashMap();
/**
* 명령어와 처리클래스가 매핑되어 있는 Command.properties파일을 읽어서 Map객체인 commandMap에 저장하는 메소드
*/
public void init(ServletConfig config) throws ServletException {
* 명령어와 처리클래스가 매핑되어 있는 Command.properties파일을 읽어서 Map객체인 commandMap에 저장하는 메소드
*/
public void init(ServletConfig config) throws ServletException {
/**
* web.xml에서 propertyConfig에 해당하는 init-param 의 값을 읽어옴
* web.xml에 propertyConfig의 init-param값을 Command.properties로 설정해놓는다.
*/
String props = config.getInitParameter("propertyConfig");
* web.xml에서 propertyConfig에 해당하는 init-param 의 값을 읽어옴
* web.xml에 propertyConfig의 init-param값을 Command.properties로 설정해놓는다.
*/
String props = config.getInitParameter("propertyConfig");
/**
* 명령어와 처리클래스의 매핑정보를 저장할 Properties객체 생성
*/
Properties pr = new Properties();
* 명령어와 처리클래스의 매핑정보를 저장할 Properties객체 생성
*/
Properties pr = new Properties();
/**
* Command.propertiesd파일을 읽어와서 그 내용을 properties 객체인 pr에 저장
*/
FileInputStream f = null;
try {
f = new FileInputStream(props);
pr.load(f);
}
catch (IOException e) {
throw new ServletException(e);
}
throw new ServletException(e);
}
finally {
if (f != null) try { f.close(); } catch(IOException ex) {}
}
/**
* Iterator객체는 Enumeration객체를 확장시킨 개념의 객체이고
* 객체를 하나씩 꺼내서 그 객체명으로 Properties객체에 저장된 객체에 접근
*/
Iterator keyIter = pr.keySet().iterator();
while( keyIter.hasNext() ) {
String command = (String)keyIter.next();
String className = pr.getProperty(command);
/**
* 위에서 저장한 Properties파일의 문자열을 클래스로 만들고
* 클래스의 객체를 생성한다.
* 그리고 Map객체인 commandMap에 객체를 저장한다.
*/
try {
Class commandClass = Class.forName(className);
Object commandInstance = commandClass.newInstance();
commandMap.put(command, commandInstance);
}
if (f != null) try { f.close(); } catch(IOException ex) {}
}
/**
* Iterator객체는 Enumeration객체를 확장시킨 개념의 객체이고
* 객체를 하나씩 꺼내서 그 객체명으로 Properties객체에 저장된 객체에 접근
*/
Iterator keyIter = pr.keySet().iterator();
while( keyIter.hasNext() ) {
String command = (String)keyIter.next();
String className = pr.getProperty(command);
/**
* 위에서 저장한 Properties파일의 문자열을 클래스로 만들고
* 클래스의 객체를 생성한다.
* 그리고 Map객체인 commandMap에 객체를 저장한다.
*/
try {
Class commandClass = Class.forName(className);
Object commandInstance = commandClass.newInstance();
commandMap.put(command, commandInstance);
}
catch (ClassNotFoundException e) {
throw new ServletException(e);
}
throw new ServletException(e);
}
catch (InstantiationException e) {
throw new ServletException(e);
}
throw new ServletException(e);
}
catch (IllegalAccessException e) {
throw new ServletException(e);
}
}
}
throw new ServletException(e);
}
}
}
/**
* get 방식의 서비스 메소드가 사용자의 요청을 받는다.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestPro(request, response);
}
/**
* post 방식의 메소드가 사용자의 요청을 받는다.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestPro(request, response);
}
* post 방식의 메소드가 사용자의 요청을 받는다.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestPro(request, response);
}
/**
* 사용자의 요청을 분석해서 해당 작업을 처리
*/
private void requestPro(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String view = null;
CommandAction com=null;
/**
* 요청URI에서 명령어를 추출하는 부분
*/
try {
String command = request.getRequestURI();
/**
* request.getContextPath()는
* ex) http://localhost:8181/jsp_ex/chap19/list.do 에서
* /jsp_ex/chap19/list.do부분을 가져오고
*
* request.getContextPath()는
* ex) /jsp_ex 부분을 가져온다.
*
*
* command.indexOf(request.getContextPath())는
* request.getContextPath()가 값이 있으면 시작 인덱스번호를 리턴하는데 값은 0인다.
*
* request.getContextPath().length()는
* ex) /jsp_ex 의 길이를 리턴하고 이때값은 7이다.
*
* command.substring(request.getContextPath().length())는
* command변수에서 인덱스 번호가 request.getContextPath().length() 즉 7부터 끝까지 추출한다.
* ex) /chap19/list.do를 추출한다.
*/
if (command.indexOf(request.getContextPath()) == 0) {
command = command.substring(request.getContextPath().length());
}
/**
* 명령어 처리클래스가 리턴된다. Properties에서 정의된 실제action객체
* ex) /chap19/list.do 의 실제 action경로
*/
com = (CommandAction)commandMap.get(command);
/**
* requestPro()메소드 호출하고( 즉 해당 명령어Action페이지를 호출한다. )
* 그리고 그 해당 action페이지에서 리턴되는 값(포워딩될 리턴view페이지)을 저정한다.
*/
view = com.requestPro(request, response);
}
catch(Throwable e) {
throw new ServletException(e);
}
throw new ServletException(e);
}
/**
* 해당 뷰페이지로 포워딩해준다.
*/
RequestDispatcher dispatcher =request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}
}
'C. Java' 카테고리의 다른 글
VI 편집기 명령어 (0) | 2020.12.15 |
---|---|
자바 추상클래스 final 알아보기 (0) | 2018.01.18 |
java 자바 자료모음 (0) | 2017.07.02 |
java 자바 자료형 (0) | 2017.07.02 |
java 자바 path 설정 (0) | 2017.07.02 |