bitcocom/gsm31

BoardFrontController.java

Opened this issue · 0 comments

package kr.board.frontcontroller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
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 kr.gsm.controller.BoardContentController;
import kr.gsm.controller.BoardDeleteController;
import kr.gsm.controller.BoardFormController;
import kr.gsm.controller.BoardInsertController;
import kr.gsm.controller.BoardListController;
import kr.gsm.controller.BoardUpdateController;
import kr.gsm.controller.BoardUpdateFormController;
import kr.gsm.controller.Controller;
import kr.gsm.dao.BoardDAO;
import kr.gsm.entity.Board;
@WebServlet("/")
public class BoardFrontController extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 클라이언트의 모든 요청을 받는 컨트롤러(FrontController)
		// 1. 클라이언트의 요청을 확인하는 작업(command구하기)
		String reqUrl=request.getRequestURI();
		String ctx=request.getContextPath();
		String command=reqUrl.substring(ctx.length());
		System.out.println(command);
		// 2. command에 맞게 분기작업(control작업) : if~else if~
		BoardDAO dao=new BoardDAO();
		String view=null;
		Controller controller=null;
        HandlerMapping mapping=new HandlerMapping();
        controller=mapping.getController(command);
        view=controller.requestHandler(request, response);
		// 3. View의 경로를 확인후 분기(forward, redirect)하는 작업
		if(view!=null) {
			if(view.indexOf("redirect:/")!=-1) {
				response.sendRedirect(view.split(":/")[1]); // redirect:/bList.do
			}else {
				RequestDispatcher rd=request.getRequestDispatcher(ViewResolver.makeView(view));
				rd.forward(request, response); // JSP
			}//_if_
		}//_if_
	}
}