[국비학원 기록/JSP] 표현 언어 EL, 연산자, 내장 객체, 자바 빈(Bean), ArrayList, HashMap 예제
my code archive
반응형
표현 언어(Expression Language

-변수의 값을 출력할 때 사용하는 스크립트 언어, 데이터 출력 기능.

-자바 코드가 들어가는 표현식을 좀더 편리하게 사용하기 위함.

표현식 <%= =%>  ==> ${표현식 or 값}

 

1. EL 특징

 

1)변수, 연산자 포함 가능

-산술 연산자

/ , div

%, mod

 

-비교 연산자

==, eq

!= , ne

<, lt

>, gt

<=, le

>=, ge

 

 

-논리 연산자

-empty 연산자

 

2)JSP 내장 객체에 저장된 속성 및 자바빈 속성도 EL 출력 가능

 

3)내장 객체도 제공된다.

 

 

2.EL 제공 내장 객체

 

1)4가지 영역 (scope)에 속성값 저장, 읽어오기

-pageScope : page 영역에 저장된 속성값을 읽어올 수 있음.
                  (page 영역에 바인딩된 객체를 참조함)
-requestScope : request 영역에 저장된 속성값을 읽어올 수 있음.
-sessionScope : session 영역에 저장된 속성값을 읽어올 수 있음.
-applicationScope : application 영역에 저장된 속성값을 읽어올 수 있음.

 

2)요청 매개변수

-param : request.getParameter() 메서드 호출과 같음, 1개의 값을 전달하는 요청 매개변수 처리

-paramValues : request.getParameterValues() 메서드 호출과 같음, 여러개의 값을 전달하는 요청 매개변수 처리

 

3)헤더값

-header : request.getHeader()

-headerValues

 

4)쿠키값

-Cookies

 

5)JSP 내용

-pageContext

 

6)초기 매개변수

-initParam

 

3. 빈 속성 접근 방법

${빈이름.속성이름}

 

4. Collection 객체 접근 형식(방법)

${Collection객체 이름[index].속성이름}

 

5. HashMap에 저장된 객체 접근 방법

${Hash객체이름.키이름}

 

6. has-a 관계 빈 속성 접근 방법

${부모빈 이름.자식 속성이름.속성이름}

 

예제 -- 연산자

 

