[Web] Day 4 - JSP

sueยท2024๋…„ 1์›” 8์ผ

๐Ÿ“’๊ตญ๋น„ํ•™์› [Web]

๋ชฉ๋ก ๋ณด๊ธฐ
6/21
post-thumbnail

-request.getParameter๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” ํ•ด๋‹นํŽ˜์ด์ง€์—์„œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ์ง€, ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์—†์Œ.

1. ๋งŒ๋…„๋‹ฌ๋ ฅ ๋งŒ๋“ค๊ธฐ

โœ๏ธ Test1.

๐Ÿ’ป ์ž…๋ ฅ

ํ™”๋ฉด1 : calendar
ํ™”๋ฉด2 : calendar_ok ์—†์ด form - get๋ฐฉ์‹์ด์ง€๋งŒ ์ž๊ธฐ์ž์‹ ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก


  • ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„๋‚ด๋Š”๊ฑด ๋ณ€์ˆ˜ = request.getParameter()๊ฐ€ ํ•„์š”ํ•จ
  • a href="calendar.jsp?year=<%preYear %>&month=<%preMonth %>"
  • get๋ฐฉ์‹ ์“ธ๋•Œ๋Š” ํŒŒ์ผ์ด๋ฆ„ + ? + ๊ตฌ๋ถ„์ž & ๋„ฃ์–ด์„œ <% %> ๊ฒฐ๊ณผ๊ฐ’ ๋‚ด๊ธฐ


calendar.jsp

<%@page import="java.util.Calendar"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	Calendar cal = Calendar.getInstance(); //์บ˜๋ฆฐ๋”๋งŒ์˜ ๊ฐœ์ฒด์ƒ์„ฑ
	
	int nowYear = cal.get(Calendar.YEAR);
	int nowMonth = cal.get(Calendar.MONTH)+1;
	int nowDay = cal.get(Calendar.DAY_OF_MONTH);

	//์‚ฌ์šฉ์ž๊ฐ€ get๋ฐฉ์‹์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์€ ๋…„,์›”
	String strYear = request.getParameter("year");
	String strMonth = request.getParameter("month");

	//๋‹ฌ๋ ฅ์— ํ‘œ์‹œํ•  ๋…„, ์›”
	//์ฒ˜์Œ ์‹œ์ž‘ํ• ๋•Œ๋Š” ๋„˜์–ด์˜ค๋Š” gP๊ฐ€ ์—†์–ด์„œ null์ด๋ฏ€๋กœ, ๋ฌด์กฐ๊ฑด system-now๋กœ ๋จผ์ € ๋งŒ๋“ค์–ด์•ผ ํ•จ
	int year = nowYear;
	int month = nowMonth;

	//2๋ฒˆ์งธ ์‹คํ–‰
	if (strYear != null) {
		year = Integer.parseInt(strYear); //์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ์ด์œ  : +1 , -1 ํ•ด์ค˜์•ผํ•˜๋‹ˆ๊นŒ
	}

	if (strMonth != null) {
		month = Integer.parseInt(strMonth);
	}

	//1์›” ์ „๋‹ฌ  - ์ „ ๋…„๋„ 12์›”
	int preYear = year, preMonth = month - 1;
	if (preMonth < 1) {// 23.12 <- 24.01
		preYear = year - 1;
		preMonth = 12;
	}

	//12์›” ๋‹ค์Œ ๋‹ฌ์€ ๋‹ค์Œํ•ด 1์›”
	int nextYear = year, nextMonth = month + 1;
	if (nextMonth > 12) {
		nextYear = year + 1;
		nextMonth = 1;
	}
	
	
	//ํ‘œ์‹œํ•  ๋‹ฌ๋ ฅ ์…‹ํŒ…
	//1 : ๋งค๋‹ฌ์˜ ์ฒซ๋‚ ์€ 1
	cal.set(year,month-1,1); 
	
	int startDay = 1;
	int endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	
	//๋‹ฌ๋ ฅ๋งŒ๋“ค ๋• ์ฃผ์˜ ์ˆ˜๊ฐ€ ํ•„์š”ํ•จ
	//(year๋…„ month์›” 1์ผ์˜ ์š”์ผ๊ตฌํ•˜๊ธฐ)
	int week = cal.get(Calendar.DAY_OF_WEEK);
	
	
