✅ 전체 흐름: HTML → Servlet → JSP (MVC 구조)
🔵 [요청] ① index.html
<!-- index.html -->
<a href="exam5_2">예제5-2 (request 내장 객체로 모든 HTTP 헤더 정보값 출력하기)</a>
- HTML은 단순히 "요청"만 함.
- 이건 브라우저가
http://localhost:8080/exam5_2로 요청 보내는 거.
🟡 [컨트롤러] ② exam5_2.java (Servlet)
@WebServlet("/exam5_2")
public class exam5_2 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HashMap<String, String> pack = new HashMap<>();
Enumeration en = req.getHeaderNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = req.getHeader(key);
pack.put(key, value);
}
req.setAttribute("mapdate", pack); // 데이터 전달
RequestDispatcher ds = req.getRequestDispatcher("chapter05/exam5_2.jsp");
ds.forward(req, resp); // JSP로 이동
}
}
- 여기서 요청을 처리하고, **모델 데이터를 준비(Map)**해서 JSP로 넘김.
- 이것이 Controller의 역할.
🟢 [뷰] ③ exam5_2.jsp
<%@ page import="java.util.Map" %>
<%
Map<String, String> map = (Map<String, String>) request.getAttribute("mapdate");
for (String key : map.keySet()) {
out.println(key + " : " + map.get(key) + "<br>");
}
%>
- JSP는 받은 Map을 반복문으로 출력만 함.
- 데이터를 직접 처리하지 않고 출력만 하는 뷰(View) 역할.
🔁 전체 흐름
[HTML 요청]
↓
<a href="exam5_2"> → 브라우저 요청 발생
↓
[컨트롤러: exam5_2.java]
- HTTP 헤더 수집
- HashMap에 저장
- JSP로 데이터 전달
↓
[뷰: exam5_2.jsp]
- Map 받아서 화면 출력
이걸 한 줄로 요약하면:
"HTML이 요청 보내고, 서블릿이 처리하고, JSP가 출력한다"
✅ JSP/Servlet MVC
1. 요청 (Request)
어디서? index.html, <form>, <a href="">
역할: 사용자가 어떤 페이지나 기능을 요청함
<a href="nameList">이름 리스트 보기</a>
2. 컨트롤러 (Servlet)
어디서? nameList.java
역할:
- 요청 처리
- 데이터 준비
- JSP로 전달 (setAttribute)
@WebServlet("/nameList")
public class nameList extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ArrayList<String> list = new ArrayList<>();
list.add("꼬리");
list.add("홍길동");
req.setAttribute("nameList", list); // JSP로 데이터 전달
RequestDispatcher ds = req.getRequestDispatcher("chapter05/nameList.jsp");
ds.forward(req, resp); // 뷰로 이동
}
}
자료구조:
ArrayList,HashMap, DTO 등 → JSP에 한 번에 넘기기 위해 사용
setAttribute("이름", 값) 으로 데이터를 저장해 보냄
3. 뷰 (JSP)
어디서? nameList.jsp
역할:
- 전달받은 데이터를 꺼내서 화면에 출력
- 로직은 최소한만
<%@ page import="java.util.ArrayList" %>
<%
ArrayList<String> list = (ArrayList<String>) request.getAttribute("nameList");
for (String name : list) {
out.println(name + "<br>");
}
%>
getAttribute("이름") 으로 Servlet에서 넘긴 데이터를 꺼냄
request는 JSP에 기본 내장된 객체라 바로 사용 가능
✅ 전체 흐름 한 줄 요약
HTML 요청 → Servlet 처리 + setAttribute("이름", 값) → JSP 출력 (getAttribute("이름"))
✅ 공식처럼 기억할 것
| 구분 | 사용 메서드 | 위치 | 설명 |
|---|---|---|---|
| 전달 | setAttribute("key", value) |
Servlet | JSP로 데이터 넘길 때 |
| 받기 | getAttribute("key") |
JSP | 넘겨 받은 데이터 꺼낼 때 |
✅ 자주 쓰는 자료구조 예
| 구조 | 사용 목적 |
|---|---|
ArrayList |
여러 개를 순서대로 보낼 때 (리스트, 글 목록) |
HashMap |
여러 개를 이름 붙여서 보낼 때 (헤더, 속성) |
DTO |
여러 값들을 클래스 하나에 묶어서 전달할 때 |
'JSP' 카테고리의 다른 글
| 예제 5-4 실습할 때 헷갈렸던 것 (0) | 2025.05.23 |
|---|---|
| Forward와 Redirect 방식 비교 (0) | 2025.05.23 |
| JSP에서 Enumeration 개념 (0) | 2025.05.23 |
| 147p 도서목록 표시하기 DAO DTO (0) | 2025.05.22 |
| JSP 주요 문법 기호 정리 (0) | 2025.05.21 |