웹개발 교육/jsp

[63일] jsp (43) - EL

ewok 2022. 10. 26. 13:14

el 폴더에서 작업

 

elTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>elTest1.jsp</title>
</head>
<body>
	<h3>* EL(Expression Language) 표현언어 *</h3>
	
	123+456<br>
	표현식 : <%=123%><br>
	표현식 : <%=123+456%><br>
	<hr>
	표현언어 : ${123}<br>
	표현언어 : ${123+456}
	<hr>
	
	<h3>* EL에서 사용되는 연산자 *</h3>
	더하기 : ${123+456}<br>
	빼기 : ${123-456}<br>
	곱하기 : ${123*456}<br>
	나누기 : ${123/456}<br>
	나머지 : ${123%456}<br>
	
	나누기 : ${123 div 456}<br>
	나머지 : ${123 mod 456}<br>
	
	2가 3보다 작다 : ${2 < 3}<br>
	2가 3보다 크다 : ${2 > 3}<br>
	
</body>
</html>

 

EL 파라미터

elTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>elTest2.jsp</title>
</head>
<body>
	<h3>* EL 파라미터 예제 *</h3>
	<form action="elTest2ok.jsp">
		아이디 : <input type="text" name="id"> <br>
    	비번  : <input type="password" name="pw"> <br>
        <input type="submit" value="확인">
	</form>
</body>
</html>

 

elTest2ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>elTest2ok.jsp</title>
</head>
<body>
	<h3>* EL 파라미터 결과 *</h3>
	1)JSP방식<br>
	아이디 : <%=request.getParameter("id")%><br>
	비번 : <%=request.getParameter("pw")%><br>
	
	<hr>
	
	2)EL방식<br>
	아이디 : ${param.id}<br>
	비번 : ${param.pw}<br>

</body>
</html>

EL 내장객체

elTest3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>elTest3.jsp</title>
</head>
<body>
	<h3>* EL 내장객체 (Collection) *</h3>
<%
	//1)JSP 내장객체
	//->pageContext, request, session, application
	
	//내부변수 선언
	//※ 참조 : myweb프로젝트의 /webapp/scope 폴더
	pageContext.setAttribute("kor", 100);  //현재 페이지에서만 유효
	request.setAttribute("eng", 200);      //전역변수(부모와 자식페이지에서만 유효)
	session.setAttribute("mat", 300);      //전역변수(모든페이지에서 유효 + 개인사용자 + 시간)
	application.setAttribute("aver", 400); //전역변수(모든페이지에서 유효 + 모든사용자)
%>
	
	1)JSP<br>
	kor : <%=pageContext.getAttribute("kor")%><br>
	eng : <%=request.getAttribute("eng")%><br>
	mat : <%=session.getAttribute("mat")%><br>
	aver : <%=application.getAttribute("aver")%><br>
	<hr>
	
<!-- 
	2)EL의 내장객체(Collection)
	-> param, pageScope, requestScope, sessionScope, applicationScope
 -->
 	2)EL<br>
 	kor : ${pageScope.kor}<br>
 	eng : ${requestScope.eng}<br>
 	mat : ${sessionScope.mat}<br>
 	aver : ${applicationScope.aver}<br>
 	<hr>
 	
 	<!-- EL에서는 내장객체명을 생략할 수 있다(pageScope, requestScope, sessionScope, applicationScope) -->
 	<!-- EL에서는 page -> request -> session -> application 순으로 자동으로 검색 -->
 	kor : ${kor}<br>
 	eng : ${eng}<br>
 	mat : ${mat}<br>
 	aver : ${aver}<br>
 	<hr>
	
	<!-- 각 Scope에 내장변수명이 동일한 경우 -->
<%
	pageContext.setAttribute("num", 500);
	request.setAttribute("num", 600);
	session.setAttribute("num", 700);
	application.setAttribute("num", 800);
%>
	<!-- 내장객체명이 생략되면 자동으로 작은 영역 순으로 찾게 된다 -->
	num : ${num} <!-- 500 -->
	<hr>
	
	<!-- 각 Scope의 내장변수가 없는 경우 -->
	JSP : <%=request.getAttribute("uname")%> <!-- null -->
	<br>
	EL : @${requestScope.uname}@ <!-- 빈문자열 -->
	
</body>
</html>

 

 

JSP와 EL의 값 공유

elTest4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>elTest4.jsp</title>
</head>
<body>
	<h3>* JSP와 EL의 값 공유 *</h3>
<%
	//JSP와 EL은 현재 페이지에서는 서로 값을 공유할 수 없다
	String uname = "아이티윌";
%>
	이름(JSP) : <%=uname%><br><!-- 아이티윌 -->
	이름(EL) : ${uname}       <!-- 빈문자열 -->
	<hr>
		
<%
	//현재 페이지에서 JSP와 EL이 서로 값을 공유하기 위해서는 pageScope를 활용한다
	pageContext.setAttribute("uid", "KOREA");
%>
	아이디(JSP) : <%=pageContext.getAttribute("uid")%><br> <!-- KOREA -->
	아이디(EL) : ${pageScope.uid}<br> <!-- KOREA -->
	아이디(EL) : ${uid}<br> <!-- KOREA -->
	
</body>
</html>