%>
<!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=UTF-8">
<title>๋งŒ๋…„๋‹ฌ๋ ฅ</title>

<style type="text/css">


body {
	font-size: 11pt;
}

td {
	font-size: 11pt;
}

</style>

</head>
<body>

<br/><br/>

<table align="center" width="210" cellpadding="2" cellspacing="1">
	<tr>
		<td align="center">
		<a href="calendar.jsp?year=<%=preYear %>&month=<%=preMonth %>">โ—€</a>
		<b>&nbsp;<%=year %>๋…„&nbsp;&nbsp;<%=month %>์›”&nbsp;</b>	
		<a href="calendar.jsp?year=<%=nextYear %>&month=<%=nextMonth %>">โ–ถ</a>
		</td>
	</tr>
</table>


<table align="center" width="210" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr>
	<td bgcolor="#e6e4e6" align="center"><font color="red">์ผ</font></td>
	<td bgcolor="#e6e4e6" align="center">์›”</td>
	<td bgcolor="#e6e4e6" align="center">ํ™”</td>
	<td bgcolor="#e6e4e6" align="center">์ˆ˜</td>
	<td bgcolor="#e6e4e6" align="center">๋ชฉ</td>
	<td bgcolor="#e6e4e6" align="center">๊ธˆ</td>
	<td bgcolor="#e6e4e6" align="center"><font color="blue">ํ† </font></td>
</tr>

<%
	int newLine = 0;
	out.print("<tr height='20'>");
	for(int i=1; i<week; i++){
		out.print("<td bgcolor='#ffffff'>&nbsp;</td>"); //<td> ๊ณต๋ฐฑ์„ ์œ„ํ•œ td </td>
		newLine++;
	}

	for(int i=startDay;i<=endDay; i++){
		
		//์ผ์š”์ผ์€ red, ํ† ์š”์ผ์€ blue, ํ‰์ผ์€ black, ์˜ค๋Š˜ color
		String fontColor = (newLine==0)?"red":(newLine==6)?"blue":"black";
		String bgColor = (nowYear==year)&&(nowMonth==month)&&(nowDay==i)?"#e6e4e6":"#ffffff"; 
		
		out.print("<td align='center' bgcolor='"+ bgColor + "'><font color='" + fontColor + "'>" + i +
				"</font></td>");
		//<td align='center' bgcolor='#e6e4e6'>
		//<font color ='black'>8(์ผ)</font></td>
		
		newLine++;
		
		
		if(newLine==7 && i != endDay){//0~6๊นŒ์ง€๊ฐ€ 1์ฃผ๋‹ˆ๊นŒ  
			out.print("</tr><tr height='20'>"); //</tr>๋กœ ์ค„๋ฐ”๊พธ๊ณ  , <tr> ๋‹ค์‹œ tr์—ด๊ธฐ
			newLine=0;
		}
	}

	
	
	while(newLine>0 && newLine<7){
		out.print("<td bgcolor = '#ffffff'>&nbsp;</td>");
		newLine++;
	}

	out.print("</tr>"); // 128line <tr>๋‹ซ์•„์ฃผ๊ธฐ
	
%>

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


๐Ÿ“Œ ์ถœ๋ ฅ

์ฃผ์†Œ๊ฐ’์ด๋ž‘ ํ•ญ์ƒ ๋งž๊ฒŒ ๋‚˜์˜ค๋Š”์ง€ ๋น„๊ตํ•˜๊ธฐ

โ–ถ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ๋‹ฌ

โ—€ ๋ˆ„๋ฅด๋ฉด ์ „๋‹ฌ


2.

โœ๏ธ Test2.

Redirect / Forward

: ๋‘๋ฒˆ์งธ ->๋ฅผ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ•˜๋А๋ƒ ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ํ•˜๋А๋ƒ์˜ ์ฐจ์ด

: [์„ค๋ช… ์ž˜ ๋˜์–ด์žˆ์Œ] ์ถœ์ฒ˜ https://doublesprogramming.tistory.com/63

