JiYoung Dev πŸ–₯

[JSP/Servlet] Web MVC (2023.05.19) λ³Έλ¬Έ

Study/Back-End

[JSP/Servlet] Web MVC (2023.05.19)

Shinjio 2023. 5. 19. 14:51
λ°˜μ‘ν˜•

🎈 λ””μžμΈ νŒ¨ν„΄(Design Pattern) 

μ˜μ‚¬μ†Œν†΅ μˆ˜λ‹¨μ˜ 일쒅

μ†Œν”„νŠΈμ›¨μ–΄ κ³΅ν•™μ˜ μ†Œν”„νŠΈμ›¨μ–΄ λ””μžμΈμ—μ„œ νŠΉμ • λ¬Έλ§₯μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ λ°œμƒν•˜λŠ” λ¬Έμ œμ— λŒ€ν•΄ μž¬μ‚¬μš© κ°€λŠ₯ν•œ ν•΄κ²°μ±…

μ†ŒμŠ€λ‚˜ 기계 μ½”λ“œλ‘œ λ°”λ‘œ μ „ν™˜λ  수 μžˆλŠ” μ™„μ„±λœ λ””μžμΈμ€ μ•„λ‹ˆλ©° λ‹€λ₯Έ 상황에 맞게 μ‚¬μš©λ  수 μžˆλŠ” λ¬Έμ œλ“€μ„ ν•΄κ²°ν•˜λŠ”λ°μ— μ“°μ΄λŠ” ν…œν”Œλ¦Ώ

 

ν”„λ‘œκ·Έλž˜λ¨Έκ°€ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ΄λ‚˜ μ‹œμŠ€ν…œμ„ λ””μžμΈν•  λ•Œ κ³΅ν†΅λœ λ¬Έμ œλ“€μ„ ν•΄κ²°ν•˜λŠ”λ°μ— μ“°μ΄λŠ” ν˜•μ‹ν™”λœ κ΄€ν–‰

 

🎈 μ›Ή λ””μžμΈ νŒ¨ν„΄

μ½”λ“œμ˜ μž¬μ‚¬μš©κ³Ό μœ μ§€λ³΄μˆ˜μ˜ νŽΈλ¦¬μ„±μ„ μœ„ν•΄μ„œ 정해진 μ•½μ†λŒ€λ‘œ ꡬ쑰λ₯Ό λ””μžμΈ ν•˜λŠ” 것

 

Spring ν”„λ ˆμž„μ›Œν¬μ—μ„œλŠ” MVC νŒ¨ν„΄μ΄ κΈ°λ³Έ

 

🎈 MVCνŒ¨ν„΄

 

 

πŸ“– μ›Ή MVC νŒ¨ν„΄ 2가지

 

βš™οΈ Model1

view, controller λͺ¨λ‘ jspμ—μ„œ μž‘μ„±

 

βš™οΈ Model2

viewλŠ” jsp (html이 주이기 λ•Œλ¬Έμ—)

λ‘œμ§μ²˜λ¦¬λŠ” servlet으둜 μž‘μ„±ν•˜λŠ” 것 (javaκ°€ μ£Όμ΄λ―€λ‘œ)

 

λ°μ΄ν„°λ² μ΄μŠ€μ™€ 연결은 model2 servlet으둜 처리 

 

🎈 MVCνŒ¨ν„΄ μ‹€μŠ΅

πŸ“– Model1 μ‹€μŠ΅

βš™οΈ λ°μ΄ν„°λ² μ΄μŠ€ μ—°λ™ν•˜κΈ°

 

 

βš™οΈ Model1 μ½”λ“œ μž‘μ„±

 

<%@page import="com.smhrd.model.Students"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="com.smhrd.model.FullStack"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- WEB MVC MODEL 1 : λΉ„μ¦ˆλ‹ˆμŠ€ 둜직(Controller), ν™”λ©΄ν‘œν˜„(view) >> JSP에 μž‘μ„±
		ν”„λ‘œμ νŠΈμ˜ 규λͺ¨κ°€ μž‘μ„ λ•Œ μ‚¬μš© -->
		
<!-- DB 연동 -->
<!-- 0. JDBC κ΄€λ ¨ 도ꡬ κ°€μ Έμ˜€κΈ°
	: 라이브러리 μΆ”κ°€ (WEBAPP > WEB-INF) -->

