java

JSP - ๊ฒŒ์‹œํŒ๋งŒ๋“ค๊ธฐ(1) - ํŒฉํ† ๋ฆฌ ํŒจํ„ด ์˜ˆ์ œ

dev_summer 2020. 11. 9. 15:24

 

 

์ฐธ๊ณ ์ž๋ฃŒ :

https://readystory.tistory.com/117

 

[์ƒ์„ฑ ํŒจํ„ด] ํŒฉํ† ๋ฆฌ ํŒจํ„ด(Factory Pattern) ์ดํ•ด ๋ฐ ์˜ˆ์ œ

์ด๋ฒˆ์— ์‚ดํŽด๋ณผ ๋””์ž์ธ ํŒจํ„ด์€ ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜์ธ ํŒฉํ† ๋ฆฌ ํŒจํ„ด(Factory Pattern)์ž…๋‹ˆ๋‹ค. ์ด ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์ธ ์šฉ์–ด์ธ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด(Factory Method Pattern)์œผ๋กœ๋„ ๋„๋ฆฌ

readystory.tistory.com

[์ƒ์„ฑ ํŒจํ„ด] ํŒฉํ† ๋ฆฌ ํŒจํ„ด(Factory Pattern) ์ดํ•ด ๋ฐ ์˜ˆ์ œ

์ด๋ฒˆ์— ์‚ดํŽด๋ณผ ๋””์ž์ธ ํŒจํ„ด์€ ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜์ธ ํŒฉํ† ๋ฆฌ ํŒจํ„ด(Factory Pattern)์ž…๋‹ˆ๋‹ค. ์ด ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์ธ ์šฉ์–ด์ธ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด(Factory Method Pattern)์œผ๋กœ๋„ ๋„๋ฆฌ ์•Œ๋ ค..

readystory.tistory.com

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ java์˜ ํด๋ž˜์Šค๋กœ ๋งŒ๋“œ๋Š”๊ฒƒ์„ ๋ชจ๋ธ๋ง์ด๋ผ๊ณ  ํ•œ๋‹ค.

์ปดํฌ์ง€์…˜, ํด๋ž˜์Šค๊ฐ€ ๋ถ€์†ํ’ˆ์ด ๋˜์–ด ํ•จ๊ฒŒ ์‚ฌ์šฉํ•  ๋•Œ ์ปดํฌ์ง€์…˜(ํ•ฉ์„ฑ) ํ•œ๋‹ค.

ํŒฉํ† ๋ฆฌ : ์œ„์ž„ํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ ํ•˜๋Š” ๊ฒƒ

โ€‹

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹น :-)

โ€‹

 

 

 

1. ๊ธฐ๋ณธ ์…‹ํŒ…

- server์˜ server.xml์— context path="/"๋กœ ์…‹ํŒ…๋งŒ๋“ฌ. ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ฐ›์•„ ๋ถ„๋ฐฐํ•˜๊ธฐ์œ„ํ•ด ๋ฃจํŠธํŒŒ์ผ๋กœ ์…‹ํŒ…

- action๊ณผ controller ํŒจํ‚ค์ง€ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜์—ฌ ์—ญํ• ์„ ๋‚˜๋ˆ„์–ด์คŒ

- controllerํŒจํ‚ค์ง€์— ์ปจํŠธ๋กค๋Ÿฌ ์„œ๋ธ”๋ฆฟํŒŒ์ผ ์ƒ์„ฑ

- @webservlet("ํŒจํ‚ค์ง€๋ช…") : ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฒ•, ํŒŒ์ผ์ด๋ฆ„์œผ๋กœ ์…‹ํŒ…

- ๊ธฐ๋ณธ์ ์œผ๋กœ UTF-8์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์•ผ ํ•จ.

request.setCharacterEncoding("utf-8"); // ์š”์ฒญํ• ๋•Œ๋„ utf-8๋กœ ์š”์ฒญ

response.setContentType("text/html;charset=utf-8"); // ์‘๋‹ตํ• ๋•Œ๋„ utf-8๋กœ ์‘๋‹ต

 

 

 

2. ์ปจํŠธ๋กค๋Ÿฌ์˜ ์—ญํ•  : ๋ถ„๋ฐฐ๋งŒ ํ•ด์คŒ ๊ฐ์ฒด ์ƒ์„ฑ์€ ActionํŒจํ‚ค์ง€์˜ ํŒฉํ† ๋ฆฌํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ถ„๋ฐฐ

*do get, do post๋งŒ ์ƒ์„ฑํ•˜๊ณ  do get์€ do post๋กœ ๋ฐ”๋กœ ๋„˜๊ธฐ๊ฒŒ ํ•จ.

1. String cmd = req.getParameter("cmd");
2. Action action = BoardFactory.route("cmd");
3. action.execute(req, resp);

 

1) ์š”์ฒญํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ Stringํ˜•์œผ๋กœ ์ €์žฅ

2) BoardFactory์˜ routeํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฐ›์€ ์š”์ฒญ์— ๋งž๊ฒŒ ๋ถ„๋ฐฐํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Actionํ˜•์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์Œ.

3) ์ „๋‹ฌ๋ฐ›์€ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์ธ execute ์‹คํ–‰. ์ด๋Š” ๊ฐ์ฒด์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ ๊ฐ•์ œ์„ฑ์„ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด Action์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ƒ์„ฑ,๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•ด๋†“์•„์•ผํ•จ.

 

 

3. ํŒฉํ† ๋ฆฌํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑํ•˜์—ฌ ์ปจํŠธ๋กค๋Ÿฌ์— ์ „๋‹ฌ

2-2)์˜ BoardFactory.route("cmd");๋Š” ์ผ๋‹จ Stringํ˜•์œผ๋กœ cmd๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์•ผ ํ•˜๋ฉฐ route๋ผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰ ๋  ์ˆ˜ ์žˆ๊ฒŒ ์…‹ํŒ… ๋˜์–ด์•ผ ํ•จ.

 

2-3)์˜ Actionํ˜•์˜ action์ด execute๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ ๊ฐ์ฒด๋“ค์€ Action์„ ์ƒ์†๋ฐ›์•„ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์‹คํ–‰๋ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์•ผํ•จ.

 

 

- BoardFactory

package com.cos.board.action.board;

import com.cos.board.action.Action;

public class BoardFactory {

	public static Action route(String cmd) {
		
		if(cmd.equals("list")) {
			return new BoardListAction();
		}else if(cmd.equals("delete")) {
			return new BoardDeleteAction();
		}else if(cmd.equals("write")) {
			return new BoardWriteAction();
		}else if(cmd.equals("update")) {
			return new BoardUpdateAction();
		}else if(cmd.equals("detail")) {
			return new BoardDetailAction();
		}
	
		return null;
	}

}

 

 

- Action

package com.cos.board.action;

import java.io.IOException;

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

public interface Action {
	void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
	
}

 

- Action์„ ์ƒ์†๋ฐ›์€ BoardListAction, ๊ฐ ๊ธฐ๋Šฅ์— ๋งž๊ฒŒ ๋‹ค๋ฅธ java๋„ ๊ตฌํ˜„

package com.cos.board.action.board;

import java.io.IOException;

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

import com.cos.board.action.Action;

public class BoardListAction implements Action {

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}

 

 

 

๋„์ :-)

์˜ค๋Š˜๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹น