Web programming

(22.11.16)Web ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ํšŒ์› ๊ฐ€์ž…๋“ฑ๋ก(JSP, jsp:Bean ์•ก์…˜ํƒœ๊ทธ)

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 11. 17.

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๊ฐ์ฒด์— ๋‹ด์•„ ์ง๋ ฌํ™” ํ•œ๋‹ค.

 

 

์‹คํ–‰ ๊ฒฐ๊ณผ:

๋Œ“๊ธ€