본문 바로가기
강의

0224 JSP2

by IT새내기IM 2023. 2. 24.

JSP2 - 8 - 149~150

 

8 - 151

el_test1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="el_test2.jsp" method="post">

<table>
	<tr>
		<td>이름 : </td>
		<td><input type="text" name="name" value="홍길동"></td>
	</tr>
	<tr>
		<td colspan=2 align=center><input type="submit" value="입력"></td>
	</tr>
</table>
</form>

</body>
</html>

el_test2  ( EL에서는 request객체로 받는게 아니라 param이라는 객체로 받는다.)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
request.setCharacterEncoding("utf-8");
String name= request.getParameter("name");

%>
name :<%=name%>
name : ${param.name}<br>
name : ${param['name']}<br>
<hr>
<!-- EL에서는 request객체로 받는게 아니라 param이라는 객체로 받는다.) -->
<%
request.setAttribute("id", "1111");
// id라는 속성과 1111라는 값을 추가하겠다.
%>
id : <%=request.getAttribute("id")%>
id : ${requestScope.id}<br>

</body>
</html>

 

152
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<body>

<!-- EL은 스크립틀릿처럼 많은 기능을 제공하지않음  -->
<!-- 모델,컨트롤러에서 쓰인기능을 간단하게 보이는역햘이라서 -->

\${5+7} = ${5+7 }<br>
\${10 % 3} = ${10 % 3}<br> 
 \${7 < 8} = ${7 < 8} <br>
 ${5+3 == 8 ? "참" : 10}
</body>

</html>
156  관리자 0 07-15
155  관리자 0 07-15
154  관리자 0 07-15
153

c태그 사용을위해서는

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 을 선언해줘야함

jar-core-1.1.3.jar도 필요

 

jstl_core_ex1,2,3

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

<c:set var="test" value="hi! 안녕" />
<c:out value="${test }" /><br><hr>
out : ${test }<br><hr>
<c:remove var="test"/>
remove : ${test } <br>
<c:catch var="err">
<%=10/0 %>
</c:catch>
err : ${err }   
<%-- err은 <% %>(스크립틀릿)은 적용되지만  $(EL)은 무시함 --%>
<br><hr>

<c:if test="${5>0}">  
<!-- test는조건 -->
 참이네
</c:if>

<c:if test="${5<0}">  
<!-- 참일때만뜸 거짓이라 출력x -->
 거짓이네    
</c:if>
<hr>

<c:set var="a" value="-2"/>

<c:choose>
	<c:when test="${a > 0}">
	양수
	</c:when>
	<c:when test="${a == 0}">
	0
	</c:when>
	<c:otherwise>
	음수
	</c:otherwise>
</c:choose>
<!-- if , else if , else의 역할이됨 -->
<hr>
<c:forEach var="aa" begin="1" end="10" step="2">
${aa } ,
</c:forEach>
<!-- 반복문의영역 -->

<hr>
<c:forTokens var="bb" items="a,b,c,d,e" delims=",">
	${bb }
</c:forTokens>
<!-- 변수에 리스트들을 주는느낌 delims로 구분하고 items를 변수를 통해 출력-->
<hr>
<c:set var="cc" value="홍길동,유관순,유산슬" />
<c:forTokens var="dd" items="${cc }" delims=",">
${dd }
</c:forTokens>
<hr>
<script>
// 	location.href="index.jsp";
</script>
<%
// response.sendRedirect("index.jsp");
String id = "a123";
%>
<c:redirect url="index.jsp" />
<c:set var="pass" value="b213" />

<a href="index.jsp?id=<%=id%>&pass=${pass}">이동</a>

</body>
</html>

JSTL(JSP Standard Tag Library) fmt

1. jstl_fmt_ex.jsp

 

- JSTL fmt란 국제화/형식화의 기능을 제공해주는 JSTL 라이브러리다.

- 구체적으로 설명하면 국제화는 다국어 내용을 처리, 형식화는 날짜,숫자 형식 등을 처리하는 것을 의미한다.

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>d

을 써줘야함

 

연습문제 계산기 EL을 이용한

jstl - test01

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

<form action="EL_cul_proc.jsp" method="post">
	<input type="text" name="ex1">
	<select name="ex2">
		<option value="+"> + </option>
		<option value="-"> - </option>
		<option value="*"> * </option>
		<option value="/"> / </option>
	</select> 
	<input type="text" name="ex3"> 
	<input type="submit" value="확인"> 

</form>

</body>
</html>

EL-cul_proc

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

<c:set var="ex1" value="${param.ex1}"/>
<c:set var="ex2" value="${param.ex2}"/>
<c:set var="ex3" value="${param.ex3}"/>

<c:if test="${param.ex2 == '+' }">
	${param.ex1 + param.ex3 }
</c:if>

<c:if test="${param.ex2 == '/' }">

	<c:choose>
	<c:when test="${param.ex3 == 0}">
	0으로 나눌수없습니다.
	</c:when>
	<c:otherwise>
	${param.ex1 / param.ex3 }
	</c:otherwise>
	</c:choose>
	
</c:if>

</body>
</html>

연습문제 test02 약에 requestScope가 똑깥이 s 인경우

requestScope가 생략되면 s[0]이 어느 객체것인지 모르는데 제일작은영역에 해당하게됨 

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

문자열 : ${"test" }<br>
문자열 : ${'test' }<br>
정수 : ${123 }<br>
<!-- 변수는 숫자로 못만든다.  -->
소수점 : ${12.34 }<br>
Boolean : ${true }<br>
null : ${null }<br>
<hr>
<%
int ss[] = new int[] {10,20,30};

pageContext.setAttribute("s", new int[] {1,2,3});
request.setAttribute("s", new int[] {4,5,6});
%>
<%=ss[0] %> <%=ss[1] %> <%=ss[2] %><br>
${s[0] } ${pageScope.s[1] } ${pageScope.s[2] }<br>
${s[0] } ${requestScope.s[1] } ${requestScope.s[2] }<br>



<c:set var="ex1" value="${param.ex1}"/>

</body>
</html>

 

연습문제 test03

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

<form method="post">
id : <input name="id"><br>
pass : <input name="pass"><br>
<button>login</button>
<hr>
<h3>스크립틀릿</h3>
id : <%=request.getParameter("id")%>
pass : <%=request.getParameter("pass")%> 
<h3>EL</h3>
id : ${param.id}
pass : ${param.pass}
 
</form>
</body>
</html>

 

test04

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

<c:set var="sum" value="0" />
<c:forEach var="i" begin="1" end="10">
	<c:set var="sum" value="${sum=sum+i}" />

</c:forEach>
총합 : <c:out value="${sum}" />

</body>
</html>

EL은 sum += i; 처리가 안됨  , 총합:55

 

test05 별찍기

<%@ 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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

*<br>
**<br>
***<br>
****<br>

<c:forEach var="i" begin="0" end="3">
	<c:forEach var="j" begin="0" end="${i }">*</c:forEach>
	<br>
</c:forEach>


<c:set var="i" value="3" />
<c:forEach var="x" begin="0" end="${i }">
	<c:forEach var="y" begin="0" end="${i }">*</c:forEach>
	<c:set var="i" value="${i-1 }" />
	<br>
</c:forEach>

</body>
</html>

ㅁㄴㅇ

'강의' 카테고리의 다른 글

0227 - MVC패턴2 LinkedList , Vector , ArrayList  (0) 2023.02.27
0224 2nd project 연습, JQUERY  (0) 2023.02.24
0222 JSP2  (0) 2023.02.23
0223 JSP2  (0) 2023.02.23
0222 MySQL lec01  (0) 2023.02.22

댓글