JAVA

login project _JSP & el/jstl 방식 비교

장꾸꾸 2020. 11. 16. 09:57

step06_JSTLProject.txt
0.00MB
step07_JSTLProject.txt
0.00MB

 

 

daesuni.github.io/jstl/

 

JSTL의 기본 개념과 사용방법 정리

1. JSTL이란?

daesuni.github.io

<%@ 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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<fmt:requestEncoding value="UTF-8"/>
<form action="/eljstl/jstltest/jstlEx3.jsp"  method="post">
	반복하고싶은문구 : <input type="text" name="str" ><br>
	반복하고싶은횟수 : <input type="text" name="num"><br>
	<input type="submit" value="실행">
</form><br>

<c:forEach begin="1" end="${param.num}" step="1">
	${param.str}<br>
</c:forEach><br><br>
</body>
</html>

<%@page import="java.util.Date"%>
<%@ 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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="date" value="<%=new Date()%>"/>
오늘날짜 : <fmt:formatDate value="${date}"/><br>
현재시간 : <fmt:formatDate value="${date}" type="time"/><br><br> 

<fmt:formatNumber value="12345678" groupingUsed="true"/>원<br>
<fmt:formatNumber value="1.2345678" pattern="#.##"/><br>
<fmt:formatNumber value="1.2" pattern="#.##"/><br>
<fmt:formatNumber value="0.12" type="percent"/><br>
<fmt:formatNumber value="12345678" type="currency"/><br>
</body>
</html>

 

<%@ 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>
<c:set var="country" value="${'japan'}"/>
<c:if test="${!empty country}">
	국가명:${country}
</c:if>
<c:if test="${empty country}">
	국가명: empty
</c:if><br><br>

<c:if test="${country=='korea'}">
	국가명: 대한민국
</c:if>
<c:if test="${country!='korea'}">
	국가명: 기타 다른 나라
</c:if>
</body>
</html>

jstl은 if와 else가 안됨. 그래서 ==과 !=으로 나눠줌

 

<%@ 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>
<c:set var="color" value="${'black'}"/>

<c:choose>
	<c:when test="${color=='red'}">빨간색</c:when>
	<c:when test="${color=='green'}">초록색</c:when>
	<c:when test="${color=='blue'}">파랑색</c:when>
		<c:otherwise>기타색</c:otherwise> 
<%-- 		요렇게도 쓸 수 있음 <c:out value='기타색'/> --%>
</c:choose>
</body>
</html>

 

 

jstlInput.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jstlInput.html</title>
</head>
<body>
<form action="/eljstl/jstltest/jstlResult.jsp" method="post">
<table border="1">
	<tr>
		<td>이름</td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>나이</td>
		<td><input type="text" name="age"></td>
	</tr>
	<tr>
		<td>색깔</td>
		<td>
		<select name="color" style="width:100px">
		<option value="red">빨 강</option>
		<option value="green">초 록</option>
		<option value="blue">파 랑</option>
		<option value="magenta">보 라</option>
		<option value="etc">기 타</option>
		</select>
		</td>
	</tr>
	<tr>
		<td>취미</td>
		<td>
		<input type="checkbox" name="hobby" value="잠자기">잠자기
		<input type="checkbox" name="hobby" value="영화">영화
		<input type="checkbox" name="hobby" value="게임">게임
		<input type="checkbox" name="hobby" value="운동">운동
		<input type="checkbox" name="hobby" value="음악">음악		
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
		<input type="submit" value="SEND">
		<input type="Reset" value="CANCEL">
		</td>
	</tr>
</table>
</form>
</body>
</html>

 

jstlResult.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 charset="UTF-8">
<title>jstlResult.jsp</title>
</head>
<body>
<fmt:requestEncoding value="UTF-8"/>
이름 : ${param.name}<br>
나이 : ${param.age}
<c:if test="${param.age >= 19}">
	(성인)
</c:if>
<c:if test="${param.age < 19}">
	(청소년)
</c:if><br>

좋아하는 색상 : 
<c:choose>
	<c:when test="${param.color=='red'}">빨간색</c:when>
	<c:when test="${param.color=='green'}">초록색</c:when>
	<c:when test="${param.color=='blue'}">파란색</c:when>
	<c:when test="${param.color=='magenta'}">보라색</c:when>
	<c:otherwise>기타색</c:otherwise>
</c:choose><br>
취미 : 
<c:forEach items="${paramValues.hobby}" var="ob">
	${ob}&nbsp;&nbsp;&nbsp;
</c:forEach><br><br>

${paramValues.hobby[0]}
${paramValues.hobby[1]}
${paramValues.hobby[2]}
${paramValues.hobby[3]}
${paramValues.hobby[4]}

</body>
</html>
<!-- 
이름 : 홍길동
나이 : 17세(청소년)     ==>   20세(성인)    (if이용)
좋아하는색상 : 빨간색          ==>  초록색, 파란색,보라색,기타색   (choose이용)
취미 : 잠자기  영화  운동      ==>  (forEach이용)
뒤로
-->





 

user.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="userProc.jsp" method="POST">
	<div>
		이름 <input type="text" name="name">
	</div>
	<div>
		영어 <input type="text" name="english">
	</div>
	<div>
		수학 <input type="text" name="math">
	</div>
	<div>
		<button type="submit">결과보기</button>
	</div>
</form>
</body>
</html>

userProc.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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<fmt:requestEncoding value="UTF-8"/>
<c:set var="avg" value="${(param.english + param.math) / 2}" />
나의 이름은 ${param.name}입니다. <br>
나의 평균은 ${avg}입니다. <br>

<c:if test="${avg >= 60}">
	결과는 합격입니다!
</c:if>
<c:if test="${avg < 60}">
	결과는 불합격입니다!
</c:if>
</body>
</html>