๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JSP

[JSP] ๋‚ด์žฅ ๊ฐ์ฒด์™€ ์•ก์…˜ ํƒœ๊ทธ - ๋‚ด์žฅ ๊ฐ์ฒด

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 10.
๋ฐ˜์‘ํ˜•

โ–ถ ๋‚ด์žฅ ๊ฐ์ฒด

์›น ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณ ์ •๋œ ์ด๋ฆ„์˜ ๊ฐ์ฒด๋ฅผ JSP ๋‚ด์žฅ ๊ฐ์ฒด(Implicit Object)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

๋‚ด์žฅ ๊ฐ์ฒด ๋ณ€์ˆ˜๋ช…

ํด๋ž˜์Šค/์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…

์„ค๋ช…

request

javax.servlet.

ServletRequest(javax.servlet.http.HttpServletRequest)

ํด๋ผ์ด์–ธํŠธ์˜ HTTP ์š”์ฒญ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด(HTTP ํ—ค๋” ์ •๋ณด, ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ)

response

javax.servlet.

ServletResponse(javax.servlet.http.HttpServletResponse)

HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด

session

javax.servlet.http.HttpSession

ํด๋ผ์ด์–ธํŠธ์˜ ์„ธ์…˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด

pageContext

javax.servlet.jsp.PageContext

ํŽ˜์ด์ง€ ์‹คํ–‰์— ํ•„์š”ํ•œ ์ปจํ…์ŠคํŠธ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด

out

javax.servlet.jsp.JspWriter

์‘๋‹ต ํŽ˜์ด์ง€ ์ „์†ก์„ ์œ„ํ•œ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ๊ฐ์ฒด

application

javax.servlet.ServletContext

๋™์ผํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปจํ…์ŠคํŠธ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด

config

javax.servlet.ServletConfig

ํ•ด๋‹น ํŽ˜์ด์ง€์˜ ์„œ๋ธ”๋ฆฟ ์„ค์ • ์ •๋ณด(์ดˆ๊ธฐํ™” ์ •๋ณด)๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด

page

java.lang.Object(javax.servlet.jsp.HttpJspPage)

ํ•ด๋‹น ํŽ˜์ด์ง€ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)

exception

java.lang.Throwable

์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ์ฒด

โ€‹โ€‹

 

โ€‹

โ–ถ request ๊ฐ์ฒด

JSP ํŽ˜์ด์ง€์—์„œ๋Š” HttpServletRequest ๊ฐ์ฒด๋ฅผ request๋ž€ ์ด๋ฆ„์˜ ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

String

getParameter

(String name)

name์ด๋ž€ ์ด๋ฆ„์œผ๋กœ ์ง€์ •๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ• ๋‹น๋œ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์ง€์ •๋œ ์ด๋ฆ„์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์œผ๋ฉด null์„ ๋ฆฌํ„ดํ•œ๋‹ค.

String[ ]

getParameterValues

(String name)

name์ด๋ž€ ์ด๋ฆ„์œผ๋กœ ์ง€์ •๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๋ชจ๋“  ๊ฐ’์„ String ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ํ•˜๋‚˜์˜ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” checkbox์™€ ๊ฐ™์€ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ํ•˜๋‚˜์˜ ์ด๋ฆ„์— ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๊ฐ€์ง€๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” getParameter(String name) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Enumeration

getParameterNames( )

์š”์ฒญ์— ํฌํ•จ๋œ ๋ชจ๋“  ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ java.util.Enumeration ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

โ€‹

โ€‹

requestTest1_Form.jsp (์‹คํ–‰ ํŽ˜์ด์ง€)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test</title>
<style>
	h1, #commandCell {
   		text-align: center;
	}
	table {
   		margin: auto;
   		width: 400px;
   		border: 1px solid red;
	}
</style>
</head>
<body>
	<h1>Request ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.</h1>
	<form action="requestTest1.jsp" method="post">
		<table>
			<tr>
				<td><label for="name">์ด๋ฆ„</label></td>
				<td><input type="text" name="name" id="name"></td>
			</tr>
			<tr>
				<td><label for="gender">์„ฑ๋ณ„</label></td>
				<td>๋‚จ<input type="radio" name="gender" value="male" id="gender"> 
				         ์—ฌ<input type="radio" name="gender" value="female"></td>
			</tr>
			<tr>
				<td><label for="hobby">์ทจ๋ฏธ</label></td>
				<td>๋…์„œ<input type="checkbox" name="hobby" value="๋…์„œ" id="hobby">
				         ๊ฒŒ์ž„ <input type="checkbox" name="hobby" value="๊ฒŒ์ž„"> 
				    TV์‹œ์ฒญ <input type="checkbox" name="hobby" value="TV์‹œ์ฒญ">
				         ์ถ•๊ตฌ <input type="checkbox" name="hobby" value="์ถ•๊ตฌ">
				         ๊ธฐํƒ€ <input type="checkbox" name="hobby" value="๊ธฐํƒ€">
				</td>
			</tr>
			<tr>
				<td colspan="2" id="commandCell"><input type="submit" value="์ „์†ก"></td>
			</tr>	
		</table>
	</form>
