Web programming

(22.11.03)Web ํ”„๋กœ๊ทธ๋ž˜๋ฐ: MVC (Model View Control) ๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐServlet, JSP

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

 

GuguServlet์„ ์ƒ์„ฑํ•˜๊ณ  ๊ตฌ๊ตฌ๋‹จ 5๋‹จ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•ด ๋ณด๋ผ

 

์„œ๋ธ”๋ฆฟ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ์ž๋ฐ”๋กœ ์›น์„ ๊ตฌ์ถ•ํ•ด๋ณด๊ธฐ

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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>5๋‹จ ์•”๊ธฐ</h3>";
        int dan=5;
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

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

 

์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ ํŒŒ์ผ ์ ‘๊ทผํ•ด๋ณด๊ธฐ

Server.xml์—์„œ ์„ค์ •ํ•ด๋‘” ํฌํŠธ ๋ฒˆํ˜ธ์™€ ํ”„๋กœ์ ํŠธ๋ช… ("JavaWeb")๊ณผ @WebServlet("/gugu")๋ฅผ  ์ฃผ์†Œ๋กœ ์ž…๋ ฅํ•˜์—ฌ ์›นํŽ˜์ด์ง€๋ฅผ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.


์ฃผ์†Œ๋ฅผ ์š”์ฒญํ•˜๋ฉด ์š”์ฒญ ๋ฐ›์€๋Œ€๋กœ ์‘๋‹ตํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๋Š” ๋™์  ์ปจํ…์ธ  ๋งŒ๋“ค๊ธฐ

์ฃผ์†Œ์ฐฝ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„ ?๋ฅผ ์จ์•ผํ•œ๋‹ค.(get๋ฐฉ์‹ ์š”์ฒญ) ๊ทธ๋ฆฌ๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„๊ณผ =, ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐธ๋ฅ˜๋ฅผ ์ค˜์•ผํ•œ๋‹ค.(http ์•ฝ์†)

 

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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sDan = request.getParameter("dan");
        int iDan = Integer.parseInt(sDan); //Unboxing
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๊ตฌ๊ตฌ๋‹จ ์•”๊ธฐ</h3>";
        int dan= iDan;
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

์š”์ฒญ ๋ฐฉ์‹:

 

์›นํŽ˜์ด์ง€:

ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ์ง€ ์•Š๊ณ  ์ฃผ์†Œ์ฐฝ์— ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋‚ด๋ถ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด 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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sDan = request.getParameter("dan");
        int dan = 0;
        dan = sDan==null2 : Integer.parseInt(sDan);
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๊ตฌ๊ตฌ๋‹จ ์•”๊ธฐ</h3>";
        
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

์ˆ˜์ • ํ›„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ฃผ์ง€ ์•Š๊ณ  url๋ฅผ ์ž…๋ ฅ ํ–ˆ์„ ์‹œ ๊ฒฐ๊ณผ


get ๋ฐฉ์‹ ์š”์ฒญ์„ ์‚ฌ์šฉํ•˜์—ฌ id,pw๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์„œ๋ธ”๋ฆฟ์—์„œ ์ธ์ฆํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋กœ๊ทธ์ธ ์„ฑ๊ณต/์‹คํŒจ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜์‹œ์˜ค

 

์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ

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
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("/login")
public class LoginServlet extends HttpServlet {
    
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sId = request.getParameter("id");
        String sPw = request.getParameter("pw");
        String result = null;
        if(sId.equals("newjeans")&&(sPw.equals("1234"))) {
            result = "๋กœ๊ทธ์ธ ์„ฑ๊ณต";
        }else {
            result= "๋กœ๊ทธ์ธ ์‹คํŒจ";
        }
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๋กœ๊ทธ์ธ ํ•˜๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ</h3>";
        html += "<h4>"+result+"</h4>";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

url๋กœ ์š”์ฒญํ•˜๊ธฐ

 

๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ

๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ‹€๋ ธ์„ ์‹œ ๊ฒฐ๊ณผ

์•„์ด๋””๋ฅผ ํ‹€๋ ธ์„ ์‹œ ๊ฒฐ๊ณผ

 


id, pw๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๋ฉด ์—๋Ÿฌ ํ‘œ์‹œ ๋‚˜๋Š”๊ฒŒ ์•„๋‹Œ "id, pw๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค."๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ

 

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
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("/login")
public class LoginServlet extends HttpServlet {
    
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sId = request.getParameter("id");
        String sPw = request.getParameter("pw");
        String result = null;
        if(sId==null||sPw==null){
            result= "id,pw๋Š” ํ•„์ˆ˜ ์ž…๋ ฅํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค.";
        }else if(sId.equals("newjeans")&&(sPw.equals("1234"))) {
            result = "๋กœ๊ทธ์ธ ์„ฑ๊ณต";
        }else{
            result= "๋กœ๊ทธ์ธ ์‹คํŒจ";
        }
        