Redirect : client๋Š” ์„œ๋ฒ„์•ˆ์— ์žˆ๋Š” a.jspํ•œํ…Œ ์š”์ฒญํ–ˆ์ง€๋งŒ, ์—†์œผ๋‹ˆ๊นŒ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด๊ณ ์žˆ๋Š” result.jsp๋กœ ๋ฐ”๋€œ (์ฃผ์†Œ๊ฐ€ ๋ฐ”๋€œ)

  • ex) ์„œ๋ฒ„ํ•œํ…Œ DB์— INSERT/DELETE/UPDATE/SESSION์‹œ์ผœ์ค˜
    : client๊ฐ€ 3๋ฒˆ์„ ์š”์ฒญํ–ˆ์ง€๋งŒ a.jsp๋Š” ์—†์œผ๋‹ˆ๊นŒ DB์— INSERT๋ฅผ ํ•ด์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ Result.jsp์— ๋„ฃ์–ด์ฃผ๊ณ (์ฃผ์†Œ๋ฐ”๋€œ) , ๊ทธ ์ดํ›„์— client์—๊ฒŒ ํ™•์ธํ•ด๋ด๋ผ~
  • ๋ณด์™„ ์ทจ์•ฝ

Forward : client๊ฐ€ a.jsp์— ์š”์ฒญ์„ ํ•ด์„œ ์•ˆ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, result.jsp์—์„œ ๋งŒ๋“ค์–ด์„œ ๊ทธ ๋งŒ๋“  ๊ฒฐ๊ณผ๋ฅผ ์ž๊ธฐ๊ฐ€ a.jsp๊ฐ€ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์˜ด. (์ฃผ์†Œ๊ฐ€ ์•ˆ๋ฐ”๋€œ)

  • ex) servelt : ์ฃผ๋กœ forward๋ฅผ ์”€ (redirect๋„ ์”€)

-> ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ๋Š” test_for๋กœ ๋ณด์—ฌ์ง€์ง€๋งŒ, ์‚ฌ์‹ค result์—์„œ ์ด๋ฃจ์–ด์ง€๊ณ ์žˆ์–ด์„œ ๋ณด์™„์— ๊ฐ•ํ•จ.

:jsp๋Š” servelt์ด ์—†์Œ - redirect์”€


ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ ใ…ฃ Server (์ด ์•ˆ์— .jsp / result.jsp - file์ด ๋งŒ๋“ค์–ด์ ธ์žˆ์Œ)





<onclick="this.form.action='test1_red.jsp'">
  • this.form.action๋ผ๊ณ  ์›๋ž˜๋Š” ์•ˆ์”€. ์ง€๊ธˆ์€ ์•„๋ž˜๋ž‘ ์ฃผ์†Œ๊ฐ€ ๋‹ฌ๋ผ์„œ ์ด๋ ‡๊ฒŒ ์“ด๊ฑฐ์ž„

์„œ๋ฒ„ -> request -> ํด๋ผ์ด์–ธํŠธ
ํด๋ผ์ด์–ธํŠธ -> response -> ์„œ๋ฒ„


๐Ÿ’ป ์ž…๋ ฅ

test1.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();

%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>


<form action="" method="post">

์ด๋ฆ„ : <input type="text" name="name"><br/>

<input type="submit" value="๋ฆฌ๋‹ค์ด๋ ‰ํŠธ"
	onclick="this.form.action='test1_red.jsp'">
	
<input type="submit" value="ํฌ์›Œ๋“œ"
	onclick="this.form.action='test1_for.jsp'">

</form>



</body>
</html>

test1_red

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	
	String name = request.getParameter("name");
	
	//๋ฆฌ๋‹ค์ด๋ ‰ํŠธ(์œ„์— ๋ฐ›์€ name์„ "result.jsp"๋กœ ๊ฐ€๋ผ๊ณ  ํด๋ผ์ด์–ธํŠธํ•œํ…Œ ๋Œ์•„๊ฐ€์„œ ๋งํ•จ)
	//๊ธธ์ด ๊บพ์—ฌ๋ฒ„๋ฆฐ๊ฑด name์ด null๊ฐ’์ž„ (์ฃผ์†Œ๋ฐ”๋€œ)
	response.sendRedirect("result.jsp");
	
	
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์ด๋ฆ„: <%=name %>

</body>
</html>

