JSP ๊ฐ๋ฐ ๋ฐฉ๋ฒ๋ก Model1
ํ์ ๊ฐ์ ๋ฑ๋ก ํผ ๋ฐ ์ฒ๋ฆฌ (memJoin.jsp, joinProc.jsp, Member, MemService.java)
<jsp:useBean> ์ก์ ์ฌ์ฉ
์ด๋ฆ, ์ฑ๋ณ, ์์ผ, ์ด๋ฉ์ผ, ๊ฒฝ๋ ฅ์ฐ์, ๊ด์ฌ ๊ณผ๋ชฉ(5๊ฐ ์ด์ ์ ์), ๊ฐ์ธ ์๊ฐ, ํ์์ ๋ฒํธ๋ ์ ๋ ฅ ์(๊ฐ์ ์)์ผ๋ก ์ง์
์๋ฒ์ธก์์๋ List<Member>๋ฅผ ์ฌ์ฉํ์ฌ ์ง๋ ฌํํ์ฌ ํ์ผ์ ์ ์ฅ(member.ser)
์ ์ฅ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉ์์๊ฒ ์๋ฐ ์คํฌ๋ฆฝํธ alert()๋ก ํ์
์ ์ฅ ๊ฒฐ๊ณผ ๋ฉ์์ง๋ฅผ ํ์ํ ํ์๋ ํ๋ฉด์ ๋ณ๊ฒฝ์ ์๋๋ก ํจ
ํผ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ํ์
memJoin.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
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ํ์๊ฐ์
</title>
</head>
<style type="text/css">
main { width:fit-content; border:1px solid black; padding:1em;
margin:0 auto; }
h1 { text-align: center; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
<script type="text/javascript">
function formCheck(){
var name = $('#name').val();
var gender ='';
var birth = $('#birth').val();
var email = $('#email').val();
var exp = $('#exp').val();
var subject ='';
var inv = $('#inv').val();
$('input[name=gender]').each(function(index, item){
if(item.checked) {
gender = $(item).val();
}
});
$('input[name=subject]').each(function(index, item){
if(item.checked) {
subject = $(item).val();
}
});
if(name==''|| gender==''|| birth==''|| email=='' ||exp==''
|| subject=='' || inv==''){
alert('๋ชจ๋ ํญ๋ชฉ ์
๋ ฅ์ ํ์์
๋๋ค.');
return false;
}
//alert($('#memberAdd').serialize());
return join();
}
function join(){
console.log('join() ์์');
$.ajax({
url : 'joinProc.jsp',
method:'post',
data: $('#memberAdd').serialize(),
cache:false,
dataType:'json',
success:function(res){
alert(res.added ?'ํ์์ ๋ณด ์ถ๊ฐ ์ฑ๊ณต':'์ ๋ณด์ถ๊ฐ ์คํจ');
},
error : function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
<body>
<main>
<h1>ํ์ ๊ฐ์
</h1>
<form id = 'memberAdd' onsubmit="return formCheck();">
<div><label>*์ด ๋ฆ</label><input id="name" type="text" name="name"></div>
<div><label id="gender">*์ฑ ๋ณ</label></div>
<div><label>๋จ ์</label><input type="radio" name="gender" value="๋จ์"></div>
<div><label>์ฌ ์</label><input type="radio" name="gender" value="์ฌ์"></div>
<div><label>*์ ์ผ</label><input id="birth" type="date" name="birth"></div>
<div><label>*๋ฉ ์ผ</label><input type="text" id="email" name="email"></div>
<div><label>*๊ฒฝ๋ ฅ ์ฐ์</label><input id="exp" type="number" name="exp" value="0"></div>
<div><label id="subject">*๊ด์ฌ ๊ณผ๋ชฉ</label></div>
<div><input type="checkbox" name="subject" value="Java">Java</div>
<div><input type="checkbox" name="subject" value="Python">Python</div>
<div><input type="checkbox" name="subject" value="Spring">Spring</div>
<div><input type="checkbox" name="subject" value="Android">Android</div>
<div><input type="checkbox" name="subject" value="c++">c++</div>
<textarea id="inv" name="inv" rows="5" cols="20" placeholder="๊ฐ์ธ ์๊ฐ ํ์ ์์ฑ"></textarea>
<div><button type="submit" >์ ์ฅ</button></div>
<div><button type="reset">์ทจ์</button></div>
</form>
</main>
</body>
</html>
|
cs |
๋ค์ํ input ํ์ ์ ํ๊ทธ๋ค์ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ ํ ์ ์๋๋ก ํ๋ค.
์ด ํญ๋ชฉ๋ค์ด ํ๋๋ผ๋ ๋น ์ ธ์ ์๋ฒ์ ๋์ด ๊ฐ๋ฉด ์ ๋๋ฏ๋ก ํผ ์ฒดํฌ๋ฅผ ํด์ค์ผํ๋ค. ํผ ์ฒดํฌ๋ฅผ ํด์ฃผ๊ธฐ์ํด jquery๋ฅผ ์ด์ฉํ์ฌ ํผ์ฒดํฌ ํจ์๋ฅผ ๋ง๋ ๋ค. ์ฑ๋ณ๊ณผ ๊ด์ฌ๊ณผ๋ชฉ์ ์ด์ฉ์๊ฐ ๊ณ ๋ฅด๋ ๊ฒ์ด๊ธฐ์ ์ ํํ value๋ฅผ ๋ฝ์์ค์ผํ๋ฏ๋ก ๋ฐ๋ก ๋ก์ง ์ฒ๋ฆฌ ํด์ค๋ค. ์ด์ ๋ฝ์์จ value๋ค์ด ํ๋๋ผ๋ ๋น๊ฐ์ด ์์ผ๋ฉด alert์ฐฝ์ ํตํด ๋ชจ๋ ํญ๋ชฉ์ ์ ๋ ฅํด์ผํ๋ค๊ณ ์๋ ค์ฃผ๊ณ ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ์ ํ์ ๋ฑ๋ก ๋ก์ง์ด ์คํ๋๊ฒ ํด์ค๋ค.
ํ์๋ฑ๋ก ๋ก์ง ๊ฒฝ์ฐ joinProc.jsp ํตํด ๋์ด์จ added ๊ฐ์ด ์ฐธ์ด๋ฉด ํ์ ์ ๋ณด ๋ฑ๋ก ์ฑ๊ณต์ ๋์ฐ๊ณ ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ๋ ์คํจ๋ฅผ ๋์ฐ๊ฒ ํ๋ค. return์ false๋ฅผ ํด์ค์ผ์ง ํผ์ด submit๋ก ๋์ด๊ฐ์ ๋ ์ค๋ณตํ์ฌ ๋์ด๊ฐ์ง ์๊ฒ ํด์ค์ผํ๋ค.
joinProc.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@page import="org.json.simple.JSONObject"%>
<%@page import="com.ezen.web.hello.MemberService"%>
<%@page import="java.util.*"%>
<%@page import="com.ezen.web.hello.Member"%>
<%@ page contentType="application/json; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="mem" class= "com.ezen.web.hello.Member">
<jsp:setProperty name="mem" property="*"/>
</jsp:useBean>
<jsp:useBean id="svc" class= "com.ezen.web.hello.MemberService" scope="session"/>
<jsp:setProperty name="svc" property="member" value="<%=mem %>"/>
<%
boolean added = svc.addMember();
JSONObject jsobj = new JSONObject();
jsobj.put("added", added);
%>
<%= jsobj.toJSONString() %>
|
cs |
<jsp:useBean id="mem" class= "com.ezen.web.hello.Member">
<jsp:setProperty name="mem" property="*"/>
</jsp:useBean>
<jsp:useBean id="svc" class= "com.ezen.web.hello.MemberService" scope="session"/>
<jsp:setProperty name="svc" property="member" value="<%=mem %>"/>
<jsp:useBean> ์ก์ ํ๊ทธ๋ค์ joinProc.jsp์์ Member.java์ ๋ชจ๋ ํ๋กํผํฐ(๋ฉค๋ฒ ๋ณ์)๋ค์ mem์ด๋ผ๋ ์ฐธ์กฐ๋ณ์์ setํด์ฃผ๊ณ ๊ทธ mem์ ๋ค์ MemberService.java์ ํ๋กํผํฐ member ๊ฐ์ฒด์ ๋ด๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด joinProc.jsp์์๋ MemberService.java์ member๋ฅผ ๋ถ๋ฆด ์ ์๋ค.
MemberService.java์์ ๋ง๋ addMember()๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ json๊ฐ์ฒด์ boolean ๊ฐ์ ๋ฃ๊ณ memJoin.jsp์ res.added์ ๋ณด๋ด์ค๋ค.
Member.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
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
|
import java.io.Serializable;
public class Member implements Serializable, Comparable<Member>
{
private int num;
private String name;
private String birth;
private String email;
private String gender;
private String exp;
private String[] subject;
private String inv;
public Member() {}
public Member(int num)
{
this.num = num;
}
public Member(String name, String birth, String email,
String gender, String exp, String[] subject, String inv) {
super();
this.name = name;
this.birth = birth;
this.email = email;
this.gender = gender;
this.exp = exp;
this.subject = subject;
this.inv = inv;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getExp() {
return exp;
}
public void setExp(String exp) {
this.exp = exp;
}
public String[] getSubject() {
return subject;
}
public void setSubject(String[] subject) {
this.subject = subject;
}
public String getInv() {
return inv;
}
public void setInv(String inv) {
this.inv = inv;
}
@Override
public int compareTo(Member o) {
if(this.num>o.num) {
return 1;
}else if(this.num<o.num) {
return -1;
}else return 0;
}
@Override
public boolean equals(Object obj) {
Member other = (Member) obj;
return this.num==other.num;
}
}
|
cs |
Member.java๋ ๋ชจ๋ธ ํด๋์ค๋ก ๋ค๋ฃฐ ๋ฐ์ดํฐ์ ๋ชจ๋ธ์ ๊ธฐ๋ณธ์ ์ค์ ํด์ค๋ค. ๋ฑ๋กํ ์์๋๋ก ๋ฒํธ๋ฅผ ๋ถ์ฌํ๊ธฐ ์ํด์ compareTo() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํด์ฃผ๊ณ ๊ฐ์ฒด๋ฅผ ๊บผ๋ด์ฌ ๋ ๋ฒํธ๋ฅผ ํ์ธํ์ฌ ๊บผ๋ด์ฌ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ equals() ๋ฉ์๋๋ ์ค๋ฒ๋ผ์ด๋ ํด์ค๋ค. ๋ชจ๋ ๋ฉค๋ฒ ๋ณ์๋ getter, setter ๋ฉ์๋๋ฅผ ๋ง๋ค์ด ๊ฐ์ฒด๋ฅผ ๋ถ๋ฅด๊ณ ๊ฐ์ ์ ํ ์ ์๊ฒ ํ๋ค. ๋ฑ๋ก๋ ๋ฐ์ดํฐ ๊ฐ๋ค์ ํ์ผ์ ์ง๋ ฌํ ํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ Serializable๊ณผ ๋์กฐ๋ฅผ ์ํ์ฌ Comparable์ implements ํด์ค์ผ ํ๋ค.
MemberService.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.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
public class MemberService {
private Member member;
private HttpServletRequest request;
private String fpath = "D:/java_test/member.ser";
public MemberService(){}
public MemberService(HttpServletRequest request)
{
this.request = request;
}
public void setMember(Member member) {
this.member = member;
}
public boolean addMember()
{
List<Member> list = deserialize();
Collections.sort(list);
int num = 1;
if(list.size()>0) {
num = list.get(list.size()-1).getNum()+1;
}
member.setNum(num);
list.add(member);
return serialize(list);
}
private boolean serialize(List<Member> list)
{
try {
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(fpath));
objOut.writeObject(list);
objOut.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private List<Member> deserialize()
{
File f = new File(fpath);
List<Member> list = null;
if(!f.exists()) {
list = new ArrayList<Member>();
}else {
try {
ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(fpath));
list = (List<Member>)objInput.readObject();
objInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
|
cs |
MemberService.java๋ ํต์ฌ ๋ก์ง๋ค์ด ๋์๊ฐ๋ ์๋น์ค ํด๋์ค์ด๋ค. deserialize()๋ ํ์ผ์ ์ญ์ง๋ ฌํํ์ฌ Member๊ฐ์ฒด๊ฐ ๋ด๊ธด List๋ฅผ ๋ฐํํด์ค๋ค. ํ์ผ์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด(์ฒซ ๋ฑ๋ก์๋ฅผ ๋ฑ๋กํ ์์ ํ์ผ์ด ์กด์ฌํ์ง ์๋๋ค.) ํ์ผ์ ์ญ์ง๋ ฌํ ํ์ง ์๊ณ ์๋ก์ด list ๋ณ์๋ฅผ ๋ง๋ ๋ค. ํ์ผ์ด ์์ ์์ ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์ญ์ง๋ ฌํ ํ์ฌ liet๋ฅผ returnํด์ค๋ค. serialize()๋ ๋ฐ์ดํฐ๋ฅผ ํ์ผ์ ์ง๋ ฌํํ์ฌ ์ฑ๊ณตํ๋ฉด true๋ฅผ ์คํจํ๋ฉด false๋ฅผ returnํ๋ค.
addMember()๋ฉ์๋๋ list์ ์ ์ผ ๋ง์ง๋ง member ๊ฐ์ฒด์ ๋ฒํธ์ +1์ ํ์ฌ ์๋กญ๊ฒ ์ ์ฅํ Member ๊ฐ์ฒด์ ๋ฒํธ๋ก ๋ถ์ฌํ๊ณ ์๋ฒ๋ก ๋์ด์จ ๋ฐ์ดํฐ์ ํจ๊ป list๊ฐ์ฒด์ ๋ด์ ์ง๋ ฌํ ํ๋ค.
์คํ ๊ฒฐ๊ณผ:
๋๊ธ