JSP 페이지에서는 EL(Expression Language)의 내장 객체를 사용하여
세션이나 쿠키에 있는 데이터를 손쉽게 가져올 수 있다.
대부분의 객체는 내장객체.key값 의 형태로 가져올 수 있다.
세션 (Session)
세션의 경우 sessionScope 내장 객체를 사용할 수 있다.
<% session.getAttribute("key값"); %>
${sessionScope.key값}
요청 (Request)
요청으로 넘어온 데이터는 requestScope 내장 객체를 사용할 수 있다.
<% request.getAttribute("key값); %>
${requestScope.key값}
페이지 (PageContext)
pageContext에 저장된 데이터는 pageScope를 사용한다.
<% pageContext.setAttribute("data", data); %>
<%= pageContext.getAttribute("data") %>
${pageScope.data}
* request의 메서드에 접근
request의 메서드인 getRemoteUser() 나 contextPath() 같은 메서드의 값을 받기 위해서는 request 앞에 pageContext를 명시해주어야한다.
${pageContext.request.contextPath}
${pageContext.request.remoteUser}
파라미터 (Parameter)
파라미터로 넘어온 데이터는 param 내장 객체를 사용할 수 있다.
<% request.getParameter("key값"); %>
${param.key값}
* 파라미터 다중값
체크박스와 같이 다중값이 나오는 파라미터의 경우 paramValues를 사용한다.
<% request.getParameterValues("key값"); %>
${paramValues.key값}
어플리케이션 (Applictaion)
어플리케이션에 저장된 데이터는 applicationScope 내장 객체를 사용할 수 있다.
<% application.getAttribute("key값"); %>
${applicationScope.key값}
쿠키 (Cookie)
쿠키의 경우 cookie 내장 객체를 사용할 수 있다.
<% request.getCookies(); %>
${cookie.쿠키이름}
* null 체크
EL에서 null 체크는 '== null ' 연산을 통해 확인할 수도 있지만 'empty', 'not empty' 키워드를 사용해 비교할 수도 있다.
<!-- username이 null인지 확인 -->
<!-- null일 경우 true, null이 아닐 경우 false -->
${empty param.username}
<!-- username이 null이 아닌지 확인 -->
<!-- null일 경우 false, null이 아닐 경우 true -->
${not empty param.username}
'JSP' 카테고리의 다른 글
| JSTL Fmt를 이용한 다국어 처리 (0) | 2023.01.11 |
|---|---|
| commons-fileupload 라이브러리 DiskFileUpload 클래스를 사용한 파일 업로드 (0) | 2023.01.04 |
| 내장 객체 (Implicit Object) (0) | 2022.12.28 |
| getRemoteAddr()의 반환값을 IPv4로 변경 (0) | 2022.12.28 |
| JSTL 사용하기 (0) | 2022.12.23 |