IT story/JSP

[Jsp 강의] L11ELtag

jason719 2016. 10. 27. 19:01

2016. 10. 27. (Thu)

L11ELtag

EL(Expression Language) Tag에 대해 알아보자

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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>EL 태그를 알아보자</title>
</head>
<body>
    <h1>EL 태그</h1>
    <h1><a href="./L01ELDataType.jsp">el 태그의 데이터 삽입</a></h1>
    <h1><a href="./L02ELLogin.jsp">el의 parameter</a></h1>
    <h1><a href="./L03ELHeader.jsp">el의 header</a></h1>
    <h1><a href="./L04ELCookie.jsp">el의 cookie</a></h1>
    <h1><a href="./L05ELScope.jsp">el의 scope</a></h1>
</body>
</html>
cs


L01ELDataType.jsp에서 el 태그의 테이터 삽입에 대해 알아보자

예제 1. L01ELDataType.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ 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>Expression Language Tag</title>
</head>
<body>
    <h1>EL 태그의 데이터 타입</h1>
    <h3>
    &lt;%="Hello" %> : <%="Hello" %> <%-- == out.print() --%>
    &nbsp;////&nbsp;
    \${"Hello"} : ${"Hello"}
    <%-- \$ el tag로 인식하지 않는다.
         &lt;% script로 인식하지 않는다. --%>
    </h3>
    <%--el tag는 출력 or 연산에 사용 --%>
    <h3>정수 : ${8}</h3>
    <h3>실수 : ${5.5}</h3>
    <h3>문자열 : ${"JspLesson"}</h3>
    <h3>논리 : ${true}</h3>
    <h3>null : ${null}</h3>
    
    <h3>\${5+10} : ${5+10}</h3>
    <h3>\${'5'+10} : ${'5'+10}</h3>
    <h3>\${"5"+10} : ${"5"+10}</h3>
    <h3>el은 Script요소가 강하다.</h3>
    <%-- script는 자바를 모르는 사람을 위한 언어 -> 컴파일이 되지 않는다.
        스크립트 언어번역기를 컴파일 대신 사용. 문자열 -> 코드로 인식
        컴파일 되지 않기 때문에 데이터 타입 (기본 데이터타입)이 중요하지 않음
        데이터타입을 몰라도 코드를 작성할 수 있다.
        Java 개발자에게는 불확실성이 더욱 어렵게 느껴진다. --%>
    <h3>\${10%3} : ${10%3 } </h3>
    <h3>\${10 mod 3} : ${10 mod 3 } </h3>
    
    <h3>\${5>2} : ${5>2}</h3>
    <h3>\${5 gt 2} : ${5 gt 2}</h3>
    
    <h3>\${5<2} : ${5<2}</h3>
    <h3>\${5 lt 2} : ${5 lt 2}</h3>
    
    <%-- 연산자(%,>,<)를 태그에서 사용하는 특수문자이기 때문에 mod, gt, lt등을 사용한다. --%>
    <h3>\${(5>2)?"참":"거짓"} : ${(5>2)?"참":"거짓"}</h3>
    
    <%-- jstl core태그를 이용하면 el의 변수를 선언할 수 있다. --%>
    <%String input = "출력"; %>
    ${empty input}
    <%=input %>
</body>
</html>
cs


L02ELLogin.jsp에서 EL의 Parameter에 대해 알아보자

예제 2. L02ELLogin.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ 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>파라미터를 전달</title>
</head>
<body>
    <form action="L02ELParam.jsp" method="post">
        <p>
            <label>아이디:</label>
            <input type="text" name="id" value="">
        </p>
        <p>
            <label>비밀번호:</label>
            <input type="password" name="pwd" value="">
        </p>
        <p>
            <label>취미: </label>
            <input type="checkbox" name="hobs" value="그림">그림
            <input type="checkbox" name="hobs" value="요리" checked>요리
            <input type="checkbox" name="hobs" value="만화" >만화
            <input type="checkbox" name="hobs" value="게임" >게임
            <input type="checkbox" name="hobs" value="운동"checked>운동
        </p>
        <button type="submit">제출</button>&nbsp;&nbsp;&nbsp;<a href="./../index.jsp">홈으로</a>
    </form>
 
</body>
</html>
cs


예제 3. L02ELParam.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ 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>el param으로 파라미터 받기</title>
</head>
<body>
    <%request.setCharacterEncoding("utf-8"); %>
    <h1>당신이 입력한 정보입니다.</h1>
    <h3>id: <%= request.getParameter("id")%></h3>
    <h3>pwd: <%= request.getParameter("pwd")%></h3>
    <h3>취미: 
    <%
    String[] hobs = request.getParameterValues("hobs");
    for(String hob : hobs){
        out.print(hob+" ");
    }
    %>
    </h3>
    <hr>
    <h1>당신이 입력한 정보 (el param 이용)</h1>
    <h3>id: ${param.id}</h3>
    <h3>pwd: ${param["pwd"]}</h3>
    <h3>취미(param): ${param.hobs}</h3>
    <h3>취미(paramValues): ${paramValues.hobs[0]},${paramValues.hobs[1]},${paramValues.hobs[2]},${paramValues.hobs[3]},${paramValues.hobs[4]}</h3>
    
    
    
