1. EL(Expression Language) : 표현언어
○ 표현식(<%=..%>)을 대신하는 효과를 가진다.
○ null값을 가지는 변수에 대해 좀 더 관대하고 데이터형을 자동으로 변환해 준다.
○ ${ }
<%@ page contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
request.setAttribute("result", "테스트");
%>
<!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>EL</title>
</head>
<body>
<!-- action에 아무것도 적지 않으면 자기자신을 리로딩한다 -->
<form action="" method="post">
수1: <input type="text" name="su1"></br>
수2: <input type="text" name="su2"></br>
<input type="submit" value="결과"></br>
</form>
<%=request.getAttribute("result") %></br>
${result }</br>
<!-- su1의 값 받기 -->
su1: <%request.getAttribute("su1"); %></br>
su1:${param.su1 }</br>
결과 : ${param.su1 + param.su2 }</br>
${param.su1 }은 ${param.su1%2==0?"짝수":"홀수" }</br>
${10*20 }</br>
</body>
</html>
○ 수1과 수2 를 입력한 후 결과 버튼을 눌렀을 때
2. JSTL(JSP Standard Tag Library)
○ 사용자가 만든 태그를 커스텀 태그라고 하는데, 이 중에 자주 사용하는 태그를 표준으로 만들어 놓은 것
○ 태그를 사용자가 만들어 사용할 수 있다
○ JSTL을 불러오는 작업
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
이 작업을 해 줘야지 JSTL을 사용할 수 있다.
다이아몬드로 있는 것들이 JSTL
3. Core
test5.jsp
◈ ${param.변수명 }
form의 변수를 사용할 경우에 쓴다
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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>구구단</title>
</head>
<body>
<form action="" method="post">
수 : <input type="text" name="su" /><br/>
<input type="submit" value="결과" /><br/>
</form>
<br/>
<c:if test="${!empty param.su }">
<c:if test="${param.su % 2 == 0 }">
${param.su }:짝수<br/>
</c:if>
<c:if test="${param.su % 2 ==1 }">
${param.su }:홀수<br/>
</c:if>
</c:if>
</body>
</html>
(예제 1)
gugudan.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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>
<form action="" method="post">
단:<input type="text" name="dan" /><br/>
<input type="submit" value="결과" /><br/>
</form>
<br/>
<c:if test="${!empty param.dan }">
<table>
<tr>
<td align="center">**${param.dan }단**</td>
</tr>
<c:forEach var="i" begin="2" end="9" step="1">
<tr><td>${param.dan } × ${i } = ${param.dan*i }</td></tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
DataVO.java
package com.svt;
public class DataVO {
private String name;
private int age;
public DataVO(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;
}
}
test6.jsp
○ VO에 값을 넣음
<%@page import="java.util.ArrayList"%>
<%@page import="com.svt.DataVO"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
List<DataVO> lists = new ArrayList<DataVO>();
DataVO vo = new DataVO("배수지",25);
lists.add(vo);
vo = new DataVO("안상희",30);
lists.add(vo);
vo = new DataVO("이슬기",32);
lists.add(vo);
vo = new DataVO("천송이",40);
lists.add(vo);
request.setAttribute("lists", lists);
%>
<jsp:forward page="result.jsp" />
result.jsp
확장 for문도 사용 가능하다
<c:forEach var="vo" items="${lists }">
→for(DataVO vo : lists)
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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>
<table>
<tr>
<td width="100">이름</td>
<td width="100">나이</td>
</tr>
<c:forEach var="vo" items="${lists }">
<tr>
<td>${vo.name }</td>
<td>${vo.age }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
결과
<c:forEach var="a" begin="1" end="${param.su2 }" step="1">
<c:set var="result" value="${result * param.su1 }" />
${param.su1 }^${a} = ${result }<br/>
</c:forEach>
→ a가 su2보다 작을때까지
result = result*su1
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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></title>
</head>
<body>
<form action="" method="post">
수1:<input type="text" name="su1"><br/>
수2:<input type="text" name="su2"><br/>
<input type="submit" value="결과"><br/>
</form>
<br/><br/>
<c:if test="${!empty param.su1 }">
<c:set var="result" value="1" /> <!-- 변수 초기화 -->
<c:forEach var="a" begin="1" end="${param.su2 }" step="1">
<c:set var="result" value="${result * param.su1 }" />
${param.su1 }^${a} = ${result }<br/>
</c:forEach>
</c:if>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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></title>
</head>
<body>
<form action="" method="post">
수 : <input type="text" name="su" /><br/>
<input type="submit" value="결과" /><br/>
</form>
<c:if test="${!empty param.su }">
<c:choose>
<c:when test="${param.su%3==0 && param.su%4==0 }">
${param.su }는 3과 4의 배수<br/>
</c:when>
<c:when test="${param.su%4==0 }">
${param.su }는 4의 배수<br/>
</c:when>
<c:when test="${param.su%3==0 }">
${param.su }는 3의 배수<br/>
</c:when>
<c:otherwise>
${param.su }는 3과 4의 배수가 아니다.<br/>
</c:otherwise>
</c:choose>
</c:if></body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!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>
<c:set var="url" value="gugudan.jsp"/>
<c:import url="${url }" var="uu">
<c:param name="dan" value="5" />
</c:import>
</body>
</html>
◈ 실제로 출력은 <c:out>이 해준다
escapeXml="false" 이 없으면 코딩이 화면에 출력된다
'STUDY > JSP' 카테고리의 다른 글
게시판(서블릿) - BoardServlet.jave (0) | 2019.02.26 |
---|---|
Jsp 11일차 - DBCP (0) | 2019.02.25 |
Jsp 11일차 - 서블릿(Servlet) (0) | 2019.02.25 |
Jsp 8일차 - 게시판(자바스크립트) (0) | 2019.02.21 |
Jsp 8일차 - 게시판(css작성) (0) | 2019.02.21 |
댓글