Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[60] 230324 JSP: ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 60์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/HTML5, CSS3, JSP

[60] 230324 JSP: ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 60์ผ]

yjyuwisely 2023. 3. 24. 15:33

230324 60th class

์ง„๋„: ๊ต์žฌ๊ฐ€ ์•„๋‹Œ ์ž์ฒด ์ˆ˜์—… (๊ต์žฌ: ์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€, ์ €์ž: ์ตœ๋ฒ”๊ท )
์ฑ… ์˜ˆ์ œ ์ฝ”๋“œ: https://github.com/madvirus/jsp23
์ €์ž ๋ธ”๋กœ๊ทธ: https://javacan.tistory.com/

 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | ์ตœ๋ฒ”๊ท  - ๊ต๋ณด๋ฌธ๊ณ 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | [์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€]๋Š” JSP 2.3์˜ ์ƒˆ๋กœ์šด ํŠน์ง• ๋ฐ˜์˜๊ณผ JSP๋ฅผ ์ง€์›ํ•˜๋Š” ์š”์†Œ์ธ ์„œ๋ธ”๋ฆฟ๊ณผ ํ‘œํ˜„ ์–ธ์–ด ๋“ฑ์˜ ์ƒˆ๋กœ์šด

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋ชฐ๋ž๋˜ ๊ฒƒ

Exception
is a standard class from which every exceptions inherit. 
SQLException is a class that inherits from Exception and that is designed specifically for database(SQL) exceptions


๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

input ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ํƒ€์ดํ•‘ํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ, ๋‹จ์ˆœํ•˜๊ฒŒ ๋ณด์ด๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
๋’ค์— readonly๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜, 
ํ‘œํ˜„์‹์„ ๋‘๊ณ  input ํƒœ๊ทธ์— type์„ hidden์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

๋ฒˆํ˜ธ: <input type="text" name="no" value="<%=rs.getString("no") %>" readonly> <!-- ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ• -->
๋ฒˆํ˜ธ: <%=rs.getInt("no") %> <input type="hidden" name="no" value="<%=rs.getString("no") %>"> <!-- ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•: hidden ์“ด๋‹ค. -->

2023.03.24 - [๐ŸŒณK-Digital Revision 2023โœจ/JSP] - [60] 230324 JSP: ์นดํŽ˜ ๊ธ€์“ฐ๊ธฐ ์‘์šฉ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 60์ผ]

์ด ๊ธ€์˜ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ์žˆ๋Š” ๋‚ด์šฉ์ธ๋ฐ, ์œ„์˜ ๋‚ด์šฉ์„ ์‘์šฉํ•œ ๊ฒƒ์ด๋‹ค.

input type="hidden"์œผ๋กœ input ํ˜•ํƒœ๋Š” ์ˆจ๊ธด๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์˜ ์ž‘์„ฑ์ผ๊ณผ ์‹œ๊ฐ„์€ MySQL์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. 

<%=session.getAttribute("user_id")%>
<input type="hidden" name="id" value="<%=session.getAttribute("user_id")%>">

1 index.jsp ๋กœ๊ทธ์ธ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ๋œจ๋Š” ํŽ˜์ด์ง€ 
2 insertBoard.jsp ๋กœ๊ทธ์ธํ–ˆ์„ ๋•Œ๋งŒ ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ๋‚˜ํƒ€๋‚œ๋‹ค.
๋กœ๊ทธ์ธํ–ˆ์„ ๋•Œ๋งŒ ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ๋‚˜ํƒ€๋‚œ๋‹ค.
3 insertBoardServer.jsp  
4 InsertMember.jsp ํšŒ์›๊ฐ€์ž… ์–‘์‹
5 InsertMemberServer.jsp ํšŒ์›๊ฐ€์ž… ์„œ๋ฒ„ 
6 LoginServer.jsp ๋กœ๊ทธ์ธ ์„œ๋ฒ„ 
7 LogoutServer.jsp ๋กœ๊ทธ์•„์›ƒ ์„œ๋ฒ„
8 modifyBoard.jsp ์ˆ˜์ •, ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
9 removeBoard.jsp ๊ฒŒ์‹œํŒ ๊ธ€ ์‚ญ์ œํ•˜๊ธฐ
10 viewBoardDetail.jsp ์ˆ˜์ •, ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
11 viewBoardList.jsp mySQL์˜ board ํ…Œ์ด๋ธ”์˜ DB๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. 
12 viewLoginForm.jsp ๋กœ๊ทธ์ธ ์–‘์‹, ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ๋„ ๋„ฃ๋Š”๋‹ค.  
13 viewMemberList.jsp  
14 viewMemberListbook.jsp ์ฑ…์˜ ์˜ˆ์‹œ

