티스토리 뷰

1. 아래를 프로그래밍 하시오.
-JSP 프로젝트 생성

-전체 reset 버튼도 달아 줄것
-/gugudan - input 박스에 단을 입력받음 -해당 구구단 출력 (get방식)

 

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
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
package Nov18;
 
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;
 
/**
 * Servlet implementation class GuGuDan2
 */
@WebServlet("/gugudan2")
public class GuGuDan2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GuGuDan2() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        String dan = request.getParameter("dan");
        System.out.println("구구단: " + dan + "단");
 
        PrintWriter writer = response.getWriter();
        response.setContentType("text/html; charset=euc-kr");
 
        writer.println("<html><head></head><body>");
 
        int d = Integer.valueOf(dan);
 
        for (int i = 1; i < 10; i++) {
            writer.println(d + "x" + i + "= " + d * i + "</br>");
        }
 
        writer.println("</body></html>");
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}
 
cs

 

 

webapp( .html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>GuGuDan ! </h1>
    <form action="gugudan2" method="get">
        몇 단: <input type="text" name="dan" size="10"></br>
        <input type="submit" value="전송"></br>
        <input type="reset" value="reset">
    </form>
 
</body>
</html>
cs

 

 



-/rectangle - input 박스에 가로, 세로 입력 -해당 넓이 출력 (get방식)

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
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
package edu.kosmo.ex;
 
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;
 
/**
 * Servlet implementation class RecServlet
 */
@WebServlet("/rectangle")
public class RecServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        try {
            String width = request.getParameter("width");
            String height = request.getParameter("height");
 
            double area = Double.valueOf(width) * Double.valueOf(height);
 
            System.out.println("가로 세로 get방식 호출");
 
            response.setContentType("text/html; charset=euc-kr");
            PrintWriter writer = response.getWriter();
            writer.println("<html><head></head><body>");
            writer.println("<h1> 넓이는:" + area + "</h1>");
            writer.println("</body></html>");
 
        } catch (Exception e) {
 
            response.setContentType("text/html; charset=euc-kr");
            PrintWriter writer = response.getWriter();
            writer.println("<html><head></head><body>");
            writer.println("<h1> 에러입니다 </h1>");
            writer.println("</body></html>");
            System.out.println("에러");
        }
 
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}
 
cs

 

 

 

webapp( .html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
    <h1>Rectangle</h1>
    <form action="rectangle" method="get">
        가로: <input type="text" name="width" size="10"></br>
        세로: <input type="text" name="height" size="10"></br>
        <input type="submit" value="전송">
        <input type="reset" value="reset">
    </form>
 
</body>
</html>
cs

 

 

 

 



-/triangle - input 박스 두 개에 가로 세로 입력 - 해당 넓이 출력 (post방식)

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
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
package Nov18;
 
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;
 
/**
 * Servlet implementation class Triangle
 */
@WebServlet("/tri")
public class Triangle extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Triangle() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        response.setContentType("text/html; charset=euc-kr");
 
        writer.println("<h1> Wrong place :p <h1>");
 
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String width = request.getParameter("width");
        String height = request.getParameter("height");
 
        PrintWriter writer = response.getWriter();
        response.setContentType("text/html; charset=euc-kr");
 
        writer.println("<html><head></head><body>");
 
        int w = Integer.valueOf(width);
        int h = Integer.valueOf(height);
        double area = w * h *0.5 ;
 
        writer.println("Area of the Triangle: " + area);
 
        writer.println("</body></html>");
 
        System.out.println("가로: " + width);
        System.out.println("세로: " + height);
 
    }
 
}
 
cs

 

 

 

webapp( .html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Triangle ! ! ! </h1>
    <form action="tri" method="post">
        가로: <input type="text" name="width" size="10"></br>
        세로: <input type="text" name="height" size="10"></br>
        
        <input type="submit" value="전송"></br>
        <input type="reset" value="reset">
    </form>
 
</body>
</html>
cs

 

전송 클릭

 

주소(http://localhost:8282/JSP/tri)로 바로 갔을 경우

 

(.html)상 method를 get으로 설정 했을 경우

1
2
    <form action="tri" method="get">
 
cs

 

 


-/circle- input 박스에 반지름 입력 -해당 넓이 출력 (post방식)

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
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
package Nov18;
 
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;
 
/**
 * Servlet implementation class Circle
 */
@WebServlet("/cir")
public class Circle extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Circle() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html; charset=euc-kr");
        PrintWriter writer = response.getWriter();
        
        writer.println("<h1>Not here :D</h1>");
 
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String radius = request.getParameter("radius");
 
        response.setContentType("text/html; charset=euc-kr");
        PrintWriter writer = response.getWriter();
 
        writer.println("<html><head></head><body>");
 
        int r = Integer.valueOf(radius);
        double area = r * r * Math.PI;
        writer.println("Area of Circle: " + area);
 
        writer.println("</body></head>");
 
        System.out.println("반지름: " + radius);
 
    }
 
}
 
cs

 

 

 

webapp( .html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Circle ~ ~ ~ ! </h1>
    <form action="cir" method="post">
        반지름: <input type="text" name="radius" size="10"></br>
        
        <input type="submit" value="전송"></br>
        <input type="reset" value="reset">
    </form>
 
</body>
</html>
cs

 

 

 

 

 

주소(http://localhost:8282/JSP/cir)로 바로 갔을 경우

 

(.html)상 method를 get으로 설정 했을 경우

1
 <form action="cir" method="get">
cs

 

 



-/gugu - 구구단을 좀더 예쁘게 출력(테이블 좀 입혀 주세요.^^;)  (get방식)
Servlet( . java)

webapp( .html)



 

 


2. get 방식 과 post 방식에 대하여 설명하시오.

 

 

 


3. 컨텍스트 패스(Context Path)란 무엇인가?

 

ㅏㅗㅓㅏㅗㅓㅏ

 


4. 아래의 객체에 대하여 설명하시오.
HttpServletRequest request, HttpServletResponse response)

 



5. 아래의 submit 에 대하여 설명하시오.
<button type="submit" value="전송">

'면접준비 > KOSMO 허쌤 숙제' 카테고리의 다른 글

스프링 - Command 객체, 성적 산출, 석탑만들기 (21/12/16)  (0) 2021.12.16
학습정리-11-17  (0) 2021.11.18
학습정리-11-10  (0) 2021.11.10
학습정리-11-09  (0) 2021.11.09
학습정리-11-08  (0) 2021.11.08
댓글