βΆ μμ κ°μ²΄μ μμ±
곡μ λλ λ°μ΄ν°λ₯Ό μμ±(Attribute)μ΄λΌκ³ νλ©° μμ±μ 곡μ ν μ μλ μ ν¨ λ²μλ₯Ό μμ(Scope)μ΄λΌκ³ νλ€.
request κ°μ²΄λ ν΄λΉ μΉ μ ν리μΌμ΄μ μ΄ μ€νλκ³ μλ λμμ μμ±μ μ¬μ©ν μ μλ€. μ΄λ κ² ν΄λΉ μμμμ μμ±μ μ¬μ©ν μ μλ λ΄μ₯ κ°μ²΄λ€μ μμ κ°μ²΄λΌκ³ λΆλ₯Έλ€.
μμ |
μμ κ°μ²΄ |
μμ±μ μ ν¨ λ²μ |
page |
pageContext |
ν΄λΉ νμ΄μ§κ° ν΄λΌμ΄μΈνΈμ μλΉμ€λ₯Ό μ 곡νλ λμμλ§ μ ν¨ (μλΈλ¦Ώ μΈμ€ν΄μ€μ _jspServicer() λ©μλκ° μ€νλλ λμμλ§ μ ν¨ |
request |
request |
ν΄λΌμ΄μΈνΈμ μμ²μ΄ μ²λ¦¬λλ λμ μ ν¨ (ν¬μλ© λλ includeλ₯Ό μ΄μ©νλ κ²½μ° μ¬λ¬ κ°μ νμ΄μ§μμλ μμ² μ λ³΄κ° κ³μ μ μ§λλ―λ‘ request μμμ μμ±μ μ¬λ¬ νμ΄μ§μμ 곡μ ν μ μλ€.) |
session |
session |
μΈμ μ΄ μ μ§λλ λμ μ ν¨ (νλμ λΈλΌμ°μ (ν΄λΌμ΄μΈνΈ)μ 1κ°μ μΈμ μ΄ μμ±λλ―λ‘ κ°μ μΉλΈλΌμ°μ λ΄μμ μ€νλλ νμ΄μ§λ€μ΄ μμ±μ 곡μ ν μ μλ€.) |
application |
application |
μΉ μ ν리μΌμ΄μ μ΄ μ€νλκ³ μλ λμ μ ν¨ (μΉ μ»¨ν μ΄λμμ ν΄λΉ μ ν리μΌμ΄μ μ μ€μ§ νλλ§μ΄ μ€νλλ―λ‘ 4κ°μ§ μμ μ€ κ°μ₯ ν° μμμ ν΄λΉνλ€.) μΉ μ»¨ν μ΄λλ₯Ό μ’ λ£νλ©΄ μ ν리μΌμ΄μ μμ κ°μ²΄κ° μλ©Έλλ€. |
β
β
βΆ μμ±κ³Ό κ΄λ ¨λ λ©μλλ€
λ¦¬ν΄ νμ |
λ©μλλͺ |
ν΄μ€ |
Object |
getAttribute(String key) |
key κ°μΌλ‘ λ±λ‘λμ΄ μλ μμ±μ Object νμ μΌλ‘ λ¦¬ν΄ (key κ°μ ν΄λΉνλ μμ±μ΄ μμ κ²½μ° nullμ 리ν΄) |
Enumeration |
getAttributeNames() |
ν΄λΉ μμμ λ±λ‘λμ΄ μλ λͺ¨λ μμ±λ€μ μ΄λ¦μ Enumeration νμ μΌλ‘ λ¦¬ν΄ |
μμ |
setAttribute (String key, Object obj) |
ν΄λΉ μμμ key κ°μ μ΄λ¦μΌλ‘ obj κ°μ²΄λ₯Ό λ±λ‘ |
μμ |
removeAttribute(String key) |
key κ°μΌλ‘ λ±λ‘λμ΄ μλ μμ±μ μ κ±° |
β
β
attributeTest1_Form.jsp (μ²μ μμ νμ΄μ§)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Attribute Test Form</title>
</head>
<body>
<h2>μμκ³Ό μμ± ν
μ€νΈ</h2>
<form action="attributeTest1.jsp" method="post">
<table border="1">
<tr><td colspan="2">Application μμμ μ μ₯ν λ΄μ©λ€</td></tr>
<tr>
<td>μ΄λ¦</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>μμ΄λ</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="μ μ‘"></td>
</tr>
</table>
</form>
</body>
</html>
<μ€ν κ²°κ³Ό>
β
β
attributeTest1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Attribute Test</title>
</head>
<body>
<h2>μμκ³Ό μμ± ν
μ€νΈ</h2>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String id=request.getParameter("id");
if(name != null && id != null) {
application.setAttribute("name", name);
application.setAttribute("id", id);
}
%>
<h3><%=name %>λ λ°κ°μ΅λλ€.<br><%=name %>λμ μμ΄λλ <%=id %>μ
λλ€.</h3>
<form action="attributeTest2.jsp" method="post">
<table border="1">
<tr><td colspan="2">Session μμμ μ μ₯ν λ΄μ©λ€</td></tr>
<tr>
<td>e-mail μ£Όμ</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>μ§μ£Όμ</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td>μ νλ²νΈ</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="μ μ‘"></td>
</tr>
</table>
</form>
</body>
</html>
<μ€ν κ²°κ³Ό>
ββ
β
attributeTest2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Attribute Test</title>
</head>
<body>
<h2>μμκ³Ό μμ± ν
μ€νΈ</h2>
<%
request.setCharacterEncoding("UTF-8");
String email=request.getParameter("email");
String address=request.getParameter("address");
String tel=request.getParameter("tel");
session.setAttribute("email", email);
session.setAttribute("address", address);
session.setAttribute("tel", tel);
String name=(String)application.getAttribute("name");
%>
<h3><%=name %>λμ μ λ³΄κ° λͺ¨λ μ μ₯λμμ΅λλ€.</h3>
<a href="attributeTest3.jsp">νμΈνλ¬ κ°κΈ°</a>
</body>
</html>
<μ€ν κ²°κ³Ό>
β
attributeTest3.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>Attribute Test</title>
</head>
<body>
<h2>μμκ³Ό μμ± ν
μ€νΈ</h2>
<table border="1">
<tr><td colspan="2">Application μμμ μ μ₯λ λ΄μ©λ€</td></tr>
<tr>
<td>μ΄λ¦</td>
<td><%=application.getAttribute("name") %></td>
</tr>
<tr>
<td>μμ΄λ</td>
<td><%=application.getAttribute("id") %></td>
</tr>
</table>
<br>
<table border="1">
<tr><td colspan="2">Session μμμ μ μ₯λ λ΄μ©λ€</td></tr>
<%
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements()) {
String attributeName=(String)e.nextElement();
String attributeValue=(String)session.getAttribute(attributeName);
%>
<tr>
<td><%=attributeName %></td>
<td><%=attributeValue %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
<μ€ν κ²°κ³Ό>
β
attributeTest4.jsp
<%@ 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>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>
pageValue = <%=pageContext.getAttribute("pageScope") %><br>
requestValue = <%=request.getAttribute("requestScope") %>
</body>
</html>
<μ€ν κ²°κ³Ό>
β
β
attributeTest5.jsp
<%@ 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>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>
<jsp:forward page="attributeTest5Result.jsp"></jsp:forward>
</body>
</html>
-forward μ‘μ νκ·Έλ‘ μμ²μ λ€λ₯Έ νμ΄μ§λ‘ ν¬μλ©νμ κ²½μ°μλ requestλ₯Ό 곡μ νλ€.
β
β
attributeTest5Result.jsp
<%@ 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>
pageValue = <%=pageContext.getAttribute("pageScope") %><br>
requestValue = <%=request.getAttribute("requestScope") %>
</body>
</html>
<μ€ν κ²°κ³Ό>
'JSP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JSP] μ‘μ νκ·Έλ₯Ό νμ©ν ν νλ¦Ώ νμ΄μ§ μμ± (0) | 2021.02.10 |
---|---|
[JSP] μ‘μ νκ·Έ (0) | 2021.02.10 |
[JSP] λ΄μ₯ κ°μ²΄μ μ‘μ νκ·Έ - λ΄μ₯ κ°μ²΄ (0) | 2021.02.10 |
[JSP] μ€ν¬λ¦½νΈ μμ (0) | 2021.02.09 |
[JSP] μ§μμ΄ (Directive) (0) | 2021.02.09 |
λκΈ