본문 바로가기
자바과정/JSP

JSP EL 사용 예제(with 자바빈즈)

by Parkej 2021. 3. 22.

// beantest.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

	<form name=form method="post" action="beantest2.jsp">


		<jsp:useBean id="test" class="el.test.Eltest" scope="request" />

		<select name="sel">
			<%--
	for(String list:test.getProductList()){
		out.print("<option>"+list+"</option>");
	}
--%>

			<%--
	String[] temp = test.getProductList();
	for(String list:temp){
		out.print("<option>"+list+"</option>");
	}
--%>

			<c:forEach items="${test.getProductList() }" var="sel">
				<option>${sel }</option>
			</c:forEach>

		</select> <input type="submit" value="선택">
	</form>


</body>
</html>



// beantest2.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="test" class="el.test.Eltest" scope="request" />
1. test 값 : <%=request.getParameter("sel") %>
<br>
2. num1 : <%=test.getNum1() %>
<br>
3. num2 : <%=test.getNum2() %>
<br>
4. num1 + num2 : <%=test.getNum1() + test.getNum2() %>
<hr>
1. 선택한 상품 : ${param.sel }<br>
2. num1 : ${test.num1 }<br>
3. num2 : ${test.num2 }<br>
4. num1 + num2 :  ${test.num1 + test.num2 }
</body>
</html>

// Eltest.java

package el.test;

public class Eltest {
	
	private String[] productList = {"test1","test2","test3","test4","test5"};
	private int num1=30;
	private int num2=50;

	public String[] getProductList() {
		return productList;
	}

	public int getNum1() {
		return num1;
	}
	public int getNum2() {
		return num2;
	}
}

//// EL 기본 예제들

// jstl.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:set value="hello world" var="msg" />
msg : ${msg}<br>
msg : <%= pageContext.getAttribute("msg") %>
<c:set value="btaman" var ="msg1"/>

<br>

msg : <c:out value="${msg1} "/>
<c:remove var ="msg1"/>
after remove : <c:out value ="${msg1}"/>

<br>

catch<br>
<c:catch var="errMsg">
<%= 9/0 %>
</c:catch>

<br>
<c:set value = "superman" var = "msg"/>
msg : ${msg }<br>
<c:if test="${msg == 'superman' }" var = "result">
test result : ${result}
</c:if>

<form>
<select name="sel">
<option>-</option>
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
</select>
<input type="submit" value="선택">
</form>
<br>

<c:choose>
	<c:when test="${param.sel =='a' }"> a 선택</c:when>
	<c:when test="${param.sel =='b' }"> b 선택</c:when>
	<c:when test="${param.sel =='c' }"> c 산텍</c:when>
	<c:otherwise> a,b,c 이외의 것을 선택 </c:otherwise>
</c:choose>

<c:forTokens items="123-456-789" delims="-" var="sel">
	<br>${sel}
</c:forTokens>
</body>
</html>

// url EL 예제들

// import.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

import를 이용해 포함한 것.<hr>
<c:import url="set.jsp" var="myurl"/>
<c:out value="${myurl }" escapeXml="false"/>
<hr>
</body>
</html>


// redirect.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:redirect url="/Core/jstl.jsp">
<c:param name="sel">a</c:param>
</c:redirect>
</body>
</html>

// set.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
페이지 이동확인
</body>
</html>

// url.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:url value="/Core/jstl.jsp" var="target">
	<c:param name="sel">a</c:param>
</c:url>
<hr>
단순출력 : ${target }<br>
링크연동 : <a href="${target }">jstl.jsp-a선택</a>

</body>
</html>
반응형

'자바과정 > JSP' 카테고리의 다른 글

JSP 실습 (이벤트 등록)  (0) 2021.03.15
JSP 파일 업로드  (0) 2021.03.12
JSP 회원가입 (자바빈즈 사용)  (0) 2021.03.10
JSP 실습 Template  (0) 2021.03.10
JSP 실습 application과 session (scope)  (0) 2021.03.10

댓글