[Day8] JSP/Servlet 8 [12/13]
-๋ณต์ต-
์ฟ ํค: ์๋ฒ(jsp/servlet)์์ ์์ฑ -> ์๋ฒ์์ ํด๋ผ์ด์ธํธ๋ก ์ฟ ํค ์์ฑํ๋ผ๋ ๋ช ๋ น์ ๋ด๋ฆฌ๋๊ฒ์
js document.cookie ์์ฑ์ฌ์ฉ ์์ฑ, ์ญ์ , ์์
jsp Cookie ํด๋์ค c = new (cn,cv);
e/d/p/s
c.setXXX();
response.addCookie( c ) ; ์๋ต ํค๋ ์ถ๊ฐ.
์ญ์ : setE(0) ์๋ต ์ฆ์ ์ญ์ .
-1 ๋ธ๋ผ์ฐ์ ๋ซํ๋ ์ฟ ํค ์ญ์
์๋ก์ด ์ฟ ํค ๊ฐ์ฒด ์์ฑ : setE(0)
------------------------------------------------------------------------
์ธ์ : ์๋ฒ์ ์ ์ฅ๋๋ ๊ฒ /
[ํด] -> ์/์ -> [์] ์ธ์ ์ ์ฅ๊ณต๊ฐ(์บ๋น๋ท)
JSESSIONID ์ฟ ํค๊ฐ
jsp 9๊ฐ์ง ๊ธฐ๋ณธ ๊ฐ์ฒด ์ค session ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด๋๋ค์..
session="false"
request.getSession()
servlet ์ธ์ ์์ฑ ๋ฐฉ๋ฒ ์ค๋ช .
request.getSession() => session ์๋ก ์ธ์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํ
request.getSession(false) => session null ๋ฐํ
if( session== null){
}else{
}
------------------------------------------------------------------------
include ์ง์์์ include ์ก์ ํ๊ทธ์ ๋ํด์ ์ค๋ช ํ์ธ์.
ํ์ด์ง ๋ชจ๋ํ : ๊ณตํต์ ์ธ ๋ถ๋ถ -> ์ ์ง๋ณด์, ๊ด๋ฆฌ. ์์ฐ์ฑ
webapp / layout - top/bottom
์ผ์ ๊ด๋ฆฌ : left, right
์ผ์ ๊ด๋ฆฌํด๋ /layout - l,r
๊ฒ์ํ : left, right
์ฒ๋ฆฌ ๊ณผ์ ์ดํด.
include ์ง์์ : ์๋ธ๋ฆฟ ํด๋์ค 1๊ฐ -> ์ฒ๋ฆฌ ๊ณตํต์ฝ๋ฉ, ๋ชจ๋ํ ์ถ๊ฐ
jsp:include ์ก์ ํ๊ทธ : ์๋ธ๋ฆฟ ๊ฐ๊ฐ ์์ฑ -> ํ๋ฆ ์ด๋. ๊ณตํต์ฝ๋ฉ, ๋ชจ๋ํ ์ถ๊ฐ
------------------------------------------------------------------------
์ปค๋ฅ์ ํ ์ ์ ? Connection -> ํ(pool)
์ฌ์ฉ ์ด์ ? DB ์ฐ๋ + connection ๊ฐ์ฒด ์์ฑ + ๋ซ๊ธฐ / ๋์ ์ ์์ ๋ง์๊ฒฝ์ฐ / ์ฑ๋ฅ ๋จ์ด์ง๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ? WAS(ํฐ์บฃ) ์ด ์ ๊ณตํ๋ DBCP
1) WEB-INF/lib/tomcat-dbcp.jar ์ถ๊ฐ
2) META-INF/context.xml ์๋ฒ ์์ + ์๋(์ปค๋ฅ์ ํ ๊ฐ๋ )
3) WEB-INF/web.xml ์ค์ ์ถ๊ฐ
4) ํ ์คํธ conn.close() -> ๋ฐํ
5) com.util.ConnectionProvider.java
getConnection(){ CP } ***
์ฐธ๊ณ ) DBConn.getConnection();
- ex03_default.jsp / ex03_logon.jsp / ex03_logout.jsp ๋ก๊ทธ์ธ์ฒ๋ฆฌ.
- [ ์ธ์ ์ ์ฌ์ฉํด์ ๋ก๊ทธ์ธ ์ฒ๋ฆฌ ]
ex02_default.jsp / ex02_logon.jsp / ex02_logout.jsp ๋ก๊ทธ์ธ์ฒ๋ฆฌ. ๋ณต์ฌ ๋ถ์ด๊ธฐ.
session_auth.jspf
default.jsp
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/session_auth.jspf" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 12. ์คํ 2:10: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>
div{
border: 1px solid gray;
width:300px;
height:100px;
padding:20px;
}
</style>
</head>
<body>
<h3>ex02_default.jsp - ๋ฉ์ธ ํ์ด์ง</h3>
<pre>
p 242 ์ธ์
์ ์ฌ์ฉํ ๋ก๊ทธ์ธ ์ํ ์ ์ง
</pre>
<%
if( logonID == null ){ // ๋ก๊ทธ์ธ X
%>
<div id="logon">
<form action="ex02_logon.jsp">
์์ด๋ : <input type="text" name="id" value="admin" /><br>
๋น๋ฐ๋ฒํธ : <input type="password" name="passwd" value="1234" /><br>
<input type="submit" value="๋ก๊ทธ์ธ" />
<input type="button" value="ํ์๊ฐ์
" />
<%
String error = request.getParameter("error");
if( error != null ){
%>
<span style="color:red">๋ก๊ทธ์ธ ์คํจํ์ต๋๋ค.</span>
<%
} //
%>
<script>
$(function (){
$("#logon span").fadeOut(5000);
})
</script>
</form>
</div>
<%
}else{ // ๋ก๊ทธ์ธ O
%>
<div id="logout">
[<%= logonID %>]๋ ํ์ํฉ๋๋ค<br>
<a href="ex02_logout.jsp">๋ก๊ทธ์์</a>
</div>
<%
}
%>
<a href="/jspPro/cstvsboard/list.htm">๊ฒ์ํ</a><br>
<a href="#">๊ณต์ง์ฌํญ</a><br>
<%
if( logonID != null ){
%>
<!-- ๋ฐ๋์ ์ธ์ฆ(๋ก๊ทธ์ธ)์ ๋ฐ์์ผ์ง ์ฌ์ฉํ ์ ์๋ ๋ฉ๋ด -->
<a href="#">์ผ์ ๊ด๋ฆฌ</a><br>
<a href="#">์๋ฃ์ค</a><br>
<!-- ์ธ์ฆ + ๊ด๋ฆฌ์ ๊ถํ์ด ์์ด์ผ ์ฌ์ฉํ ์ ์๋ ๋ฉ๋ด -->
<%
if( logonID.equals("admin") ){
%>
<a href="#">๊ธ์ฌ๊ด๋ฆฌ</a><br>
<a href="#">๊ทผํ๊ด๋ฆฌ</a><br>
<%
} // if
} // if
%>
jstl ๋ฒ์
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/session_auth.jspf" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 12. ์คํ 2:10: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>
div{
border: 1px solid gray;
width:300px;
height:100px;
padding:20px;
}
</style>
</head>
<body>
<h3>ex02_default_EL_JSTL.jsp - ๋ฉ์ธ ํ์ด์ง</h3>
<pre>
p 242 ์ธ์
์ ์ฌ์ฉํ ๋ก๊ทธ์ธ ์ํ ์ ์ง
</pre>
<c:choose>
<c:when test="${ empty logonID }">
<div id="logon">
<form action="ex02_logon.jsp">
์์ด๋ : <input type="text" name="id" value="admin" /><br>
๋น๋ฐ๋ฒํธ : <input type="password" name="passwd" value="1234" /><br>
<input type="submit" value="๋ก๊ทธ์ธ" />
<input type="button" value="ํ์๊ฐ์
" />
<c:if test="${ not empty param.error }">
<span style="color:red">๋ก๊ทธ์ธ ์คํจํ์ต๋๋ค.</span>
</c:if>
<script>
$(function (){
$("#logon span").fadeOut(5000);
})
</script>
</form>
</div>
</c:when>
<c:otherwise>
<div id="logout">
[<%= logonID %>]๋ ํ์ํฉ๋๋ค<br>
<a href="ex02_logout.jsp">๋ก๊ทธ์์</a>
</div>
</c:otherwise>
</c:choose>
<a href="/jspPro/cstvsboard/list.htm">๊ฒ์ํ</a><br>
<a href="#">๊ณต์ง์ฌํญ</a><br>
<c:if test="${ not empty logonID }">
<a href="#">์ผ์ ๊ด๋ฆฌ</a><br>
<a href="#">์๋ฃ์ค</a><br>
<!-- EL ์ฐ์ฐ์ : == eq -->
<c:if test="${ logonID eq 'admin' }">
<a href="#">๊ธ์ฌ๊ด๋ฆฌ</a><br>
<a href="#">๊ทผํ๊ด๋ฆฌ</a><br>
</c:if>
</c:if>
logon.jsp
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- <h3>ex03_logon.jsp</h3> -->
<%
// DB member ํ
์ด๋ธ id/pwd ์ฒดํฌ
String id= request.getParameter("id");
String passwd= request.getParameter("passwd");
//
if( id.equals("admin") && passwd.equals("1234") ){
// ๊ด๋ฆฌ์ ์ธ์ฆ
// ์ธ์
์ ์ฅ
session.setAttribute("auth", id );
String location = "ex02_default.jsp";
response.sendRedirect(location);
}else if(id.equals("hong") && passwd.equals("1234") ){
// ์ผ๋ฐ๊ณ์ฆ์ผ๋ก ์ธ์ฆ
session.setAttribute("auth", id );
String location = "ex02_default.jsp";
response.sendRedirect(location);
}else if(id.equals("park") && passwd.equals("1234") ){
// ์ผ๋ฐ๊ณ์ฆ์ผ๋ก ์ธ์ฆ
session.setAttribute("auth", id );
String location = "ex02_default.jsp";
response.sendRedirect(location);
}else{ // ์ธ์ฆ ์คํจ
String location = "ex02_default.jsp?error";
response.sendRedirect(location);
}
%>
logout.jsp
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/session_auth.jspf" %>
<!-- <h3>ex02_logout.jsp</h3> -->
<%
// ์ธ์
์ญ์
// 1) ์์ฒญ ์ฝ 20๋ถ ์ ๋ ํ์ง ์์ผ๋ฉด ์๋์ผ๋ก ์ธ์
์ข
๋ฃ.
// 2) ์ธ์
๊ฐ์ ์ง๊ธ ๋ฐ๋ก ์ข
๋ฃ.
session.invalidate();
%>
<script>
alert("[<%= logonID %>]๋ ๋ก๊ทธ ์์ ํ์
จ์ต๋๋ค.");
location.href = "ex02_default.jsp";
</script>
p 245 ์ฐ๊ด๋ ์ ๋ณด ์ ์ฅ์ ์ํ ํด๋์ค ์์ฑ
-> ํ๋์ ๋ฆฌ์คํธ๋ก ์ธ์ ์ ์ ์ฅ!
1. ์ธ์
- ์ธ์ฆ์ ๋ณด ์ ์ฅ auth=logonID(hong)
- ๊ถํ์ ๋ณด ์ ์ฅ ๊ด๋ฆฌ์. ์ผ๋ฐํ์. MVP ํ์ ๋ฑ๋ฑ ๋ฑ๊ธ
- ์ฅ๋ฐ๊ตฌ๋ ์ ๋ณด ์ ์ฅ.
์ํ์ฝ๋๋ฒํธ/์๋/๋ ์ง ๋ฑ๋ฑ
์ํ์ฝ๋๋ฒํธ/์๋/๋ ์ง ๋ฑ๋ฑ
์ํ์ฝ๋๋ฒํธ/์๋/๋ ์ง ๋ฑ๋ฑ
์ํ์ฝ๋๋ฒํธ/์๋/๋ ์ง ๋ฑ๋ฑ
์ํ์ฝ๋๋ฒํธ/์๋/๋ ์ง ๋ฑ๋ฑ
ArrayList< ์ฅ_์ํด๋์ค> ์ฅlist;
session.setAttribute("์ฅlist", ์ฅlist);
๊ด๋ จ๋ ์ฌ๋ฌ ๊ฐ์ง์ ์ ๋ณด๋ฅผ ์ธ์ ์ ์ฅ.
( ๋ก๊ทธ์ธํ ํ์ ์ ๋ณด ๋ชจ๋ ์ ์ฅ : ์์ด๋/์ด๋ฆ/๋ฑ๊ธ/์ฃผ์/์ฐ๋ฝ์ฒ ๋ฑ๋ฑ)
session.setAttribute("id", ์์ด๋);
session.setAttribute("name", ์์ด๋);
session.setAttribute("grade", ์์ด๋);
session.setAttribute("addr", ์์ด๋);
session.setAttribute("tel", ์์ด๋);
Member m = new Member(); + ํ๋
session.setAttribute("member", m);
p 250 Chapter 11 ํํ์ธ์ด(Expression Language : EL)
์ถ๋ ฅ์ธ์ด
- EL ์๋ฃํ
- EL ๊ธฐ๋ณธ ๊ฐ์ฒด param
- EL ์ฐ์ฐ์ eq ==
- EL ์ปฌ๋ ์
- EL ๋ฉ์๋ ํธ์ถ๊ฐ๋ฅ ( ๋ฐฉ๋ฒ ) ***
- EL ๋๋ค์๊ณผ ์คํธ๋ฆผ API
1) EL ์ ์ ?
- ๋ค๋ฅธ ํํ์ ์คํฌ๋ฆฝํธ ์ธ์ด.
- ํํ์ ๋ณด๋ค ๊ฐ๊ฒฐํ๊ณ ํธ๋ฆฌํ๋ค.
- JSTL 1.0์์ ์๊ฐ๋จ. EL + JSTL
- JSP 2.0 ์ EL ํฌํจ,
JSP 2.3 == EL 3.0 ํ ์.
2) EL ๊ธฐ๋ฅ
- JSP์ ๋ค ๊ฐ์ง ๊ธฐ๋ณธ ๊ฐ์ฒด๊ฐ ์ ๊ณตํ๋ ์์ญ(scope)์ ์์ฑ ์ฌ์ฉ.
pageContext scope
request scope
session scope
application scope
- ์์น ์ฐ์ฐ, ๊ด๊ณ ์ฐ์ฐ( == eq , != ne), ๋ ผ๋ฆฌ ์ฐ์ฐ์( not ! , empty ) ์ ๊ณต
- ์๋ฐํด๋์ค์ ๋ฉ์๋ ํธ์ถ ๊ฐ๋ฅ
- ๋๋ค์ ์ฌ์ฉ( EL 3.0 )
- ์ฟ ํค, ๊ธฐ๋ณธ ๊ฐ์ฒด์ ์์ฑ ๋ฑ JSP๋ฅผ ์ํ ํํ ์ธ์ด์ ๊ธฐ๋ณธ ๊ฐ์ฒด ์ ๊ณต ( param )
- ์คํธ๋ฆผ API๋ฅผ ํตํ ์ปฌ๋ ์ ์ฒ๋ฆฌ ( EL 3.0 )
- ์ ์ (static) ๋ฉ์๋ ์คํ( EL 3.0 )
<%--
3) EL ๊ตฌ์ฑ( ์ ์ธ ํ์ )
${ expr }
p 252 JSP 2.1 ๋ถํฐ #{ expr } X => ์คํ๋ง(Spring)
์ฐจ์ด์ : ์ค์ EL์ ๊ฐ์ ์ธ์ ์์ฑํ๋๋์ ์๋ค.
--%>
p 253 EL ๊ธฐ์ด
- EL ์๋ฃํ, ์ฐ์ฐ์ ์๋ค.
1. boolean - true, false
2. ์ ์ : 0~9 , -8 == java.lang.[Long]
3. ์ค์ : 3.14 == java.lang.Double
4. ๋ฌธ์์ด : "" ''
" \" \' \\ " == java.lang.String
5. ๋ ํ์ : null
${true } ${ -10 } ${ 3.14 } ${"ํ\"๊ธธ๋" } ${ null }<br> |
p 254 EL ๊ธฐ๋ณธ ๊ฐ์ฒด
1. pageContext == page ๊ธฐ๋ณธ๊ฐ์ฒด ๋์ผ
2. pageScope Map ๊ฐ์ฒด์ด๋ค.
requestScope
sessionScope
applicationScope
3. param == String request.getParameter()
paramValues == request.getParameterValues() Map ๊ฐ์ฒด
4. header == request.getHeader() Map ๊ฐ์ฒด
headerValues == request.getHeaders() Map ๊ฐ์ฒด
5. cookie Map ๊ฐ์ฒด <์ฟ ํค์ด๋ฆ, ๊ฐ ๋ฐฐ์ด >
6. initParam == application.getInitParameter(์ด๋ฆ) Map ๊ฐ์ฒด
[ web.xml ]
<context-param>
<description>๋๋ฒ๊น ๋ ๋ฒจ</description>
<param-name>debugLevel</param-name>
<param-value>3</param-value>
</context-param>
${ initParam.debugLevel }
------------------------------------------------------------------------
์์ )
1) ํํ์
<%
String rURI = request.getRequestURI();
%>
์์ฒญ URI : <%= rURI %><br>
์์ฒญ URI( ํํ์) : <%= request.getRequestURI() %><br>
<%
request.setAttribute("name", "ํ๊ธธ๋");
%>
<%
String name = (String)request.getAttribute("name");
%>
name(ํํ์) = <%= name%><br>
code ํ๋ผ๋ฏธํฐ(ํํ์) : <%= request.getParameter("code") %>
2) EL
์์ฒญ URI(EL) : ${ pageContext.request.requestURI }
name(EL) = ${ requestScope.name }
name(EL) = ${ name } <!-- ์์ญ์ ์๋ name X : 4๊ฐ์ง ์์ญ์ ๋ชจ๋ ๋ค์ ธ์ ์ป์ด์จ๋ค.-->
code ํ๋ผ๋ฏธํฐ(EL) : ${ param.code }
- 1) 2) ๊ฐ์ ์ฝ๋ฉ!
p 256 EL ์ฟ ํค ์ฌ์ฉ๋ฐฉ๋ฒ ***
${ cookie.ID.value } Map ๊ฐ์ฒด <์ฟ ํค์ด๋ฆ, ๊ฐ ๋ฐฐ์ด >
1) ํํ
cookie.ID ์ฟ ํค๊ฐ์ด ID ์ธ Cookie ๊ฐ์ฒด๋ฅผ ์ป์ด์จ๋ค.
Cookie.ID.value ( getValue () ) Cookie ๊ฐ์ฒด์ value(๊ฐ)
2) ํํ
cookie["ID"]
1) 2) ํํ ๋ ๋ค ๊ฐ๋ฅ!
์ฟ ํค ok.jsp
<!-- EL ์ฌ์ฉํ์ง ์๊ณ -->
<%
Cookies cookies = new Cookies(request);
// key ์ฟ ํค์ด๋ฆ value ์ฟ ํค๊ฐ์ฒด
Set<Entry<String, Cookie>> set = cookies.cookieMap.entrySet();
Iterator<Entry<String, Cookie>> ir = set.iterator();
while( ir.hasNext() ){
Entry<String, Cookie> entry = ir.next();
String cookieName = entry.getKey();
Cookie c = entry.getValue();
%>
<li><%= cookieName %> - <%= c.getValue() %> </li>
<%
} // while
%>
<!-- EL ์ฌ์ฉํ๊ณ -->
EL user : ${ cookie.user.value }<br>
EL id : ${ cookie["id"].value }<br>
EL age : ${ cookie.age.value }<br>
<!-- EL + JSTL ๋ชจ๋ ์ฟ ํค ์ ๋ณด๋ฅผ ์ถ๋ ฅ -->
<c:forEach items="${ cookie }" var="entry">
<li>${ entry.key } - ${ entry.value.value }</li>
</c:forEach>
* entry.value๋ ์ฟ ํค ๊ทธ ์์ฒด์ -> entry.value.value๋ก ๋ฐ์์์ผํจ
* ๋ฒ์๊ฐ ์์ ๊ฒ๋ถํฐ name ๊ฐ ๊ฐ์ ธ์ด (๊ทธ๋ฅ name์ด๋ผ๊ณ ํ์๋ - page -> request -> session -> application)
p 258 EL ์ฐ์ฐ์
+ ๋ง์
- ๋บ์
*
/ div
% mod
-2 ๋จํญ์ฐ์ฐ์.
${ 10 + 3 }<br>
${ 10 - 3 }<br>
${ 10 * 3 }<br>
${ 10 / 3 }<br>
<%-- ${ 10 div 3 }<br> --%>
${ 10 % 3 }<br>
<%-- ${ 10 mod 3 }<br> --%>
<!-- 13 ๊ทธ ์ด์ ? + EL ๋ฌธ์์ด ์ฐ๊ฒฐ์ฐ์ฐ์ X, + ์์น์ฐ์ฐ์( ๋ง์
) -->
${ "10" + 3 }<br> <!-- java "103" -->
<%-- ${ "test" + 3 }<br> --%>
<%-- ${ null + 3 } ==> 0 --%>
p 260 EL ๋น๊ต ์ฐ์ฐ์
== eq
!= ne
< lt
> gt
<= le
>= ge
${ 100 == 10 }<br>
${ 100 eq 10 }<br>
${ 100 != 10 }<br>
<%-- ${ 100 ne 10 }<br> --%>
${ 100 > 10 }<br>
${ 100 gt 10 }<br>
${ 100 < 10 }<br>
${ 100 lt 10 }<br>
${ 100 >= 10 }<br>
${ 100 ge 10 }<br>
${ 100 <= 10 }<br>
${ 100 le 10 }<br>
๋ ผ๋ฆฌ ์ฐ์ฐ์
&& and
|| or
! not
${ true && false }<br>
${ true and false }<br>
${ true || false }<br>
${ true or false }<br>
${ !false }<br>
${ not false }<br>
[ empty ์ฐ์ฐ์ ]
null true
"" true
๊ธธ์ด 0 true
๋น Map true
๋น ์ปฌ๋ ์ true ArrayList<BoardDTO> list= null
ArrayList<BoardDTO> list= new ArrayList<>(); // ๋น์์๋ ์ปฌ๋ ์ .
{ not empty list }
์์ ๊ฒฝ์ฐ ์ธ์ ๋ชจ๋ false ๊ฐ์ผ๋ก ์ฒ๋ฆฌํ๋ค.
์ผํจ ์ฐ์ฐ์ ? :
p 262 ๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ์. +( ๋ง์ ์ฐ์ ์ฐ์ฐ์. )
<%
request.setAttribute("title", "์ค๋ผํด");
%>
<!-- EL์์ ๋ฌธ์์ด ์ฐ๊ฒฐํ๋ ์ฐ์ฐ์ ์ถ๊ฐ += ์ฐ์ฐ์ -->
${ "์ ๋ชฉ : " += title } <br>
<!--
[ p 262 ์ธ๋ฏธ์ฝ๋ก ์ฐ์ฐ์. EL 3.0]
1+1 ๋ง์
์ฒ๋ฆฌํ๊ณ
10+10 ๋ง์
์ฒ๋ฆฌํ๊ณ
ํ์ง๋ง ์ถ๋ ฅ์ ๋ท์ ๊ฒ๋ง ์ถ๋ ฅํด์ 20 ๋์จ๋ค.
-->
${ 1+1 ; 10+10 }<br>
<!-- p 265 EL 3.0
๋ณ์ var1์ 10 ๊ฐ์ ํ ๋นํ๊ณ var1 ๋ณ์๋ฅผ ์ถ๋ ฅํ๋ค. -->
${ var1 = 10 }<br>
EL ์ฐ์ฐ์ ์ฐ์ ์์๊ฐ ์๋ค.
\${ expr } == EL ํํ์ด ์๋๋ผ "๋ฌธ์์ด"
p 266 EL์์ ๊ฐ์ฒด์ ๋ฉ์๋ ํธ์ถ
EL์์ statc ๋ฉ์๋ ํธ์ถ
<%
long price = 22345;
// EL ์์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ค์ฝ๋(์์ญ) ์ ์ฅ
request.setAttribute("price", price);
// (๋ฌธ์ ) ์ธ ์๋ฆฌ ๋ง๋ค ์ฝค๋ง๋ฅผ ์ฐ์ด์ ์ถ๋ ฅํ๊ณ ์ถ๋ค. 22,345
// (๋ฌธ์ ) EL ์ธ ์๋ฆฌ ๋ง๋ค ์ฝค๋ง๋ฅผ ์ฐ์ด์ ์ถ๋ ฅํ๊ณ ์ถ๋ค. 22,345
%>
price( ํํ์ ) = <%= price %><br>
price( ํํ์ ) = <%= String.format("%,d", price) %><br>
<%
// ํ์ํ ํด๋์ค
String pattern = "##,###";
DecimalFormat df = new DecimalFormat(pattern);
%>
price( ํํ์ ) = <%= df.format(price) %><br>
price( EL ) = ${ requestScope.price }<br>
price( EL ) = ${ price }<br>
<!-- ์ธ์๋ฆฌ๋ง๋ค ์ฝค๋ง ์ถ๋ ฅ ? -->
price( EL ) = <fmt:formatNumber value="${ price }" pattern="#,###"/><br>
<!-- ex04_06_02.jsp -->
p 266 [ EL์์ ์ธ์คํด์ค, (์คํํฑ ๋ฉ์๋) ์ฌ์ฉ ]
<%
long price = 22345;
request.setAttribute("price", price);
// days08.FormatUtil.java
%>
<%
// 1. ์ธ์คํด์ค ๋ฉ์๋ ์ฌ์ฉํ ์ฝ๋ฉ.
FormatUtil f = new FormatUtil();
request.setAttribute("f", f);
%>
FormatUtil.number() = ${ f.number(price, "#,##0") }<br>
<!-- // 2 ์คํํฑ ๋ฉ์๋ ์ฌ์ฉํ ์ฝ๋ฉ -->
FormatUtil.staticNumber() = ${ FormatUtil.staticNumber(price, "#,##0") }<br>
FormatUtil.java
package days08;
import java.text.DecimalFormat;
public class FormatUtil {
// 1 ์ธ์คํด์ค ๋ฉ์๋
public String number(long number, String pattern) {
DecimalFormat df = new DecimalFormat(pattern);
return df.format(number);
}
// 2 ์คํํฑ ๋ฉ์๋
public static String staticNumber(long number, String pattern) {
DecimalFormat df = new DecimalFormat(pattern);
return df.format(number);
}
}
p 292 Chatper 12 ํ์ค ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ( JSTL )
- JSTL
- ๊ฐ๋ฐ์ ํ์์ ์ํด์ ๋ง๋๋ ํ๊ทธ : ์ปค์คํ ํ๊ทธ
- ๋ง์ ๊ฐ๋ฐ์๋ค์ด ๊ณตํต์ ๊ธฐ๋ฅ์ ์ปค์คํ ํ๊ทธ๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉ์ ํ๋๋ผ.
-> ๋ชจ์์ ํ์คํํด์ ํ๊ทธ ๋ง๋ค์...
- JSTL( Jsp Standard Tag Library)
- JSTL
*** 1) c ์ฝ์ด ๋ณ์์ง์, ํ๋ฆ ์ ์ด, URL์ฒ๋ฆฌ
+)
ํ๋ฆ ์ ์ด : if choose, forEach, forTokens
URL ์ฒ๋ฆฌ : import, redirect, url
๊ธฐํ : catch, out(JspWriter์ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค.)
2) fmat ๊ตญ์ ํ(ํ์ํ) ์ซ์,๋ ์ง,๋ฌธ์์ด ํ์, ์ง์ญ
\22.345
$22.345
3) xml XML xml ์ฝ์ด, ํ๋ฆ์ ์ด, xml๋ณํ X
4) sql ๋ฐ์ดํฐ๋ฒ ์ด์ค
ex06.jsp
5) fn ํจ์ String ํจ์, ์ปฌ๋ ์ ํจ์
ex 1)
ex07.jsp ๋ณ์ ์ ์ธ, ์ ๊ฑฐ c:set, c:remove
p 295 ์ฝ์ดํ๊ทธ c ๋ณ์ ์ ์ธ : set , remove var : EL๋ณ์๋ช scope : [page], request, session, application value : ๋ฌธ์์ด, ํํ์, EL |
<%
String name = "ํ๊ธธ๋";
// request.setAttribute("name", name);
%>
> name : <%= name %><br>
<!-- page, request, session, application ์์ญ ๊ฒ์ฌ -->
<%-- <c:set value="ํ๊ธธ๋" var="cname" scope="request"></c:set> --%>
<%-- <c:set value="<%= name %>" var="cname" scope="request"></c:set> --%>
<c:set var="cname" scope="request"><%= name %></c:set>
> name : ${ cname }<br>
* EL์ ๊ฒฝ์ฐ name์ attribute์์ ๋ฃ์ด์ผ ์ธ์ ๊ฐ๋ฅ!
* ๋ง์ฝ ๋ฃ์ด์ฃผ์ง ์์ใท๋ฉด c:set์ผ๋ก ๋ณ์ ํ ๋น
ex07_02.jsp
1) c:set var="๋ณ์๋ช
" value="๊ฐ" scope="4 ์์ญ" 2) c:set target="" property="" value="" target : ์์ฑ(property) ๊ฐ์ ์ค์ ํ ๋์ ๊ฐ์ฒด ์ง์ ํํ์, EL , ์๋ฐ๋น, ๋งต property : ์ค์ ํ ์์ฑ์ ์ด๋ฆ ๊ฐ์ฒด set์ด๋ฆ == setter ์ ์ธ์ด ๋์ด์ ธ ์์ด์ผ ๋๋ค. property="dname" setDname() ์ ์ธ๋์ด์ ธ ์์ด์ผ ํ๋ค. value : ๊ฐ p 298 ํ 12.3 var ์์ฑ, scope ์์ฑ : ํํ์ X, EL X target, value, property : ํํ์ O, EL O |
<%
DeptDTO dto = new DeptDTO(10, "QC", "SEOUL");
//request.setAttribute("dto", dto);
%>
<c:set var="dto" value="<%= dto %>"></c:set>
<%
// dto.setDname("XXX");
%>
<%-- <c:set target="<%= dto %>"></c:set> --%>
<c:set target="${ dto }" property="dname" value="YYY"></c:set>
deptno : ${ dto.deptno } <br>
dname : ${ dto.dname }<br>
loc : ${ dto.loc }<br>
<%--
<c:forEach items="${ list }" var="dto">
<c:forEach items="<%= list %>" var="dto">
</c:forEach>
--%>
<์์ฝ>
ex07_03.jsp
<%
Map<String, String> map = new HashMap<>();
map.put("id", "admin");
map.put("passwd", "1234");
// request.setAttribute("map", map);
%>
<c:set var="m" value="<%= map %>"></c:set>
id = ${ m.id }<br>
passwd = ${ m.passwd }<br>
<hr>
<pre>
c:remove ํ๊ทธ๋ c:set ์ผ๋ก ์ ์ธ๋ ๋ณ์๋ฅผ ์ญ์ ํ๋ ํ๊ทธ์ด๋ค.
</pre>
<%-- <c:remove var="m" /> --%>
<%
// map.put("name", "ํ๊ธธ๋");
%>
<%-- <c:set target="<%= map %>"></c:set> --%>
<c:set target="${ m }" property="name" value="ํ๊ธธ๋"></c:set>
name = ${ m.name }<br>
ex +)
ex08.jsp ํ๋ฆ์ ์ด
num parameter = ${ param.num }<br>
<%--
<c:set var="pn" value="${ param.num }" ></c:set>
<c:if test="${ pn mod 2 eq 0 }">์ง์</c:if>
<c:if test="${ pn mod 2 ne 0 }">ํ์</c:if>
--%>
<!-- ์์ ๊ฐ์ด c:set์ผ๋ก ๋ณ์๋ฅผ ์ ์ธํ์ง ์์๋๋๋ค. -->
<%--
<c:if test="${ param.num mod 2 eq 0 }">์ง์</c:if>
<c:if test="${ param.num mod 2 ne 0 }">ํ์</c:if>
--%>
<c:choose>
<c:when test="${ param.num mod 2 eq 0 }">์ง์</c:when>
<c:otherwise>ํ์</c:otherwise>
</c:choose>
ex08_02.jsp (ex)
<form>
๊ตญ์ด : <input type="text" name="kor">
</form>
<!-- 98/10 = ํจ์ Math.ceil( 9.8) ์ ์ญ = 9 -->
<c:set value="${ param.kor }" var="kor"></c:set>
<%--
<c:choose>
<c:when test=" ${ kor >= 90 && kor <= 100 }">์</c:when>
<c:when test="${ kor ge 80 and kor lt 90 }">์ฐ</c:when>
<c:when test="">๋ฏธ</c:when>
<c:when test="">์</c:when>
<c:otherwise>๊ฐ</c:otherwise>
</c:choose>
--%>
ex08_03.jsp forEach ์ ์ด๋ฌธ
[ p 303 c:forEach ํ๊ทธ ] - ๋ฐฐ์ด ex08_04.jsp - ์ปฌ๋ ์ X ArrayList<BoardDTO> list - ๋งต ex08_04.jsp ์ ์ ์ฅ๋์ด์๋ ๊ฐ๋ค์ "์์ฐจ์ ์ผ๋ก" ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ๋ค. - begin="" ์์๊ฐ - end="" ๋๊ฐ - items="" EL - step="" ์ฆ๊ฐ์น - var="" ๋ณ์๋ช - varStatus="" ๋ณ์์ํ |
<!-- 1+2+..+10=55 -->
<!-- sum ๋ณ์ ์ ์ธ -->
<c:set var="sum" value="0"></c:set>
<c:set var="start" value="1"></c:set>
<%
int end = 10;
int ์ฆ๊ฐ์น = 2;
%>
<c:forEach begin="${start }" end="<%= end %>" step="<%= ์ฆ๊ฐ์น %>" var="i">
<%--
<c:if test="${ i eq 10 }">${ i }</c:if>
<c:if test="${ i ne 10 }">${ i }+</c:if>
--%>
<!-- += ๋ฌธ์์ด ์ฐ๊ฒฐ์ฐ์ฐ์ , ์ผํญ์ฐ์ฐ์ -->
${ i += (i==10? "":"+") }
<c:set var="sum" value="${ sum + i }"></c:set>
</c:forEach>
= ${ sum }
<br>
<!-- ํ๋ก์ ํธํ ๋ ์ฌ์ฉ -->
<!-- p 305
varStatus="status"
status.index ๋ฃจํ ์คํ์์์ ํ์ฌ ์ธ๋ฑ์ค
status.count ๋ฃจํ์ ์คํ ํ์
status.begin, end, step
status.first ์ฒซ true
status.last ๋ง์ง๋ง
status.current
-->
<c:forEach begin="1" end="5" var="i" varStatus="status">
<c:if test="${ status.first }">์ฒซ๋ฒ์งธ : </c:if>
<c:if test="${ status.last }">๋ง์ง๋ง๋ฒ์งธ : </c:if>
test - ${ i } - index : ${ status.index }<br>
ex08_04.jsp c:forEach ํ๊ทธ - ๋ฐฐ์ด + ๋งต์ฒ๋ฆฌ
<!-- int [] m = { 1,2,3,4,5 } -->
<%
// int [] m = { 3,5,2,4,1};
%>
<%-- <c:set var="m" value="<%= m %>"></c:set> --%>
<c:set var="m" value="<%= new int[]{ 3,5,2,4,1} %>"></c:set>
<c:forEach items="${ m }" var="n" varStatus="status">
<li> m[${ status.index }] = ${ n }</li>
</c:forEach>
<hr>
<pre>
c:forEach ํ๊ทธ - ๋งต(Map) ์ฒ๋ฆฌ
</pre>
<%
Map<String, String> map = new HashMap<>();
map.put("id", "admin");
map.put("age", "20");
map.put("name", "ํ๊ธธ๋");
%>
<c:set var="person" value="<%= map %>"></c:set>
${ person.id }<br>
${ person["id"] }<br>
<ol>
<c:forEach items="${ person }" var="entry">
<li>${ entry.key } : ${ entry.value }</li>
</c:forEach>
</ol>
ex08_05.jsp p 307 [ c:forTokens ํ๊ทธ == split() ๋น์ท ]+
p 307 URL ์ฒ๋ฆฌ ํ๊ทธ
c:url ํ๊ทธ : URL ์์ฑ
c:redirect ํ๊ทธ : ๋ฆฌ๋ค์ด๋ ํธ ์ฒ๋ฆฌ response.sendRedirect()
<c:forTokens items="ํ์ ์ฐ,์ด์ฌ๋ฏผ.์ํํ,๊ถ์ฌํ,์ค์น์ฃผ" delims=",." var="name">
${ name }<br>
</c:forTokens>
<%-- url
<c:url var="searchUrl" value="http://localhost/jspPro/days08/ex08.jsp">
</c:url>
<c:redirect url="${searchUrl }">
<c:param name="searchCondigion" value="title"></c:param>
<c:param name="searchWord" value="title - 30"></c:param>
</c:redirect>
--%>
<%
String location = "http://localhost/jspPro/days08/ex08.jsp?searchCondigion=title&searchWord=ํ๊ธธ๋";
response.sendRedirect(location);
%>
- ๊ทธ๋ฅ String์ผ๋ก ํ๋๊ฒ ๋์ (์๋๋ฉด ๊ธธ์ด์ ธ์)
ex08_06.jsp
p 311
c:out ํ๊ทธ : JspWriter์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๋ ํ๊ทธ
value="" ์ถ๋ ฅํ ๊ฐ.
default="" ์ถ๋ ฅํ ๊ฐ์ด ์์ ๊ฒฝ์ฐ์ ๊ธฐ๋ณธ ์ถ๋ ฅํ ๊ฐ.
escapeXml="" < < > > & ' "
<span>
p 312 ์์ 12.6 ( 5๋ถ ์ฝ๋ฉ )
<%
FileReader fr = null;
try{
String path = "days08/ex01.jsp";
String realPath = request.getRealPath(path);
fr = new FileReader( realPath );
%>
์์ค ์ฝ๋ = <%= path %><br>
<c:out value="<%= fr %>" escapeXml="true"></c:out>
<%
}catch(Exception e){
%>
์๋ฌ : <%= e.getMessage() %>
<%
}finally{
if( fr != null) {
try { fr.close(); }catch(IOException ex){}
}
}
%>
ex08_07.jsp try-catch (์์์)
<c:catch var="ex">
//
//
<%
// ?name=admin
String name = request.getParameter("name").toUpperCase();
%>
//
//
//
</c:catch>
<%-- <c:if test="${ ex != null }"> --%>
<c:if test="${ not empty ex }">
์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.
${ ex }
</c:if>
ex4)
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 13. ์คํ 12:40:33</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>ex06.jsp</h3>
<pre>
scott.dept ํ
์ด๋ธ์ ๋ ์ฝ๋ ์ถ๋ ฅ
</pre>
<!-- Spring + mybatis ํ๋ ์์ํฌ -->
<sql:query var="rs" dataSource="jdbc/myoracle">
SELECT deptno, dname, loc
FROM dept
</sql:query>
<c:forEach items="${ rs.rows }" var="dto">
<li>${ dto.deptno } - ${dto.dname } - ${ dto.loc }</li>
</c:forEach>
* ๊ฒฐ๊ณผ๋ฌผ var="rs"์ ๋ด์ & dataSource(xml.web์ ์ปค๋ฅ์ ํ)
p 315 ๊ตญ์ ํ ํ๊ทธ
- ํน์ ์ง์ญ์ ๋ฐ๋ผ ์๋ง์ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ ๋ ์ฌ์ฉํ๋ ํ๊ทธ
- ์ซ์ ๋ฐ ๋ ์ง ํฌ๋งคํ ( ์ฃผ๋ก ์ฌ์ฉ. )
formatNumber ์ซ์ ํฌ๋งทํ p 325
foramtDate
parseDate
parseNumber
<%
long price = 22345;
// ์ธ ์๋ฆฌ๋ง๋ค ์ฝค๋ง ์ถ๋ ฅ.
%>
<fmt:formatNumber
type="number"
value="<%= price %>"
pattern="##,###.00"
var="fmt_price"/>
price : ${ fmt_price }<br>
price : <fmt:formatNumber value="<%= price %>"
type="currency"
currencySymbol="\\" /><br>
price : <fmt:formatNumber value="<%= price %>"
type="percent" /><br>
<hr>
<%
String strPrice = "1,200.34"; // java double ํ๋ณํ
// 1. strPrice.replaceAll("," , "") "1200.34"
// 2. double dprice = Double.parseDouble(strPrice);
// java parsing/formatting
int qty = 5;
// dprice * qty = ์ด ํ๋งค๊ธ์ก.
%>
<fmt:parseNumber value="<%= strPrice %>" pattern = "0,000.00" var="parsingPrice" />
<c:set var="q" value="<%= qty %>" />
์ด ํ๋งค๊ธ์ก : ${ parsingPrice * q}<br>
p 327
๋ ์ง ์ถ๋ ฅ - formatDate
๋ ์ง ํ์ฑ - parseDate
<pre>
p 327
๋ ์ง ์ถ๋ ฅ - formatDate
๋ ์ง ํ์ฑ - parseDate
</pre>
<%
Date now = new Date();
%>
now : <%= now %><br><!-- Tue Dec 13 16:39:50 KST 2022 -->
<c:set var="now" value="<%= now %>"></c:set>
<li> <fmt:formatDate value="${ now }"/> </li><!-- 2022. 12. 13. -->
<li> <fmt:formatDate value="${ now }" dateStyle="default"/> </li><!-- -->
<li> <fmt:formatDate value="${ now }" dateStyle="full"/> </li><!-- -->
<li> <fmt:formatDate value="${ now }" dateStyle="short"/> </li><!-- -->
<li> <fmt:formatDate value="${ now }" dateStyle="long"/> </li><!-- -->
<li> <fmt:formatDate value="${ now }" dateStyle="medium"/> </li><!-- -->
<li> <fmt:formatDate value="${ now }" pattern="yyyy๋
MM์ dd์ผ" /> </li><!-- -->
<hr>
<li> <fmt:formatDate value="${ now }" type="date" /> </li><!-- -->
<li> <fmt:formatDate value="${ now }" type="time" /> </li><!-- -->
<li> <fmt:formatDate value="${ now }" type="both" /> </li><!-- -->
<%
String strNow = "2022๋
12์ 13์ผ";
// ๋ ์ง ํ ๋ณํ
// 1. fmt:parseDate ํ๊ทธ
// 2. java ์ฝ๋ฉ
// Date now = new Date(2022-1900, 12-1, 13);
// 3. java ์ฝ๋ฉ
/*
String pattern = "yyyy๋
MM์ dd์ผ";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date now = sdf.parse(strNow);
*/
// 4.
/*
String pattern = "yyyy๋
MM์ dd์ผ";
DateTimeFormatter f = DateTimeFormatter.ofPattern(pattern);
LocalDate now = LocalDate.parse(strNow, f);
*/
%>
<fmt:parseDate value="<%= strNow %>" pattern= "yyyy๋
MM์ dd์ผ" var="now"/>
now : ${ now }
p 332 JSTL์ EL์์ ์ฌ์ฉํ ํจ์๋ฅผ ์ ๊ณต
p 334 ์์ ์ฝ๋ฉ~
</pre>
<%
String msg = "<b>Message</b>์
๋๋ค.";
// msg.length()
%>
<!-- EL ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํด์ -->
<c:set var="msg" value="<%= msg %>"/>
${ msg }<br>
${ fn:length( msg ) }<br>
${ fn:toUpperCase( msg ) }<br>
${ fn:substring( msg , 1, 4 ) }<br>
${ fn:contains( msg , "ssa" ) }<br>
${ fn:indexOf( msg , "ssa" ) }<br>
<%-- ${ fn:ํ12.19 ์ ๊ณต๋๋ ํจ์ ( msg ) }<br> --%>
'๐จโ๐ป Web Development > JDBC | JSP | Servlet' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day10] JSP/Servlet 10 - (0) | 2022.12.19 |
---|---|
[Day9] JSP/Servlet 9 - MVC ํจํด, filter (0) | 2022.12.18 |
[Day7] JSP/Servlet 7 - ์ฟ ํค, ์ธ์ , ์ปค๋ฅ์ ํ (0) | 2022.12.16 |
[Day6] JSP/Servlet 6 - ๊ฒ์ํ (0) | 2022.12.15 |
[Day5] JSP/Servlet 5 - JSP 4scope, ์๋ฌํ์ด์ง, ๋ชจ๋ํ(include), ์๋ฐ๋น(javabean) (0) | 2022.12.15 |
์ต๊ทผ๋๊ธ