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>");
}
}
}
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(JSP| ๐FrontController ํจํด๊ณผ Command ํจํด), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@kji306301/JSP-FrontController-ํจํด๊ณผ-Command-ํจํด์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค