본문 바로가기

Programming/Servlet & JSP

JSTL 연습하기

WebContent/lib > jstl.jar, standard.jar 가져오기

 

exam01

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<!--기본 저장Scope는 page-->
	<c:set var="num1" value="5" scope="session" />
	<c:set var="num2" value="7" />
	<c:set var="result" value="${num1 * num2 }" />
	${num1}과 ${num2}의 곱은 ${result}
</body>
</html>

 

exam02

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
<c:set var="num" value="100" scope="session"/>
<c:set var="num" value="200" scope="application"/>
<!--name이 같고 scope가 다르면 모두 삭제가 된다.-->
<c:remove var="num" scope="session"/>
${sessionScope.num}
${applicationScope.num}
</body>
</html>

 

exam03

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<c:set var="num" value="10"/>
	<c:if test="${num ge 10 }">10 이상입니다!</c:if>
</body>
</html>

 

exam04

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<c:set var="num" value="0" />
	<c:choose>
		<c:when test="${num==0 }">
		안녕!
		</c:when>
		<c:when test="${num eq 1 }">
		잘가!
		</c:when>
		<c:otherwise>
		뭐야...?
		</c:otherwise>
	</c:choose>
</body>
</html>

 

exam05

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<table border=1px solid black>
		<c:forEach var="mul1" begin="2" end="9" step="1">
			<tr>
				<c:forEach var="mul2" begin="1" end="9" step="1">
					<td>${mul1}*${mul2} = ${mul1*mul2}</td>
				</c:forEach>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

 

exam06

<%@page import="java.util.ArrayList"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<%
		ArrayList<String> list = new ArrayList<String>();
		list.add("학생1");
		list.add("학생2");
		list.add("학생3");
		list.add("학생4");
		list.add("학생5");
		pageContext.setAttribute("list", list);
	%>

	<c:forEach items="${list }" var="item">
		${item }
	</c:forEach>
</body>
</html>

 

exam07

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<c:forEach items="${list }" var="dto">
	${dto.name}<br>  <!--getter method를 .으로 사용-->
	</c:forEach>
</body>
</html>

 

exam08

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>
	<c:set var="num" value="200" scope="page"/>
	<c:set var="num" value="1000" scope="request"/>
	<c:out value="${requestScope.abc}" default="없음"/>
</body>
</html>

 

com.AddMember

package com;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

@WebServlet("/AddMember")
public class AddMember extends HttpServlet {

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		MemberDTO m1 = new MemberDTO("학생1", 22);
		MemberDTO m2 = new MemberDTO("학생2", 23);
		MemberDTO m3 = new MemberDTO("학생3", 24);
		MemberDTO m4 = new MemberDTO("학생4", 25);
		MemberDTO m5 = new MemberDTO("학생5", 26);

		ArrayList<MemberDTO> list = new ArrayList<MemberDTO>();
		list.add(m1);
		list.add(m2);
		list.add(m3);
		list.add(m4);
		list.add(m5);

		HttpSession session = request.getSession();
		session.setAttribute("list", list);

		response.sendRedirect("exam08.jsp");
	}

}

 

com.MemberDTO

package com;

public class MemberDTO {
	private String name;
	private int age;

	public MemberDTO(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

'Programming > Servlet & JSP' 카테고리의 다른 글

회원관리(MemberSystem)  (0) 2020.02.24
게시판(FileUpload)  (0) 2020.02.24
쿠키 값 읽어오기  (0) 2020.02.23
체크박스 EL식으로 값 가져오기  (0) 2020.02.23
EL 연습하기  (0) 2020.02.23