[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - controller

 

앞서 FrontController 패턴으로 구현한다 하였다. 따라서 이 어플리케이션의 모든 웹 요청은 controller에서 정의한 servlet으로 요청이 된다. 즉 모든 요청이 이 부분에서 제어가 된다.

 

📌ShopFrontController.java

package controller;

import java.io.IOException;
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 vo.ActionForward;
import action.Action;
import action.ClothesCartAddAction;
import action.ClothesCartListAction;
import action.ClothesCartQtyDownAction;
import action.ClothesCartQtyUpAction;
import action.ClothesCartRemoveAction;
import action.ClothesCartSearchAction;
import action.ClothesListAction;
import action.ClothesRegistAction;
import action.ClothesRegistFormAction;
import action.ClothesViewAction;

@WebServlet("*.shop")
public class ShopFrontController extends HttpServlet {

	//클라이언트에서 요청이 get방식 또는 post 방식 어떤 방식으로 와도 요청을 처리하기 위해서
	//doGet메소드와 doPost메소드에서 공통적으로 doProcess 메소드를 호출하고 있다.
	//즉 ShopFrontController 서블릿으로 들어오는 모든 요청은 doProcess를 호출하게 되어있다.
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8"); //한글 처리
		
		//1. 요청파악
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String command = requestURI.substring(contextPath.length());
		Action action = null;
		ActionForward forward = null;
		
		//2.각 요청별로 비지니스로직 호출
		if(command.equals("/clothesList.shop")){
			action = new ClothesListAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesView.shop")){
			action = new ClothesViewAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartAdd.shop")){
			action = new ClothesCartAddAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartList.shop")){
			action = new ClothesCartListAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartSearch.shop")){
			action = new ClothesCartSearchAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartRemove.shop")){
			action = new ClothesCartRemoveAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartQtyUp.shop")){
			action = new ClothesCartQtyUpAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesCartQtyDown.shop")){
			action = new ClothesCartQtyDownAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesRegist.shop")){
			action = new ClothesRegistAction();
			//프로젝트명 + 기능 + 형태(?)
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(command.equals("/clothesRegistForm.shop")){
			action = new ClothesRegistFormAction();
			try {
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//3. 포워딩
		if(forward !=null){
			if(forward.isRedirect()){
				response.sendRedirect(forward.getPath());
			}else{
				RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
				dispatcher.forward(request, response);
				
			}
		}
		
	}
	
}

 

추가로 URL 경로를 얻어오는 부분은 다음 링크를 참고하면 된다.

https://yeo-computerclass.tistory.com/228

 

요청(request) URL 정보 추출하기

📌HttpServletRequest의 메소드로 URL 정보 추출 요청 URL에서 특정 정보만 추출하는 HttpServletRequest의 메소드는 다음과 같다. 메소드 설명 반환값 getRequestURL() 요청 URL 리턴(단, 매개변수 제외) http://..

yeo-computerclass.tistory.com