ํšŒ์›๊ฐ€์ž… ์–‘์‹
์ฝ”๋“œ) insertMember.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์›๊ฐ€์ž… ์–‘์‹</title>
</head>
<body>
<form action="insertMemberServer.jsp" method="post"> <!-- post๋กœ ํ•ด์•ผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ URL์— ๋…ธ์ถœ ์•ˆ ๋œ๋‹ค. -->
<table>
<tr><td>์•„์ด๋””</td></tr>
<tr><td><input type="text"></td></tr>

<tr><td>๋น„๋ฐ€๋ฒˆํ˜ธ</td></tr>
<tr><td><input type="password"></td></tr>

<tr><td>์ด๋ฆ„</td></tr>
<tr><td><input type="text"></td></tr>

<tr><td>์ƒ๋…„์›”์ผ</td></tr>
<tr><td><input type="text" placeholder="๋…„(4์ž)">
		<select>
			<option>์›”</option>
			<option>1</option>
			<option>2</option>
			<option>3</option>
			<option>4</option>
			<option>5</option>
			<option>6</option>
			<option>7</option>
			<option>8</option>
			<option>9</option>
			<option>10</option>
			<option>11</option>
			<option>12</option>
		</select>
		<input type="text" placeholder="์ผ"></td>
		
<tr><td>์„ฑ๋ณ„</td></tr>
<tr><td>
		<select>
			<option>์„ฑ๋ณ„</option>
			<option>๋‚จ์ž</option>
			<option>์—ฌ์ž</option>
			<option>์„ ํƒ ์•ˆ ํ•จ</option>
		</select>	
</td></tr>

<tr><td>์ด๋ฉ”์ผ</td><tr>	
<tr><td><input type="text"></td></tr>

<tr><td>์—ฐ๋ฝ์ฒ˜</td><tr>	
<tr><td><input type="text"></td></tr>

<tr><td><input type="submit" value="๊ฐ€์ž…ํ•˜๊ธฐ"></td><tr>				
</table>
</form>
</body>
</html>

๊ฒฐ๊ณผ)

ํšŒ์›๊ฐ€์ž… ์–‘์‹์˜ ๊ฒฐ๊ณผ


name is the name that is used when the value is passed (in the URL or in the posted data).
id is used to uniquely identify the element for CSS styling and JavaScript.

2023.02.03 - [๐Ÿ’ป Self-Study Revisionโœจ/๋‚˜์˜ ๊ฐœ๋ฐœ ์ผ์ง€โœ๐Ÿป] - ๐Ÿฅ[K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 21์ผ~26์ผ] HTML, CSS: ์ฒซ ๋ฒˆ์งธ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ํ•˜๋ฉด์„œ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๋ชจ์Œ(1)


MySQL์— ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๋Š”๋‹ค.

desc member;
insert into member values('abcd12','1234','์ •์ž๋ฐ”','20230324','m','abcd@gmail.com','01012345678')

