Jsp 빈즈는 JSP와의 연동을 위해 만들어진 자바 컴포넌트에요.

데이터베이스 연동이나 뷰 데이터 교환에 사용돼요

model1과 model2에서 사용할 수 이써요

jsp model2에서는 뷰 컴포넌트로만 활용이 권장돼요(비즈니스 로직X)

 

총 네개의 파일을 만들거에요.

 

1. 회원가입 페이지 join.jsp

2. 값이 넣고 빠지는 JoinBeans.java

3. setProperty 태그를 사용 할 input.jsp

4. getProperty 태그를 사용 할 output.jsp

 

 

 

1. join.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<%@ 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>
<%
    request.setCharacterEncoding("UTF-8");
%>
<style type="text/css">
table, th, td {
    border-style: solid;
    border-color: #CC3D3D;
    border-width: 1px;
    border-collapse: collapse;
}
</style>
 
</head>
<body>
 
    <form name="join" onsubmit="return validate()" action="./input.jsp" method="post">
        <!-- validate함수가 리턴하는 결과가 true인지 false인지에 따라 submit 이벤트가 계속 진행되거나 중단됨 -->
        <table border="1" height="600" align="center">
            <tr align="center">
                <th colspan="2" bgcolor="#FFA7A7">회원 기본 정보</th>
            </tr>
            <tr align="center">
                <td>아이디 :</td>
                <td align="left">&nbsp;<input type="text" name="id" size="20">
                    &nbsp;4~12자의 영문 대소문자와 숫자로만 입력
                </td>
            </tr>
            <tr align="center">
                <td>비밀번호 :</td>
                <td align="left">&nbsp;<input type="password" name="pw" size="20">
                    &nbsp;4~12자의 영문 대소문자와 숫자로만 입력
                </td>
            </tr>
            <tr align="center">
                <td>비밀번호 확인 :</td>
                <td align="left">&nbsp;<input type="password" name="pw1"
                    size="20"></td>
            </tr>
            <tr align="center">
                <td>메일주소 :</td>
                <td align="left">&nbsp;<input type="text" name="email" size="30">
                    &nbsp;예:id@domain.com
                </td>
            </tr>
            <tr align="center">
                <td>이름 :</td>
                <td align="left">&nbsp;<input type="text" name="name" size="30"></td>
            </tr>
 
            <tr align="center">
                <th colspan="2" bgcolor="#FFA7A7">개인 신상정보</th>
            </tr>
            <tr align="center">
                <td>주민등록번호 :</td>
                <td align="left">&nbsp;
                <input type="text" name="mynum" size="30"> &nbsp;예:123456-1234567
                </td>
            </tr>
            <tr align="center">
                <td>생일 :</td>
                <td align="left">&nbsp; 
                <input type="text" name="year" size="5">
                    &nbsp;&nbsp; <select name="month">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>
                        <option value="11">11</option>
                        <option value="12">12</option>
                </select> 월 &nbsp;&nbsp; 
                <select name="day">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>
                        <option value="11">11</option>
                        <option value="12">12</option>
                        <option value="13">13</option>
                        <option value="14">14</option>
                        <option value="15">15</option>
                        <option value="16">16</option>
                        <option value="17">17</option>
                        <option value="18">18</option>
                        <option value="19">19</option>
                        <option value="20">20</option>
                        <option value="21">21</option>
                        <option value="22">22</option>
                        <option value="23">23</option>
                        <option value="24">24</option>
                        <option value="25">25</option>
                        <option value="26">26</option>
                        <option value="27">27</option>
                        <option value="28">28</option>
                        <option value="29">29</option>
                        <option value="30">30</option>
                        <option value="31">31</option>
                </select> 일
                </td>
            </tr>
            <tr align="center">
                <td>관심분야 :</td>
                <td align="left">&nbsp; 
                <input type="checkbox" name="favorite" value="computer">컴퓨터 &nbsp; 
                <input type="checkbox" name="favorite" value="인터넷">인터넷 &nbsp; 
                <input type="checkbox" name="favorite" value="여행">여행 &nbsp; 
                <input type="checkbox" name="favorite" value="영화감상">영화감상 &nbsp; 
                <input type="checkbox" name="favorite" value="음악감상">음악감상 &nbsp;
                </td>
            </tr>
            <tr align="center">
                <td>자기소개 :</td>
                <td align="left">&nbsp;
                <textarea name="intro" cols="80" rows="10"></textarea></td>
            </tr>
        </table>
        <center>
            <input type="submit" value="회원가입"> 
            <input type="reset" value="다시입력">
        </center>
    </form>
 
</body>
</html>
cs

 

 

2. JoinBeans.java

 

회원가입 폼에 있는 애들을 다 bean에 만들어줬어요.

