Web programming

(22.11.14)Web ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ, JSP, HTML, jsp:Bean, ์˜์—ญ๊ฐ์ฒด

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

<form> ํƒœ๊ทธ ์•ˆ์— ๊ณผ์ผ ์ด๋ฆ„ ๋ชฉ๋ก์ด <checkbox>์™€ ํ•จ๊ป˜ ์ œ์‹œ๋˜์–ด์žˆ๊ณ  ํŠน์ • ๊ณผ์ผ ์ด๋ฆ„์— ์—ฐ๊ฒฐ๋œ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•˜๊ณ  ํผ ์•ˆ์˜ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์„œ๋ฒ„์ธก์—์„œ ํ•ด๋‹น ๊ณผ์ผ์ด ์‚ญ์ œ๋˜๊ณ  ์„œ๋ฒ„์ธก์—์„œ ์ •์ƒ์ ์œผ๋กœ ์‚ญ์ œ๋˜๋ฉด ํด๋ผ์ด์–ธํŠธ ์ธก์œผ๋กœ ์„ฑ๊ณต ๋ฉ”์‹œ์ง€๊ฐ€ ํด๋ผ์ด์–ธํŠธ ์ธก์œผ๋กœ ์ „๋‹ฌ๋˜๋„๋ก ํ•˜์‹œ์˜ค.

 

form_proc.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
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.util.*"%>
<%@ page contentType="application/json; charset=utf-8" pageEncoding="utf-8"%>
<%
    /*
    String hobby = request.getParameter("hobby");
    String[] hobbies= request.getParameterValues("hobby");
    */
    List<String> cart = new ArrayList<>();
    cart.add("Apple");
    cart.add("Orange");
    cart.add("Pineapple");
    cart.add("Kiwi");
    
    String[] fr = request.getParameterValues("fruits");
    int cnt = 0;
    
    for(int i=0;i<fr.length;i++)
    {
        for(int j=(cart.size()-1)/*์›์†Œ์˜ ๋์—์„œ ๋ถ€ํ„ฐ ์‚ญ์ œ*/;j>=0;j--)
        {
            if(cart.get(j).equals(fr[i])){
                cart.remove(j);
                cnt++;    
            }
        }
    }
    boolean removed = cnt==fr.length ? true : false;
    JSONObject jsobj = new JSONObject();
    jsobj.put("removed" , removed);
    out.print(jsobj.toString());
    out.flush();
%>
cs

 

form_test.html

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ํผ ํƒœ๊ทธ ๋‹ค๋ฃจ๊ธฐ</title>
</head>
<body>
<hr>
<style>
    li { display:inline; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" 
crossorigin="anonymous"></script>
 
<script>
function remove(){
    var ser = $('#remform').serialize();
    $.ajax({
        url:'form_proc.jsp',
        method:'post',
        data: ser,
        cache: false,
        dataType: 'json',
        success: function(res){
            alert(res.removed);
        },
        error : function(xhr, status, err){
            alert(err);
        }
    });
    return false;
}
</script>
<div >
    <form  id="remform"onsubmit="return remove();">
        <ul>
            <div id="Apple">
                <input type="checkbox" name="fruits" value="Apple">
                <li>Apple</li>
            </div>
            <div id="Orange">
                <input type="checkbox" name="fruits" value="Orange">
                <li>Orange</li>
            </div>
            <div id="Pineapple">
                <input type="checkbox" name="fruits" value="Pineapple">
                <li>Pineapple</li>
            </div>
            <div id="Kiwi">
                <input type="checkbox" name="fruits" value="Kiwi">
                <li>Kiwi</li>
            </div>
        </ul>
        <div><button type="submit">์‚ญ์ œ</button></div>
    </form>
</div>
<div><button type="button" onclick="rem();">ํ…Œ์ŠคํŠธ</button></div>
</body>
</html>
cs
 

 

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


์„œ๋ธ”๋ฆฟ๊ณผ JSP์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€?

 

์„œ๋ธ”๋ฆฟ์€ ์›น๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ธ๋‹ค. ์ฆ‰ ์š”์ฒญ์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‘๋‹ต์„ ํ•  ๋•Œ html๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

jsp๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์‘๋‹ต์„ ํ•  ๋•Œ html๋กœ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฐจ์ด์ ์ด๋ผ๋ฉด ์„œ๋ธ”๋ฆฟ์€ ์ •์ƒ์ ์ธ ์ž๋ฐ” ํด๋ž˜์Šค๋‹ค. ์ž๋ฐ” ํด๋ž˜์Šค์ด๋ฉด์„œ ๋™์‹œ์— http ์š”์ฒญ ์ฒ˜๋ฆฌ์— ํŠนํ™”๋œ ํด๋ž˜์Šค์ด๋‹ค. ์ด์šฉ์ž์œผ์ด ์š”์ฒญ์„ ๋ฐ›์•„ ์„œ๋น„์Šค() ํ˜ธ์ถœ์‹œ์ผœ์ค€๋‹ค. jsp๋Š” ์ž๋ฐ” ํด๋ž˜์Šค๋Š” ์•„๋‹ˆ์ง€๋งŒ, jsp๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ํ†ฐ์บฃ์ด ์ž๋ฐ”ํด๋ž˜์Šค(์„œ๋ธ”๋ฆฟ)๋กœ ๋ณ€ํ™˜(์ปดํŒŒ์ผ)์‹œ์ผœ์ค€๋‹ค. ์ปดํŒŒ์ผ ๋œ ํ›„ ์‹คํ–‰๋˜๋ฉด ๊ทธ ์•ˆ์˜ ์„œ๋น„์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋Œ์•„๊ฐ€๊ฒŒ ๋œ๋‹ค. 

 

์ผ๋ฐ˜์ ์ธ java ํด๋ž˜์Šค (Plain Old Java Object) ํฌ์กฐ ํด๋ž˜์Šค

 

Java ๊ฐœ๋ฐœ๋ฐฉ๋ฒ•๋ก  Model1: ์„œ๋ธ”๋ฆฟ ์ œ์™ธ (jsp(์ปจํŠธ๋กค๋Ÿฌ์™€ view์—ญํ• ์„ ํ•จ), ํฌ์กฐ ์ž๋ฐ” ํด๋ž˜์Šค(์„œ๋น„์Šค ๋กœ์ง ํด๋ž˜์Šค, ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค)๋งŒ ์‚ฌ์šฉ)

Java ๊ฐœ๋ฐœ๋ฐฉ๋ฒ•๋ก  Model2: MVC


 

Java ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก  ๋ชจ๋ธ 1์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋žตํ•œ ๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ ์›น ๋งŒ๋“ค๊ธฐ

 

gugu.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@page import="com.ezen.web.hello.GuguService"%>
<%@ page contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    GuguService gs = new GuguService(); //์„œ๋น„์Šค๋ฅผ ๋ถˆ๋Ÿฌ์„œ ์ž‘์—… ์‹œํ‚ค๊ธฐ
    String gString = gs.createGugu(request);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>
</head>
<body>
<h3>
๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ
</h3>
<%= gString %> <!--์š”์ฒญ๋ฐ›์€ ๊ฒฐ๊ณผ ๋ณด๊ธฐ -->
</body>
</html>
cs

view์˜ ๊ธฐ๋Šฅ์„ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋ผ๋„ ํ…์ŠคํŠธ๋Š” html์„ ์จ์•ผํ•œ๋‹ค.

 

 

java ํด๋ž˜์Šค

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ezen.web.hello;
 
import javax.servlet.http.HttpServletRequest;
 
public class GuguService 
{
        public String createGugu(HttpServletRequest request) {
        String sDan = request.getParameter("dan");
        int dan = sDan==null2 : Integer.parseInt(sDan);
        
        String data = "";
        for(int i =1;i<10;i++) {
            data += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        return data;
    }
}
cs

 

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


ํ…Œ์ŠคํŠธ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉ์ž๊ฐ€ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌ ํ•œ๋’ค ํ…Œ์ŠคํŠธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ”„๋ก ํŠธ์—”๋“œ์—์„œ๋„ ์‚ฌ๋ผ์ง€๊ฒŒ ๋งŒ๋“ค๊ธฐ

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ํผ ํƒœ๊ทธ ๋‹ค๋ฃจ๊ธฐ</title>
</head>
<body>
<hr>
<style>
    li { display:inline; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" 
crossorigin="anonymous"></script>
 
<script>
function remove(){
    var ser = $('#remform').serialize();
    $.ajax({
        url:'form_proc.jsp',
        method:'post',
        data: ser,
        cache: false,
        dataType: 'json',
        success: function(res){
            alert(res.removed);
        },
        error : function(xhr, status, err){
            alert(err);
        }
    });
    return false;
}
function rem(){
    $('input[type=checkbox]'/*์„ ํƒ์ž๋ฅผ ๋‘๋Š” ๊ณณ*/).each(function(index, item){
        //console.log(item.checked);
        if($(item).is(':checked')){
            $('div#'+$(item).val()).remove();
        }
    });
}
</script>
<div >
    <form  id="remform"onsubmit="return remove();">
        <ul>
            <div id="Apple">
                <input type="checkbox" name="fruits" value="Apple">
                <li>Apple</li>
            </div>
            <div id="Orange">
                <input type="checkbox" name="fruits" value="Orange">
                <li>Orange</li>
            </div>
            <div id="Pineapple">
                <input type="checkbox" name="fruits" value="Pineapple">
                <li>Pineapple</li>
            </div>
            <div id="Kiwi">
                <input type="checkbox" name="fruits" value="Kiwi">
                <li>Kiwi</li>
            </div>
        </ul>
        <div><button type="submit">์‚ญ์ œ</button></div>
    </form>
</div>
<div><button type="button" onclick="rem();">ํ…Œ์ŠคํŠธ</button></div>
</body>
</html>
cs

 

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

Apple ๊ณผ Orange ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•˜๊ณ  ํ…Œ์ŠคํŠธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋‹ˆ ๋‹ค์Œ ๊ฐ™์ด ์‚ฌ๋ผ์กŒ๋‹ค.


์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํ™•์ธ ๋ฌธ๊ตฌ๊ฐ€ ๋œจ๋ฉด์„œ ํ”„๋ก ํŠธ์—”๋“œ์—์„œ๋„ ํ•ญ๋ชฉ ์‚ญ์ œ๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

 

form_test.html

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ํผ ํƒœ๊ทธ ๋‹ค๋ฃจ๊ธฐ</title>
</head>
<body>
<hr>
<style>
    li { display:inline; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" 
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" 
crossorigin="anonymous"></script>
 
<script>
function remove(){
    var ser = $('#remform').serialize();
    $.ajax({
        url:'form_proc.jsp',
        method:'post',
        data: ser,
        cache: false,
        dataType: 'json',
        success: function(res){
            if(res.removed){
                alert('์‚ญ์ œ ์„ฑ๊ณต!');
                $('input[type=checkbox]'/*์„ ํƒ์ž๋ฅผ ๋‘๋Š” ๊ณณ*/).each(function(index, item){
                    if($(item).is(':checked')){
                        $('div#'+$(item).val()).remove();
                    }
                });            
            }else{
                alert('์‚ญ์ œ ์‹คํŒจ');
            }
        },
        error : function(xhr, status, err){
            alert(err);
        }
    });
    return false;
}
</script>
<div >
    <form  id="remform"onsubmit="return remove();">
        <ul>
            <div id="Apple">
                <input type="checkbox" name="fruits" value="Apple">
                <li>Apple</li>
            </div>
            <div id="Orange">
                <input type="checkbox" name="fruits" value="Orange">
                <li>Orange</li>
            </div>
            <div id="Pineapple">
                <input type="checkbox" name="fruits" value="Pineapple">
                <li>Pineapple</li>
            </div>
            <div id="Kiwi">
                <input type="checkbox" name="fruits" value="Kiwi">
                <li>Kiwi</li>
            </div>
        </ul>
        <div><button type="submit">์‚ญ์ œ</button></div>
    </form>
</div>
</body>
</html>
cs

 

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


๋ผ๋””์˜ค ๋ฐ•์Šค๋ฅผ  ๋งŒ๋“ค์–ด Java, JSP, Python, Spring ๋ชฉ๋ก์„ ์ œ์‹œํ•˜๊ณ  ์ด์šฉ์ž๊ฐ€ 1๊ฐœ๋งŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋กํ•œ๋‹ค.

ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์„ ํƒ๋œ ํ•ญ๋ชฉ 1๊ฐœ๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‚˜๋จธ์ง€ ํ•ญ๋ชฉ์€ ๋ชจ๋‘ ์‚ญ์ œ๋˜๋„๋ก ํ•˜๋ผ

 

subject.html

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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ณผ๋ชฉ ๊ณ ๋ฅด๊ธฐ</title>
<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(){
    $('#sampleform button').click(function(){
        $('input:radio').each(function(index, item){
            if(!item.checked){
                $('div#'+$(item).val()).remove();
            }
        });
    });
})
</script>
</head>
<body>
<div >
    <form id="sampleform">
        
            <div id="Java">
                <input type="radio" id="Java" name="subject" value="Java">
                <label>Java</label>
            </div>
            <div id="JSP">
                <input type="radio" id="JSP" name="subject" value="JSP">
                <label>JSP</label>
            </div>
            <div id="Python">
                <input type="radio" id="Python" name="subject" value="Python">
                <label>Python</label>
            </div>
            <div id="Spring">
                <input type="radio" id="Spring" name="subject" value="Spring">
                <label>Spring</label>
            </div>
        
        <button type="button">[ํ™•์ธ]</button>
    </form>
</div>
</body>
</html>
cs

 

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


jsp:useBean์„ ์ด์šฉํ•˜์—ฌ ์ž๋ฐ” ๊ฐœ๋ฐœ๋ฐฉ๋ฒ•๋ก  ๋ชจ๋ธ1 ํ•ด๋ณด๊ธฐ

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
<%@page import="org.json.simple.JSONObject"%>
<%@page import="com.ezen.web.hello.GuguService"%>
<%@ page contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<jsp:useBean id="svc" class= "com.ezen.web.hello.GuguService" scope="page"></jsp:useBean>
<%
    String gString = svc.createGugu(request);
%>
<%--
    GuguService gs = new GuguService(); //์„œ๋น„์Šค๋ฅผ ๋ถˆ๋Ÿฌ์„œ ์ž‘์—… ์‹œํ‚ค๊ธฐ
    String gString = gs.createGugu(request);
    JSONObject jsobj = new JSONObject();
    jsobj.put("content" , gString);
    out.print(jsobj.toJSONString());
    out.flush();
--%>
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>
</head>
<body>
<h3>
๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ
</h3>
<%= gString %>
</body>
</html>
 
cs

 

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

Bean ์˜ค๋ธŒ์ ํŠธ์˜ scope

-page ์˜์—ญ : Bean ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ๊ทธ ํŽ˜์ด์ง€(JSP)์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. page ์˜์—ญ์˜ ์˜์—ญ๊ฐ์ฒด - pageContext ๊ฐ์ฒด

-request ์˜์—ญ: Bean ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด request ๊ฐ์ฒด์— ์ €์žฅ๋จ (request ๊ฐ์ฒด)

-session ์˜์—ญ: Bean ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด HttpSession ๊ฐ์ฒด์— ์ €์žฅ๋จ(ํ•œ ์ด์šฉ์ž๊ฐ€ ์ ‘์†ํ•˜๋Š” ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋ชจ๋“  jsp, servlet )

๋ธŒ๋ผ์šฐ์ €๋‹น ํ•œ ๊ฐœ์”ฉ (100๋ช…์ด ์ ‘์†ํ•˜๋ฉด 100๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.)

-application ์˜์—ญ: ํ•œ ์›น์„œ๋ฒ„ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  jsp, servlet 

ํ•œ ์„œ๋ฒ„๋‹น ํ•œ ๊ฐœ์”ฉ (100๋ช…์ด ํ•œ ์„œ๋ฒ„์— ์ ‘์†ํ•œ๋‹ค๋ฉด 100๋ช…์€ 1๊ฐœ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค.)

 

์˜์—ญ ์•ˆ์—์„  ์–ด๋””์—์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด: ์˜์—ญ ๊ฐ์ฒด

๋Œ“๊ธ€