์ฝ”๋“œ) ํšŒ์›๊ฐ€์ž… ์„œ๋ฒ„ insertMemberServer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>    
<% request.setCharacterEncoding("utf-8"); %> <!-- ํ•œ๊ธ€๊นจ์ง ํ•ด๊ฒฐ -->    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์›๊ฐ€์ž… ์„œ๋ฒ„</title>
</head>
<body>
<%
	//์—ฌ๊ธฐ์„œ insertํ•  ๊ฒƒ์„ ๋„ฃ๋Š”๋‹ค. (์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ƒ๋…„์›”์ผ, ์„ฑ๋ณ„, ์ด๋ฉ”์ผ, ์—ฐ๋ฝ์ฒ˜)
	//์•„์ด๋””
	String id = request.getParameter("id");
	//๋น„๋ฐ€๋ฒˆํ˜ธ
	String password = request.getParameter("password");
	//์ด๋ฆ„
	String name = request.getParameter("name");
	//์ƒ๋…„์›”์ผ
	String birthday = request.getParameter("year")+request.getParameter("month")+request.getParameter("day"); //์—ฐ๊ฒฐํ•  ๋•Œ concat์„ ์จ๋„ ๋œ๋‹ค.
	//์„ฑ๋ณ„
	String gender = request.getParameter("gender");
	//์ด๋ฉ”์ผ
	String email = request.getParameter("email");
	//์—ฐ๋ฝ์ฒ˜
	String phone = request.getParameter("phone");
	
	// 1. JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ(mysql)
	Class.forName("com.mysql.jdbc.Driver");
	//java api์— db์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
	Connection conn = null;
	
	//workbench sql์„ ์ž‘์„ฑํ•œ ํ›„ ctrl+enter(๊ฒฐ๊ณผ๋„์ถœ)
	//sql๋ฌธ์žฅ์„ ์ž‘์„ฑ์„ ํ•˜๊ณ , ๊ทธ sql๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
	//Statement stmt = null;
	PreparedStatement pstmt = null;
	
	try{
		String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
		String dbUser = "root";
		String dbPass = "1234";
		
		//2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜ ์ƒ์„ฑ
		conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
		
		// ์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ DB ์—ฐ๊ฒฐ ์ฝ”๋“œ 
		
		//3. Statement ์ƒ์„ฑ
		//SQL ์ฝ”๋“œ ๋ณต์‚ฌ
		String query = "insert into member values(?,?,?,?,?,?,?);"; //๋ฌผ์Œํ‘œ(?) ์ฒ˜๋ฆฌ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋œ๋‹ค.
		pstmt = conn.prepareStatement(query);
		pstmt.setString(1, id);//์ฒซ๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” id๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(2, password);//2๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” password๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(3, name);//3๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” name๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(4, birthday);//4๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” birthday: year,month,day๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(5, gender);//5๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” gender๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(6, email);//6๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” email๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(7, phone);//7๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” phone๊ฐ’์„ ๋„ฃ์–ด๋ผ.
			
		//4. ์ฟผ๋ฆฌ ์‹คํ–‰
		//workbench์—์„œ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์žฅ์„ ์‹คํ–‰)๋ฅผ ํ†ตํ•ด ๋„์ถœ๋œ select๊ฒฐ๊ณผ๋ฅผ Resultset์— ์ €์žฅ 
		//rs = pstmt.executeQuery();
		pstmt.executeUpdate(); //insert๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ. insert์ž‘์—… ๋•Œ select์ผ ๋•Œ executeUpdate()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‚˜๋จธ์ง€๋Š” rs=pstmt.executeQuery();๋ฅผ ์“ด๋‹ค.
		//insert ํ•œ ํ›„ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ ํ™”๋ฉด(viewBoardList.jsp)์œผ๋กœ ์ด๋™ํ•˜๋ผ๊ณ  ์‘๋‹ตํ•œ๋‹ค. 
		response.sendRedirect("viewBoardList.jsp");
		
	}catch(Exception ex){
		System.out.println(ex.getMessage()); //์ฝ˜์†”์— ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋„์šด๋‹ค.
		out.println("DB ์—ฐ๊ฒฐ ์‹คํŒจ");
	}finally{
		if(conn!=null){conn.close();}
	}	
%>
</body>
</html>

์•„์ด๋””๋ถ€ํ„ฐ ์—ฐ๋ฝ์ฒ˜๊นŒ์ง€ ์ž…๋ ฅํ•œ๋‹ค.

๊ฒฐ๊ณผ)

๋งˆ์ง€๋ง‰์— ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒƒ์ด ๋ณด์ธ๋‹ค.


