11/28 52-4 [JSP] EL의 기타 내장객체
여기선 그 다운 받은 JSTL을 사용할 거라 맨 위 상단에
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 넣어주고 시작합니다.!
이제 기타 내장객체를 하나 씩 살펴봅시다
EL에서는 저장 된 값 출.력.만. 가능하다 !
값을 설정하거나 영역에 속성을 저장(설정)하지 못한다
1.EL의 pageContext 내장객체
: jsp의 pageContext 내장객체와 같다. 단 자바빈 규칙으로 접근가능한 메소드만 제공한다(static?)
[ 자바코드로 Context루트얻기 ]
방법1.(request 내장객체) : <%= request.getContextPath() %>
방법2(pageContext내장객체) :<%=((HttpServletRequest)pageContext.getRequest()).getContextPath() %>
/* 변수에 담아서 출력해도 됨
ServletRequest req = pageContext.getRequest();
String contextPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
<%= contextPath%> 요렇게 !
*/
[ el로 Context루트 얻기]
\${pageContext.request.contextPath} : ${pageContext.request.contextPath}
[자바코드로 세션의 유효시간 얻기]
<%=session.getMaxInactiveInterval() %> 초<br/> =>세션에서 바로
<%=request.getSession().getMaxInactiveInterval() %>초<br/> =>리퀘스트에서 세션
<%=pageContext.getSession().getMaxInactiveInterval() %>초<br/> =>페이지컨텍스트에서 세션
<%=((HttpServletRequest)pageContext.getRequest()).getSession().getMaxInactiveInterval() %>초<br/>
=> 페이지컨텍스트에서 리퀘스트 를 리퀘스트로 형변환해서 거기서 세션 뭐여이게....
[EL로 세션의 유효시간 얻기]
${ pageContext.session.maxInactiveInterval }초<br/>
${ pageContext.request.session.maxInactiveInterval}초<br/> ==>이건 알아서 항변환되기 때문에 변환할 필요가 없다!
get이 빠지고 대문자를 소문자로변형하면 되네 오오오 점점영역이 커지네?
2.EL의 heard 내장객체
[자바코드로 요청헤더값 얻기]
<%=request.getHeader("user-agent") %>
[EL로 요청헤더값 얻기 ]
el은 request 내장객체가 없대! 그래서 pageContext를 이용하는 줄 알았더니 안된다네!
무조건 header내장객체를 사용하자 ㅎㅎㅎ..
${pageContext.request.header.user-agent} : ${pageContext.request.header.user-agent } pageContext 이용 안되구요
\${header.user-agent } : ${header.user-agent } 해더명에 특수문자 들어가면 .으로 접근 안되구요
\${header["user-agent"] } : ${header["user-agent"] } 이렇게만 가능합니다 !
3.EL의 cookie내장객체
우선 자바코드로 쿠키 설정
<%
Cookie cookie = new Cookie("KOSMO","한소인");
cookie.setPath(reques.getContextPath());
response.addCookid(cookie);
%>
[자바코드로 쿠키값 얻기]
<%
Cookie[ ] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cook : cookies){
String name = cook.getName();
String value = cook.getValue();
out.println(name+":"+value+":"+"<br/>") ;
}
}
%>
[EL로 쿠키값 얻기]
방법1. 쿠키내장객체 이용
\${pageContext.request.cookies} : ${pageContext.request.cookies} 요 이게 됨,...
방법2. 쿠기 내장객체 미사용 $에서 하나 씩 꺼내와서 item에 담는다
<c:forEach var -="item" item="${pageContext.request.cooies}">
${item.name} : ${item.value} <br/>
</c:forEach >
방법3. cookie.쿠키명.value로 쿠키명에 해당하는 쿠키값을 얻을 수 있다.(내장객체 사용)
\${cookie.KOSMO.value}:${cookie.KOSMO.value}
4.EL의 initParam내장객체
컨텍스트 초기화 파라미터를 얻어 올수 있는 내장 객체
[Context초기화 파라미터] : Context를 구성하는 모든 서블릿에서 공유할 수 있는 변수
-Context초기화 파라미터는 ServletContext의
getInitParameter("파라미터명")메소드를 통해서 얻는다.
[Servlet초기화 파라미터] : 해당 서블릿에서만 사용할 수 있는 변수
-Servlet초기화 파라미터는 ServletConfig의
getInitParameter("파라미터명")메소드를 통해서 얻는다
※초기화 파라미터는 web.xml에 등록!!
[자바코드로 컨텍스트 초기화 파라미터 얻기]
<ul class="list-unstyled">
<li>ORACLE-URL : <%= application.getInitParameter("ORACLE-URL") %></li>
<li>ORACLE-DRIVER : <%= application.getInitParameter("ORRACLE-DRIVER") %></li>
<li>PAGE-SIZE : <%= application.getInitParameter("PAGE-SIZE") %></li>
<li>BLOCK-PAGE : <%= application.getInitParameter("BLOCK-PAGE") %></li>
</ul>
[el로 컨텍스트초기화 파라미터 얻기] : 오 진짜 앞에 영역 빼고 get 빼고 대문자로 소문자로하면 끝 !
<ul class="list-unstyled">
<li>ORACLE-URL :${ initParam["ORACLE-URL"]}</li>
<li>ORACLE-DRIVER : ${initParam["ORACLE-DRIVER"]}</li>
<li>PAGE-SIZE : ${initParam["PAGE-SIZE"]}</li>
<li>BLOCK-PAGE : ${initParam["BLOCK-PAGE"]}</li>
<li>BLOCK-PAGE : \${pageContext.servletContext.initParameter["BLOCK-PAGE"]}</li> <!-- 이런속성 없대 -->
</ul>
[컬렉션에 저장된 객체를 el로 출력]
<%
//데이터 준비
MemberDTO first=new MemberDTO("KIM","1234","김길동","20");
MemberDTO second=new MemberDTO("LEE","1234","이길동","30");
//리스트계열컬렉션에 객체 저장
List<MemberDTO> list = Arrays.asList(first,second);
//맵계열
Map<String,MemberDTO> map = new HashMap<>();
map.put("first",first);
map.put("second",second);
%>
[자바코드로출력]
<h4>자바코드로 출력</h4>
<h5>리스트계열 컬렉션</h5>
<%=list %>
<h6>일반 for문으로 출력</h6>
<% for(int i=0 ; i <list.size(); i++) {%>
<%=list.get(i) %><br/>
<%} %>
<h6>확장 for문으로 출력</h6>
<%
for(MemberDTO member: list) { %>
<%=member %>
<% }
%> <h5>맵계열 컬렉션</h5>
<%
Set<String> keys = map.keySet();
for(String key : keys){ %>
<%=key %> : <%=map.get(key) %> <br/>
<% }
%>
[EL로 출력]
<h4>EL로 출력</h4>
<c:set var="list" value="<%=list %>" />
<c:set var="map" value="<%=map %>" />
<h5>리스트계열 컬렉션</h5>
<h6>JSTL 미사용</h6>
\${list }: ${list } <br/>
\${list[0]} : ${list[0]} <!-- list.get(0)와 같다 -->
<ul class="list-unstyled">
<li>이름 : ${list[0].name},아이디:${list[0].id},비번:${list[0].pwd}</li>
<li>이름 : ${list[1].name},아이디:${list[1].id},비번:${list[1].pwd}</li>
</ul>
\${list[100]} : ${list[100]} <!--출력만 안 됨 -->
<h6>JSTL사용 :저장된 객체 수 모름</h6>
<ul class="list-unstyled">
<c:forEach var="item" items="${list}">
<li>이름:${item.name},아이디:${item.id},비번:${item.pwd}</li>
</c:forEach>
</ul>
<h5>맵계열 컬렉션</h5>
<h6>JSTL 미 사용 : 키를 알 때</h6>
\${map} : ${map} <br/>
<!-- map.get("first")와 같다 -->
\${map.first} : ${map.first}
<ul class="list-unstyled">
<li>이름:${map.first.name },아이디:${map['first'].id },비번:${map["first"]['pwd'] }</li>
<li>이름:${map.second.name },아이디:${map['second'].id },비번:${map["second"]['pwd'] }</li>
</ul>
<h6>JSTL 사용:키를 모를 때</h6>
<ul class="list-unstyled">
<!--jstl 의 forEach사용시 var는 변수명, value는 벨류값 -->
<c:forEach var="item" items="${map}">
키는 ${item.key }, 값은 ${item.value }
이름:${item.value.name }, 아이디:${item.value.id }, 비번:${item.value.pwd }<br/>
</c:forEach>
</ul>
\${ }에서 자바객체의 메소드 호출 가능
단,객체의 값을 변화시키는 메소드는 호출이 불가할 수 있다(에러)
호출가능한 메소드는 주로 게터류의 메소드들만 가능
<h4>컬렉션의 읽기용 메소드 호출</h4>
\${list.isEmpty()} : ${list.isEmpty()}<br/>
\${list.size()} : ${list.size()}<br/>
\${map.isEmpty()} : ${map.isEmpty()}<br/>
\${map.size()} : ${map.size()}<br/> \${list.clear()} <!-- 얘는 에러남 -->
${map.clear()} <!-- 얘는 되네 올 -->
\${map.size()} : ${map.size()}<br/>