test1_for

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String name = request.getParameter("name");
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์ด๋ฆ„: <%=name %>
<jsp:forward page="result.jsp"/>

</body>
</html>

result

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String name = request.getParameter("name");
	
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋˜๋Š” ํฌ์›Œ๋“œ ํŽ˜์ด์ง€<br/>
์ด๋ฆ„: <%=name %>

</body>
</html>

3. โญ SESSION - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ

โœ๏ธ Test3.

๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌ = session ์ด์šฉ

  • session : ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ €์žฅ๊ณต๊ฐ„์œผ๋กœ, [ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ ์•ˆ์—์„œ๋งŒ] ๋‹ค๋ฅธ ํด๋”์—์„œ๋„ ๊ฐ€๋Šฅํ•จ


ex) ํŽ˜์ด์ง€๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ, ๋‹‰๋„ค์ž„ ๊ณ„์† ๋‚˜์˜ค๋Š”๊ฑฐ - (session๊ณต๊ฐ„์— ์žˆ๋Š” ๋‹‰๋„ค์ž„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ)


  • align์€ ๊ฐ€๋กœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ
  • valign์€ ์„ธ๋กœ๋ฅผ ๊ธฐ์ค€ (top / middle / buttom)

-๋กœ๊ทธ์ธ์„ ํ•˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ main์ฐฝ์œผ๋กœ ๋Œ์•„๊ฐ
๊ทผ๋ฐ login_ok์—์„œ ๋จธ๋ฌผ๋Ÿฌ์žˆ์Œ

-> ๊ทธ๋ž˜์„œ session์— redirect ๋ฅผ ํ•ด์•ผํ•จ
(๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๊ผˆ์œผ๋‹ˆ๊นŒ)


  • session์•ˆ์—์„œ ์ฐพ๋Š” ์ฝ”๋”ฉ
  • login_ok ๋‹ค๋…€์˜ค๋ฉด Id๊ฐ€ ์ƒ๊ธฐ๋‹ˆ๊นŒ null๊ฐ’์—์„œ ๋ฒ—์–ด๋‚จ

๐Ÿ’ป ์ž…๋ ฅ

login.jsp

<%@page import="java.util.Calendar"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String userId = (String)session.getAttribute("userId");
	
	
	//session.setMaxInactiveInterval(30*60); //30๋ถ„
	//session.setMaxInactiveInterval(5);//5์ดˆ
	
	Calendar cal = Calendar.getInstance(); //์บ˜๋ฆฐ๋”๋งŒ์˜ ๊ฐœ์ฒด์ƒ์„ฑ
	
	int nowYear = cal.get(Calendar.YEAR);
	int nowMonth = cal.get(Calendar.MONTH)+1;
	int nowDay = cal.get(Calendar.DAY_OF_MONTH);

	//์‚ฌ์šฉ์ž๊ฐ€ get๋ฐฉ์‹์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์€ ๋…„,์›”
	String strYear = request.getParameter("year");
	String strMonth = request.getParameter("month");

	//๋‹ฌ๋ ฅ์— ํ‘œ์‹œํ•  ๋…„, ์›”
	//์ฒ˜์Œ ์‹œ์ž‘ํ• ๋•Œ๋Š” ๋„˜์–ด์˜ค๋Š” gP๊ฐ€ ์—†์–ด์„œ null์ด๋ฏ€๋กœ, ๋ฌด์กฐ๊ฑด system-now๋กœ ๋จผ์ € ๋งŒ๋“ค์–ด์•ผ ํ•จ
	int year = nowYear;
	int month = nowMonth;

	//2๋ฒˆ์งธ ์‹คํ–‰
	if (strYear != null) {
		year = Integer.parseInt(strYear); //์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ์ด์œ  : +1 , -1 ํ•ด์ค˜์•ผํ•˜๋‹ˆ๊นŒ
	}

	if (strMonth != null) {
		month = Integer.parseInt(strMonth);
	}

	//1์›” ์ „๋‹ฌ  - ์ „ ๋…„๋„ 12์›”
	int preYear = year, preMonth = month - 1;
	if (preMonth < 1) {// 23.12 <- 24.01
		preYear = year - 1;
		preMonth = 12;
	}

	//12์›” ๋‹ค์Œ ๋‹ฌ์€ ๋‹ค์Œํ•ด 1์›”
	int nextYear = year, nextMonth = month + 1;
	if (nextMonth > 12) {
		nextYear = year + 1;
		nextMonth = 1;
	}
	
	
	//ํ‘œ์‹œํ•  ๋‹ฌ๋ ฅ ์…‹ํŒ…
	//1 : ๋งค๋‹ฌ์˜ ์ฒซ๋‚ ์€ 1
	cal.set(year,month-1,1); 
	
	int startDay = 1;
	int endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	
	//๋‹ฌ๋ ฅ๋งŒ๋“ค ๋• ์ฃผ์˜ ์ˆ˜๊ฐ€ ํ•„์š”ํ•จ
	//(year๋…„ month์›” 1์ผ์˜ ์š”์ผ๊ตฌํ•˜๊ธฐ)
	int week = cal.get(Calendar.DAY_OF_WEEK);
	
	