<elTest1.jsp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>표현 언어에서 사용되는 데이터들 출력</title>
</head>
<body>
    <h1>표현 언어로 여러 가지 데이터 출력</h1>
    <h1>
        ${100 }<br>
        ${"좋은 아침입니다." }<br>
        ${10 + 1 }<br>
        ${"10" + 1 }<br>        <!-- 숫자형 문자열과 실제 문자를 더하면 문자열이 자동으로 숫자로 형변환됨. -->
        ${null + 10 }<br>
        <%-- ${"좋은 아침" + 11 <br> --%>        <!-- 문자열과 숫자는 더할 수 없다. -->
        <%-- ${"Hello" + "World" --%>        <!-- 문자열끼리 더할 수 없다. -->
    </h1>
</body>
</html>
cs

<elTest2.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>표현 언어의 여러 가지 연산자</title>
</head>
<body>
    <h2>여러 가지 산술 연산자</h2>
    <h1>
        ${100/9 }<br>
        ${100 div 9 }<br>        <!-- div를 사용해 나누기 연산을 함 -->
        ${100 % 9 }<br>
        ${100 mod 9 }<br>        <!-- mod를 사용해 나머지를 구함 -->
    </h1>
    
    <br>
    <h2>여러 가지 비교 연산자</h2>
    <h3>
        ${10 == 10 }<br>
        ${10 eq 10 }<br>
        ${"Seoul" == "Seoul" }<br>    <!-- 문자열이 서로 같은지 비교 -->
        ${"Seoul" eq "Seoul" }
    </h3>
</body>
</html>
cs

<elTest3.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    int num1 = 3;
    pageContext.setAttribute("num2", 4);
    pageContext.setAttribute("num3", "5");
    pageContext.setAttribute("num4", "8");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>표현 언어(EL) - 연산자</title>
</head>
<body>
    <h3>변수 선언 값 확인 및 할당</h3>
    스크립틀릿에서 선언한 변수 : ${num1 }<br>
    page 영역에서 저장된 변수 : ${num2 }<br>
    변수 할당 및 바로 출력 : ${num1 = 7 }<br>
    변수 할당 및 별도 출력 : ${num2 = 8;'' }<br>
    별도 출력 : ${num2 }<br>
    
    <h3>산술 연산자</h3>
    num3 div num4 : ${num3 div num4 }<br>
    num2 mod num4 : ${num2 mod num4 }<br>
    
    <h3>비교 연산자</h3>
    num4 > num3 : ${num4 gt num3}<br>
    num1 < num3 : ${num1 lt num3 }<br>
    num2 >= num4 : ${num2 ge num4 }<br>
    
    ${"hello" != "apple" }<br>
    ${"hello" ne "apple" }<br>
</body>
</html>
cs

<elTest4.jsp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="m1" class="kr.co.ezenac.el.MemberBean" scope="page"/>        <!-- 유즈빈 생성 -->
<jsp:setProperty property="name" value="이순신" name="m1"/>                    <!-- 빈의 name 속성에 값을 설정함 -->
<jsp:useBean id="m2" class="java.util.ArrayList" scope="page"></jsp:useBean><!-- ArrayList 객체를 빈으로 생성 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>표현 언어 여러 연산자들</title>
</head>
<body>
    empty 연산자
    <h2>
        ${empty m1 }<br>            <!-- m1의 name 속성에 값이 설정되어 있으므로 false 반환됨 -->
        ${not empty m1 }<br><br>
        
        ${empty m2 }<br>            <!-- ArrayList 객체인 m2는 비어있음 -->
        ${not empty m2 }<br><br>
    </h2>
</body>
</html>
cs

 

예제 -- 내장객체

 

<login.jsp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>로그인</title>
</head>
<body>
    <form action="result.jsp">
        아이디 : <input type="text" size="20"><br>
        비밀번호 : <input type="password" size="20"><br>
        <input type="submit" value="로그인">
        <input type="reset" value="다시 입력"><br><br>
        <a href="http://localhost:8080/Chap14_EL/ELTest02/memberForm.jsp">회원 가입하기</a><br>
        <!-- request의 getContextPath() 이용해 컨텍스트 이름 가져옴 -->
        <a href="<%=request.getContextPath() %>/ELTest02/memberForm.jsp">회원 가입하기</a>
        <!-- 자바 코드 사용하지 않고 pageContext의 속성인 request 의 contextPath 속성 사용 -->
        <a href="${pageContext.request.contextPath }/ELTest02/memberForm.jsp">회원 가입하기</a>
    </form>
</body>
</html>
cs

 

<MemberBean.java>

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
52
53
54
55
56
57
58
59
60
61
62
import java.sql.Date;
 
public class MemberBean {
 
    private String id;
    private String pwd;
    private String name;
    private String email;
    private Date joinDate;
    
    public MemberBean() {
        // TODO Auto-generated constructor stub
    }
 
    public MemberBean(String id, String pwd, String name, String email) {
        super();
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.email = email;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPwd() {
        return pwd;
    }
 
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Date getJoinDate() {
        return joinDate;
    }
 
    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }
}
cs

 

<member1.jsp>: 내장객체 param 이용

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("id");
    String pwd = request.getParameter("pwd");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="20%"><b>아이디</b></td>
            <td width="20%"><b>비밀번호</b></td>
            <td width="20%"><b>이름</b></td>
            <td width="20%"><b>이메일</b></td>
        </tr>
        <tr align="center">
            <td><%=id %></td>                <!-- getParameter()로 가져온 회원 정보를 표현식으로 출력 -->
            <td><%=pwd %></td>
            <td><%=name %></td>
            <td><%=email %></td>
        </tr>
        <tr align="center">
            <td>${param.id }</td>            <!-- param 객체를 이용해 getParameter() 이용하지 않고 출력 -->
            <td>${param.pwd }</td>
            <td>${param.name }</td>
            <td>${param.email }</td>
        </tr>
    </table>
</body>
</html>
cs

 

<member2.jsp>: requestScope 이용

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="20%"><b>아이디</b></td>
            <td width="20%"><b>비밀번호</b></td>
            <td width="20%"><b>이름</b></td>
            <td width="20%"><b>이메일</b></td>
            <td width="20%"><b>주소</b></td>
        </tr>
        <tr align="center">
            <td>${param.id }</td>
            <td>${param.pwd }</td>        <!--param 객체를 이용해 getParameter()이용하지 않고 출력  -->
            <td>${param.name }</td>
            <td>${param.email }</td>
            <td>${requestScope.address }</td>    <!--requestScope 이용해 바인딩된 주소정보 출력  -->
        </tr>
        
    </table>
</body>
</html>
cs

 

<member3.jsp>: useBean 이용해 회원 정보 저장

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>    
<jsp:useBean id="m" class="kr.co.ezenac.el.MemberBean"/>   <!-- 회원 정보 저장할 빈 생성함 -->
<jsp:setProperty property="*" name="m"/>                   <!-- 전송된 회원 정보를 빈의 속성에 설정 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="20%"><b>아이디</b></td>
            <td width="20%"><b>비밀번호</b></td>
            <td width="20%"><b>이름</b></td>
            <td width="20%"><b>이메일</b></td>
 
        </tr>
        <tr align="center">
            <td><%=m.getId() %></td>    <!-- 표현식 이용해 회원 정보 출력 -->
            <td><%=m.getPwd() %></td>
            <td><%=m.getName() %></td>
            <td><%=m.getEmail() %></td>
        </tr>    
        <tr align="center">
             <td>${m.id }</td>
             <td>${m.pwd }</td>        <!-- 빈 id와 속성이름으로 접근해 회원 정보 출력  -->
             <td>${m.name }</td>
             <td>${m.email }</td>
        </tr>
    </table>
</body>
</html>
cs

 

<member4.jsp>: useBean, setProperty, ArrayList 이용

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
<%@page import="kr.co.ezenac.el.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>    
<jsp:useBean id="m1" class="kr.co.ezenac.el.MemberBean"/>   <!-- 회원 정보 저장할 빈 생성함 -->
<jsp:setProperty property="*" name="m1"/>                   <!-- 전송된 회원 정보를 빈의 속성에 설정 -->
<jsp:useBean id="membersList" class="java.util.ArrayList" />
 
<%
    MemberBean m2 = new MemberBean("ezenacccccc","0311","이젠","ezenac@gmail.com");
    membersList.add(m1);
    membersList.add(m2);            //두개의 MemberBean 객체를 ArrayList에 저장함.
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="20%"><b>아이디</b></td>
            <td width="20%"><b>비밀번호</b></td>
            <td width="20%"><b>이름</b></td>
            <td width="20%"><b>이메일</b></td>
 
        </tr>
        <tr align="center">
            <td>${membersList[0].id }</td>    
            <td>${membersList[0].pwd }</td>
            <td>${membersList[0].name }</td>
            <td>${membersList[0].email }</td>
        </tr>    
        <tr align="center">
            <td>${membersList[1].id }</td>    
            <td>${membersList[1].pwd }</td>
            <td>${membersList[1].name }</td>
            <td>${membersList[1].email }</td>
        </tr>
    </table>
</body>
</html>
cs

 

<member5.jsp>: useBean, ArrayList, HashMap 이용

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
52
53
54
55
56
57
58
59
60
61
62
63
<%@page import="kr.co.ezenac.el.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="m1" class="kr.co.ezenac.el.MemberBean"/>        <!-- 회원 정보 저장할 빈 생성함 -->
<jsp:setProperty property="*" name="m1"/>                        <!-- 전송된 회원 정보를 빈의 속성에 설정 -->
<jsp:useBean id="memberList" class="java.util.ArrayList"/>        <!-- MemberBean 객체를 저장할 ArrayList -->
<jsp:useBean id="memberMap" class="java.util.HashMap"/>            <!-- 회원 정보 저장할 HashMap 객체 -->
 
<%
    memberMap.put("id", "ezenac100");
    memberMap.put("pwd", "0311");
    memberMap.put("name", "이젠100");
    memberMap.put("email", "ezenac100@gmail.com");                /* HashMap에 key/value 쌍으로 회원 정보 저장함 */
    
    //회원 정보를 저장하는 MemberBean 객체 생성
    MemberBean m2= new MemberBean("ezenac200", "0311", "이젠200", "ezen200@gmail.com");
    
    //전송된 회원 정보와 자바 코드로 생성한 회원 정보를 ArrayList에 저장함.
    memberList.add(m1);
    memberList.add(m2);
    
    //회원 정보가 저장된 membersList를 membersList라는 key로 HashMap에 저장함.
    memberMap.put("membersList", memberList);
%>
<!DOCTYPE html>        
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>회원 정보 출력</title>
</head>
<body>
    <table border="1" align="center">
        <tr align="center" bgcolor="#99ccff">
            <td width="20%"><b>아이디</b></td>
            <td width="20%"><b>비밀번호</b></td>
            <td width="20%"><b>이름</b></td>
            <td width="20%"><b>이메일</b></td>
        </tr>
        <tr align="center">                                <!-- HashMap 이름 뒤에 . 연산자로 저장 시 사용한 key를 사용하여 Value로 가져옴. -->
            <td>${memberMap.id }</td>
            <td>${memberMap.pwd }</td>
            <td>${memberMap.name }</td>
            <td>${memberMap.email }</td>
        </tr>
        <tr align="center">
            <td>${memberMap.membersList[0].id }</td>    <!-- HashMap 저장된 ArrayList에 .로 접근한 후 다시 각각의 속성을 .이용해 접근 -->
            <td>${memberMap.membersList[0].pwd }</td>
            <td>${memberMap.membersList[0].name }</td>
            <td>${memberMap.membersList[0].email }</td>
        </tr>
        <tr align="center">
            <td>${memberMap.membersList[1].id }</td>
            <td>${memberMap.membersList[1].id }</td>
            <td>${memberMap.membersList[1].id }</td>
            <td>${memberMap.membersList[1].id }</td>
        </tr>
    </table>
</body>
</html>
cs

 

반응형
profile

my code archive

@얼레벌레 개발자👩‍💻

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

반응형