CommandHandler : 핸들러 인터페이스
package mvc.command;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface CommandHandler {
public String process(HttpServletRequest request, HttpServletResponse response) throws ServletException;
}
NullHandler - 뷰페이지 : viewNullPage.jsp(생략)
package mvc.command;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NullHandler implements CommandHandler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
return "/view/viewNullPage.jsp";
}
}
BoardListHandler : 핸들러 구현체 - 뷰페이지 : viewBoardList.jsp(생략)
package mvc.command;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class BoardListHandler implements CommandHandler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
System.out.println("boardList");
// 글 목록 읽기 요청
// 뷰 페이지에서 사용할 정보 저장, (Dao객체 사용)
String value = "어떤 값";
request.setAttribute("value",value);
// 뷰페이지 리턴
return "/view/viewBoardList.jsp";
}
}
BoardWriteFormHandler: 핸들러 구현체 - 뷰페이지 : viewWriteForm.jsp(생략)
package mvc.command;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BoardWriteFormHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
return "/view/viewWriteForm.jsp";
}
}
/WEB-INF/commandHandler.properties
BoardList = mvc.command.BoardListHandler
BoardWriteForm = mvc.command.BoardWriteFormHandler
ControllerUsingFile : 컨트롤러 서블릿
package mvc.command;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
@WebServlet(urlPatterns = "*.do" )
public class ControllerUsingFile extends HttpServlet {
private Map<String,CommandHandler> commandHandlerMap = new HashMap<>();
private void proceedRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String command = request.getParameter("cmd");
CommandHandler handler = commandHandlerMap.get(command);
if(handler==null){
handler=new NullHandler();
}
String viewPage = null;
try {
viewPage = handler.process(request,response);
} catch (ServletException e) {
e.getMessage();
}
if(viewPage!=null){
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request,response);
}
}
@Override
public void init() throws ServletException {
// String configFile = getInitParameter("configFile");
String configFile = "/WEB-INF/commandHandler.properties";
Properties prop = new Properties();
String configFilePath = getServletContext().getRealPath(configFile);
try{
FileReader fis = new FileReader(configFilePath);
prop.load(fis);
} catch (IOException e) {
throw new ServletException();
}
Iterator<Object> keyIter = prop.keySet().iterator();
while (keyIter.hasNext()){
String command = (String) keyIter.next();
String handlerClassName = prop.getProperty(command);
try{
Class<?> handlerClass = Class.forName(handlerClassName);
CommandHandler handlerInstance =
(CommandHandler) handlerClass.getDeclaredConstructor().newInstance();
commandHandlerMap.put(command,handlerInstance);
} catch (ClassNotFoundException | NoSuchMethodException |
IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new ServletException();
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proceedRequest(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
}
}
서블릿 초기화 파라미터 <context-param> (0) | 2020.07.06 |
---|---|
서블릿 초기화 파라미터 web.xml <init-param> 애노테이션 @WebServlet @WebInitParam (0) | 2020.07.06 |
서블릿 컨트롤러, 커맨드 패턴 (0) | 2020.07.05 |
컨트롤러 서블릿 패턴 (0) | 2020.07.05 |
커넥션 풀 connection pool : 초기화파라미터 지정 (0) | 2020.07.05 |
댓글 영역