프로그래밍 정리/JSP, Servlet, DB(oracle)

웹 프로그래밍 21(JSP,servlet)-FrontController패턴

Wooni0477 2020. 1. 6. 11:34
반응형
웹 프로그래밍 21(JSP,servlet)-FrontController패턴


*기존방식 패턴

기존 패턴은 디렉터리 형태로 서블릿에 @WebServlet("/Hello") 식으로 작성해주었다.

이번에 배울 패턴은 확장자 패턴으로 어떤 형태건 간에 ".do"끝나는 모든 컴포넌트를 한번에 받아 처리한다.

*FrontController

전에 하던 방식은 클라이언트의 여러 요청에 의해 많은 서블릿이 생기게 된다.

개발 및 유지보수를 위해 다양한 요청을 한곳으로 집중시켜 처리하는 방식을 배워보자

-기존 요청방식

-FrontController 방식

-실전 예제

모두 이름을 다르게하지만 끝에 .do를 붙여 서블릿에 모든 요청을 처리하게끔 해보자

-fc.jsp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<a href="insert.do">insert</a>

<hr />

<a href="http://localhost:8181<%=request.getContextPath()%>/update.do"> update</a><br />

request.getContextPath() : <%=request.getContextPath()%>

<hr />

<a href="http://localhost:8181/ex_frontController/select.do">select</a>

<hr />

<a href="<%=request.getContextPath()%>/delete.do">delete</a>

</body>

</html>

-frontCon.java(서블릿)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

package com.java.ex;

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 frontCon

*/

@WebServlet("*.do")

public class frontCon extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public frontCon() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

action(request,response);

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

action(request,response);

}

protected void action(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException

{

String uri = request.getRequestURI();

String conPath = request.getContextPath();

String command = uri.substring(conPath.length());

System.out.println("uri : " + uri);

System.out.println("conPath : " + conPath);

System.out.println("command : " + command);

System.out.println("");

if(command.equals("/insert.do")){ //끝부부만 짤라 해당 요청 확인

System.out.println("insert");

System.out.println("----------------");

}else if(command.equals("/update.do")){

System.out.println("update");

System.out.println("----------------");

}else if(command.equals("/select.do")){

System.out.println("select");

System.out.println("----------------");

}else if(command.equals("/delete.do")){

System.out.println("delete");

System.out.println("----------------");

}

}

}

-출력 화면

fc.jsp

-frontCon.java(insert.do 이동)


반응형