</body>
</html>

 

 

requestTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test</title>
<style>
    h1 {
   		text-align: center;
	}
	table {
   		margin: auto;
   		width: 400px;
   		border: 1px solid red;
	}
</style>
</head>
<body>
<h1>Request ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.</h1>
<table>
	<tr>
		<td>์ด๋ฆ„</td>
		<td><%=request.getParameter("name") %></td>
	</tr>
	<tr>
		<td>์„ฑ๋ณ„</td>
		<td>
			<%if(request.getParameter("gender").equals("male")) {%>๋‚จ์ž
			<% } else {%>์—ฌ์ž<%} %>
			<%//=request.getParameter("gender").equals("male") ? "๋‚จ์ž" : "์—ฌ์ž" %>
		</td>
	</tr>
	<tr>
		<td>์ทจ๋ฏธ</td>
		<td>
			<%
			String[] hobby=request.getParameterValues("hobby");
			for(int i = 0; i < hobby.length; i++) {
			%>
			<%=hobby[i] %>&nbsp;&nbsp;
			<%} %>
		</td>
	</tr>
</table>
</body>
</html>

 

 

์ด๋ ‡๊ฒŒ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋กœ ๋งŒ๋“ค์–ด๋„๋จ!

<%=request.getParameter("gender").equals("male") ? "๋‚จ์ž" : "์—ฌ์ž" %>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

์‹ฌํ™” ) ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์„๋•Œ ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๊ฒŒ ํ•˜๋ ค๋ฉด?

โ€‹

โ€‹

requestTest1_Form.jsp (์‹คํ–‰ ํŽ˜์ด์ง€)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test</title>
<style>
	h1, #commandCell {
   		text-align: center;
	}
	table {
   		margin: auto;
   		width: 400px;
   		border: 1px solid red;
	}
</style>
<script>
	function checkName() {
		if(document.f.name.value.trim() == '') {
			alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.');
			document.f.name.focus(); // focus()๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ์ด๋ฆ„ ์œ„์น˜์— ํฌ์ปค์Šค๊ฐ€ ๊ฐ„๋‹ค.
			return false;
		}
		document.f.submit();
	}
	function checkName1(f1) { // ๊ฐ์ฒด ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋ฉด document๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด์„œ ํŽธํ•˜๋‹ค.
		
		if(f1.name.value == '') {
			alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.');
			f1.name.focus();
			return false;
		}
		f1.submit();
	}
</script>
</head>
<body>
	<h1>Request ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.</h1>
	<form action="requestTest1.jsp" method="post" name="f" onsubmit="return checkName();">
		<table>
			<tr>
				<td><label for="name">์ด๋ฆ„</label></td>
				<td><input type="text" name="name" id="name"></td>
			</tr>
			<tr>
				<td><label for="gender">์„ฑ๋ณ„</label></td>
				<td>๋‚จ<input type="radio" name="gender" value="male" id="gender"> 
				         ์—ฌ<input type="radio" name="gender" value="female"></td>
			</tr>
			<tr>
				<td><label for="hobby">์ทจ๋ฏธ</label></td>
				<td>๋…์„œ<input type="checkbox" name="hobby" value="๋…์„œ" id="hobby">
				         ๊ฒŒ์ž„ <input type="checkbox" name="hobby" value="๊ฒŒ์ž„"> 
				    TV์‹œ์ฒญ <input type="checkbox" name="hobby" value="TV์‹œ์ฒญ">
				         ์ถ•๊ตฌ <input type="checkbox" name="hobby" value="์ถ•๊ตฌ">
				         ๊ธฐํƒ€ <input type="checkbox" name="hobby" value="๊ธฐํƒ€">
				</td>
			</tr>
			<tr>
				<td colspan="2" id="commandCell"><input type="submit" value="์ „์†ก"></td>
			</tr>	
		</table>
	</form>
</body>
</html>

 

โ˜…์ฃผ์˜โ˜…

์ทจ๋ฏธ ์ฒดํฌ๋ฐ•์Šค์— ์•„๋ฌด๊ฒƒ๋„ ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

 

โ€‹

โ€‹

์ด๋Ÿด๋•Œ๋Š”

    <tr>
		<td>์ทจ๋ฏธ</td>
		<td>
			<%
			String[] hobby=request.getParameterValues("hobby");
			if(hobby!=null)
			for(int i = 0; i < hobby.length; i++) {
			%>
			<%=hobby[i] %>&nbsp;&nbsp;
			<%} %>
		</td>
	</tr>   

์ด ์กฐ๊ฑด๋ฌธ์„ ๋„ฃ์–ด์ค€๋‹ค.

 

 

requestTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test</title>
<style>
	h1 {
   		 text-align: center;
	}
	table {
  		 margin: auto;
   		width: 400px;
  		 border: 1px solid red;
	}
</style>
</head>
<body>
<h1>Request ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.</h1>
<table>
	<tr>
		<td>์ด๋ฆ„</td>
		<td><%=request.getParameter("name") %></td>
	</tr>
	<tr>
		<td>์„ฑ๋ณ„</td>
		<td>
			<%if(request.getParameter("gender").equals("male")) {%>๋‚จ์ž
			<% } else {%>์—ฌ์ž<%} %>
			<%//=request.getParameter("gender").equals("male") ? "๋‚จ์ž" : "์—ฌ์ž" %>
		</td>
	</tr>
	<tr>
		<td>์ทจ๋ฏธ</td>
		<td>
			<%
			String[] hobby=request.getParameterValues("hobby");
			if(hobby!=null)
			for(int i = 0; i < hobby.length; i++) {
			%>
			<%=hobby[i] %>&nbsp;&nbsp;
			<%} %>
		</td>
	</tr>
</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ HTTP ํ—ค๋” ์ •๋ณด์™€ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ๋“ค

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

String

getHeader

(String headerName)

HTTP ์š”์ฒญ ํ—ค๋”์— headerName์œผ๋กœ ์ง€์ •๋œ ์ด๋ฆ„์œผ๋กœ ํ• ๋‹น๋œ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. headerName์œผ๋กœ ์ง€์ •๋œ ์ด๋ฆ„์ด ์—†์„ ๊ฒฝ์šฐ null์„ ๋ฆฌํ„ดํ•œ๋‹ค.

Enumeration

getHeaders

(String headerName)

headerName์œผ๋กœ ์ง€์ •๋œ ์ด๋ฆ„์œผ๋กœ ํ• ๋‹น๋œ ๋ชจ๋“  ๊ฐ’์„ java.util.Enumeration ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

Enumeration

getHeaderNames( )

HTTP ์š”์ฒญ ํ—ค๋”์— ํฌํ•จ๋œ ๋ชจ๋“  ํ—ค๋” ์ด๋ฆ„์„ java.util.Enumeration ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

int

getIntHeader

(String headerName)

headerName ํ—ค๋”์˜ ๊ฐ’์„ int ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ์ง€์ •๋œ ํ—ค๋”๊ฐ’์„ int๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์„ ๊ฒฝ์šฐ์—๋Š” NumberFormatException์ด ๋ฐœ์ƒํ•˜๊ณ  headerName ํ—ค๋”๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.

โ€‹โ€‹

โ€‹

requestTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test2</title>
<style>
    h1 {
    	text-align: center;
	}
	table {
   		margin: auto;
   		width: 400px; 
   		border: 1px solid red;
	}
</style>
</head>
<body>
	<h1>ํ—ค๋”์ •๋ณด ์˜ˆ์ œ</h1>
	<table>
		<tr>
			<td>ํ—ค๋”์ด๋ฆ„</td>
			<td>ํ—ค๋”๊ฐ’</td>
		</tr>
		<%
		Enumeration e = request.getHeaderNames();
		while(e.hasMoreElements()) {
			String headerName = (String)e.nextElement();
		%>
		<tr>
			<td><%=headerName %></td>
			<td><%=request.getHeader(headerName) %></td>
		<%} %>
	</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

โ–ถ ์ฟ ํ‚ค, URL/URI, ์š”์ฒญ ๋ฐฉ์‹๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ๋“ค

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

Cookie[ ]

getCookies( )

HTTP ์š”์ฒญ ๋ฉ”์„ธ์ง€์˜ ํ—ค๋”์— ํฌํ•จ๋œ ์ฟ ํ‚ค๋ฅผ javax.servlet.http.Cookie ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getServerName( )

์„œ๋ฒ„์˜ ๋„๋ฉ”์ธ๋ช…์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

int

getServerPort( )

์„œ๋ฒ„์˜ ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ intํ˜•์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

StringBuffer

getRequestURL( )

์š”์ฒญ URL์„ StringBuffer๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getRequestURI( )

์š”์ฒญ URI๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getQueryString( )

์š”์ฒญ์— ์‚ฌ์šฉ๋œ ์ฟผ๋ฆฌ ๋ฌธ์žฅ์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getRemoteHost( )

ํด๋ผ์ด์–ธํŠธ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getRemoteAddr( )

ํด๋ผ์ด์–ธํŠธ์˜ IP ์ฃผ์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getProtocol( )

์š”์ฒญ์— ์‚ฌ์šฉ๋œ ํ”„๋กœํ† ์ฝœ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getMethod( )

์š”์ฒญ์— ์‚ฌ์šฉ๋œ ์š”์ฒญ ๋ฐฉ์‹(GET, POST ๋“ฑ)์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getContextPath( )

