상세 컨텐츠

본문 제목

서블릿 컨트롤러, 커맨드 패턴, 설정 파일 [.properties] 사용

JSP

by kwanghyup 2020. 7. 5. 23:33

본문

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);
    }

}

 

관련글 더보기

댓글 영역