[Day5] JSP/Servlet 5 [12/8]
p 132 JSP κΈ°λ³Έ κ°μ²΄μ 4κ°μ§μ μμ( scope )
1) ν νμ΄μ§ μ¬μ©λλ μμ : page scope(μμ) - page[Context] κ°μ²΄
ex01.html -> page scope
2) νλμ μμ²μ μ¬μ©λλ μμ : request scope - request κ°μ²΄
ex01.html -> r ν¬μλ© r ex01.jsp
<-
3) λΈλΌμ°μ ex01.02.03. μ¬λ¬ λ² μμ² : session scope - session κ°μ²΄ (λΈλΌμ°μ λ«κΈ°μ μ κ³μ μ¬μ©κ°λ₯ 곡κ°)
4) λͺ¨λ ν΄λΌμ΄μΈνΈ(λΈλΌμ°μ ) κ° κ³΅μ ν΄μ μ¬μ©νλ μμ : application μμ - application κ°μ²΄
(λΈλΌμ°μ μκ΄μμ΄ λͺ¨λ μ¬λ μμ²ν΄μ 곡μ νλ μμ)
κ·Έλ¦Ό 5.11 λ€ κ°μ§ μμκ³Ό κΈ°λ³Έ κ°μ²΄μμ κ΄κ³.
p 135 μμ± λ©μλ
setAttribute();
getAttribute();
getAttributeNames()
removeAttribute( name )
-ex01_02.jsp-
<%
// 4κ°μ§ μμ(scope) κ°κ° κ°μ μ μ₯.
application.setAttribute("name", "admin");
session.setAttribute("age", 20);
request.setAttribute("addr", "seoul");
pageContext.setAttribute("tel", "010-");
%>
a name : <%= application.getAttribute("name") %><br>
s age : <%= session.getAttribute("age") %><br>
r addr : <%= request.getAttribute("addr") %><br>
p tel : <%= pageContext.getAttribute("tel" ) %><br>
<br>
<a href="ex01_03.jsp">ex01_03.jsp</a>
a name , s age O
r addr , p tel X
* μλμ΄
-ex01_03.jsp-
a name : <%= application.getAttribute("name") %><br>
s age : <%= session.getAttribute("age") %><br>
r addr : <%= request.getAttribute("addr") %><br>
p tel : <%= pageContext.getAttribute("tel" ) %><br>
* μ΄ν리μΌμ΄μ & μΈμ : λμ΄
* 리νμ€νΈ & νμ΄μ§μ»¨ν μ€νΈ : μλμ΄
-ex01_04.jsp-
a name : <%= application.getAttribute("name") %><br>
<!--
μΈμ
μ κ°μ λ‘ μ’
λ£νμ§ μλ λμ
μ μν μ¬μ©μλ§ κ³μ μ¬μ©ν μ μλ μμμ΄ μΈμ
μ΄λΌλ μμμ΄κ³
μΈμ¦, κΆν, μ₯λ°κ΅¬λ λ±λ± ꡬν
-->
s age : <%= session.getAttribute("age") %><br>
r addr : <%= request.getAttribute("addr") %><br>
p tel : <%= pageContext.getAttribute("tel" ) %><br>
* ν¬λ‘¬ λ€ λ«κ³ λ€μ λμ°λ©΄ -> μ΄ν리μΌμ΄μ λ§ μ΄μμμ (μλ²κ° μ΄μμλ ν)
* μλ² ν΄λ¦°νκ³ λ€μ μμνλ©΄ μμ
*** request, session -> λ§μ΄μ / application μ’ μ
- MVCμμ request
- λ‘κ·ΈμΈ,λ‘κ·Έμμ session
p139 Chapter 06 μλ¬ μ²λ¦¬
1. μμΈ μ²λ¦¬
2. μλ¬ νμ΄μ§
3. μν μ½λ(404, 500)μ μμΈ νμ λ³ μλ¬ νμ΄μ§ μ§μ
name νλ¦¬λ―Έν° κ° :
<%
try{
%>
<%= request.getParameter("name").toUpperCase() %>
<%
}catch(Exception e ){
%>
name νλΌλ―Έν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.
<%
}
%>
* 500 : μλ°μλ¬
* μμΈ λ°μν λ
μμ κ°μ΄ μΌλΆλ§ try~catch λ¬ΈμΌλ‘ μμΈ μ²λ¦¬ν κ²μ΄ μλλΌ.
μμΈ μ²λ¦¬ νμ΄μ§( μλ¬ νμ΄μ§ )λ‘ μ΄λ...!
p 143 μλ¬ νμ΄μ§ μ§μ νκΈ°.
ex02_02.jsp
ex02_02.jsp νμ΄μ§μμ λ§μ½μ μλ¬κ° λ°μνλ©΄
errorPage μμ±μ μ§μ λ νμ΄μ§λ‘ μ΄λ...νλ€.
viewErrorMessage.jsp νμΌμ WEB-INF ν΄λ > jsp ν΄λ μΌλ‘ μ΄λ.
γ΄μλ¬νμ΄μ§λ₯Ό μΈλΆμμ μ κ·Ό X
<%@ page errorPage="/error/viewErrorMessage.jsp"%>
<%@ page errorPage="/WEB-INF/jsp/viewErrorMessage.jsp"%>
p155 Chapter 07 νμ΄μ§ λͺ¨λνμ μμ² νλ¦ μ μ΄
jsp: μ λμ¬ == JSP μ‘μ νκ·Έ
*** 1. jsp:include μ‘μ νκ·Έ -> λͺ¨λν
*** 2. include μ§μμ -> λͺ¨λν
3. jsp:forward μ‘μ νκ·Έ
4. κΈ°λ³Έ κ°μ²΄λ₯Ό μ΄μ©ν κ° μ λ¬
[ νμ΄μ§ λͺ¨λν ]
μΉ μ¬μ΄νΈμ λͺ¨λ νμ΄μ§μ 곡ν΅λΆλΆ -> λͺ¨λν ( jsp )
( μλ¨ λΆλΆ, μΌμͺ½λ©λ΄λΆλΆ, νλ¨λΆλΆ )
μ½λ μ€λ³΅ μ κ±° X, μ μ§ λ³΄μ μ©μ΄
jsp:include μ‘μ νκ·Έλ₯Ό μ¬μ©ν΄μ λͺ¨λνν JSP νμ΄μ§λ₯Ό ν¬ν¨μν¨λ€.
μ€μ) main.jsp
μ²λ¦¬ A
μΆλ ₯λ²νΌ (A+B+C)
jsp:include sub.jsp -> μμ² μ΄λ sub.jsp B
μ²λ¦¬ C
* sub.jsp include μ‘μ νκ·Έ μν΄μ μμ² νλ¦μ΄ sub.jsp ν¬μλ©λλ κ²μ²λΌ / μ΄μ κΉμ§λ μΆλ ₯λ²νΌμ μ μ₯λμ΄μλ μν
* μΆλ ₯λ²νΌμ λͺ¨λμ΄ μΆκ°
* μμ²νλ¦μ΄ λ°λλ€λ κ²μ΄ ν¬μΈνΈμ!
μ)
1) κ³΅ν΅ λΆλΆμ λͺ¨λν top.jsp, left.jsp, bottom.jsp
webapp/layout/ top.jsp, bottom.jsp λͺ¨λ νμ΄μ§μ 곡ν΅λΆλΆ
webapp/days05/layout/ left.jsp, top.jsp λͺ¨λ νμ΄μ§μ 곡ν΅λΆλΆ
ex03_template.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%!
// jsp:include top.jsp μΈμν μ μλ€.
// μ ? μ²λ¦¬ κ³Όμ μ΄ν΄ κ·Έλ¦Ό7.1 ( p 156 )
String id="admin";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. μ€μ 11:53:14</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex03_template.jsp</h3>
<h3>ex03_template.jsp?msg=hello</h3>
<div style="text-align: center">
<table width="600" border="1" align="center">
<tr>
<td colspan="3">
<!-- top λΆλΆ -->
<jsp:include page="/layout/top.jsp" flush="false"></jsp:include>
</td>
</tr>
<tr height="300">
<td width="150" valign="top" style="background: yellow">
<!-- left λΆλΆ -->
<jsp:include page="/days05/layout/left.jsp" flush="false"></jsp:include>
</td>
<td>
<a href="ex03_memberAdd.jsp">νμ μΆκ°</a><br>
νμ κ΄λ¦¬ Content λΆλΆ<br>
νμ κ΄λ¦¬ Content λΆλΆ<br>
νμ κ΄λ¦¬ Content λΆλΆ<br>
νμ κ΄λ¦¬ Content λΆλΆ<br>
νμ κ΄λ¦¬ Content λΆλΆ<br>
</td>
<td valign="top" style="background: red">
<!-- right λΆλΆ -->
<jsp:include page="/days05/layout/right.jsp" flush="false"></jsp:include>
</td>
</tr>
<tr>
<td colspan="3">
<!-- bottom λΆλΆ -->
<!--
ex03_template.jsp -> bottom.jsp μμ² νλ¦
νλΌλ―Έν° κ°μ μ λ¬νκ³ μΆλ€.
-->
<jsp:include page="/layout/bottom.jsp" flush="false">
<jsp:param value="hi~~~" name="msg"/>
</jsp:include>
</td>
</tr>
</table>
*flush μμ± : μλ΅λ²νΌμ λ£κ³ μ μ‘μν¬κ±΄μ§? false
*top bottom (style css) -> mainμμ μ£Όλ©΄λ¨,
* js -> μ΄λμμ΄λ μκ΄μμ
-left-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<ul>
<li>λ‘κ·ΈμΈ</li>
<li>νμκ΄λ¦¬</li>
<li>μΌμ κ΄λ¦¬</li>
<li>λμλ§</li>
</ul>
-right-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
κ΄κ³ 1<br>
κ΄κ³ 2<br>
κ΄κ³ 3<br>
κ΄κ³ 4<br>
*template μμ²ν λ λ겨μ μ€λ νλΌλ―Έν°κ°μ bottom μμ λμ°κ³ μΆμλ (쿼리문μΌλ‘)
-> bottom.jsp μ String msg = erquest.getParameter("msg")
* λ§μ½ μμ²νλλ° νλΌλ―Έν° κ°μ bottomμ νλΌλ―Έν° κ°μ μ λ¬νκ³ μΆμ λ
templateμμ jsp:param
<jsp:include page="/layout/bottom.jsp" flush="false">
<jsp:param value="hi~~~" name="msg"/>
* mainνμ΄μ§μ μ μλ³μ μ μΈν΄λ λ³κ°μ μμ²νλ¦μΌλ‘ κ°κΈ° λλ¬Έμ bottomμμ μΈμνμ§ λͺ»ν¨ -> paramμΌλ‘ λ겨μ€μΌν¨!
p 168 [ include μ§μμλ₯Ό μ΄μ©ν μ€λ³΅λ μ½λ μ½μ ]
%@ page
%@ taglib
%@ include
// [WEB-INF/inc/include.jspf ]
// μ¬λ¬ νμ΄μ§μ μ€λ³΅λ μλ μ½λ©μ΄ μΆκ°.. %@ include μ§μμ .. μ¬μ©.
request.setCharacterEncoding("UTF-8");
String contextPath = request.getContextPath();
-> WEB-INF/inc ν΄λ λ§λ€μ΄μ μ¬κΈ° λ£μ (μΈλΆ μ κ·Ό λΆκ°νλλ‘)
-include.jspf-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
// [WEB-INF/inc/include.jspf ]
// μ¬λ¬ νμ΄μ§μ μ€λ³΅λ μλ μ½λ©μ΄ μΆκ°.. %@ include μ§μμ .. μ¬μ©.
request.setCharacterEncoding("UTF-8");
String contextPath = request.getContextPath();
String name ="admin";
%>
-jsp-
<%@ include file="/WEB-INF/inc/include.jspf" %>
[μ§λ¬Έ] κ°μ¬λ λ¨μν μ€λ³΅μ λ¬Έμ λ©΄ ν νλ¦Ώμ μ¬μ©ν΄λ λμ§μλμ?
include μ§μμ - bottom.jsp κ³΅ν΅ λͺ¨λν μΆκ°, κ³΅ν΅ μ€λ³΅ μ½λ©
jsp:include μ‘μ νκ·Έ - bottom.jsp κ³΅ν΅ λͺ¨λν μΆκ°, κ³΅ν΅ μ€λ³΅ μ½λ©
[ μ²λ¦¬ κ³Όμ μ°¨μ΄μ μ΄ν΄ ]
p 169 include μ§μμ μ²λ¦¬ κ³Όμ [κ·Έλ¦Ό 7.11]
include μ§μμ - 곡ν΅μ½λ©μ΄ μ»΄νμΌλμ΄μ§λ ν¬ν¨λλ μ (μλΈλ¦Ώ κ°μ²΄ νλ)
jsp:include μ‘μ νκ·Έ - μλ΅λ²νΌ λ΄κ³ μλ‘μ΄μμ² μΌμ΄λ ν λ€μ μλΈλ¦Ώ κ°μ²΄κ° μλ΅ (μλΈλ¦Ώ κ°μ²΄ λκ° , νλ¦ λ¬λΌμ§)
p 173 μ½λ μ‘°κ° μλ ν¬ν¨ κΈ°λ₯
1) 곡ν΅μ μΌλ‘ μ¬μ©νλ λ³μ μ§μ
@ include file="/common/variable.jspf"
2) μ μκΆ - κ°λ¨ν λ¬Έμ₯.
@ include file="/common/footer.jspf"
λͺ¨λ νμ΄μ§μ μμ μ½λ©μ ν νμμμ΄ web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>/days05/*</url-pattern>
<include-prelude>/common/variable.jspf</include-prelude>
<include-coda>/common/footer.jspf</include-coda>
</jsp-property-group>
</jsp-config>
p 177 include μ‘μ νκ·Έμ include μ§μμμ μ°¨μ΄μ (λΉκ΅ )
p 177 jsp:forwrd μ‘μ νκ·Έ X
p 190 Chapter 08 μλ°λΉκ³Ό jsp:useBean μ‘μ νκ·Έ
1. μλ°λΉ(bean) == μλΉμ¦(javabeans) ?
JSP κΈ°λ° + μΉ μ΄ν리μΌμ΄μ μμ
[μ 보]λ₯Ό νν(μ μ₯)νλ [κ°μ²΄] == DTO(μ 보κ°μ§κ³ μ λ¬) == VO(μ 보)
Spring κΈ°λ° POJO
2. μλ°λΉ(bean) κ·μ½ / μλΈλ¦Ώ κ·μ½ p,hs, doget,dopost,service μ€λ²λΌμ΄λ©.
1) private νλ μ μΈ
2) getter, setter μ μΈ
3) μ§λ ¬νκ° κ°λ₯ν ν΄λμ€ μ μΈ
*μ§λ ¬ν :
3. μλ°λΉμ μμ±, λ³κ²½ μ΄λ²€νΈ, κ°μ²΄ μ§λ ¬νλ₯Ό μν νμ€μ΄λ€.
μ) days05.MemberInfo.java μλ°λΉ μ μΈ
package days05;
import java.io.Serializable;
import java.util.Date;
// JSP κΈ°λ° μΉ μ΄ν리μΌμ΄μ
μμ νμμ 보λ₯Ό μ μ₯ν κ°μ²΄ - μλ°λΉ μ μΈ
public class MemberInfo implements Serializable{
private static final long serialVersionUID = 2490129593045133574L;
// νλ private
private String id;
private String passwd;
private String name;
private Date registerDate;
private String email;
// getter, setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
} // class
ex04_02.jsp μΆκ°.
<form action="ex04_05.jsp" method="get">
<table border="1" style="width:400px">
<tr>
<td>μμ΄λ</td>
<td><input type="text" name="id" value="admin"></td>
</tr>
<tr>
<td>μ΄λ¦</td>
<td><input type="text" name="name" value="κ΄λ¦¬μ"></td>
</tr>
<tr>
<td>λΉλ°λ²νΈ</td>
<td><input type="password" name="passwd" value="1234"></td>
</tr>
<tr>
<td>μ΄λ©μΌ</td>
<td><input type="text" name="email" value="admin@naver.com"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit">
<input type="reset">
</td>
</tr>
</table>
</form>
03.jsp
<%@page import="days05.MemberInfo"%>
<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--
http://localhost/jspPro/days05/ex04_03.jsp
?
id=admin
&
name=%EA%B4%80%EB%A6%AC%EC%9E%90
&
passwd=1234
&
email=admin%40naver.com
-->
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String passwd = request.getParameter("passwd");
String email = request.getParameter("email");
Date registerDate = new Date(); // μ€λλ μ§λ₯Ό κ°μ
μΌλ‘ μ€μ .
// μλ°λΉ( = JSP μ 보 μ μ₯νλ κ°μ²΄ ) μμ±
MemberInfo mi = new MemberInfo();
mi.setId(id);
mi.setName(name);
mi.setPasswd(passwd);
mi.setRegisterDate(registerDate);
mi.setEmail(email);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. μ€ν 2:21:05</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex04_03.jsp - MemberInfo μ 보 μΆλ ₯ </h3>
μμ΄λ : <%= mi.getId() %><br>
μ΄λ¦ : <%= mi.getName() %><br>
λΉλ°λ²νΈ : <%= mi.getPasswd() %><br>
λ±λ‘μΌ : <%= mi.getRegisterDate() %><br>
μ΄λ©μΌ : <%= mi.getEmail() %><br>
04.jsp
<%@page import="days05.MemberInfo"%>
<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String passwd = request.getParameter("passwd");
String email = request.getParameter("email");
Date registerDate = new Date(); // μ€λλ μ§λ₯Ό κ°μ
μΌλ‘ μ€μ .
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. μ€ν 2:28:17</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex04_04.jsp - MemberInfo μ 보 μΆλ ₯ </h3>
<pre>
// μλ°λΉ( = JSP μ 보 μ μ₯νλ κ°μ²΄ ) μμ±
MemberInfo mi = new MemberInfo();
mi.setId(id);
mi.setName(name);
mi.setPasswd(passwd);
mi.setRegisterDate(registerDate);
mi.setEmail(email);
μμ μ½λ©μ jsp:useBean μ‘μ
νκ·Έλ‘ μμ .....
</pre>
<jsp:useBean id="mi" class="days05.MemberInfo"></jsp:useBean>
<!-- Spring -->
<jsp:setProperty property="id" name="mi" value="<%= id %>"/>
<jsp:setProperty property="name" name="mi" value="<%= name %>"/>
<jsp:setProperty property="passwd" name="mi" value="<%= passwd %>"/>
<jsp:setProperty property="registerDate" name="mi" value="<%= registerDate %>"/>
<jsp:setProperty property="email" name="mi" value="<%= email %>"/>
μμ΄λ : <%= mi.getId() %><br>
μ΄λ¦ : <%= mi.getName() %><br>
λΉλ°λ²νΈ : <%= mi.getPasswd() %><br>
λ±λ‘μΌ : <%= mi.getRegisterDate() %><br>
μ΄λ©μΌ : <%= mi.getEmail() %><br>
05.jsp
<%@page import="days05.MemberInfo"%>
<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
Date registerDate = new Date(); // μ€λλ μ§λ₯Ό κ°μ
μΌλ‘ μ€μ .
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. μ€ν 2:28:17</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex04_05.jsp - MemberInfo μ 보 μΆλ ₯ </h3>
<pre>
1. νλλͺ
== name μμ± ( 쑰건 )
2. μ΅κ΄μ μΌλ‘..
γ±.
scott.dept / deptno, dname, loc
γ΄.
DeptDTO
private int deptno;
setter, geter
γ·.
μ
λ ₯νΌ
μ
λ ₯νκ·Έ name ="deptno"
</pre>
<jsp:useBean id="mi" class="days05.MemberInfo"></jsp:useBean>
<jsp:setProperty property="*" name="mi"/>
<jsp:setProperty property="registerDate" name="mi" value="<%= registerDate %>"/>
μμ΄λ : <%= mi.getId() %><br>
μ΄λ¦ : <%= mi.getName() %><br>
λΉλ°λ²νΈ : <%= mi.getPasswd() %><br>
λ±λ‘μΌ : <%= mi.getRegisterDate() %><br>
μ΄λ©μΌ : <%= mi.getEmail() %><br>
[ κΌ μ§ν¬ κ² ]
μλ°λΉμ νλλͺ κ³Ό λ°λμ λμΌν name μμ±μ μ€μ νλ€.
->νΈλ¦¬νμ : μ΄λ¦ λκ°κΈ° λλ¬Έμ νλΌλ―Έν° λ°μ νμμμ (05μμ μ²λΌ νλ²μ λ°μμ¬μμλ€) - register date λΉΌκ³
μ΅κ·ΌλκΈ