Java programming

(22.11.02)Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๋„คํŠธ์›Œํฌ ๋ฐ์ดํ„ฐ ํ†ต์‹ , Stream, ์›น์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

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

๋ฉ”์‹œ์ง€์™€ ์ˆ˜์‹ ์ž ์†ก์‹ ์ž๋ฅผ ๊ฐ์ฒดํ™” ํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ”๊พธ์–ด ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹  ํ•˜๊ธฐ

 

ChatMsg ํด๋ž˜์Šค ์ฝ”๋“œ

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
import java.io.Serializable;
 
public class ChatMsg implements Serializable{
    private String sender;
    private String receiver;
    private String message;
    private String uid;
    private String upw;
    
    public ChatMsg() { }
 
    public ChatMsg(String sender, String receiver, String message) {
        super();
        this.sender = sender;
        this.receiver = receiver;
        this.message = message;
    }
    public ChatMsg(String msg) {
        this.message=msg;
    }
    
    public ChatMsg(String uid, String upw) {
        this.uid=uid;
        this.upw=upw;
    }
 
    public String getUid() {
        return uid;
    }
 
    public void setUid(String uid) {
        this.uid = uid;
    }
 
    public String getUpw() {
        return upw;
    }
 
    public void setUpw(String upw) {
        this.upw = upw;
    }
 
    public String getSender() {
        return sender;
    }
 
    public void setSender(String sender) {
        this.sender = sender;
    }
 
    public String getReceiver() {
        return receiver;
    }
 
    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}
cs

๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒดํ™” ํ•  ChatMsg ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ๋งŒ๋“ ๋‹ค. ๊ฐ์ฒดํ™” ๋œ ๋ฐ์ดํ„ฐ๋Š” ์ง๋ ฌํ™”๋ฅผ ํ†ตํ•ด ์ŠคํŠธ๋ฆผ์œผ๋กœ ์ž…๋ ฅ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์—  ์ง๋ ฌํ™” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implements ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค ์ฝ”๋“œ

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
import java.io.*;
import java.net.*;
import java.util.Scanner;
 
