서블릿 10장

Recursion_Error·2023년 6월 2일
0

표현언어 Expression Language

자바코드를 사용하지 않고 출력,추출

변수, 숫자, 문자열, 수식, boolean, null을 사용할 수 있음

EL 사용을 위해 isElgnored="false"로 지정하면 EL 구문이 문자열로 인식됨.

EL 내장객체

EL 내장객체 - 영역 관련 내장객체

  • pageScope : page 영역에 바인딩된 속성을 참조하는 내장객체

    	${내장객체.속성이름}
request.setAttribute("nation","korea")

/* request 내장 객체에 바인딩 되어 있는 nation 속성 참조 */
${requestScope.nation}

/* 내장 객체 이름을 생략하고 속성 이름만 출력*/
${nation}
바인딩 우선순위
pageScope > requestScope > sessionScope > applicationScope
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	request.setAttribute("city", "seoul");
	session.setAttribute("nation", "korea");
	application.setAttribute("nation", "france");
%>
<jsp:forward page = "scopeTo.jsp"/>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	out.println(request.getAttribute("city"));
	out.println("<br>");
%>

1. ${city} <br>

<%-- session이 application 영역보다 우선순위가 높기 때문에 korea 출력 --%>
2. ${nation} <br>

<%-- applicationScope 내장객체 이름을 지정했으므로 france --%>
3. ${applicationScope.nation}


<%-- 
출력 :
 seoul
 1. seoul
 2. korea
 3. france
--%>
  • requestScope : request 영역에 바인딩된 속성을 참조하는 내장객체
  • sessionScope : session 영역에 바인딩된 속성을 참조하는 내장객체
  • applicationScope : application 영역에 바인딩된 속성을 참조하는 내장객체

EL 내장객체 - 파라미터 관련 내장객체

클라이언트에서 전달된 파라미터의 값을 추출해 출력
  • param : 클라이언트에서 전달되는 하나의 파라미터 값을 추출해 출력
	${param.파라미터 이름}
  • paramValues : 클라이언트에서 전달되는 하나 이상의 값을 가진 파라미터 값을 추출해 출력
	${paramValues.파라미터 이름[index]}

EL 내장객체 - 헤더 관련 내장객체

  • header : 하나의 값을 가지는 헤더 값 출력
	//지정 헤더 값 추출 및 출력
	${header.헤더이름}
    ${header.["헤더이름"]}

헤더 이름에 '_'기호가 아닌 다른 특수문자가 있다면 headerValues 사용

  • headerValues : 하나 이상의 값을 가지는 헤더 값 출력
${header.헤더이름[index]}
// 특수문자, 기호가 있을 경우 밑에 방법 사용
${header.["헤더이름"][index]}

EL 내장객체 - 쿠키 관련 내장객체

  • cookie : value 속성을 사용해 쿠키 출력
${cookie.쿠키이름.value}
${cookie.쿠키이름.["value"]}
// '_' 가 아닌 특수문자가 있다면 아래 두 방법을 사용
${cookie["쿠키이름"].value}
${cookie["쿠키이름"].["value"]}
	<%-- 쿠키 생성 --%>
	Cookie cookie1 = new Cookie("userName", "hong");
	response.addCookie(cookie1);

EL 내장객체 - JSP 관련 내장객체

현재 수행중인 JSP문서를 참조할 수 있는 pageContext 내장 객체 제공
  • pageContext : 페이지 정보 추출

표현언어를 이용한 빈즈 멤버 변수 출력

${빈즈이름.멤버변수이름}
아이디, 이름, 메일 추출하기
package chapter10;

public class ExpBean {

	private String userId;
	private String userName;
	private String userMail;
	
    /* Getter Setter */
	public String getUserId() {
		return userId;
	}
	
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public String getUserName() {
		return userName;
	}
	
	public void setusername(String userName) {
		this.userName = userName;
	}
	
	public String getUserMail() {
		return userMail;
	}
    
	public void setUserMail(String userMail) {
		this.userMail = userMail;
	}
}
<form name="myform" method="post" action="expBeanTo.jsp">
		아이디: <input type="text" name="userId" size=20><br>
		이 름: <input type="text" name="userName" size=20><br>
		메 일: <input type="text" name="userMail" size=30><br>
		<input type="submit" value="저장" ><br>
</form>
<%@ page import="chapter10.*" %>

<% request.setCharacterEncoding("utf-8"); %>
 
<jsp:useBean id="expBean" class="chapter10.ExpBean" scope="request"/>
<jsp:setProperty property="*" name="expBean"/>

<b> Getter를 이용한 추출 </b> <br>
아이디 : <%=expBean.getUserId() %><br>
이름 : <%=expBean.getUserName()%><br>
메일 : <%=expBean.getUserMail()%><br>

<b> 액션 태그를 이용한 추출</b> <br>
아이디 : <jsp:getProperty property="userId" name="expBean"/><br>
이름 : <jsp:getProperty property="userName" name="expBean"/><br>
메일 : <jsp:getProperty property="userMail" name="expBean"/><br>

<b>EL을 이용한 추출</b> <br>
아이디 : ${expBean.userId} <br>
이름 : ${expBean.userName} <br>
메일 : ${expBean.userMail} <br>

profile
무한으로 돌리는 . . .

0개의 댓글