๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [MVC] FrontController ํŒจํ„ด ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [MVC] FrontController ํŒจํ„ด

๋˜ํšจ๋‹ˆ 2020. 6. 30. 15:13

MVC(Model - View - Controller) ํŒจํ„ด

 

 

 

 

 

 

 

MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ : ์„œ๋ธ”๋ฆฟ

 

1. ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ „์†กํ•œ HTTP ์š”์ฒญ์„ ๋ฐ›๋Š”๋‹ค. ์„œ๋ธ”๋ฆฟ์˜ doGet()๋ฉ”์„œ๋“œ๋‚˜ doPost()๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

2. ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์š”์ฒญํ–ˆ๋Š”์ง€ ๋ถ„์„ํ•œ๋‹ค. 

3. ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

4. ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฒฐ๊ณผ๋ฌผ์„ ๊ฐ€๊ณตํ•˜์—ฌ, request ๋‚˜ session์˜ setAttribute()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ์†์„ฑ์— ์ €์žฅํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ €์žฅํ•œ ๊ฒฐ๊ณผ๊ฐ’์€ ๋ทฐ JSP์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. 

5. ์›น ๋ธŒ๋ผ์šฐ์ €์— ๊ฒฐ๊ณผ๋ฅผ ์ „์†กํ•  JSP๋ฅผ ์„ ํƒํ•œ ํ›„, ํ•ด๋‹น JSP๋กœ ํฌ์›Œ๋”ฉํ•œ๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ redirect

 

 

 

FrontController ํŒจํ„ด


ํด๋ผ์ด์–ธํŠธ์˜ ๋‹ค์–‘ํ•œ ์š”์ฒญ๋“ค์„ ํ•œ๊ณณ์œผ๋กœ ์ง‘์ค‘์‹œ์ผœ์„œ ๊ฐœ๋ฐœ ๋ฐ ์œ ์ง€๋ณด์ˆ˜์— ํšจ์œจ์„ฑ์„ ๊ทน๋Œ€ํ™” ์‹œํ‚ค๋Š” ํŒจํ„ด์ด๋‹ค.

 

์ด๋ฅผํ†ตํ•ด ํ•ด๋‹นํ•˜๋Š” ๊ฐ๊ฐ์˜ ๋‹ค์–‘ํ•œ ์š”์ฒญ๋“ค์— ๋”ฐ๋ผ ์š”์ฒญ์— ๋งž๋Š” ์„œ๋ธ”๋ฆฟ์„ ํ–ฅํ•˜๊ฒŒ ํ–ˆ๋Š”๋ฐ ํ•œ๊ณณ์œผ๋กœ ์„œ๋ธ”๋ฆฟ์„ ๋ชจ์•„์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

 

 

์ด๋Ÿฌํ•œ ํ˜•ํƒœ๋ฅผ FrontController ํŒจํ„ด์ ์šฉ ํ›„

 

 


 

FrontController.java

package com.java.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FrontController
 */
public class FrontController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
	super();
	// TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	// TODO Auto-generated method stub
	doGet(request, response);
    }

}

Command.java

package com.java.mvc06;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Command {
    public String proRequest(HttpServletRequest requst, HttpServletResponse response);
}

ListAction.java

package com.java.mvc06;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java.mvc05.Command;

public class ListAction implements Command {

    @Override
    public String actionDo(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	return null;
    }

}

WriteAction.java

package com.java.mvc06;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java.mvc05.Command;

public class WriteAction implements Command {

    @Override
    public String actionDo(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	return null;
    }

}

cmdURL.properties

/write.stu = com.java.mvc06.WriteAction
/list.stu = com.java.mvc06.ListAction

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>mvcExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
	
  <servlet>
  	<servlet-name>Directory</servlet-name>
  	<servlet-class>com.java.mvc01.Directory</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Directory</servlet-name>
  	<url-pattern>*.nhn</url-pattern>
  </servlet-mapping>

  <servlet>
  	<servlet-name>Controller</servlet-name>
  	<servlet-class>com.java.mvc02.Controller</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Controller</servlet-name>
  	<url-pattern>*.kitri</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>SampleCnt</servlet-name>
  	<servlet-class>com.java.mvc03.Controller</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Controller</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>CommandController</servlet-name>
  	<servlet-class>com.java.mvc04.CommandCnt</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>CommandController</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>FrontCnt</servlet-name>
  	<servlet-class>com.java.mvc05.FrontCnt</servlet-class>
  	
  	<init-param>
	  	<param-name>configFile</param-name>
	  	<param-value>C:\sohyunkim\mvc\workspace\mvcExample\WebContent\WEB-INF\commandURI.properties</param-value>
	  </init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>FrontCnt</servlet-name>
  	<url-pattern>*.happy</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>FrontController</servlet-name>
  	<servlet-class>com.java.mvc06.FrontController</servlet-class>
  	
  	<init-param>
  		<param-name>configFile</param-name>
  		<param-value>/WEB-INF/cmdURL.properties</param-value>
  	</init-param>
  </servlet>

  <servlet-mapping>
  	<servlet-name>FrontController</servlet-name>
  	<url-pattern>*.stu</url-pattern>
  </servlet-mapping>
  
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="http://localhost:8181/mvcExample/write.kitri">Controller - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.kitri">Controller - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />
	
	<a href="http://localhost:8181/mvcExample/write.do">SampleController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.do">SampleController - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />

	<a href="http://localhost:8181/mvcExample/write.action">CommandController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.action">CommandController - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />
	
	<a href="http://localhost:8181/mvcExample/write.happy">FrontController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.happy">FrontController - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />

	<a href="http://localhost:8181/mvcExample/write.stu">FrontController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.stu">FrontController - ๊ธ€๋ชฉ๋ก</a>
</body>
</html>

 

๋ฐ˜์‘ํ˜•
Comments