        String html = "";
        html += "<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๋กœ๊ทธ์ธ ํ•˜๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ</h3>";
        html += "<h4>"+result+"</h4>";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

์‹คํ–‰ ํ›„ :

sId==null||sPw==null์ด else if๋ฌธ์— ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ฒซ if๋ฌธ์— ์™€์•ผํ•œ๋‹ค.


๊ตฌ๊ตฌ๋‹จ ์•„๋ž˜์ชฝ์— ๋งํฌ๋ฅผ ์ œ์‹œํ•˜์—ฌ ์ด์šฉ์ž๊ฐ€ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ์ˆ˜์˜ ๊ตฌ๊ตฌ๋‹จ์ด ํ‘œ์‹œ๋˜๋„๋ก ํ•œ๋‹ค.

 

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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sDan = request.getParameter("dan");
        int dan = 0;
        dan = sDan==null2 : Integer.parseInt(sDan);
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๊ตฌ๊ตฌ๋‹จ ์•”๊ธฐ</h3>";
        
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        html += "<p>";
        html += "[";
        html += "<a href=gugu?dan=2> 2 </a>";
        html += "<a href=gugu?dan=3> 3 </a>";
        html += "<a href=gugu?dan=4> 4 </a>";
        html += "<a href=gugu?dan=5> 5 </a>";
        html += "<a href=gugu?dan=6> 6 </a>";
        html += "<a href=gugu?dan=7> 7 </a>";
        html += "<a href=gugu?dan=8> 8 </a>";
        html += "<a href=gugu?dan=9> 9 </a>";
        html += "]";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

๋งํฌ ํƒœ๊ทธ๋Š” a์ด๋‹ค

html += "<p>";

p๋Š” paragraph์˜ ์•ฝ์ž๋กœ ๋‹ค์Œ ๋ฌธ๋‹จ์œผ๋กœ ๋„์›Œ์ค€๋‹ค.

 

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

๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ž๋™์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ’์ด ๋„ฃ์–ด์ ธ์„œ ๋‚˜์˜จ๋‹ค.


๊ฒŸ๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ ํฌ์ŠคํŠธ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ์†Œ์ฐฝ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚จ์ง€ ์•Š์•„ ์ •๋ณด๋ฅผ ๋ณด์•ˆํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ผ์ˆ˜ ์žˆ๋‹ค.(์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์ด ๋‚จ์ง€ ์•Š๋Š”๋‹ค.)

 

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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sDan = request.getParameter("dan");
        int dan = 0;
        dan = sDan==null2 : Integer.parseInt(sDan);
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๊ตฌ๊ตฌ๋‹จ ์•”๊ธฐ</h3>";
        
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        //์•ก์…˜์€ ์š”์ฒญ ๋ฐ› ๊ฑธ ์‹คํ–‰ํ•  ์‹ค๋ฌด์ž๋ž€ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.
        html += "<p>";
        html += "<form action='gugu' method='post'>";
        html += " ๋‹จ์ˆ˜ <input type= 'text' name='dan'>";
        html += "<button type= 'submit'>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</button>";
        html += "</form>";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

<form> ์‚ฌ์šฉ

 

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

5๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ™•์ธํ•˜๋‹ˆ 5๋‹จ์ด ๋‚˜์˜ค๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ์ดํ„ฐ๋Š” ์ฃผ์†Œ์ฐฝ์— ํ‘œ์‹œ ๋˜์ง€ ์•Š๋Š”๋‹ค.


 

์ง์ ‘์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ด ์•„๋‹Œ ์˜ต์…˜์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹๋„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์…€๋ ‰ํŠธ ํƒœ๊ทธ๋ฅผ ์“ฐ๋ฉด ๋œ๋‹ค.

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
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("/gugu")
public class GuguServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sDan = request.getParameter("dan");
        int dan = 0;
        dan = sDan==null2 : Integer.parseInt(sDan);
        
        String html = "";
        html +="<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๊ตฌ๊ตฌ๋‹จ ํ•™์Šต๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๊ตฌ๊ตฌ๋‹จ ์•”๊ธฐ</h3>";
        