%>


<!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=UTF-8">
<title>๋กœ๊ทธ์ธ</title>

<style type="text/css">


body {
	font-size: 11pt;
}

td {
	font-size: 11pt;
}

</style>



<script type="text/javascript">

	function sendIt() {
		
		let f = document.myForm;
		
		if(!f.userId.value){
			alert("์•„์ด๋”” ์ž…๋ ฅ!");
			f.userId.focus();
			return;
		}
		
		if(!f.userPwd.value){
			alert("ํŒจ์Šค์›Œ๋“œ ์ž…๋ ฅ!");
			f.userPwd.focus();
			return;
		}
		
		f.submit();
	}

</script>


</head>
<body>

<br/>

<table border="1" width="600" align="center" style="font-size: 9pt;">
<tr height="20">
	<td colspan="2" align="right">
	| ๊ฒŒ์‹œํŒ |
	<%if(userId==null){%>
	์ผ์ •๊ด€๋ฆฌ
	<%}else{ %>
	<a href="schedule.jsp">์ผ์ •๊ด€๋ฆฌ</a> 
	<%} %>
	|
	</td>
</tr>	

<tr height="400">
	<td width="200" valign="top">
	<%if(userId==null){%>
	<form action="login_ok.jsp" method="post" name="myForm">
		์•„์ด๋””: <input type="text" name="userId" size="10"/><br/>
		ํŒจ์Šค์›Œ๋“œ: <input type="password" name="userPwd" size="10"/><br/>
		<input type="button" value="๋กœ๊ทธ์ธ" onclick="sendIt();"/>
	</form>
		<%}else{ %>
		<b><%=userId %></b>๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”<br/>
		<a href="logout.jsp">๋กœ๊ทธ์•„์›ƒ</a>
		<%} %>
	</td>
	<td valign="middle" align="center">
	<%if(userId==null){%>
	<b>๋กœ๊ทธ์ธํ•˜๋ฉด ์ƒˆ๋กœ์šด ์„ธ์ƒ์ด ๋ณด์ž…๋‹ˆ๋‹ค. </b>
	<%}else{ %>
	
	<!-- ************************************ -->
	
<table align="center" width="210" cellpadding="2" cellspacing="1">
	<tr>
		<td align="center">
		<a href="login.jsp">
		<img alt = "today" src="../jsp3/image/today.png" align="left"/></a>
		<a href="login.jsp?year=<%=preYear %>&month=<%=preMonth %>">โ—€</a>
		<b>&nbsp;<%=year %>๋…„&nbsp;&nbsp;<%=month %>์›”&nbsp;</b>	
		<a href="login.jsp?year=<%=nextYear %>&month=<%=nextMonth %>">โ–ถ</a>
		</td>
	</tr>
</table>


<table align="center" width="210" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr>
	<td bgcolor="#e6e4e6" align="center"><font color="red">์ผ</font></td>
	<td bgcolor="#e6e4e6" align="center">์›”</td>
	<td bgcolor="#e6e4e6" align="center">ํ™”</td>
	<td bgcolor="#e6e4e6" align="center">์ˆ˜</td>
	<td bgcolor="#e6e4e6" align="center">๋ชฉ</td>
	<td bgcolor="#e6e4e6" align="center">๊ธˆ</td>
	<td bgcolor="#e6e4e6" align="center"><font color="blue">ํ† </font></td>
</tr>
 
<%
	int newLine = 0;
	out.print("<tr height='20'>");
	for(int i=1; i<week; i++){
		out.print("<td bgcolor='#ffffff'>&nbsp;</td>"); //<td> ๊ณต๋ฐฑ์„ ์œ„ํ•œ td </td>
		newLine++;
	}

	for(int i=startDay;i<=endDay; i++){
		
		//์ผ์š”์ผ์€ red, ํ† ์š”์ผ์€ blue, ํ‰์ผ์€ black, ์˜ค๋Š˜ color
		String fontColor = (newLine==0)?"red":(newLine==6)?"blue":"black";
		String bgColor = (nowYear==year)&&(nowMonth==month)&&(nowDay==i)?"#e6e4e6":"#ffffff"; 
		
		out.print("<td align='center' bgcolor='"+ bgColor + "'><font color='" + fontColor + "'>" + i +
				"</font></td>");
		//<td align='center' bgcolor='#e6e4e6'>
		//<font color ='black'>8(์ผ)</font></td>
		
		newLine++;
		
		
		if(newLine==7 && i != endDay){//0~6๊นŒ์ง€๊ฐ€ 1์ฃผ๋‹ˆ๊นŒ  
			out.print("</tr><tr height='20'>"); //</tr>๋กœ ์ค„๋ฐ”๊พธ๊ณ  , <tr> ๋‹ค์‹œ tr์—ด๊ธฐ
			newLine=0;
		}
	}

	
	
	while(newLine>0 && newLine<7){
		out.print("<td bgcolor = '#ffffff'>&nbsp;</td>");
		newLine++;
	}

	out.print("</tr>"); // 128line <tr>๋‹ซ์•„์ฃผ๊ธฐ
	
%>

</table>
	<%} %>
	</td>
