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"> <input type="text" name="id" size="20">
4~12자의 영문 대소문자와 숫자로만 입력
</td>
</tr>
<tr align="center">
<td>비밀번호 :</td>
<td align="left"> <input type="password" name="pw" size="20">
4~12자의 영문 대소문자와 숫자로만 입력
</td>
</tr>
<tr align="center">
<td>비밀번호 확인 :</td>
<td align="left"> <input type="password" name="pw1"
size="20"></td>
</tr>
<tr align="center">
<td>메일주소 :</td>
<td align="left"> <input type="text" name="email" size="30">
예:id@domain.com
</td>
</tr>
<tr align="center">
<td>이름 :</td>
<td align="left"> <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">
<input type="text" name="mynum" size="30"> 예:123456-1234567
</td>
</tr>
<tr align="center">
<td>생일 :</td>
<td align="left">
<input type="text" name="year" size="5">년
<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> 월
<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">
<input type="checkbox" name="favorite" value="computer">컴퓨터
<input type="checkbox" name="favorite" value="인터넷">인터넷
<input type="checkbox" name="favorite" value="여행">여행
<input type="checkbox" name="favorite" value="영화감상">영화감상
<input type="checkbox" name="favorite" value="음악감상">음악감상
</td>
</tr>
<tr align="center">
<td>자기소개 :</td>
<td align="left">
<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">
<!-- 자바빈 객체의 멤버 변수를 접근 하는 것이 아니라 getter 호출 -->
<jsp:getProperty property="id" name="joinbean"/>
</td>
</tr>
<tr align="center">
<td>비밀번호 :</td>
<td align="left">
<jsp:getProperty property="pw" name="joinbean"/>
</td>
</tr>
<tr align="center">
<td>메일주소 :</td>
<td align="left">
<jsp:getProperty property="email" name="joinbean"/>
</td>
</tr>
<tr align="center">
<td>이름 :</td>
<td align="left">
<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">
<jsp:getProperty property="mynum" name="joinbean"/>
</td>
</tr>
<tr align="center">
<td>생일 :</td>
<td align="left">
<jsp:getProperty property="year" name="joinbean"/>년
<jsp:getProperty property="month" name="joinbean"/>월
<jsp:getProperty property="day" name="joinbean"/>일
</td>
</tr>
<tr align="center">
<td>관심분야 :</td>
<td align="left">
<!-- 배열을 순서대로 출력하기 위해 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">
<jsp:getProperty property="intro" name="joinbean"/>
</td>
</tr>
</table>
</body>
</html> |
cs |
출력화면