        for(int i =1;i<10;i++) {
            html += String.format("%d * %d = %d<br>", dan, i ,dan*i);
        }
        html += "<p>";
        html += "<form action='gugu' method='post'>";
        html += "<select name='dan'>";
        html += "<option>2</option>";
        html += "<option>3</option>";
        html += "<option>4</option>";
        html += "<option>5</option>";
        html += "<option>6</option>";
        html += "<option>7</option>";
        html += "<option>8</option>";
        html += "<option>9</option>";
        html += "</select>";
        html += "<button type= 'submit'>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</button>";
        html += "</form>";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

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

๋‹จ์ˆ˜๋ฅผ ๊ณจ๋ผ์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


id์™€ pw๋ฅผ <form> ํ˜•์‹์œผ๋กœ ๋ฐ›์•„์„œ ๋กœ๊ทธ์ธ์ด ์„ฑ๊ณต์ธ์ง€ ์‹คํŒจ์ธ์ง€ ํ‘œ์‹œํ•˜์‹œ์˜ค

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
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("/login")
public class LoginServlet extends HttpServlet {
    
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        
        request.setCharacterEncoding("utf-8");
        String sId = request.getParameter("id");
        String sPw = request.getParameter("pw");
        String result = null;
        if(sId==null||sPw==null){
            result= "id,pw๋Š” ํ•„์ˆ˜ ์ž…๋ ฅํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค.";
        }else if(sId.equals("newjeans")&&(sPw.equals("1234"))) {
            result = "๋กœ๊ทธ์ธ ์„ฑ๊ณต";
        }else{
            result= "๋กœ๊ทธ์ธ ์‹คํŒจ";
        }
        String html = "";
        html += "<! doctype html>";
        html += "<html>";
        html += "<head>";
        html += "<title>๋กœ๊ทธ์ธ ํ•˜๊ธฐ</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ</h3>";
        html += "<form action='login' method='post'>";
        html += " id <input type= 'text' name='id' value='์•„์ด๋””์ž…๋ ฅ'>";
        html += " pw <input type= 'password' name='pw' value='์•”ํ˜ธ์ž…๋ ฅ'>";
        html += "<button type= 'submit'>๋กœ๊ทธ์ธ ํ•˜๊ธฐ</button>";
        html += "<h4>"+result+"</h4>";
        html += "</form>";
        html += "</body>";
        html += "</html>";
        
        out.println(html);
    }
}
cs

 

 

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

๋กœ๊ทธ์ธํ•˜๊ธฐ


์š”์ฒญ์€ ์„œ๋ธ”๋ฆฟ์—์„œ ํ•˜๊ณ  ๋ทฐ๋Š” jsp์—์„œ ํ•˜๊ธฐ

์ฝ”๋“œ ๋‚˜๋ˆ„๊ธฐ(MVC :Model(Data, ex: java) View(ex: JSP) Controller(data์™€ ๋ทฐ๋ฅผ ์—ฐ๋™์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ ex:servlet)

๋กœ์ง ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

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
import java.io.IOException;
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("/guguc")
public class GuguController extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        request.setCharacterEncoding("utf-8");
        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);
        }
        //data๋ฅผ request๊ฐ์ฒด์— ๋„ฃ์–ด์„œ ๋ณด๋ƒ„
        request.setAttribute("data", data);
        getServletContext().getRequestDispatcher("/gugu.jsp").forward(request, response);
    }
}
cs

 

 

JSPํŒŒ์ผ( ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋กœ์ง)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% //Scriptlet 
    String gugu = (String)request.getAttribute("data");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</title>
</head>
<body>
<h3>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</h3>
<%
    out.println(gugu);
%>
</body>
</html>
cs

<% %>//์Šคํฌ๋ฆฝํŠธ๋ฆฟ

 

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

์„œ๋ธ”๋ฆฟ์˜ ์ฃผ์†Œ๋กœ jspํŒŒ์ผ์ด ์—ฐ๋™๋˜์–ด ํ™”๋ฉด์— ๋ณด์—ฌ์ง„๋‹ค.


gugu.jsp์—์„œ 2~9๊นŒ์ง€ ๋งํฌ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ๊ตฌ๊ตฌ๋‹จ์ด ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ค๊ธฐ

 

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% //Scriptlet 
    String gugu = (String)request.getAttribute("data");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</title>
</head>
<body>
<h3>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</h3>
<%
    out.println(gugu);
%>
<p>
<hr>
<div>
<a href=guguc?dan=2> 2 </a>
<a href=guguc?dan=3> 3 </a>
<a href=guguc?dan=4> 4 </a>
<a href=guguc?dan=5> 5 </a>
<a href=guguc?dan=6> 6 </a>
<a href=guguc?dan=7> 7 </a>
<a href=guguc?dan=8> 8 </a>
<a href=guguc?dan=9> 9 </a>
</div>
</body>
</html>
cs

 

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


3๊ฐ€์ง€ ์ปดํฌ๋„ŒํŠธ(component)๋กœ ๋กœ์ง ๋‚˜๋ˆ„๊ธฐ

๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ชจ๋ธ์ด๋ผ ํ•˜๊ณ  ๊ทธ ์™ธ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ๊ฐ–๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์„œ๋น„์Šค ํด๋ž˜์Šค ๋ผ๊ณ  ํ•œ๋‹ค.

 