public class Client
{
    public static void main(String[] args)
    {
        try {
            Socket s = new Socket("127.0.0.1",1234);
            System.out.println("์„œ๋ฒ„์ ‘์† ์„ฑ๊ณต");
            
            //์„œ๋ฒ„์—์„œ ์ „์†กํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•œ๋‹ค
            InputStream is = s.getInputStream();
            ObjectInputStream oin = new ObjectInputStream(is);
            
            
            new ClientThread(s, oin).start(); //๋„คํŠธ์›Œํฌ ์ถœ๋ ฅ ์“ฐ๋ ˆ๋“œ
            
            while(true) {//๋„คํŠธ์›Œํฌ ์ž…๋ ฅ ์“ฐ๋ ˆ๋“œ
                ChatMsg m = (ChatMsg)oin.readObject();
                System.out.println("์„œ๋ฒ„์ธก์—์„œ ๋ณด๋‚ด์˜จ ๋ฉ”์„ธ์ง€:"+m.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("ํด๋ผ์ด์–ธํŠธ ์ข…๋ฃŒ");
    }
    //๋„คํŠธ์›Œํฌ ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•  ํด๋ผ์ด์–ธํŠธ ์ธก ์“ฐ๋ ˆ๋“œ
    static class ClientThread extends Thread {
        Socket s;
        ObjectOutputStream oos;
        ObjectInputStream oin;
        boolean loginpass;
        ClientThread(Socket s,ObjectInputStream oin){
            this.s=s;
            this.oin=oin;
            try {
                ChatMsg msg = (ChatMsg)oin.readObject();
                System.out.println("์„œ๋ฒ„์ธก์—์„œ ๋ณด๋‚ด์˜จ ๋ฉ”์„ธ์ง€:"+msg);
                if(msg.getMessage().equals("์ ‘์† ์„ฑ๊ณต")) {
                    OutputStream os = s.getOutputStream();
                    this.oos = new ObjectOutputStream(os);
                    
                    loginpass = login();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            Scanner kbd = new Scanner(System.in);
            while(true) {
                System.out.println("์ž…๋ ฅํ•  ๋ฉ”์‹œ์ง€:");
                String line = kbd.nextLine().strip();
                ChatMsg cmsg = new ChatMsg();
                cmsg.setSender("ChatMsg");
                cmsg.setReceiver("Laura");
                cmsg.setMessage(line);
                
                try {
                    oos.writeObject(cmsg);
                    oos.flush();
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        private boolean login() {
            
            try {
                ChatMsg msg = new ChatMsg("Smith""12345");
                oos.writeObject(msg);
                oos.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            
            String loginresult =null;
            try {
                loginresult = ((ChatMsg)oin.readObject()).getMessage();
                System.out.println("๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ:"+loginresult);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(loginresult.equals("๋กœ๊ทธ์ธ ์„ฑ๊ณต")) {
                return true;
            }else {
                return false;
            }
        }
    }            
}
cs

ํ…์ŠคํŠธ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ์ŠคํŠธ๋ฆผ(ex PrintWriter)์„ ๊ฐ์ฒด๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ์ŠคํŠธ๋ฆผ(ObjectOutputStream)์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

 

์„œ๋ฒ„ ํด๋ž˜์Šค ์ฝ”๋“œ

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
import java.io.*;
import java.net.*;
import java.util.ArrayList;
 
public class Server 
{
    static ArrayList<ObjectOutputStream> out = new ArrayList<ObjectOutputStream>();
    
    public static void main(String[] args) 
    {
        try { 
            ServerSocket ss = new ServerSocket(1234);
            while(true) {
                System.out.println("์„œ๋ฒ„์†Œ์ผ“ ๋Œ€๊ธฐ์ค‘");
                Socket s = ss.accept(); //๋ฌดํ•œ๋Œ€๊ธฐํ•˜๋‹ค๊ฐ€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘์†ํ•˜๋ฉด ์†Œ์ผ“์„ ๋งŒ๋“ค์–ด๋ƒ„
                System.out.println("ํด๋ผ์ด์–ธํŠธ ์ ‘์†๋จ");
                new CommThread(s).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("์„œ๋ฒ„ ์ข…๋ฃŒ");
    }
    //๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ํ†ต์‹ ์šฉ ์“ฐ๋ ˆ๋“œ
    static class CommThread extends Thread {
        private Socket s;
        private ObjectOutputStream oos;
        private ObjectInputStream oin;
        private boolean loginpass;
        CommThread(Socket s) {
            this.s = s;
            try {
                OutputStream os = s.getOutputStream();
                this.oos = new ObjectOutputStream(os);
                
                
                oos.writeObject(new ChatMsg("์ ‘์† ์„ฑ๊ณต"));
                oos.flush();
                
                loginpass = login();
                
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            if(loginpass) {
                try {
                    ObjectOutputStream toClient = null;
                    while(true) {
                        ChatMsg cmsg = (ChatMsg)oin.readObject();
                        String receiver = cmsg.getReceiver();
                        
                        for(int i=0;i<out.size();i++) {
                            toClient = out.get(i);
                            toClient.writeObject(cmsg);
                            toClient.flush();
                        }
                    }
                }catch (Exception e) {
                    System.err.println("ํด๋ผ์ด์–ธํŠธ ๋‚˜๊ฐ");
                    out.remove(oos);
                    System.out.println("์ ‘์†์ž์ˆ˜" + out.size());
                    
                }
            }
            System.out.println("ํ†ต์‹  ์“ฐ๋ ˆ๋“œ ์ข…๋ฃŒ");
        }
        private boolean login() {
            try {
                InputStream is = s.getInputStream();
                this.oin = new ObjectInputStream(is);
                
                
                ChatMsg cmsg = (ChatMsg)oin.readObject();
                String uid = cmsg.getUid();
                String upw = cmsg.getUpw();
                
                if(uid.equals("Smith")&&upw.equals("12345")) {
                    out.add(oos); //๋ชจ๋“  ์ถœ๋ ฅ์šฉ ์ŠคํŠธ๋ฆผ์„ ํ•œ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
                    oos.writeObject(new ChatMsg("๋กœ๊ทธ์ธ ์„ฑ๊ณต"));
                    oos.flush();
                    return true;
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }
}
cs

 

ํด๋ผ์ด์–ธํŠธ๋ฅผ 2๊ฐœ ์‹คํ–‰ํ•˜๊ณ  ํ•œ๊ฐœ๋ฅผ ์ข…๋ฃŒ ์‹œํ‚จ๋’ค ์„œ๋ฒ„ํด๋ž˜์Šค ์ฝ˜์†” ๊ฐ’:

 

ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค ์ฝ˜์†” ๊ฐ’:

 

terminated ๋œ ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค ์ฝ˜์†” ๊ฐ’:

 


ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋‹ค๋ฅธ ํด๋ผ์ด์–ธํŠธํ•œํ…Œ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ

List๋ฅผ Map ์œผ๋กœ ๋ฐ”๊ฟ”์„œ key๋ฅผ ์•„์ด๋””๋กœ ์ฃผ์–ด ์ˆ˜์‹ ์ž๋ฅผ ์ •ํ•ด์ฃผ๋ฉด ๊ทธ ์ˆ˜์‹ ์ž์—๊ฒŒ ๋ฉ”์‹œ์ง€๊ฐ€ ๊ฐ€๊ฒŒ ํ•˜๊ธฐ

 

์„œ๋ฒ„ ํด๋ž˜์Šค ์ฝ”๋“œ

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
import java.io.*;
import java.net.*;
import java.util.HashMap;
 
public class Server 
{
    static HashMap<String, ObjectOutputStream> outmap = new HashMap<>();
    
    public static void main(String[] args) 
    {
        try { 
            
            ServerSocket ss = new ServerSocket(1234);
            while(true) {
                System.out.println("์„œ๋ฒ„์†Œ์ผ“ ๋Œ€๊ธฐ์ค‘");
                Socket s = ss.accept(); //๋ฌดํ•œ๋Œ€๊ธฐํ•˜๋‹ค๊ฐ€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘์†ํ•˜๋ฉด ์†Œ์ผ“์„ ๋งŒ๋“ค์–ด๋ƒ„
                System.out.println("ํด๋ผ์ด์–ธํŠธ ์ ‘์†๋จ");
                new CommThread(s).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("์„œ๋ฒ„ ์ข…๋ฃŒ");
    }
    //๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ํ†ต์‹ ์šฉ ์“ฐ๋ ˆ๋“œ
    static class CommThread extends Thread {
        private Socket s;
        private ObjectOutputStream oos;
        private ObjectInputStream oin;
        private boolean loginpass;
        private String uid;
        
        public String getUid() {
            return uid;
        }
        public void setUid(String uid) {
            this.uid = uid;
        }
        CommThread(Socket s) {
            this.s = s;
            try {
                OutputStream os = s.getOutputStream();
                this.oos = new ObjectOutputStream(os);
                
                
                oos.writeObject(new ChatMsg("์ ‘์† ์„ฑ๊ณต"));
                oos.flush();
                
                loginpass = login();
                
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            if(loginpass) {
                try {
                    ObjectOutputStream toClient = null;
                    while(true) {
                        ChatMsg cmsg = (ChatMsg)oin.readObject();//ํด๋ผ์ด์–ธํŠธ์—์„œ ์˜จ ๋ฉ”์‹œ์ง€ ๋ฐ›๊ธฐ
                        String receiver = cmsg.getReceiver();
                        outmap.get(receiver).writeObject(cmsg);
                        outmap.get(receiver).flush();
                    }
                }catch (Exception e) {
                    System.err.println("ํด๋ผ์ด์–ธํŠธ ๋‚˜๊ฐ");
                    outmap.remove(this.uid);
                    System.out.println("์ ‘์†์ž์ˆ˜" + outmap.size());    
                }
            }
            System.out.println("ํ†ต์‹  ์“ฐ๋ ˆ๋“œ ์ข…๋ฃŒ");
        }
        private boolean login() {
            try {
                InputStream is = s.getInputStream();
                this.oin = new ObjectInputStream(is);
                
                ChatMsg cmsg = (ChatMsg)oin.readObject();
                String uid = cmsg.getUid();
                String upw = cmsg.getUpw();
                
                if(!uid.equals("")&&uid!=null
                        &&!upw.equals("")&&upw!=null) {
                    this.uid=uid;
                    outmap.put(uid, oos); //map์— uid์™€ ๊ทธ์‚ฌ๋žŒ์˜ ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ์„ ์Œ์œผ๋กœ ์ €์žฅ
                    oos.writeObject(new ChatMsg("๋กœ๊ทธ์ธ ์„ฑ๊ณต"));
                    oos.flush();
                    return true;
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }
}
cs

ArrayList๋ฅผ HashMap๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค ์ฝ”๋“œ

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
import java.io.*;
import java.net.*;
import java.util.Scanner;
 
public class Client
{
    public static void main(String[] args)
    {
        try {
            Socket s = new Socket("127.0.0.1",1234);
            System.out.println("์„œ๋ฒ„์ ‘์† ์„ฑ๊ณต");
            InputStream is = s.getInputStream();
            ObjectInputStream oin = new ObjectInputStream(is);
            
            new ClientThread(s, oin).start(); //๋„คํŠธ์›Œํฌ ์ถœ๋ ฅ ์“ฐ๋ ˆ๋“œ
            
            while(true) {//๋„คํŠธ์›Œํฌ ์ž…๋ ฅ ์“ฐ๋ ˆ๋“œ
                ChatMsg m = (ChatMsg)oin.readObject();
                System.out.println("์„œ๋ฒ„์ธก์—์„œ ๋ณด๋‚ด์˜จ ๋ฉ”์„ธ์ง€:"+m.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("ํด๋ผ์ด์–ธํŠธ ์ข…๋ฃŒ");
    }
    //๋„คํŠธ์›Œํฌ ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•  ํด๋ผ์ด์–ธํŠธ ์ธก ์“ฐ๋ ˆ๋“œ
    static class ClientThread extends Thread {
        Socket s;
        ObjectOutputStream oos;
        ObjectInputStream oin;
        boolean loginpass;
        ClientThread(Socket s,ObjectInputStream oin){
            this.s=s;
            this.oin=oin;
            try {
                ChatMsg msg = (ChatMsg)oin.readObject();
                System.out.println("์„œ๋ฒ„์ธก์—์„œ ๋ณด๋‚ด์˜จ ๋ฉ”์„ธ์ง€:"+msg.getMessage());
                if(msg.getMessage().equals("์ ‘์† ์„ฑ๊ณต")) {
                    OutputStream os = s.getOutputStream();
                    this.oos = new ObjectOutputStream(os);
                    
                    loginpass = login();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            Scanner kbd = new Scanner(System.in);
            while(true) {
                System.out.println("์ž…๋ ฅํ•  ๋ฉ”์‹œ์ง€:");
                String line = kbd.nextLine().strip();
                System.out.println("์ˆ˜์‹ ์ž ๋ช…:");
                String rec = kbd.nextLine().strip();
                ChatMsg cmsg = new ChatMsg();
                cmsg.setReceiver(rec);
                cmsg.setMessage(line);
                try {
                    oos.writeObject(cmsg);
                    oos.flush();
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        private boolean login() {
            String loginresult = null;
            Scanner kbd = new Scanner(System.in);
            System.out.println("์•„์ด๋”” ์ž…๋ ฅ:");
            try {
                String uid = kbd.nextLine().strip();
                System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ:");
                String upw = kbd.nextLine().strip();
                ChatMsg msg = new ChatMsg(uid, upw);
                oos.writeObject(msg);
                oos.flush();
 
                loginresult = ((ChatMsg)oin.readObject()).getMessage();
                System.out.println("๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ:"+loginresult);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(loginresult.equals("๋กœ๊ทธ์ธ ์„ฑ๊ณต")) {
                return true;
            }else {
                return false;
            }
        }
    }            
}
cs

 

ํด๋ผ์ด์–ธํŠธ๋ฅผ ๋‘๊ฐœ ์‹คํ–‰ ์‹œ์ผœ ๊ฐ๊ฐ ์•„์ด๋””๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ฃผ์–ด ์‹คํ–‰ ํ–ˆ๋‹ค.

 

ํด๋ผ์ด์–ธํŠธ 1 ์ฝ˜์†”๊ฐ’:

 

ํด๋ผ์ด์–ธํŠธ2 ์ฝ˜์†” ๊ฐ’:

 

ํด๋ผ์ด์–ธํŠธ 1์—์„œ ํด๋ผ์ด์–ธํŠธ2๋ฅผ ์ˆ˜์‹ ์ž๋กœ ์ •ํ•ด์„œ ๋ณด๋‚ธ ๋‚ด์šฉ์ด ํด๋ผ์ด์–ธํŠธ1์—์„  ๋ณด์ด์ง€ ์•Š๊ณ  ํด๋ผ์ด์–ธํŠธ 2์—์„œ๋งŒ ๋ณด์ธ๋‹ค.


 

์›น ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๊ธฐ ์œ„ํ•ด์„  Java, ํ†ตํ•ฉ๊ฐœ๋ฐœํ™˜๊ฒฝ(Eclipse), Web browser(ed Chrome), WAS (Web Application Server, ex Tomcat) 4๊ฐ€์ง€๊ฐ€ ๊ฐ–์ถฐ์ ธ์•ผํ•œ๋‹ค.

ํ†ฐ์บฃ์„ ๋‹ค์šด๋ฐ›์•„ ์„ค์ •ํ•ด์ค€ ํฌํŠธ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๋งž๊ฒŒ ๋‚˜์˜จ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ†ฐ์บฃ ํŒŒ์ผ์— ์ƒˆ ํ…์ŠคํŠธ ๋ฌธ์„œ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ html๋ฌธ์„œ๋กœ ๋ฐ”๊พธ๊ธฐ

๋””๋ฒ„๊น… ํ•ด๋ณด๊ธฐ ์œ„ํ•ด์„œ ๋ฉ”๋ชจ์žฅ์— ์งง์€ html์–‘์‹์„ ๋งž์ถ˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

< / > ์ด๋ ‡๊ฒŒ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ๋“ค์€ ๋ฐ์ดํ„ฐ๋ผ ๋ถ€๋ฅด์ง€ ์•Š๊ณ  ํƒœ๊ทธ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ์ด๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์ด๋‹ค. 

 

 

์™„์„ฑ๋œ html์„ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ธ€์ž์ƒ‰์„ ๋ฐ”๊พธ๋Š” css ์ž‘์—…๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ‰์€ RGB๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚ด์–ด ์ฝ”๋”ฉํ•œ๋‹ค.

 

์Šคํฌ๋ฆฝํŠธ๋Š” ๋™์ ์ธ ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๋•Œ ์“ฐ๋Š”๋ฐ ๋ฌด์–ธ๊ฐ€ ํ–‰๋™์„ ํ•˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚œ๋‹ค.

๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด ํด๋ฆญํ•  ๋•Œ "์•ˆ๋…•ํ•˜์„ธ์š”~" ๋ฉ”์„ธ์ง€๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.


๊ธฐ์กด๊นŒ์ง€๋Š” ์ž๋ฐ” ์Šคํƒ ๋‹ค๋“œ ์—๋””์…˜์œผ๋กœ ์ฝ”๋”ฉํ–ˆ์ง€๋งŒ ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ์›น ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ• ๋ ค๋ฉด ์ž๋ฐ” ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์—๋””์…˜์œผ๋กœ ์ฝ”๋”ฉํ•ด์•ผํ•œ๋‹ค.(Java EE)

์ดํด๋ฆฝ์Šค Java EE ๋ชจ๋“œ

๋ฉ”๋ชจ์žฅ์—์„œ ๋งŒ๋“ค๋˜ 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
package com.ezen.web.hello;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
@WebServlet("/test")
public class TestServlet extends HttpServlet 
{
    
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.println("<? doctype html>");
        pw.println("<html>");
        pw.println("<meta charset='utf-8'>");
        pw.println("<head><title>์„œ๋ธ”๋ฆฟ์œผ๋กœ ์‘๋‹ต ๋งŒ๋“ค๊ธฐ</title>");
        pw.println("<body>");
        pw.println("<h1>์„œ๋ธ”๋ฆฟ์œผ๋กœ html ์‘๋‹ตํ•˜๊ธฐ</h1>");
        pw.println("</body>");
        pw.println("</head>");
        pw.println("</html>");
        pw.flush();
    }
}
cs

Servlet ํŒŒ์ผ ์„œ๋ฒ„์—์„œ ๋Œ์•„๊ฐ€๋Š” ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ

 

Run on Server ์‹คํ–‰ ๊ฒฐ๊ณผ

์†Œ์Šค ๋ณด๊ธฐ

๋Œ“๊ธ€