</body>
</html>
cs


L03ELHeader.jsp에서 EL의 Header에 대해 알아보자

예제 4. L03ELHeader.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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>el header</title>
</head>
<body>
    <h1>el태그로 host header 정보 읽기</h1>
    <%=request.getHeader("host") %>
    <h3>접속 ip: ${header.host }</h3>
    <h3>접속 경로: ${header.referer }</h3>
    <h3>유저 시스템 정보: ${header["user-agent"] }</h3>
    ${cookie.JSESSIONID.value}
</body>
</html>
cs


L04ELCookie.jsp에서 el의 Cookie에 대해 알아보자

예제 5. L04ELCookie.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ 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>
<%// id = jspLessin, pwd = admin1234 //수명 24*60*60 쿠키만들고 출력
    Cookie c = new Cookie("id","jsplesson"); 
    Cookie c2 = new Cookie("pwd","admin1234");
    c.setMaxAge(24*60*60); //日*時*分*秒
    c2.setMaxAge(24*60*60);
    response.addCookie(c);
    response.addCookie(c2);
    %>
    <h1>쿠키 set 완료</h1>
    <hr>
    <h1>쿠키 불러오기 (request 객체 이용)</h1>
    <%
    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies){
    %>
    <h3><%=cookie.getName() %>:<%=cookie.getValue() %></h3>
    <%    
    }
    %>
    <hr>
    <h1>쿠키 불러오기(el tag 이용)</h1>
    <h3>id: ${cookie.id.value}</h3>
    <h3>pwd: ${cookie.pwd.value}</h3>
</body>
</html>
cs


L05ELScope.jsp EL의 Scope에 대해 알아보자

예제 6. L05ELScope.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<%@ 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>attribute의 값을 el 태그로 출력해보자</title>
</head>
<body>
<!-- attribute는 페이지의 내장 객체, object 타입으로 저장됨(보통 header) 쿠키는 제
    parameter는 외부로 전달하는 파라미터, String 문자열로 전달.(url로 전달)
 -->
 <%
 pageContext.setAttribute("name""page Attr");
 request.setAttribute("name""request Attr");
 session.setAttribute("name""session Attr");
 application.setAttribute("name""app Attr");
 %>
 
 <h1>jsp 내장 객체 불러오기</h1>
 <h3>\${name} : ${name}</h3>
 <h3>page 속성\${pageScope.name} : ${pageScope.name}</h3>
 <h3>request 속성\${requestScope.name} : ${requestScope.name}</h3>
 <h3>session 속성\${sessionScope.name} : ${sessionScope.name}</h3>
 <h3>application 속성\${applicationScope.name} : ${applicationScope.name}</h3>
 
 <hr>
 <h1>scope 내장 객체란</h1>
 <h3>request: 클라이언트의 요청에 대해 같은 요청을 공유하는 페에지에 HttpServletRequest 객체에 저장
     request.getParameter() -> URL 파라미터 접근(get)
     request.getAttribute() -> 내장객체 접근(post)</h3>
 <h3>session: 하나의 웹브라우저 프로젝트 당 1개의 session 객체가 생성된다.
               같은 웹브라우저 내에서 요청되는 페이지들은 같은 객체를 공유한다.
               생성된 객체는 HttpSession에 저장된다. 동일한 클라이언트만 사용가능</h3>
 <h3>page: 한 번의 웹브라우저(클라이언트)의 요청에 대해 하나의 JSP 페이지가 호출되며
            웹브라우저의 요청이 들어오면 이때 단 한 개의 페이지만 대응 된다.
            생성된 객체는 PageContext에 저장된다.</h3>
 <h3>application: 하나의 웹 어플리케이션 당 1개의 application 객체가 생성된다.
                   같은 웹 어플리케이션에 요청되는 페이지들은 같은객체를 공유한다.
                   생성된 객체는 실질적으로 ServletContext에 저장된다.</h3>
</body>
</html>
cs


'IT story > JSP' 카테고리의 다른 글

[JSP 강의] L13JSTL  (0) 2016.11.17
[Jsp 강의] L12JDBC  (0) 2016.10.28
[Jsp 강의] L10Bean  (0) 2016.10.27
[Jsp 강의] L09Cookie  (0) 2016.10.27
[Jsp 강의] L08Session  (0) 2016.10.27