Notice
Recent Posts
Recent Comments
Link
μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
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 |
Tags
- Kλ°°ν°λ¦¬
- μΉνμ΄μ§λ§λ€κΈ°
- μ€λΌν΄
- μ€λΈμ
- κ°λ°
- css
- 리μ‘νΈ
- ComputerScience
- νμ΄μ¬
- Kλ°°ν°λ¦¬λ 볼루μ
- μλ°μ€ν¬λ¦½νΈ
- Java
- μ±
- database
- λ§μΌλ΄κ°μΈμμλ€μμ°λ€λ©΄
- μ»΄ν¨ν°κ³Όν
- html
- λΌνλΌμ€μλ§λ
- JavaScript
- μΉνΌλΈλ¦¬μ±
- ν°μ€ν 리μ±λ¦°μ§
- κΉλ―Έκ²½μλ§νμμ
- μ½λ©
- μν
- Python
- μλ°
- λ μ
- λ°μ΄ν°λ² μ΄μ€
- λ°μν
- νλ‘κ·Έλλ°
Archives
- Today
- Total
JiYoung Dev π₯
[JSP/Servlet] Scope (2023.05.18~19) λ³Έλ¬Έ
Scope
(μμ)
λ³μκ° μ ν¨μ±μ κ°λ μμ!
π Scope μ’ λ₯
β Page Scope
- κ° μ μ₯ : setAttribute
- κ° κ°μ Έμ€κΈ° : getAttribute
μλ° λ³μμ λ€λ₯Έμ : EL νκΈ°λ²μ μ¬μ©ν μ μλ€!
<%@ 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>
<%
//page μμ(scope)μ μ μ₯λ κ°μ ν΄λΉ νμ΄μ§ μμμλ§ μ§μλ³μμ²λΌ μ¬μ© κ°λ₯
//page μμμ μ μ₯λ κ° >> el(express language)νκΈ°λ²μΌλ‘ μ¬μ© κ°λ₯
// : μλ° λ³μλ‘ μ μΈλ κ°μ μ¬μ© λΆκ°λ₯
//setAttribute(name(String),value(Object))
pageContext.setAttribute("nickname", "νμ€ν");
String nick = (String)pageContext.getAttribute("nickname");
%>
<%=nick %><br>
${nickname} <!-- page μμ μμ μ μ₯λ nameκ° μ§μ ! -->
${nick} <!-- μλ° λ³μλ μ¬μ© λΆκ°λ₯ -->
${pageScope.nickname } <!-- nicknameμ΄ μΈμ
μλ μ¬μ©λλ λ±μ μ€λ³΅ μ¬μ©λλ κ²½μ° μ¬μ© -->
<a href="Ex01page2.jsp">2λ‘ μ΄λ</a>
</body>
</html>
β Request Scope
νμ΄μ§ μμμμ μ μ₯λ κ°μ μ λλ‘ λ€λ₯Έ νμ΄μ§λ‘ λκ²¨μ€ μ μμ!
forwardλ ν λ²λ§ λ€λ₯Έ νμ΄μ§μ μ 보λ₯Ό λκ²¨μ€ μ μμ.
μ΄ν νμ΄μ§μ κ³μ λ°μ΄ν°λ₯Ό λ겨주기 μν΄μλ forwardλ₯Ό κ³μ μ¬μ©ν΄μ€μΌ ν¨. -> sessionμ μ¬μ©νλ μ΄μ .
<%@ 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>
<%
request.setAttribute("nickname", "νμ€ν");
String nick = (String)request.getAttribute("nickname");
//pageContext.setAttribute("nickname", "νμ€ν22");
//String nick1 = (String)pageContext.getAttribute("nickname");
%>
<%=nick %><br>
${nickname } <!-- 2κ° μ΄μμ μμμμ κ°μ name κ°μ μ¬μ©νλ©΄ λ μ’μ μμμ κ°μ κ°μ Έμ΄ -->
${requestScope.nickname }
<a href="Ex02request2.jsp">2λ‘ μ΄λ</a>
</body>
</html>
forward μ¬μ©λ²
1) RequestDispatcher κ°μ²΄ μμ± ν forward() λ©μλ μ¬μ©
2) pageContext.forward()
3) <jsp: forward page="">
<%@ 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>
<%
request.setAttribute("nickname", "νμ€ν");
String nick = (String)request.getAttribute("nickname");
//ν¬μλ© λ°©μ μ΄λ
//1. RequestDispathcer νμ©
//RequestDispatcher rd = request.getRequestDispatcher("Ex02request2.jsp");
//rd.forward(request, response);
//servlet, jsp λͺ¨λ μ¬μ© κ°λ₯
//2. pageContext νμ©(νμ΄μ§μ νλ¦ μ μ΄)
//pageContext.forward("Ex02request2.jsp");
//jsp μ¬μ© κ°λ₯
%>
<!-- 3. μ‘μ
νκ·Έ νμ© : JSPμμ μ 곡νκ³ μλ 컀μ€ν
νκ·Έ,
μλ²-ν΄λΌμ΄μΈνΈ κ°μ μ΄λ€ νλμ μ·¨νλλ‘ λͺ
λ Ήνλ νκ·Έ
jspμμλ§ μ¬μ© κ°λ₯ -->
<jsp:forward page="Ex02request2.jsp"/>
</body>
</html>
β Session Scope
remove, invalidate, λΈλΌμ°μ λκΈ° μ κΉμ§ μ¬μ© κ°λ₯ν¨
<%@ 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>
<%
session.setAttribute("nickname", "νμ€ν");
String nick = (String)session.getAttribute("nickname");
%>
<%=nick %><br>
${nickname }
<a href="Ex03Session2.jsp">2λ‘ μ΄λ</a>
</body>
</html>
β Application Scope
μλ²κ° μμλκ³ μ’ λ£λ λκΉμ§λ κ³μ 곡μ κ°λ₯
<%@ 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>
<%
application.setAttribute("nickname", "νμ€ν");
String nick = (String)application.getAttribute("nickname");
%>
<%=nick %><br>
${nickname }
${applicationScope.nickname }
<a href="Ex04application2.jsp">2λ‘ μ΄λ</a>
</body>
</html>
'full stack > Back-End' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JSP/Servlet] JSP λ΄μ₯κ°μ²΄(HttpServletResponse) & μΏ ν€(Cookie) (2023.05.17) (0) | 2023.05.19 |
---|---|
[JSP/Servlet] μΈμ (session) (2023.05.18) (0) | 2023.05.19 |
[JSP/Servlet] μ μΈλ¬Έ, μ§μμ, λ΄μ₯κ°μ²΄ (2023.05.16) (0) | 2023.05.16 |
[JSP/Servlet] JSP νμμ±κ³Ό νΉμ§, ꡬμ±μμ (2023.05.15) (0) | 2023.05.15 |
[JSP/Servlet] λ°μ΄ν° μ μ‘ GET/POST(2023.05.12) (0) | 2023.05.12 |