๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [MVC] FrontController ํจํด ๋ณธ๋ฌธ
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>
'KITRI > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP ํ๋ก๊ทธ๋๋ฐ - [MVC/DB์ฐ๋] ํ์๊ฐ์ ํ์ด์ง (์์ด๋ ์ค๋ณต์ฒดํฌ, ์ ํจ์ฑ๊ฒ์ฌ, ์ฐํธ๋ฒํธ ๊ฒ์ ๊ธฐ๋ฅ) (1) (2) | 2020.07.01 |
---|---|
JSP ํ๋ก๊ทธ๋๋ฐ - ๋ก๊ทธ๋ฉ์์ง/ ํ๋ฌ๊ทธ์ธ ๋ค์ด (0) | 2020.06.30 |
JSP ํ๋ก๊ทธ๋๋ฐ - [JSTL] functions (0) | 2020.06.29 |
JSPํ๋ก๊ทธ๋๋ฐ - [JSTL] fmt (0) | 2020.06.29 |
JSP ํ๋ก๊ทธ๋๋ฐ - [JSTL] core (0) | 2020.06.29 |