<%
	//DB μ—°λ™μ‹œ ν•„μš”ν•œ 객체 μ„ μ–Έ
	Connection conn = null; //λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°
	PreparedStatement ps = null; //sql λ‹€λ£¨λŠ” 객체
	ResultSet rs = null; //sql μ‹€ν–‰ κ΄€λ ¨ 객체
	//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 뢈러온 정보
	Students stu = null;
	
	try{
	
	//1. 였라클과 μ—°κ²°ν•œ λ“œλΌμ΄λ²„ 가지고 였기(λ™μ λ‘œλ”©)
	//λ™μ λ‘œλ”© : ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ ν›„ μ½”λ“œλ₯Ό λ§Œλ‚¬μ„ λ•Œ μ‹€ν–‰
	//μ •μ λ‘œλ”© : ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ν•˜μžλ§ˆμž μ‹€ν–‰
	Class.forName("oracle.jdbc.OracleDriver");
	
	//2. μ§€μ •ν•œ λ°μ΄ν„°λ² μ΄μŠ€μ™€ μ—°κ²°ν•˜κΈ° μœ„ν•œ 객체 생성(Connection)
	//객체 생성 ν›„ λ°μ΄ν„°λ² μ΄μŠ€μ™€ μ—°κ²°μ‹œ url, λ°μ΄ν„°λ² μ΄μŠ€ 계정 정보 ν•„μš”
	String url = "jdbc:oracle:thin:@localhost:1521:xe";
	String user = "service";
	String password = "12345";
	
	conn = DriverManager.getConnection(url, user, password);
	
	//3. sqlλ¬Έ μž‘μ„±
	String sql = "select * from students where name=?";

	//sql 쿼리문을 닀루기 μœ„ν•œ 객체 생성 (PreparedStatement)
	ps = conn.prepareStatement(sql);

	//sqlλ¬Έ ? 자리 μ±„μš°κΈ° : setString(자리수, κ°’)
	ps.setString(1, "μ§€μ˜");

	//4. sql μ‹€ν–‰
	//insert, delete, update => executeUpdate() - λ°˜ν™˜νƒ€μž… int(λͺ‡ 개의 행이 μ—…λ°μ΄νŠΈ λ˜μ—ˆλŠ”μ§€)
	//select => executeQuery() - λ°˜ν™˜νƒ€μž… ResultSet(읽어본 데이터 포함, μ»€μ„œμ˜ κ°œλ…)
	//ResultSet은 next와 ν•¨κ»˜ μ‚¬μš©
	//μ™œ? 첫번째 행은 속성λͺ…을 μ˜λ―Έν•˜κΈ° λ•Œλ¬Έ
	rs = ps.executeQuery();
	
	if(rs.next()){
		//next()λ°˜ν™˜νƒ€μž…μ€ true ν˜Ήμ€ false
		//μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” 행에 데이터가 μžˆμ„ λ•Œ true, μ—†μœΌλ©΄ false
		String name = rs.getString("name");
		String major = rs.getString("major");
		String age = rs.getString("age");
		//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ°€μ Έμ˜¨ 정보λ₯Ό 객체둜 생성
		stu = new Students(name, major, age);

	}
	
	}catch(ClassNotFoundException e){
		//클래슀(oracleDrive)의 κ²½λ‘œκ°€ 잘λͺ»λμ„ 경우
		System.out.println("OracleDriver 클래슀의 κ²½λ‘œκ°€ 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
	}catch(SQLException e){
		System.out.println("SQL μ˜ˆμ™Έκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.");
	}catch(Exception e){ //λͺ¨λ“  μ˜ˆμ™Έμƒν™© μƒμœ„ 클래슀)
		System.out.println("λ‹€λ₯Έ μ˜ˆμ™Έμƒν™©μ΄ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.");
		e.printStackTrace(); //원인, λ°œμƒ 절차 μ½˜μ†”μ°½μ— 좜λ ₯
	}finally{
		//μ˜ˆμ™Έ 상황이 λ°œμƒν•˜λ˜ ν•˜μ§€ μ•Šλ˜ 무쑰건 μ‹€ν–‰
		//μ‚¬μš©ν•œ μžμ› λ°˜ν™˜(μƒμ„±ν•œ μˆœμ„œ λ°˜λŒ€λ‘œ λ°˜ν™˜)
		try{
			//μƒμœ„ catchλ¬Έμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜λ©΄ 각 κ°μ²΄λŠ” null 값을 κ°–κ²Œ 됨
			//null 값은 closeκ°€ λΆˆκ°€λŠ₯ν•˜λ―€λ‘œ μ˜ˆμ™Έμ²˜λ¦¬
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			System.out.println("finallyμ—μ„œ μ˜ˆμ™Έμƒν™© λ°œμƒ!");
			e.printStackTrace();
		}
	}
%>

이름 : <%= stu.getName() %><br>
전곡 : <%= stu.getMajor() %><br>
λ‚˜μ΄ : <%= stu.getAge() %><br>


</body>
</html>

 

βš™οΈ Model2 μ½”λ“œ μž‘μ„±

 

servlet μ½”λ“œ

package com.smhrd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.smhrd.model.Students;


@WebServlet("/Model2")
public class Model2 extends HttpServlet {
	
	//Model2.둜 μš”μ²­ λ¨Όμ €
	//1. DB연동
	//2. SQL μ‹€ν–‰ (Students ν…Œμ΄λΈ”μ˜ λͺ¨λ“  κ°’ κ°€μ Έμ˜€κΈ°)
	//3. 3λͺ…μ˜ 학생정보 >> μ„Έμ…˜μ— μ €μž₯
	//4. Ex02model.jsp둜 이동

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//DB μ—°λ™μ‹œ ν•„μš”ν•œ 객체 μ„ μ–Έ
		Connection conn = null; //λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°
		PreparedStatement ps = null; //sql λ‹€λ£¨λŠ” 객체
		ResultSet rs = null; //sql μ‹€ν–‰ κ΄€λ ¨ 객체
		//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 뢈러온 정보
		Students stu = null;
		List<Students> stuList = new ArrayList<Students>();
		PrintWriter out = response.getWriter();
		
		try{
		
		//1. 였라클과 μ—°κ²°ν•œ λ“œλΌμ΄λ²„ 가지고 였기(λ™μ λ‘œλ”©)
		//λ™μ λ‘œλ”© : ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ ν›„ μ½”λ“œλ₯Ό λ§Œλ‚¬μ„ λ•Œ μ‹€ν–‰
		//μ •μ λ‘œλ”© : ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ν•˜μžλ§ˆμž μ‹€ν–‰
		Class.forName("oracle.jdbc.OracleDriver");
		
		//2. μ§€μ •ν•œ λ°μ΄ν„°λ² μ΄μŠ€μ™€ μ—°κ²°ν•˜κΈ° μœ„ν•œ 객체 생성(Connection)
		//객체 생성 ν›„ λ°μ΄ν„°λ² μ΄μŠ€μ™€ μ—°κ²°μ‹œ url, λ°μ΄ν„°λ² μ΄μŠ€ 계정 정보 ν•„μš”
		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String user = "service";
		String password = "12345";
		
		conn = DriverManager.getConnection(url, user, password);
		
		//3. sqlλ¬Έ μž‘μ„±
		String sql = "select * from students";

		//sql 쿼리문을 닀루기 μœ„ν•œ 객체 생성 (PreparedStatement)
		ps = conn.prepareStatement(sql);

		//4. sql μ‹€ν–‰
		//insert, delete, update => executeUpdate() - λ°˜ν™˜νƒ€μž… int(λͺ‡ 개의 행이 μ—…λ°μ΄νŠΈ λ˜μ—ˆλŠ”μ§€)
		//select => executeQuery() - λ°˜ν™˜νƒ€μž… ResultSet(읽어본 데이터 포함, μ»€μ„œμ˜ κ°œλ…)
		//ResultSet은 next와 ν•¨κ»˜ μ‚¬μš©
		//μ™œ? 첫번째 행은 속성λͺ…을 μ˜λ―Έν•˜κΈ° λ•Œλ¬Έ
		rs = ps.executeQuery();
		
		HttpSession session = request.getSession();
		session.setAttribute("stuList", stuList);
		
		while(rs.next()){
			//next()λ°˜ν™˜νƒ€μž…μ€ true ν˜Ήμ€ false
			//μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” 행에 데이터가 μžˆμ„ λ•Œ true, μ—†μœΌλ©΄ false
			String name = rs.getString("name");
			String major = rs.getString("major");
			String age = rs.getString("age");
			//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ°€μ Έμ˜¨ 정보λ₯Ό 객체둜 생성
			stu = new Students(name, major, age);
			//μƒμ„±ν•œ 객체 배열에 μ €μž₯
			stuList.add(stu);		
		}
		
		}catch(ClassNotFoundException e){
			//클래슀(oracleDrive)의 κ²½λ‘œκ°€ 잘λͺ»λμ„ 경우
			System.out.println("OracleDriver 클래슀의 κ²½λ‘œκ°€ 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
		}catch(SQLException e){
			System.out.println("SQL μ˜ˆμ™Έκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.");
		}catch(Exception e){ //λͺ¨λ“  μ˜ˆμ™Έμƒν™© μƒμœ„ 클래슀)
			System.out.println("λ‹€λ₯Έ μ˜ˆμ™Έμƒν™©μ΄ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.");
			e.printStackTrace(); //원인, λ°œμƒ 절차 μ½˜μ†”μ°½μ— 좜λ ₯
		}finally{
			//μ˜ˆμ™Έ 상황이 λ°œμƒν•˜λ˜ ν•˜μ§€ μ•Šλ˜ 무쑰건 μ‹€ν–‰
			//μ‚¬μš©ν•œ μžμ› λ°˜ν™˜(μƒμ„±ν•œ μˆœμ„œ λ°˜λŒ€λ‘œ λ°˜ν™˜)
			try{
				//μƒμœ„ catchλ¬Έμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜λ©΄ 각 κ°μ²΄λŠ” null 값을 κ°–κ²Œ 됨
				//null 값은 closeκ°€ λΆˆκ°€λŠ₯ν•˜λ―€λ‘œ μ˜ˆμ™Έμ²˜λ¦¬
				rs.close();
				ps.close();
				conn.close();
			}catch(Exception e){
				System.out.println("finallyμ—μ„œ μ˜ˆμ™Έμƒν™© λ°œμƒ!");
				e.printStackTrace();
			}
		}
		
		response.sendRedirect("Ex02model2.jsp");
	}

}

 

jsp μ½”λ“œ

<%@page import="com.smhrd.model.Students"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- μ„Έμ…˜ μ•ˆμ— μ €μž₯λ˜μ–΄ μžˆλŠ” 학생 정보(3λͺ…) ν…Œμ΄λΈ”λ‘œ 좜λ ₯ν•˜κΈ° -->
<table border=1>
	<tr>
		<td>이름</td>
		<td>전곡</td>
		<td>λ‚˜μ΄</td>
	</tr>
	
	<%
		ArrayList<Students> stu = (ArrayList)session.getAttribute("stuList");
		
		for(int i = 0 ; i < stu.size() ; i++){
			out.print("<tr>");
			out.print("<td>" + stu.get(i).getName() + "</td>");
			out.print("<td>" + stu.get(i).getMajor() + "</td>");
			out.print("<td>" + stu.get(i).getAge() + "</td>");
			out.print("</tr>");
		}
	
	%>

</table>
</body>
</html>

 

< μˆ˜μ—… ν›„ μ„ μƒλ‹˜ 풀이 μ½”λ“œ >

Model2.servlet

차이점 : List둜 λ°›κ³ , μ„Έμ…˜ 생성 및 μ €μž₯ μœ„μΉ˜λ₯Ό whileλ¬Έ μ•„λž˜μ—μ„œ μž‘μ„±!

package com.smhrd.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.smhrd.model.FullStack;

@WebServlet("/Model2")
public class Model2 extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Model2둜 μš”μ²­ λ¨Όμ €!
		// 1. DB 연동
		// 2. SQL μ‹€ν–‰ (FullStack ν…Œμ΄λΈ” μ•ˆμ˜ λͺ¨λ“  κ°’ κ°€μ Έμ˜€κΈ°, 쑰건 X)
		// 3. 3λͺ…μ˜ 학생 정보 >> μ„Έμ…˜μ— μ €μž₯
		// 4. Ex02model2.jsp둜 이동
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		FullStack fs = null;
		
		//FullStack 객체에 λŒ€ν•œ λ°°μ—΄ 생성
		//Tip. List둜 μ—…μΊμŠ€νŒ…ν•˜μ—¬ μ‚¬μš©
		List<FullStack> stuList = new ArrayList<FullStack>();
		//μ—…μΊμŠ€νŒ…ν•˜λŠ” 이유 : ν•„μš”μ— 따라 λ‹€λ₯Έ 리슀트 νƒ€μž…μœΌλ‘œ λ°”κΎΈμ–΄ μ‚¬μš©ν•˜λ„λ‘
		//ListλŠ” μΈν„°νŽ˜μ΄μŠ€λ‘œ 객체 생성할 수 μ—†μŒ. λ”°λΌμ„œ new ListλŠ” λΆˆκ°€ν•¨!
		//λ”°λΌμ„œ κ΅¬ν˜„ν• λ•ŒλŠ” ArrayList ν˜Ήμ€ LinkedList둜 κ΅¬ν˜„ν•΄μ•Ό 함. 
		//-ArrayListλŠ” 데이터λ₯Ό κ²€μƒ‰ν•˜λŠ”λ° μš©μ΄ν•œ νƒ€μž…
		//-LinkedListλŠ” 쀑간 데이터λ₯Ό μ‚­μ œν•˜κ±°λ‚˜ μΆ”κ°€ν•˜λŠ”λ° 용이
		
		try{
			//0. JDBCκ΄€λ ¨ 도ꡬ κ°€μ Έμ˜€κΈ° - 라이브러리 μΆ”κ°€ (webapp > WEB-INF에 λ„£μ–΄μ£ΌκΈ°)
			//1. 였라클과 μ—°κ²°ν•  λ“œλΌμ΄λ²„ 가지고 였기(동적 λ‘œλ”©)
			//   λ™μ λ‘œλ”© : μ½”λ“œλ₯Ό λ§Œλ‚¬μ„ λ•Œ μ‹€ν–‰ (μ •μ λ‘œλ”© : ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ν•˜μžλ§ˆμž μ‹€ν–‰)
			Class.forName("oracle.jdbc.OracleDriver");

			//2. μ§€μ •ν•œ λ°μ΄ν„°λ² μ΄μŠ€μ™€ μ—°κ²°ν•˜κΈ° μœ„ν•œ 객체 생성 : Connection
			//	- url, μ‹œμŠ€ν…œ 계정 μ§€μ •ν•˜μ—¬ λ°μ΄ν„°λ² μ΄μŠ€ 접속
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String user = "service";
			String password = "12345";
			
			conn = DriverManager.getConnection(url, user, password);
			
			//sql ν‹€
			String sql = "select * from fullstack";
			
			//sql 쿼리문을 닀루기 μœ„ν•œ 객체 생성 : PreparedStatement - μ™„μ„±, μ‹€ν–‰ λ“±
			ps = conn.prepareStatement(sql);
			
			//? 자리 μ±„μš°κΈ° : setString(자리수(1λΆ€ν„° μ‹œμž‘),κ°’)
//			ps.setString(1, "μ„ μ˜ν‘œ");
			
			//INSERT, DELETE, UPDATE → executeUpdate() : λ°˜ν™˜νƒ€μž… int (λͺ‡κ°œμ˜ 행이 μ—…λ°μ΄νŠΈ λ˜μ—ˆλŠ”μ§€)
			//SELECT(read) → executeQuery() : λ°˜ν™˜νƒ€μž… ResultSet (μ½μ–΄μ˜¨ 데이터 포함, μ»€μ„œμ˜ κ°œλ…)
			//Resultset 은 next 와 ν•¨κ»˜ μ‚¬μš© (κ·Έ λ‹€μŒν–‰μœΌλ‘œ μ»€μ„œ 이동)
			//sql μ‹€ν–‰ 
			rs = ps.executeQuery();
			
			while(rs.next()){ //next() : true/false 
							//        → true : μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” 행에 데이터가 μžˆμ„ λ•Œ
							//        → false : μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” 행에 데이터가 없을 λ•Œ 
				String name = rs.getString("name"); //맀개인자 : 컬럼 - μ»¬λŸΌμ΄λ¦„ 
				String major = rs.getString(2); // ν˜Ήμ€ 컬럼번호(1λΆ€ν„° μ‹œμž‘) μ‚¬μš© κ°€λŠ₯
				String phone = rs.getString("phone");
				
				//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ°€μ Έμ˜¨ 정보λ₯Ό 객체둜 생성
				//학생 ν•œλͺ…μ˜ μ •λ³΄λ§Œ μ €μž₯
				fs = new FullStack(name, major, phone);
				
				//λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ°€μ Έμ˜¨ 정보λ₯Ό 톡해 λ§Œλ“  FullStack 객체λ₯Ό 배열에 μΆ”κ°€
				stuList.add(fs);
				
			}
			
		}catch(ClassNotFoundException e){
			//Exception : λͺ¨λ“  μ˜ˆμ™Έμƒν™©μ˜ μƒμœ„ 클래슀
			//ClassNotFoundException : 클래슀의 κ²½λ‘œκ°€ 잘λͺ»λμ„ 경우(OracleDrive 가지고 올 λ•Œ κ²½λ‘œκ°€ 잘λͺ»λμ„ 경우)
			System.out.println("OracleDriver 클래슀 λͺ»μ°ΎμŒ!");
		}catch(SQLException e){
			System.out.println("sql μ˜ˆμ™Έ λ°œμƒ!");
		}catch(Exception e){
			System.out.println("λ‹€λ₯Έ μ˜ˆμ™Έ λ°œμƒ!");
			e.printStackTrace(); //원인, λ°œμƒ 절차 μ½˜μ†”μ°½μ— 좜λ ₯
		}finally{ //μ˜ˆμ™Έμƒν™©μ΄ λ°œμƒν•˜λ˜μ§€, ν•˜μ§€ μ•Šλ˜μ§€κ°„μ— 무쑰건 μ‹€ν–‰!
			//μ‚¬μš©ν•œ μžμ› λ°˜ν™˜(μƒμ„±ν•œ μˆœμ„œ λ°˜λŒ€λ‘œ λ°˜ν™˜)
			try{ //catchλ¬Έ 였λ₯˜ λ°œμƒλ˜λ©΄ rs, ps, conn은 nullκ°’ 
				//null값은 closeλΆˆκ°€ν•˜λ―€λ‘œ try~catchλ¬Έ μ‚¬μš©ν•˜μ—¬ μ˜ˆμ™Έμ²˜λ¦¬
				rs.close();
				ps.close();
				conn.close();
			}catch(Exception e){
				System.out.println("finallyμ—μ„œ μ˜ˆμ™Έμƒν™© λ°œμƒ!");
				e.printStackTrace();
			}
		}
		
		//stuListλ₯Ό μ„Έμ…˜μ— μ €μž₯
		//μ„Έμ…˜ 객체 생성
		HttpSession session = request.getSession();
		
		//배열을 μ„Έμ…˜μ— μ €μž₯
		session.setAttribute("student", stuList);
		
		//이동
		response.sendRedirect("Ex02model2.jsp");
		
	}

}

 

Model2.jsp

차이점 : for eachλ¬Έ μ‚¬μš© !

<%@page import="java.util.List"%>
<%@page import="com.smhrd.model.FullStack"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- μ„Έμ…˜ μ•ˆμ— μ €μž₯λ˜μ–΄ μžˆλŠ” 학생정보(3λͺ…) ν…Œμ΄λΈ”λ‘œ 좜λ ₯ν•˜κΈ° -->
<%
	//μ„Έμ…˜μ— μ €μž₯된 FullStack 객체(student)에 λŒ€ν•œ κ°’ λΆˆλŸ¬μ™€μ„œ μ–΄λ ˆμ΄λ¦¬μŠ€νŠΈμ— μ €μž₯!
	List<FullStack> stuList = (ArrayList)session.getAttribute("student");
%>

<table border=1>
	<tr>
		<th>이름</th>
		<th>전곡</th>
		<th>μ „ν™”λ²ˆν˜Έ</th>
	</tr>
		<%
		for(FullStack stu:stuList){ %>
		<tr>
			<td><%=stu.getName() %></td>
			<td><%=stu.getMajor() %></td>
			<td><%=stu.getPhone() %></td>
		</tr>
		<% }%>

</table>

</body>
</html>
λ°˜μ‘ν˜•