관심분야는 check박스의 name을 같게 하고, 2개이상 체크 시 배열로 받아 올 수 있도록 했어요.

 

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package test;
 
 
public class JoinBeans {
 
    public String id;
    public String pw;
    public String email;
    public String name;
    public int mynum;
    public int year;
    public int month;
    public int day;
    public String []favorite;
    public String intro;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPw() {
        return pw;
    }
    public void setPw(String pw) {
        this.pw = pw;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMynum() {
        return mynum;
    }
    public void setMynum(int mynum) {
        this.mynum = mynum;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public String[] getFavorite() {
        return favorite;
    }
    public void setFavorite(String []favorite) {
        this.favorite = favorite;
    }
    public String getIntro() {
        return intro;
    }
    public void setIntro(String intro) {
        this.intro = intro;
    }
    
    
    
    
    
    
}
 
cs

 

 

3. input.jsp

 

input에서는 setter에 값을 넣기 위해 만들었어요.

한 페이지에 모두 써도 되지만 값을 넘기는 것과 좀더 알기 쉽게 하기 위해 나눴어요!

여기는 화면에 버튼 하나밖에 없어요.

 

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"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<!-- 자바빈 객체 생성 -->
<!-- scope 범위 
    1. scope : 현재 page 내
    2. request : request가 최종 포워딩 되는 페이지까지
    3. session : 세션을 유지할 때 까지
    4. application : 웹 애플리케이션이 종료될 때까지
 -->
<jsp:useBean id="joinbean" class="test.JoinBeans" scope="session">
 
<!-- 생성된 자바빈 객체에 프로퍼티 값을 저장하기 위해 사용 -->
<%-- 자바빈 객체의 멤버변수를 직접 수정하는 것이 아니라 setter 호출 --%>
<jsp:setProperty name="joinbean" property="*"/>
 
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="./output.jsp" method="post">
<input type="submit" value="output">
</form>
</body>
</html>
cs

 

4. output.jsp

 

출력 페이지에요

 <jsp:getProperty property="id" name="joinbean"/> 대신에

 <%= joinbean.getUsername() %> 을 써도 똑같은 값이 나와요.

getProperty를 사용하면 자바빈 객체의 멤버 변수를 직접 접근하지 않고 getter만 호출하여 값을 얻어 올 수 있어요.

 

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<!-- 자바빈 객체 생성 -->
<jsp:useBean id="joinbean" class="test.JoinBeans" scope="session"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table, th, td {
    border-style: solid;
    border-color: #CC3D3D;
    border-width: 1px;
    border-collapse: collapse;
}
</style>
</head>
<body>
<table border="1" height="600" align="center">
            <tr align="center">
                <th colspan="2" bgcolor="#FFA7A7">회원 정보 확인</th>
            </tr>
            <tr align="center">
                <td>아이디 :</td>
                <td align="left">&nbsp;
                <!-- 자바빈 객체의 멤버 변수를 접근 하는 것이 아니라 getter 호출 -->
                <jsp:getProperty property="id" name="joinbean"/>
                </td>
            </tr>
            <tr align="center">
                <td>비밀번호 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="pw" name="joinbean"/>
                </td>
            </tr>
            <tr align="center">
                <td>메일주소 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="email" name="joinbean"/>
                </td>
            </tr>
            <tr align="center">
                <td>이름 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="name" name="joinbean"/>
                </td>
            </tr>
 
            <tr align="center">
                <th colspan="2" bgcolor="#FFA7A7">개인 신상정보</th>
            </tr>
            <tr align="center">
                <td>주민등록번호 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="mynum" name="joinbean"/>
                </td>
            </tr>
            <tr align="center">
                <td>생일 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="year" name="joinbean"/>년 &nbsp;
                <jsp:getProperty property="month" name="joinbean"/>월 &nbsp;
                <jsp:getProperty property="day" name="joinbean"/>일&nbsp;
                </td>
            </tr>
            <tr align="center">
                <td>관심분야 :</td>
                <td align="left">&nbsp;
                <!-- 배열을 순서대로 출력하기 위해 for문을 사 -->
                <%
                    String[] favorite = joinbean.getFavorite();
                    for(int i=0;i<favorite.length;i++){
                        if(favorite[i] != null){
                            out.write(favorite[i]+"\n");
                        }
                    }
                
                %>
                </td>
            </tr>
            <tr align="center">
                <td>자기소개 :</td>
                <td align="left">&nbsp;
                <jsp:getProperty property="intro" name="joinbean"/>
                </td>
            </tr>
        </table>
</body>
</html>
cs

 

 출력화면

 

 

  

 
1. 동기화


- thread가 진행 중인 작업을 다른 thread가 간섭하지 못하도록 막는 것을 'thread의 동기화(synchronization)이라고 한다.



1-1. method 전체를 임계 영역으로 지정하는 방법


- synchronized method가 호출된 시점부터 해당 메서드가 포함된 객체의 lock을 얻어 작업을 수행하다가 method가 종료되면 lock을 반환한다.
 
1-2. 특정한 영역을 영역으로 지정하는 방법


- method 내의 코드 일부를 { }로 감싸고 중괄호 앞에 synchronize(참조변수)를 붙힌다.  
  이 때 참조변수는 lock을 걸고자 하는 객체를 참조하는 것.
     
 2. wait() & notify()


 - 동기화된 임계 영역의 코드를 수행하다가 작업을 더이상 진행할 상황이 아니면 
   일단 wait()을 호출하여 threadlock을 반납하고 기다리게 한다
   그러면 다른 threadlock을 얻어 해당 객체에 대한 작업을 수행할 수 있게 된다.
   나중에 작업을 진행할 수 있는 상황이 되면 notify()를 호출해서
   작업을 중단했던 thread가 다시 lock을 얻어 작업을 진행할 수 있게 한다.
- waitnotiry(notifyAll) method는 동기화 처리를 해서, 한순간에 하나의 thread만 호출이 가능하도록 한다.(동시호출 아님)


   
3. Multi thread 사용하여 ATM기에서 번갈아가며 돈을 빼는 프로그램을 만들어보장.

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
class ATM implements Runnable {        //완전추상객체 interface를 받는거/ runnable은 thread를 돌릴 수 있는 환경 제공
private long depositeMoney = 10000;
    public void run() {
        synchronized (this) {    //자기 자신의 class 내에서 동기화 하겠다.
            for (int i = 0; i < 10; i++) {
                notify();    //notify() : thread의 제어권을 넘겨준당. // 처음에는 withdraw 한번 돌고 제어권 넘기고 wait.
                try {
                    wait();
                    Thread.sleep(1000);    //1초 지연
                } catch (InterruptedException e) {    //예외처리
                    e.printStackTrace();
                }
                if (getDepositeMoney() <= 0)
                    break;
                withDraw(1000);    // withDraw 함수 호출
            }
        }
    }
    public void withDraw(long howMuch) {        
        ATM atm  = new ATM();    //객체 생성
 
        if (getDepositeMoney() > 0) {    //돈 1000원씩 까임
            depositeMoney -= howMuch;
            System.out.print(Thread.currentThread().getName()+ " , ");
            System.out.printf("잔액 : %,d 원 %n",getDepositeMoney());
        } else {        // 1000원 이하로 있을 때
            System.out.print(Thread.currentThread().getName()+ " , ");
            System.out.println("잔액이 부족합니다.");
        }
    }
    public long getDepositeMoney() {    //get
        return depositeMoney;
    }
}
 
public class SynchronizedEx {
    public static void main(String[] args) {
        ATM atm = new ATM();
        Thread mother = new Thread(atm, "mother");    //엄마 thread 생성, atm에서 thread를 만들어서 돌려라
        Thread son = new Thread(atm, "son");        
        mother.start();
        son.start();    //thread가 엄마꺼 아들꺼 두개 돌아감.
    }
}
cs




- main에서 mother thread가 먼저 호출될지 son thread가 먼저 호출 될지는 알 수 없다
- 7번 줄에서 notify() 가 호출됐을 때, 먼저 호출된 함수의 제어권이 다음 함수로 넘어가며,
  먼저 호출된 함수는 wait()함수에 의해 잠시 block 상태가 된다.
  for문이 끝날 때 까지 계속 반복하여 중복없이 번갈아 가며 1000원씩 빠져나갈 수 있게 된다.

 

 

 

-결과화면

 

 

4. join()


- 해당 작업의 수행이 끝날 때까지 기다렸다가, 수행이 끝나면 그 결과를 반환한다. 비동기 method이다.
- 비동기 method : 일반적인 method와 달리 method를 호출만 할 뿐, 그 결과를 기다리지 않는다
  내부적으로는 다른 thread에게 작업을 수행하도록 지시만 하고 결과를 기다리지 않고 돌아오는 것.

5. 간단한 예제 

 

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
class MyRunnableTwo implements Runnable{
    public void run(){
        System.out.println("run");//2
        first();    
    }
    
    public void first(){
        System.out.println("first");//3
        second();
    }
    
    public void second() {
        System.out.println("second");//4
    }
}
public class JoinEx {
    public static void main(String []args){
        System.out.println(Thread.currentThread().getName()+" start");//1
        Runnable r = new MyRunnableTwo();    //Runnable r class 생성 후 호출
        Thread myThread = new Thread(r);    //r Thread를 실행시킨다.
        myThread.start();    //시작
        try{
            myThread.join();    //모든 Thread가 돌 동안 main이 끝나지 않음 
        }catch(InterruptedException ie){
            ie.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" end");    //5
    }
}

// 주석에 써진 숫자 순서대로 출력된다. 

cs

 

 

 


 

'Java > Java' 카테고리의 다른 글

[Java] thread_1  (0) 2017.09.04

1. Thread


프로세스의 자원을 이용해서 실제로 수행하는 것이 Thread 이다.
- 개별적인 메모리공간(호출스택)을 필요로 한다.
- main Thread : main method의 작업을 수행하는것도 Thread이다.
- 실행중인 사용자 Thread 가 하나도 없을 때 프로그램은 종료한다.
  (=> main Thread가 끝나도 끝나지 않은 Thread가 있다면 프로그램은 종료되지 않음)

2. run() & start()


▶run()  
- Thread를 통해 작업하고자 하는 내용으로 run()의 { } 안을 채운다.
- main method에서 run을 호출하는 것은 생성된 Thread를 실행시키는 것이 아니라 
  단순히 class에 선언된 method를 호출하는 것.
▶start()
- 새로운 Thread가 작업을 실행하는데 필요한 호출스택(call stack)을 생성한 다음 run() 호출 
  →생성된 호출스택에 run()이 첫번째로 올라가게 한다.

3. Thread의 life Cycle

 

-쓰레드가 생성되면 아래 그림이 보여주는 네가지 상태 중 한가지 상태에 있게 된다.

 

 

3-1.
new 상태 : Thread class가 키워드 new를 통해서 인스턴스화 된 상태
runnable 상태 : start method 호출 후. 모든 실행의 준비를 마치고 스케쥴러에 의해 실행의 대상으로 선택되기를 기다리는 상태
blocked 상태 : 실행중인 Thread가 sleep 이나 join 메소드를 호출하는 등의 상황에서 cpu를 다른 thread에게 양보하고,
                        그 thread는 blocked상태가 된다.
dead 상태 : run 메소드의 실행이 완료되어서 run 메소드를 빠져나오게 되면 해당 thread는 dead 상태가 된다.
                   thread의 실행을 위해 할당받았던 메모리 등 모든 정보가 소멸.


4. Thread를 구현하는 방법

4-1. Thread class를 상속받는 방법


-Thread를 상속받은 클래스의 인스턴스를 생성한다.

 

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
 
//Single Thread
public class SingleThreadEx extends Thread{
   public int[] temp;
   public SingleThreadEx(String threadname){        //생성자 함수 (main에서 SingleThreadEx의 "첫번째"를 받음)
      super(threadname);    //참조변수
      temp = new int[10];    //배열 생성
      for(int start = 0; start < temp.length; start++){        
         temp[start] = start;    //배열 temp[start]에 값이 들어감.
      }
   }
   
   public void run(){    //Thread class의 run()을 오버라이딩
      for(int start : temp){
         try
            sleep(1000);        //1초 지연
         }catch (InterruptedException ie){    // 예외처리
            ie.printStackTrace();    //오류가 뭔지 찍어주는 함수
         }
         System.out.printf("스레드 이름 : %s ,",currentThread().getName());    //현재 thread의 이름(첫번째) 출력
         System.out.printf("temp value : %d %n", start);        //start의 값 찍어줌.
      }
   }
   public static void main(String[] args){
      SingleThreadEx st = new SingleThreadEx("첫번째");    //생성자가 불러진다.
      st.start();    // runnable 대기상태로 가서 스케쥴러가 run을 
   }
   
   
}
cs

 

4-2. Runnable interface를 구현하는 방법


-Runnable을 구현한 class의 인스턴스를 생성
-Runnable interface를 구현한 class의 instance를 생성한 다음, 이 instance를 Thread class의 생성자의 매개변수로 제공

 

 

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
public class Thread1 implements Runnable{
   private int[] temp;
   public Thread1(){
       temp=new int[10];
      for(int start=0; start<temp.length; start++){
         Thread.currentThread().setName("첫번째");
         temp[start]=start;
      }
   }
   public void run(){
      for(int start:temp){
         try{
            Thread.sleep(1000);
         }
         catch(InterruptedException ie){
            ie.printStackTrace();
            
         }
         System.out.printf("스레드 이름:%s", Thread.currentThread().getName());
         System.out.printf("temp value:%d %n",start);
      }
   }
    public static void main(String[]ar){
       Thread1 st= new Thread1();
       Thread ct = new Thread(st,"첫번째");
       ct.start();
    }
}
cs

 

 

(코드 둘 다 출력값은 똑같다)

'Java > Java' 카테고리의 다른 글

[Java] Thread_2  (0) 2017.09.04

+ Recent posts