JSP| ๐Ÿ’”FrontController ํŒจํ„ด๊ณผ Command ํŒจํ„ด

  • ํ‹ฐ๋ ‰ํ„ฐ๋ฆฌ ํŒจํ„ด: ๋ณ„๊ฐœ์˜ ์„œ๋ธ”๋ฆฟ ์ฐพ์•„๊ฐ€๊ธฐ/์–ด์ฉŒ๊ตฌ
  • ํ™•์žฅ์ž ํŒจํ„ด : ๋ฌด์กฐ๊ฑด *.do

FrontController ํŒจํ„ด

  • ๋ชจ๋“  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์„ ๋งŒ๋“ ๋‹ค > ์ค‘๋ณต ์—†์• ๊ณ  ๊ฐœ๋ฐœ ์ง‘์ค‘




  • FrontController๋ฅผ ์‚ฌ์šฉํ•  ํŒŒ์ผ
<%@ 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>

	<!-- insert๋ˆ„๋ฅด๋ฉด insert.do ์š”์ฒญ -->
	<a href="insert.do">insert</a>
	<hr />
	<a href="http://localhost:8181<%=request.getContextPath()%>/update.do">update</a>
	<hr />
	<a href="http://localhost:8181/jsp_25_2_ex1_frontex/select.do">select</a>
	<hr />
	<a href="<%=request.getContextPath()%>/delete.do">delete</a>

</body>
</html>

  • FrontController
package com.javalec.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
 */
//ํ™•์žฅ์ž ํŒจํ„ด *.do
@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
		System.out.println("doGet");
		actionDo(request, response);
	}

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

	private void actionDo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("actionDo");

		String uri = request.getRequestURI();
		System.out.println("uri : " + uri);
		String conPath = request.getContextPath();
		System.out.println("conPath : " + conPath);

		// conPath์˜ ๊ธธ์ด๋งŒํผ uri๋ฅผ ์ž๋ฆ„ > ๋’ค์— ์š”์ฒญํ•œ ํŒŒ์ผ์˜ ์ด๋ฆ„ (.do)๋งŒ ๋‚จ์Œ
		String command = uri.substring(conPath.length());
		System.out.println("command : " + command);

		// ๋’ค์— ์š”์ฒญํ•œ ํŒŒ์ผ์˜ ์ด๋ฆ„ (.do)๋งŒ ๋‚จ์Œ > ๊ฐ’ ํ™•์ธ > ๋ชจ๋“  ์ผ ํ•œ ๊ณณ์—์„œ ์ฒ˜๋ฆฌ
		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("----------------");
		}
	}

}





Command ํŒจํ„ด


1. ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ์—์„œ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›๊ณ 
2. ๊ทธ ์„œ๋ธ”๋ฆฟ์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐ›๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค
3. DAO๋กœ ์ ‘๊ทผํ•ด์„œ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.




<%@ 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>
	<!-- @WebServlet("*.do")์œผ๋กœ FrontController๊ฐ€ ๋ฐ›๋Š”๋‹ค -->
	<a href="membersAll.do">์ „์ฒด ํšŒ์› ์ •๋ณด ์กฐํšŒ</a>

</body>
</html>
package com.javalec.ex;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;

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
		System.out.println("doGet");
		actionDo(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doPost");
		actionDo(request, response);
	}
	
	private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("actionDo");
		
		String uri = request.getRequestURI();
		String conPath = request.getContextPath();
		String command = uri.substring(conPath.length());
		
		if(command.equals("/membersAll.do")) {
			response.setContentType("text/html; charset=EUC-KR");
			PrintWriter writer = response.getWriter();
			writer.println("<html><head></head><body>");
			
			//์ž‘์—… ๋„˜๊ธฐ๊ธฐ
			Service service = new MembersAllService();
			ArrayList<MemberDto> dtos = service.execute(request, response);
			
			for (int i = 0; i < dtos.size(); i++) {
				MemberDto dto = dtos.get(i);
				String id = dto.getId();
				String pw = dto.getPw();
				String name = dto.getName();
				String eMail = dto.geteMail();
				Timestamp rDate = dto.getrDate();
				String address = dto.getAddress();
				
				writer.println(id + ", " + pw + ", " + name + ", " + eMail + ", " + rDate.toLocalDateTime() + ", " + address + "<hr />");
			}
			
			writer.println("</body></html>");
		}
		
	}

}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