ํ•ด๋‹น JSP ํŽ˜์ด์ง€์˜ ์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

โ€‹

โ€‹โ€‹

requestTest3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request Test3</title>
<style>
	h1 {
   		 text-align: center;
	}
	table {
  		 margin: auto;
  		 width: 600px;
  		 border: 1px solid red;
	}
</style>
</head>
<body>
<h1>์ฟ ํ‚ค, URL/URI, ์š”์ฒญ ๋ฐฉ์‹์— ๊ด€๋ จ๋œ ์ •๋ณด ์˜ˆ์ œ</h1>
<table border="1">
	<tr>
		<td>์ฟ ํ‚ค ์ •๋ณด</td>
<%
Cookie[] cookie=request.getCookies();
if(cookie == null) {
%>
		<td>์ฟ ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค</td>
<%
} else {
	for(int i = 0; i < cookie.length; i++) {
        %>
        <td><%=cookie[i].getName() %>(<%=cookie[i].getValue() %>)&nbsp;&nbsp;</td>
        <%
    }
}
        %>
	</tr>
	<tr>
		<td>์„œ๋ฒ„ ๋„๋ฉ”์ธ๋ช…</td>
		<td><%=request.getServerName() %></td>
	<tr>
		<td>์„œ๋ฒ„ ํฌํŠธ๋ฒˆํ˜ธ</td>
		<td><%=request.getServerPort() %></td>
	<tr>
		<td>์š”์ฒญ URL</td>
		<td><%=request.getRequestURL() %></td>
	<tr>
		<td>์š”์ฒญ URI</td>
		<td><%=request.getRequestURI() %></td>
	<tr>
		<td>์š”์ฒญ ์ฟผ๋ฆฌ</td>
		<td><%=request.getQueryString() %></td>
	<tr>
		<td>ํด๋ผ์ด์–ธํŠธ ํ˜ธ์ŠคํŠธ๋ช…</td>
		<td><%=request.getRemoteHost() %></td>
	<tr>
		<td>ํด๋ผ์ด์–ธํŠธ IP ์ฃผ์†Œ</td>
		<td><%=request.getRemoteAddr() %></td>
	<tr>
		<td>ํ”„๋กœํ† ์ฝœ</td>
		<td><%=request.getProtocol() %></td>
	<tr>
		<td>์š”์ฒญ ๋ฐฉ์‹</td>
		<td><%=request.getMethod() %></td>
	<tr>
		<td>์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ</td>
		<td><%=request.getContextPath() %></td>
</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

โ–ถ response ๊ฐ์ฒด

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•œ HTTP ์‘๋‹ต(HTTP Response)์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

์—†์Œ

setHeader

(String headerName, String headerValue)

์‘๋‹ต์— ํฌํ•จ๋  ํ—ค๋” ์ •๋ณด์— headerName์˜ ์ด๋ฆ„์œผ๋กœ headerValue ๊ฐ’์„ ์„ค์ •ํ•ด ์ถ”๊ฐ€ํ•œ๋‹ค.

์—†์Œ

addCookie(Cookie cookie)

javax.servlet.http.Cookie ํƒ€์ž…์˜ ์ฟ ํ‚ค ๊ฐ์ฒด๋ฅผ ์‘๋‹ต ํ—ค๋”์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ฟ ํ‚ค์— ๋Œ€ํ•ด์„œ๋Š” Chapter8์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฌ๋‹ค.

์—†์Œ

sendRedirect(String url)

์ง€์ •๋œ URL๋กœ ์š”์ฒญ์„ ์žฌ์ „์†กํ•œ๋‹ค.

์—†์Œ

setContentType(String type)

์‘๋‹ต ํŽ˜์ด์ง€์˜ contentType์„ ์„ค์ •ํ•œ๋‹ค.

โ€‹

โ€‹

responseTest1.jsp (์‹คํ–‰ ํŽ˜์ด์ง€)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    response.sendRedirect("responseTest2.jsp");
%>

 

 

responseTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Response Test</title>
</head>
<body>
<h1>
ํ˜„์žฌ ํŽ˜์ด์ง€๋Š” responseTest1.jsp์— ์˜ํ•ด<br>
๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.
</h1>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

์‹ฌํ™” ) ๋‹ค๋ฅธ ํŽ˜์ด์ง€ ๋„์šฐ๊ธฐ

โ€‹

โ€‹

requestTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Response Test</title>
</head>
<body>
<h1>
ํ˜„์žฌ ํŽ˜์ด์ง€๋Š” responseTest1.jsp์— ์˜ํ•ด<br>
๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.
</h1>
<script>
	location.href="requestTest1_Form.jsp"
</script>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

์ด๋ ‡๊ฒŒ ๋‚˜์˜จ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ pageContext ๊ฐ์ฒด

JSP ํŽ˜์ด์ง€์™€ ๊ด€๋ จ๋œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹ค๋ฅธ ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ์–ป์–ด๋‚ด๊ฑฐ๋‚˜ ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์˜ ์ œ์–ด๊ถŒ์„ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