GuguService ํด๋ž˜์Šค ํŒŒ์ผ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import javax.servlet.http.HttpServletRequest;
 
public class GuguService 
{
    public 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
import java.io.IOException;
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("/guguc")
public class GuguController extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        request.setCharacterEncoding("utf-8");
        
        /*GuguService ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€์„œ request๋ฅผ ์ง‘์–ด ๋„ฃ์–ด 
        createGugu()๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ data์— ์ €์žฅ*/
        GuguService g= new GuguService();
        String data = g.createGugu(request);
        
        //data๋ฅผ request๊ฐ์ฒด์— ๋„ฃ์–ด์„œ ๋ณด๋ƒ„
        request.setAttribute("data", data);
        getServletContext().getRequestDispatcher("/gugu.jsp").forward(request, response);
    }
}
 
cs

 

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% //Scriptlet 
    String gugu = (String)request.getAttribute("data");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</title>
</head>
<body>
<h3>๊ตฌ๊ตฌ๋‹จ ๋ณด๊ธฐ</h3>
<%
    out.println(gugu);
%>
<p>
<hr>
<div>
<a href=guguc?dan=2> 2 </a>
<a href=guguc?dan=3> 3 </a>
<a href=guguc?dan=4> 4 </a>
<a href=guguc?dan=5> 5 </a>
<a href=guguc?dan=6> 6 </a>
<a href=guguc?dan=7> 7 </a>
<a href=guguc?dan=8> 8 </a>
<a href=guguc?dan=9> 9 </a>
</div>
</body>
</html>
cs

 

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

 


๋กœ๊ทธ์ธ ํ”„๋กœ๊ทธ๋žจ์„ 4๊ฐ€์ง€๋กœ ๋กœ์ง์„ ๋‚˜๋ˆ ์„œ ๋งŒ๋“ค๊ธฐ

( LoginController, LoginService, loginForm.jsp, loginResult.jsp )

 

 

LoginService ์ž๋ฐ” ํŒŒ์ผ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import javax.servlet.http.HttpServletRequest;
 
public class LoginService 
{
     private HttpServletRequest request;
 
     public LoginService() {}
       
     public LoginService(HttpServletRequest request) {
          this.request = request;
     }
       
     public boolean login()
     {
        String uid = request.getParameter("uid");
        String pwd = request.getParameter("pwd");
        if(uid.equals("smith"&& pwd.equals("1234") )
           return true;
        else return false;
     }
}
 
cs

 

LoginController ์ž๋ฐ”ํŒŒ์ผ

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
import java.io.IOException;
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("/loginc")
public class LoginController extends HttpServlet {
    
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        response.setContentType("text/html; charset=utf-8");
        request.setCharacterEncoding("utf-8");
          
        String cmd = request.getParameter("cmd");
          
        if(cmd==null || cmd.equals("form")) 
        {
            getServletContext().getRequestDispatcher("/loginform.jsp").forward(request, response);
        }else if(cmd.equals("login")) 
        {
            LoginService svc = new LoginService(request);
            boolean success = svc.login();
            String msg = success ? "๋กœ๊ทธ์ธ ์„ฑ๊ณต" : "๋กœ๊ทธ์ธ ์‹คํŒจ";
            request.setAttribute("msg", msg);
            getServletContext().getRequestDispatcher("/loginresult.jsp").forward(request, response);
        }
    }
}
cs

 

 

loginForm.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”</title>
<style>
   label{display:inline-block;width:3em; text-align: right; 
      margin-right:1em; }
   input { width: 5em; }
   form {width:fit-content; padding:0.5em; margin:0 auto; 
      border:1px solid black;}
   button { margin: 5px; }
   div:last-child { text-align: center;}
   
</style>
</head>
<body>
 
<form action="loginc" method="post">
   <input type="hidden" name="cmd" value="login">
   <div><label>์•„์ด๋””</label> 
      <input type="text" name="uid" value="์•„์ด๋”” ์ž…๋ ฅ">
   </div>
   <div><label>์•” ํ˜ธ</label> 
      <input type="password" name="pwd" value="๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ">
   </div>
   <div><button type="submit">๋กœ๊ทธ์ธ</button></div>
</form>
</body>
</html>
cs

 

loginResult.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<% 
   String msg = (String)request.getAttribute("msg"); 
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ</title>
</head>
<body>
<h3>
<%
   out.println(msg);
%>
</h3>
</body>
</html>
cs

 

 

 

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

๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ(์„ฑ๊ณต์‹œ):

๋กœ๊ทธ์ธ๊ฒฐ๊ณผ(์‹คํŒจ์‹œ):

๋Œ“๊ธ€