IT/Jsp

jsp - el, jstl

노마드오브 2018. 11. 19. 22:07

el_test1.jsp

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>


<%

session.setAttribute("test", "Session Test");

%>


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

이름 : <input type="text" name="name" value="홍길동">

<input type="submit" value="입력">

</form>


</body>

</html>




el_test2.jsp

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

request.setCharacterEncoding("utf-8");

%>

<h3><%=session.getAttribute("test") %></h3>

<h3><%=request.getParameter("name") %></h3>


<h3>${sessionScope.test }</h3>

<h3>${param.name }</h3>

<br>


\${5+7} = ${5+7} 


</body>

</html>





jstl_core_ex1.jsp

<%@page import="java.util.List"%>

<%@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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body> 

<%

// * 영역객체 4가지

// page | request | session | application

// String test = "Hello String";

// pageContext.setAttribute("test", "Hello String");

// session.setAttribute("id", "admin");

// session.removeAttribute("id");

%>

<c:set var="test" value="Hello JSTL!" scope="page" />

<c:set scope="session" var="test" value="admin" />

<%

// Map으로 구현된 영역객체를 다음과 같은 순서로 검색함.

// *scope 속성에 올 수 있는 값

// page -> request -> session -> application

%>

<c:out value="${test}" /><br>

<c:out value="${sessionScope.test}" /><br>


--------<br><br>


<c:remove var="test" scope="session"/><br><%-- 4개의 영역객체 모두 해당이름 삭제 --%>

<c:out value="${test}" /><br>


<c:if test="${ 5 < 10 }">

<h3>5는 10보다 작다</h3>

</c:if>


<c:choose>

<c:when test="${5+10 == 50}">

<h3>5+10은 50이다.</h3>

</c:when>

<c:otherwise>

<h3>5+10은 50이 아니다.</h3>

</c:otherwise>

</c:choose>


<c:forEach begin="1" end="10" step="2" var="i">

${pageScope.i} &nbsp;

</c:forEach>

<br>


<c:forTokens items="a,b,c,d,e,f,g" delims="," var="alphabet">

<b>${alphabet}</b> &nbsp;

</c:forTokens>


<%

List<String> list = new ArrayList<>();

list.add(0, "aaa");

list.add(1, "bbb");

list.add(2, "ccc");

pageContext.setAttribute("strList", list);

request.setAttribute("strList", list);

%>

<%-- <c:out value="${strList}" /> --%>


<c:forEach items="${requestScope.strList}" var="str">

${str} &nbsp;

</c:forEach>


</body>

</html>