-->

JSP

   EL    

 

ex01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>
 
 <%-- EL(Expression Language) ${변수 } or ${식 } --%>
 
${2+5 } <br>
${4/5 } <br><!--  몫 -->
${7 mod 5 } <br>  <!-- 나머지 -->
${2 < 3 }<br>
${3.1 le 3.2 } <br>  <!-- less 작다 true false 결과 true --> 
${ 3 gt 2 } <br><!--  크다 true false  결과 true -->
${(5 > 3) ? 5: 3 }  <br> <!--  삼항연산자 -->
</body>
</html>


0.8 

true
true 
true 

 

 

 

 

 

 

ex02.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- action 이 생략되면 현재 페이지로 값을 보냄 
method 생략 => get 방식 
-->

<form method="get">
 이름 <input name="name">
 <input type="submit" value="확인">
</form>

<%
String name=request.getParameter("name");
if(name==null)name="";

%>

이름 : <%= name %> <br>
이름 : ${param.name }


</body>
</html>

 

이름   

이름 : 
이름 :

 

 

 

 

 

 

ex03.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<form method="post" action="ex03_result.jsp">
숫자 <input type="number" name="num">
<input type="submit" value="확인">
</form>





</body>
</html>

 

ex03_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%

int num=0;
if(request.getParameter("num")!=null){
	num =Integer.valueOf(request.getParameter("num"));	

	int sum=0;
	for(int i=1; i<=num; i++){
		sum +=i;
	}
	
%>

합계 : <%= sum %> <br>


<%

}else{
	
	out.print("ex03.jsp 에서 실행하세요");
}
	
%>

<hr>

<!-- EL은 단독으로 사용되기보다는 JSTL 과 함께 사용됨 -->
<!-- set var="변수명" value="값" -->
<c:set var="sum" value="0"  />
<!-- forEach var="루프변수" begin="시작" end="끝" -->
<c:forEach var="i" begin="1" end="${param.num }">
	<c:set var="sum" value="${sum +i }" />
</c:forEach>

합계 :  ${sum }



</body>
</html>







 

 

숫자  

 

 

 

합계 : 5050 


합계 : 5050

 

 

 

 

 

ex04.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>


<h2>EL 세션변수</h2>
<!-- 
scope : page, request, session, application
기본값은 page
-->

<%
  //session.setAttribute("name", "김철수");
 //session.getAttribute("name")
 //${sessionScope.name}
%>

<c:set var="name" value="김철수" scope="session" />
<c:set var="age" value="20" scope="session" />
<c:set var="job" value="dba" scope="session" />
세션변수가 저장되었습니다. <br>
<a href="ex04_result.jsp">확인</a>




</body>
</html>

 

 

ex04_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

이름 : ${sessionScope.name } <br>
나이  : ${sessionScope.age } <br>
직업 : ${sessionScope.job }<br>

<hr>

${name }<br>
${age }<br>
${job } <br>


</body>
</html>

 

EL 세션변수

세션변수가 저장되었습니다. 
확인

 

 

 

 

 

이름 : 김철수 
나이 : 20 
직업 : dba


김철수
20
dba 

 

 

 

 

ex05.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>


<%
	ArrayList<String> items=new ArrayList<>();
	items.add("오렌지");
	items.add("사과");
	items.add("포도");
	items.add("복숭아");
//	request.setAttribute("items", items);

%>

<c:set var="items" value="<%=items %>" scope="request" />
<jsp:forward page="ex05_result.jsp" />




</body>
</html>

 

ex05_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- forEach var="루프변수" begin="시작" end="끝"
forEach var="개별값" items="집합"  -->

<c:forEach var="fruit"   items="${items}" >
 ${fruit}<br>
</c:forEach>




</body>
</html>

 

 

오렌지
사과
포도
복숭아

 

 

 

 

 

 

 

ex06.jsp

<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%

HashMap<String, String> map =new HashMap<>();
map.put("포도", "grape");
map.put("오렌지", "orange");
map.put("바나나", "banana");
map.put("사과", "apple");

%>

<c:set var="map" value="<%= map %>" scope="request" />
<jsp:forward page="ex06_result.jsp" />



</body>
</html>

 

 

ex06_result.jsp

<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%-- ${맵.변수 } or ${ 맵["변수"] } --%>
<%-- ${map.포도 }<br> 한글변수명을 사용하면 에러 --%>

${map["포도"] }<br>
${map["오렌지"] }<br>
${map["바나나"] }<br>
${map["사과"] }<br>


<hr>


<%
   HashMap<String, String> map =(HashMap<String,String>)request.getAttribute("map");
   out.println(map.get("포도") +"<br>");
   out.println(map.get("오렌지") +"<br>");
   out.println(map.get("바나나") +"<br>");
   out.println(map.get("사과") +"<br>");
%>






</body>
</html>

 

grape
orange
banana
apple


grape
orange
banana
apple

 

 

 

 

 

 

 

ex07.jsp

<%@page import="member.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%

  MemberDTO dto =new MemberDTO();
  dto.setUserid("kim");
  dto.setPasswd("1234");
  dto.setName("홍길동");

%>


<c:set var="dto" value="<%= dto %>" scope="request" />
<jsp:forward page="ex07_result.jsp"  ></jsp:forward>



</body>
</html>

 

 

 

ex07_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- 
request 영역의 변수는
requestScope.변수에서 requestScope 생략
dto.userid => getUserid() 가 호출됨

 -->
아이디 : ${ requestScope.dto.userid} <br>
비번 : ${ dto.passwd }<br>
이름 : ${ dto.name }<br>




</body>
</html>

 

아이디 : kim 
비번 : 1234
이름 : 홍길동

 

 

 

 

 

 

 

 

jsp

 

about author

PHRASE

Level 1  머나먼나라

댓글 ( 4)

댓글 남기기

작성