</tr>
</table>


</body>
</html>

๋กœ๊ทธ์ธ ์ „

๋กœ๊ทธ์ธ ํ›„


login_ok.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String userId = request.getParameter("userId");
	String userPwd = request.getParameter("userPwd");
	
	if(userId.equals("suzi") && userPwd.equals("a123")){
		session.setAttribute("userId", userId); //"๋งŒ๋“  ๋ณ€์ˆ˜", ์ˆ˜์ง€๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋Š” 6๋ฒˆ line - userId
		
		response.sendRedirect("login.jsp");
		
	}
		
	
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์•„์ด๋””๋‚˜ ํŒจ์Šค์›Œ๋“œ๊ฐ€ ํ‹€๋ฆฝ๋‹ˆ๋‹ค<br/>
<a href="login.jsp">๋‹ค์‹œ ๋กœ๊ทธ์ธ!</a>

</body>
</html>

schedule.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String id = session.getId(); //์„ธ์…˜๋ชจ์Šต ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
	int sTime = session.getMaxInactiveInterval(); //์„ธ์…˜ ์‹œ๊ฐ„
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์ผ์ •๊ด€๋ฆฌ<br/>
์„ธ์…˜ ID: <%=id %><br/>
์„ธ์…˜ ์œ ํœด์‹œ๊ฐ„: <%=sTime %>์ดˆ<br/>

</body>
</html>

์„ธ์…˜ ๊ธฐ๊ฐ„ ์ •ํ•ด์ฃผ๊ณ ์‹ถ์œผ๋ฉด -> login.jsp์— ๊ฐ€์„œ : destroy๋ฅผ ์ง€๋‚˜๊ฒŒ ๋˜๋ฉด session์ด ์‚ญ์ œ๋จ

  • ์ž๋™๋กœ๊ทธ์•„์›ƒ๋จ

logout.jsp

  • logout์ด๋‚˜ login_ok๋Š” ๋ฐ‘์— html์•ˆ์จ๋„ ๋จ
<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	session.removeAttribute("userId");//suzi์‚ญ์ œ
	session.invalidate(); //userId ๋ณ€์ˆ˜ ์‚ญ์ œ - ์ด๊ฒƒ๋งŒ ์จ๋„๋จ ๊ทธ๋Ÿผ ์ž์—ฐ์Šค๋ ˆ ๋ณ€์ˆ˜๋„ ์‚ญ์ œ ๋จ
	
	//์„ธ์…˜ ๋‚ด์šฉ ๋ณ€๊ฒฝ๋์Œ -> redirect์‹œ์ผœ์•ผํ•จ (๋กœ๊ทธ์ธ์ฐฝ์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ๋”)
	response.sendRedirect("login.jsp");