๋กœ๊ทธ์ธ ํ™”๋ฉด ๋ฐ ์„œ๋ฒ„

์ฝ”๋“œ) ๋กœ๊ทธ์ธ ์–‘์‹ viewLoginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์ธ ํ™”๋ฉด</title>
</head>
<body>
<form action="LoginServer.jsp">
<table>
	<tr><td>์•„์ด๋””</td>
		<td><input type="text" name="id"></td></tr>	
	<tr><td>๋น„๋ฐ€๋ฒˆํ˜ธ</td>
		<td><input type="password" name="password"></td></tr>
	<tr><td colspan="2"><input type="submit" value="๋กœ๊ทธ์ธ"></td></tr>

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


4ํ–‰์˜ MySQL์—์„œ ์ ์€ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ DB์— ์—†๋‹ค. (null๋กœ ๋œฌ๋‹ค.)

์ฝ”๋“œ) ๋กœ๊ทธ์ธ ์„œ๋ฒ„ LoginServer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>    
<% request.setCharacterEncoding("utf-8"); %> <!-- ํ•œ๊ธ€๊นจ์ง ํ•ด๊ฒฐ -->    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์ธ ์„œ๋ฒ„</title>
</head>
<body>
<%
	//์—ฌ๊ธฐ์„œ insertํ•  ๊ฒƒ์„ ๋„ฃ๋Š”๋‹ค. (์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ)
	//์•„์ด๋””
	String id = request.getParameter("id");
	//๋น„๋ฐ€๋ฒˆํ˜ธ
	String password = request.getParameter("password");
	
	// 1. JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ(mysql)
	Class.forName("com.mysql.jdbc.Driver");
	//java api์— db์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
	Connection conn = null;
	
	//workbench sql์„ ์ž‘์„ฑํ•œ ํ›„ ctrl+enter(๊ฒฐ๊ณผ๋„์ถœ)
	//sql๋ฌธ์žฅ์„ ์ž‘์„ฑ์„ ํ•˜๊ณ , ๊ทธ sql๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
	//Statement stmt = null;
	PreparedStatement pstmt = null;
	
	//select์˜ ๊ฒฐ๊ณผ๋ฅผ DBMS์—์„œ java๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
	ResultSet rs = null;
	
	try{
		String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
		String dbUser = "root";
		String dbPass = "1234";
		
		//2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜ ์ƒ์„ฑ
		conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
		
		// ์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ DB ์—ฐ๊ฒฐ ์ฝ”๋“œ 
		
		//3. Statement ์ƒ์„ฑ
		//SQL ์ฝ”๋“œ ๋ณต์‚ฌ
		String query = "select * from member where id=? and password =?"; //๋ฌผ์Œํ‘œ(?) ์ฒ˜๋ฆฌ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋œ๋‹ค.
		pstmt = conn.prepareStatement(query);
		pstmt.setString(1, id);//์ฒซ๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” id๊ฐ’์„ ๋„ฃ์–ด๋ผ.
		pstmt.setString(2, password);//2๋ฒˆ์งธ ๋ฌผ์Œํ‘œ์—๋Š” password๊ฐ’์„ ๋„ฃ์–ด๋ผ.
			
		//4. ์ฟผ๋ฆฌ ์‹คํ–‰
		//workbench์—์„œ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์žฅ์„ ์‹คํ–‰)๋ฅผ ํ†ตํ•ด ๋„์ถœ๋œ select๊ฒฐ๊ณผ๋ฅผ Resultset์— ์ €์žฅ 
		rs = pstmt.executeQuery();
		//pstmt.executeUpdate(); //insert๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ. insert์ž‘์—… ๋•Œ select์ผ ๋•Œ executeUpdate()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‚˜๋จธ์ง€๋Š” rs=pstmt.executeQuery();๋ฅผ ์“ด๋‹ค.
		//insert ํ•œ ํ›„ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ ํ™”๋ฉด(viewBoardList.jsp)์œผ๋กœ ์ด๋™ํ•˜๋ผ๊ณ  ์‘๋‹ตํ•œ๋‹ค. 
		//response.sendRedirect("viewBoardList.jsp");
		
		if(rs.next()){//select๊ฒฐ๊ณผ๊ฐ€ 1๊ฑด ์žˆ์Œ(๋กœ๊ทธ์ธํ•ด๋„ ๋จ)
			//session์˜์—ญ์˜ id๋ณ€์ˆ˜์— id๊ฐ’์„ ์ €์žฅ
			session.setAttribute("user_id", id);
			//๋ฉ”์ธํŽ˜์ด์ง€๋กœ ์ด๋™(index.jsp)
			response.sendRedirect("index.jsp");
		}else{//select๊ฒฐ๊ณผ๊ฐ€ ์—†์Œ(๋กœ๊ทธ์ธํ•˜๋ฉด ์•ˆ ๋จ)
			//ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๋กœ ์ด๋™
			response.sendRedirect("insertMember.jsp");
		}
		
	}catch(SQLException ex){
		System.out.println(ex.getMessage()); //์ฝ˜์†”์— ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋„์šด๋‹ค.
		out.println("DB ์—ฐ๊ฒฐ ์‹คํŒจ");
	}finally{
		if(conn!=null){conn.close();}
	}	
