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==null? 2 : 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==null? 2 : 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==null? 2 : 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==null? 2 : 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==null? 2 : 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==null? 2 : 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 |
์คํ๊ฒฐ๊ณผ:
๋ก๊ทธ์ธ ๊ฒฐ๊ณผ(์ฑ๊ณต์):
๋ก๊ทธ์ธ๊ฒฐ๊ณผ(์คํจ์):
๋๊ธ