%>

4.

โœ๏ธ Test4.

โ‘ก session์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”๋ฒ•
โ‘ข ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•
:idden์œผ๋กœ ์ˆจ๊ฒจ์„œ ๊ฐ€์ง€๊ณ ์˜ค๋Š” ๋ฒ•

  • ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ ์˜ค๋Š”๊ฑฐ getParameter
    : ๊ทธ๋ž˜์„œ hidden์œผ๋กœ ์ˆจ๊ฒจ์„œ ๋ฐ์ดํ„ฐ getParameter๋กœ 4๊ฐœ ๋‹ค ์ž˜ ๋„˜์–ด์˜ค๊ฒŒ ํ•˜๊ธฐ

ex1

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="ex2.jsp" method="post">
์ด๋ฆ„: <input type="text" name="userName" size="10"/><br/>
์ƒ์ผ: <input type="text" name="userBirth" size="10"/><br/>
<input type="submit" value="๋‹ค์Œ"/><br/>
</form>



</body>
</html>

ex2

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String userName = request.getParameter("userName");
	String userBirth = request.getParameter("userBirth");
	
	//ex1๊บผ๋ฅผ session์œผ๋กœ ex3์— ๋„์›€
	session.setAttribute("userName", userName);
	session.setAttribute("userBirth", userBirth);
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="ex3.jsp" method="post">
์•„์ด๋””: <input type="text" name="userId" size="10"/><br/>
ํŒจ์Šค์›Œ๋“œ: <input type="password" name="userPwd" size="10"/><br/>

<input type="hidden" name="userName" value="<%=userName%>"/>
<input type="hidden" name="userBirth" value="<%=userBirth%>"/>

<input type="submit" value="๊ฐ€์ž…"/><br/>
</form>

</body>
</html>

ex3

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	//String userName = (String)session.getAttribute("userName");
	//String userBirth = (String)session.getAttribute("userBirth");
	String userName = request.getParameter("userName"); // 2์—์„œ ๋ฐ›์•„์„œ ์ฃผ์„
	String userBirth = request.getParameter("userBirth");
	String userId = request.getParameter("userId");
	String userPwd = request.getParameter("userPwd");
%>

<!-- 
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	String userName = (String)session.getAttribute("UserName");
	String userBirth = (String)session.getAttribute("UserBirth");
	
	//๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
	//String userName = request.getParameter("userName");
	//String userBirth = request.getParameter("userBirth");
	String userId = request.getParameter("userId");
	String userPwd = request.getParameter("userPwd");
%> -->
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์ด๋ฆ„: <%=userName %></br/>
์ƒ์ผ: <%=userBirth %></br/>
์•„์ด๋””: <%=userId %></br/>
๋น„๋ฐ€๋ฒˆํ˜ธ: <%=userPwd %></br/>

</body>
</html>

๊ทธ๋ž˜์„œ ex1 -> ex2๋กœ ์ถ”๊ฐ€์ž‘์—… ์ง„ํ–‰ํ•จ


5.[์ด ์ ‘์†์ž ์ˆ˜] ๋ณด๊ธฐ

โœ๏ธ Test5.

๐Ÿ’ป ์ž…๋ ฅ

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String cp = request.getContextPath();
	
	int n = 0;
	
	String count = (String)application.getAttribute("count");
	
	if(count!=null){
		n = Integer.parseInt(count);
	}
	
	n++;
	
	application.setAttribute("count", Integer.toString(n));
	
	String realPath = application.getRealPath("/"); //์ œ์ผ ์ƒ๋‹จ์˜ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฒƒ
	application.log("์ ‘์†์‚ฌIP: "+request.getRemoteAddr());
	
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

์ด ์ ‘์†์ž ์ˆ˜ : <%=n %><br/>
์›น์„œ๋ฒ„ ์‹ค์ œ๊ฒฝ๋กœ: <%=realPath %>

</body>
</html>

0๊ฐœ์˜ ๋Œ“๊ธ€