%>
</body>
</html>

์ฝ”๋“œ) ๋กœ๊ทธ์ธ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ๋œจ๋Š” ํŽ˜์ด์ง€ index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>      
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์ธ ์„ฑ๊ณต</title>
</head>
<body>
<%=session.getAttribute("user_id") %>๋‹˜ ์–ด์„œ์˜ค์„ธ์š”.
${user_id}๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. <!-- ๋‘ ์ฝ”๋“œ ๋‹ค ๋œ๋‹ค. -->
</body>
</html>

๊ฒฐ๊ณผ)

์„ธ์…˜์„ ์ฃฝ์ด๋ ค๋ฉด ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์„ ๋ˆ๋‹ค. ๋˜๋Š” ์„ธ์…˜์˜ ๊ธฐ๋ณธ๊ฐ’: 30๋ถ„์ด ์ง€๋‚˜๋ฉด ์„ธ์…˜์ด ๋งŒ๋ฃŒ๋œ๋‹ค.


์ฐธ๊ณ : https://cloudstudying.kr/lectures/82#implemetation

์ˆ˜์ •๋œ ์ฝ”๋“œ) ๋กœ๊ทธ์ธํ–ˆ์„ ๋•Œ๋งŒ ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ๋‚˜ํƒ€๋‚œ๋‹ค. insertBoard.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>     
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํ…Œ์ด๋ธ”</title>
</head>
<body>
<form action = "insertBoardServer.jsp" method="post"> <!-- ๊ธ€์“ฐ๊ธฐ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋ฒ„.jsp -->
	<table border="1">
		<tr>
			<td>์ œ๋ชฉ</td>
			<td><input type="text" name="title"></td>
		</tr>
		<tr>	
			<td>๋‚ด์šฉ</td>
			<td><textarea rows="10" cols="30" name="content"></textarea></td>
		</tr>
		<tr>
			<td colspan="2"><!-- ์ขŒ์šฐ์˜ ์…€๋“ค์„ ๋ณ‘ํ•ฉํ•œ๋‹ค (๊ฐ€๋กœ๋กœ ํ•ฉ์นจ, ๊ฐ€๋กœ๊ฐ€ ๊ธด ์ง์‚ฌ๊ฐํ˜•) -->
			<c:if test="${user_id != null}"> <!-- ๋กœ๊ทธ์ธ ์•ˆ ํ•˜๋ฉด ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ์•ˆ ๋ณด์ธ๋‹ค. -->
				<input type="submit" value="๊ธ€์“ฐ๊ธฐ">
			</c:if>	
			</td>
		</tr>
	</table>
</form>
</body>
</html>

๊ฒฐ๊ณผ) ์„ธ์…˜์ด ๋งŒ๋ฃŒ๋˜์—ˆ์„ ๋•Œ

๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ์•ˆ ๋œฌ๋‹ค.