ServletRequest

getRequest( )

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

(request ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

ServletResponse

getResponse( )

์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

(response ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

JspWriter

getOut( )

์‘๋‹ต ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋ฆฌํ„ดํ•œ๋‹ค.(out ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

Object

getPage( )

์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.(page ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

ServletConfig

getServletConfig( )

์„œ๋ธ”๋ฆฟ์˜ ์ดˆ๊ธฐ ์„ค์ • ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

(config ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

ServletContext

getServletContext( )

์„œ๋ธ”๋ฆฟ์˜ ์‹คํ–‰ ํ™˜๊ฒฝ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

(application ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

HttpSession

getSession( )

ํด๋ผ์ด์–ธํŠธ์˜ ์„ธ์…˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

(session ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.)

์—†์Œ

forward(String url)

ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์— ๊ด€ํ•œ ์ œ์–ด๊ถŒ์„ URL๋กœ ์ง€์ •๋œ ์ฃผ์†Œ๋กœ ์˜๊ตฌ์ ์œผ๋กœ ๋„˜๊ธด๋‹ค. forward๋œ ํŽ˜์ด์ง€์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์‘๋‹ต๋„ ์ข…๋ฃŒ๋œ๋‹ค.

์—†์Œ

include(String url)

ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์— ๊ด€ํ•œ ์ œ์–ด๊ถŒ์„ URL๋กœ ์ง€์ •๋œ ์ฃผ์†Œ๋กœ ์ž„์‹œ๋กœ ๋„˜๊ธด๋‹ค. include๋œ ํŽ˜์ด์ง€์˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์ œ์–ด๊ถŒ์€ ๋‹ค์‹œ ์›๋ž˜์˜ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์˜จ๋‹ค. ๋”ฐ๋ผ์„œ include๋กœ ์ง€์ •๋œ ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ์›๋ž˜ ํŽ˜์ด์ง€์— ์‚ฝ์ž…ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ€์ง„๋‹ค.

โ€‹โ€‹

โ€‹

pageContextTest1.jsp (์‹คํ–‰ ํŽ˜์ด์ง€)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	pageContext.forward("pageContextTest2.jsp");
%>

 

 

pageContextTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageContext Test</title>
</head>
<body>
<%
	pageContext.include("pageContextTest3.jsp");
%>
<h2>pageContext์˜ forward ๋ฉ”์„œ๋“œ๋กœ ํฌ์›Œ๋”ฉ๋œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.</h2>
</body>
</html>

 

 

pageContextTest3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h3>include ๋˜๋Š” pageContextTest3.jsp ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.</h3>
<hr>

 

forward : ๊ถŒํ•œ์„ ์•„์˜ˆ ๋„˜๊ฒจ๋ฒ„๋ฆผ

include : ๊ถŒํ•œ์„ ์คฌ๋‹ค๊ฐ€ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ด

โ€‹

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

โ–ถ session ๊ฐ์ฒด

HTTP ํ”„๋กœํ† ์ฝœ์€ ์š”์ฒญ/์‘๋‹ต์˜ ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์–ด ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ „์†กํ•˜๊ณ  ๋‚˜๋ฉด ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง€๊ฒŒ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ •๋ณด๊ฐ€ ์œ ์ง€๋˜์–ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด ๊ฐ€์ƒ ์—ฐ๊ฒฐ์ด๋ผ๋Š” ๊ฐœ๋…์˜ ์„ธ์…˜์ด ๋“ฑ์žฅํ•˜์˜€๊ณ  JSP์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์„ธ์…˜์„ session ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด ์ค€๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

String

getId( )

ํ•ด๋‹น ์„ธ์…˜์˜ ์„ธ์…˜ ID๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ์„ธ์…˜ ID๋Š” session ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์— ์›น ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํ• ๋‹น๋œ๋‹ค.

long

getCreationTime( )

1970๋…„ 1์›” 1์ผ 00์‹œ 00๋ถ„ 00์ดˆ(epoch)๋ถ€ํ„ฐ ํ•ด๋‹น ์„ธ์…˜์ด ์ƒ์„ฑ๋œ ์ˆœ๊ฐ„๊นŒ์ง€์˜ ๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ ๋ฐ€๋ฆฌ์ดˆ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ longํ˜•์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

long

getLastAccessedTime( )

epoch๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์„ธ์…˜์— ๋งˆ์ง€๋ง‰์œผ๋กœ ์ ‘๊ทผ๋œ ์‹œ๊ฐ„๊นŒ์ง€์˜ ๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ ๋ฐ€๋ฆฌ์ดˆ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ longํ˜•์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

int

getMaxInactiveInterval( )

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ์—†์„ ์‹œ ์„œ๋ฒ„๊ฐ€ ํ•ด๋‹น ์„ธ์…˜์„ ์œ ์ง€ํ•˜๋„๋ก ์ง€์ •๋œ ์‹œ๊ฐ„์„ ์ดˆ ๋‹จ์œ„์˜ ์ •์ˆ˜๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

์—†์Œ

invalidate( )

์„ธ์…˜์˜ ์†์„ฑ ๊ฐ’์œผ๋กœ ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ๋ฐ˜๋‚ฉํ•˜์—ฌ ํ•ด๋‹น ์„ธ์…˜์„ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

boolean

isNew( )

์ƒˆ๋กœ์šด ์„ธ์…˜์ผ ๊ฒฝ์šฐ true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๊ธฐ์กด์— ์„ธ์…˜์ด ์œ ์ง€๋˜๊ณ  ์žˆ์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์—†์Œ

setMaxInactiveInterval

(int seconds)

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ์—†๋”๋ผ๋„ ์„ธ์…˜์„ ์œ ์ง€ํ•  ์‹œ๊ฐ„ ์ดˆ ๋‹จ์œ„์˜ ์ •์ˆ˜๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ์Œ์ˆ˜๋กœ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์„ธ์…˜์€ ๋ฌดํšจํ™”(invalidate)๋˜์ง€ ์•Š๋Š”๋‹ค.

์—†์Œ

forward(String url)

ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์— ๊ด€ํ•œ ์ œ์–ด๊ถŒ์„ URL๋กœ ์ง€์ •๋œ ์ฃผ์†Œ๋กœ ์˜๊ตฌ์ ์œผ๋กœ ๋„˜๊ธด๋‹ค. forward๋œ ํŽ˜์ด์ง€์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์‘๋‹ต๋„ ์ข…๋ฃŒ๋œ๋‹ค.

์—†์Œ

include(String url)

ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์— ๊ด€ํ•œ ์ œ์–ด๊ถŒ์„ URL๋กœ ์ง€์ •๋œ ์ฃผ์†Œ๋กœ ์ž„์‹œ๋กœ ๋„˜๊ธด๋‹ค. include๋œ ํŽ˜์ด์ง€์˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์ œ์–ด๊ถŒ์€ ๋‹ค์‹œ ์›๋ž˜์˜ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์˜จ๋‹ค. ๋”ฐ๋ผ์„œ include๋กœ ์ง€์ •๋œ ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ์›๋ž˜ ํŽ˜์ด์ง€์— ์‚ฝ์ž…ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ€์ง„๋‹ค.

โ€‹โ€‹

โ€‹

sessionTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	session.setMaxInactiveInterval(10);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session Test</title>
</head>
<body>
<h2>์„ธ์…˜ ํ…Œ์ŠคํŠธ</h2>
isNew() : <%=session.isNew() %><br>
์ƒ์„ฑ์‹œ๊ฐ„ : <%=session.getCreationTime() %><br>
์ตœ์ข… ์ ‘์† ์‹œ๊ฐ„ : <%=session.getLastAccessedTime() %><br>
์„ธ์…˜ ID : <%=session.getId() %><br>
</body>
</html>

4์ค„์— session.setMaxInactiveInterval(10); ์€ 10์ดˆ๊ฐ„ ์„ธ์…˜์ƒํƒœ๋ฅผ ์œ ์ง€ํ•œ๋‹ค๋Š” ๋œป

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ application ๊ฐ์ฒด (๊ฐœ๋ฐœ์ž๋“ค์€ ์ž˜ ์‚ฌ์šฉ ์•ˆํ•จ)

ํ•ด๋‹น ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹คํ–‰ ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฒ„์˜ ์ •๋ณด์™€ ์„œ๋ฒ„์ธก ์ž์›์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ด๊ฑฐ๋‚˜ ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ด๋ฒคํŠธ ๋กœ๊ทธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

application์€ ๊ฐ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋‹น ์˜ค์ง ํ•˜๋‚˜๋งŒ์˜ ๊ฐ์ฒด๋งŒ์ด ์ƒ์„ฑ๋˜๋ฏ€๋กœ ํ•ด๋‹น ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด ์˜์—ญ์—์„œ ์ž์›์„ ๊ณต์œ ํ•ด์•ผ ํ•  ๋•Œ(๋ฐฉ๋ฌธ์ž ์ˆ˜ ๋“ฑ์˜ ํ†ต๊ณ„๋ฅผ ๋‹ค๋ฃฐ ๋•Œ) ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

โ€‹

โ€‹

applicationTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Application Test</title>
</head>
<body>
<h2>application ํ…Œ์ŠคํŠธ</h2>
<table border="1">
	<tr>
		<td>JSP ๋ฒ„์ „</td>
		<td><%=application.getMajorVersion() %>.<%=application.getMinorVersion() %></td>
	</tr>
	<tr>
		<td>์ปจํ…Œ์ด๋„ˆ ์ •๋ณด</td>
		<td><%=application.getServerInfo() %></td>
	</tr>
	<tr>
		<td>์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹ค์ œ ํŒŒ์ผ์‹œ์Šคํ…œ ๊ฒฝ๋กœ</td>
		<td><%=application.getRealPath("/") %></td>
	</tr>
</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

โ€‹

โ€‹

โ–ถ out ๊ฐ์ฒด

out ๊ฐ์ฒด๋Š” ์„œ๋ธ”๋ฆฟ/JSP ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‘๋‹ต ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ๊ฐ์ฒด์ด๋‹ค.

๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ณ  ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด.

ํ‘œํ˜„์‹ ํƒœ๊ทธ์™€ out ๊ฐ์ฒด์˜ print( ) ๋ฉ”์„œ๋“œ๋Š” ๊ทธ ์ฒ˜๋ฆฌ ๊ณผ์ •์ด ์™„์ „ํžˆ ๋™์ผํ•˜๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

์—†์Œ

clear()

์ถœ๋ ฅ ๋ฒ„ํผ์— ์ €์žฅ๋œ ๋‚ด์šฉ์„ ๋ฒ„๋ฆฐ๋‹ค. ๋งŒ์ผ ์ด๋ฏธ ๋ฒ„ํผ๊ฐ€ ๋‹ค ์ฑ„์›Œ์ ธ์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†ก๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

์—†์Œ

clearBuffer()

์ถœ๋ ฅ ๋ฒ„ํผ์— ์ €์žฅ๋œ ๋‚ด์šฉ์„ ๋ฒ„๋ฆฐ๋‹ค. clear() ๋ฉ”์„œ๋“œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฒ„ํผ์— ๋‹ด๊ธด ๋‚ด์šฉ์ด ์ด๋ฏธ ์ „์†ก๋œ ์ดํ›„์—๋„ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ํ˜„์žฌ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฒ„ํผ๋งŒ์„ ๋ฒ„๋ฆฐ๋‹ค.

์—†์Œ

flush()

ํ˜„์žฌ ๋ฒ„ํผ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๊ณ  ๋ฒ„ํผ๋ฅผ ๋น„์šด๋‹ค.

์—†์Œ

close()

์ถœ๋ ฅ ๋ฒ„ํผ๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๊ณ  ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์ข…๋ฃŒํ•œ๋‹ค.

boolean

isAutoFlush()

page ์ง€์‹œ์–ด์˜ autoFlush ์†์„ฑ์œผ๋กœ ์ง€์ •๋œ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์ฆ‰ ์ถœ๋ ฅ ๋ฒ„ํผ๊ฐ€ ๋‹ค ์ฑ„์›Œ์กŒ์„ ๋•Œ ๋ฒ„ํผ ๋‚ด์šฉ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๋„๋ก ์ง€์ •๋˜์–ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , ์ถœ๋ ฅ ๋ฒ„ํผ๊ฐ€ ๋‹ค ์ฑ„์›Œ์กŒ์„ ๋•Œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ์ง€์ •๋˜์–ด ์žˆ์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

int

getBufferSize()

์ถœ๋ ฅ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์ •์ˆ˜ ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

int

getRemaining()

์ถœ๋ ฅ ๋ฒ„ํผ์˜ ๋‚จ์€ ์–‘์„ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์ •์ˆ˜ ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

์—†์Œ

print(String str)

์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์œผ๋กœ str ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

์—†์Œ

log

(String msg, java.lang.Throwable exception)

๋ฌธ์ž์—ด msg์™€ ์˜ˆ์™ธ์˜ StackTrace ์ •๋ณด๋ฅผ ๋กœ๊ทธ ํŒŒ์ผ์— ๊ธฐ๋กํ•œ๋‹ค.

โ€‹

โ€‹

outTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h2>1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์€
<%
	int sum = 0;
		for(int i = 0; i <= 100; i++) {
			sum += i;
		}
	out.print(sum + "์ž…๋‹ˆ๋‹ค.</h2>");
%>
</h2>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

outTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h2>1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์€
<%
	int sum = 0;
		for(int i = 0; i <= 100; i++) {
			sum += i;
		}
%>
<%=sum %>์ž…๋‹ˆ๋‹ค.</h2>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹โ€‹

โ€‹

outTest3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Out Test</title>
</head>
<body>
<h2>Out ํ…Œ์ŠคํŠธ</h2>
<table border="1">
	<tr>
		<td>autoFlush ์—ฌ๋ถ€</td>
		<td><%=out.isAutoFlush()%></td>
	</tr>
	<tr>
		<td>์ถœ๋ ฅ ๋ฒ„ํผ์˜ ํฌ๊ธฐ</td>
		<td><%=out.getBufferSize() %>๋ฐ”์ดํŠธ</td>
	</tr>
	<tr>
		<td>์ถœ๋ ฅ ๋ฒ„ํผ์˜ ๋‚จ์€ ์–‘</td>
		<td><%=out.getRemaining() %>๋ฐ”์ดํŠธ</td>
	</tr>
</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

โ–ถ config ๊ฐ์ฒด

config ๋‚ด์žฅ ๊ฐ์ฒด๋Š” JSP ํŽ˜์ด์ง€๊ฐ€ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜๋˜์–ด ์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ฐธ์กฐํ•ด์•ผ ํ•  ์ดˆ๊ธฐ ์„ค์ • ์ •๋ณด๋“ค์„ ์ €์žฅํ•ด ๋†“์€ ๊ฐ์ฒด์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ดˆ๊ธฐ ์„ค์ • ์ •๋ณด๋“ค์€ ์›น ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ตฌ๋™๋  ๋•Œ ๋‚ด๋ถ€์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ƒ์„ฑ/๊ด€๋ฆฌ๋˜๋ฉฐ ์„œ๋ธ”๋ฆฟ๋‹น 1๊ฐœ๋งŒ์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ ๊ฐ™์€ ์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค๋Š” ๋™์ผํ•œ config ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

String

getInitParameter
(String init_paramName)

์ปจํ…Œ์ด๋„ˆ์˜ ์„ค์ • ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ดˆ๊ธฐ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์ค‘ init_paramName์˜ ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
init_paramName์˜ ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ null์„ ๋ฆฌํ„ดํ•œ๋‹ค.

Enumeration

getInitParameterNames()

์ปจํ…Œ์ด๋„ˆ์˜ ์„ค์ • ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ดˆ๊ธฐ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ Enumeration ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

String

getServletName()

ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์˜ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

 

โ€‹

web.xml

<web-app>
	<display-name>chapter5</display-name>
	<servlet>
		<servlet-name>configTest1</servlet-name>
		<jsp-file>/configTest1.jsp</jsp-file>
		<init-param>
			<param-name>fileDir</param-name>
			<param-value>C:/test</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>configTest1</servlet-name>
		<url-pattern>/configTest1.jsp</url-pattern>
	</servlet-mapping>
</web-app>

 

 

configTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Config Test</title>
</head>
<body>
<h2>config ํ…Œ์ŠคํŠธ</h2>
<table border="1">
	<tr>
		<td>์ดˆ๊ธฐ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„</td>
		<td>์ดˆ๊ธฐ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’</td>
	</tr>
<%
	Enumeration e=config.getInitParameterNames();
		while(e.hasMoreElements()) {
			String init_paramName=(String)e.nextElement();
	%>
	<tr>
		<td><%=init_paramName %></td>
		<td><%=config.getInitParameter(init_paramName) %></td>
	</tr>
	<%  }  %>
</table>
</body>
</html>

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

โ–ถ page ๊ฐ์ฒด

page ๊ฐ์ฒด๋Š” JSP ํŽ˜์ด์ง€์— ์˜ํ•ด ์ƒ์„ฑ๋˜๋Š” ์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค ์ž์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด์ด๋‹ค. page ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๋‚ฎ๋‹ค. ๋‹ค๋งŒ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋ฐ” ์ด์™ธ์˜ ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ฅผ ์ง€์›ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•œ ์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์—์„œ page ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜๋„ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

 

 

โ–ถ exception ๊ฐ์ฒด

exception ๊ฐ์ฒด๋Š” JSP ํŽ˜์ด์ง€์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ ๊ทธ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์—๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ฒฝ์šฐ์— ํ•ด๋‹น ์—๋Ÿฌ ํŽ˜์ด์ง€์— ์ „๋‹ฌ๋˜๋Š” ์˜ˆ์™ธ ๊ฐ์ฒด์ด๋‹ค. exception ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ํŽ˜์ด์ง€๊ฐ€ ์—๋Ÿฌ ํŽ˜์ด์ง€๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ๋งŒ ํ•œ๋‹ค. ์ฆ‰ page ์ง€์‹œ์–ด์— ์˜ํ•ด isErrorPage ์†์„ฑ์ด true๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ inErrorPage ์†์„ฑ์˜ ๊ธฐ๋ณธ๊ฐ’์€ false์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ true๋กœ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ์—๋Ÿฌ ํŽ˜์ด์ง€๋กœ ์ง€์ •๋œ๋‹ค. exception ๊ฐ์ฒด๋Š” java.lang.Throwable ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

String

getMessage()

์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

์—†์Œ

printStackTrace()

ํ•ด๋‹น ์—๋Ÿฌ์˜ StackTrace ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์—†์Œ

printStackTrace
(PrintWriterout)

ํ•ด๋‹น ์—๋Ÿฌ์˜ StackTrace ์ •๋ณด๋ฅผ PrintWriter ๊ฐ์ฒด out์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์—†์Œ

printStackTrace
(PrintStreamout)

ํ•ด๋‹น ์—๋Ÿฌ์˜ StackTrace ์ •๋ณด๋ฅผ PrintStream ๊ฐ์ฒด out์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€