์ฝ”๋“œ) ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ๋„ ๋„ฃ๋Š”๋‹ค. viewLoginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์ธ ํ™”๋ฉด</title>
</head>
<body>
<form action="LoginServer.jsp">
<table>
	<tr><td>์•„์ด๋””</td>
		<td><input type="text" name="id"></td></tr>	
	<tr><td>๋น„๋ฐ€๋ฒˆํ˜ธ</td>
		<td><input type="password" name="password"></td></tr>
	<tr><td colspan="2">
	<input type="submit" value="๋กœ๊ทธ์ธ" formaction="LoginServer.jsp">
	<input type="submit" value="๋กœ๊ทธ์•„์›ƒ" formaction="LogOutServer.jsp">
	</td>
</tr>
</table>
</form>
</body>
</html>

์ฝ”๋“œ) ๋กœ๊ทธ์•„์›ƒ ์„œ๋ฒ„ LogOutServer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์•„์›ƒ ์„œ๋ฒ„</title>
</head>
<body>

<%
	session.invalidate();
%>

</body>
</html>

๋ฒˆ์™ธ ์ฝ”๋“œ) ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์— ์•„์ด๋””๋„ ๋œจ๊ฒŒ ํ•œ๋‹ค.  insertBoard.jsp
input type="hidden"์œผ๋กœ input ํ˜•ํƒœ๋Š” ์ˆจ๊ธด๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์˜ ์ž‘์„ฑ์ผ๊ณผ ์‹œ๊ฐ„์€ MySQL์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. 

<%=session.getAttribute("user_id")%>
<input type="hidden" name="id" value="<%=session.getAttribute("user_id")%>">
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>     
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํ…Œ์ด๋ธ”</title>
</head>
<body>
<form action = "insertBoardServer.jsp" method="post"> <!-- ๊ธ€์“ฐ๊ธฐ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋ฒ„.jsp -->
	<table border="1">
		<tr>
			<td>์•„์ด๋””</td>
			<td>
				<%=session.getAttribute("user_id")%>
				<input type="hidden" name="id" value="<%=session.getAttribute("user_id")%>"></td>
		</tr>
		<tr>
			<td>์ œ๋ชฉ</td>
			<td><input type="text" name="title"></td>
		</tr>
		<tr>	
			<td>๋‚ด์šฉ</td>
			<td><textarea rows="10" cols="30" name="content"></textarea></td>
		</tr>
		<tr>
			<td colspan="2"><!-- ์ขŒ์šฐ์˜ ์…€๋“ค์„ ๋ณ‘ํ•ฉํ•œ๋‹ค (๊ฐ€๋กœ๋กœ ํ•ฉ์นจ, ๊ฐ€๋กœ๊ฐ€ ๊ธด ์ง์‚ฌ๊ฐํ˜•) -->
			<c:if test="${user_id != null}"> <!-- ๋กœ๊ทธ์ธ ์•ˆ ํ•˜๋ฉด ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ์•ˆ ๋ณด์ธ๋‹ค. -->
				<input type="submit" value="๊ธ€์“ฐ๊ธฐ">
			</c:if>	
			</td>
		</tr>
	</table>
</form>
</body>
</html>

id1234๋กœ ๋กœ๊ทธ์ธํ–ˆ๋Š”๋ฐ ๋กœ๊ทธ์ธํ•œ ์•„์ด๋””๊ฐ€ ์œ„์— ๋œฌ๋‹ค.
๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก์— ์›๋ž˜ ์ž‘์„ฑ์ž๊ฐ€ null๋กœ ๋–ด์—ˆ๋Š”๋ฐ ๊ฒŒ์‹œ๊ธ€ 22๋ฒˆ์„ ๋ณด๋ฉด ์ž‘์„ฑ์ž์˜ id์ธ id1234์ด ๋‚˜์˜จ๋‹ค. (๊ฒŒ์‹œ๊ธ€ 17๋ฒˆ์˜ abcd1234๋Š” MySQL์—์„œ ์ง์ ‘ ๋„ฃ์€ ๋ฐ์ดํ„ฐ์ด๋‹ค.)


728x90
๋ฐ˜์‘ํ˜•
Comments