30: Servlet input

jk·2024년 2월 14일
0

kdt 풀스택

목록 보기
57/127



1. 아래를 프로그래밍 하시오. 2) /gugudan - input 박스에 단을 입력받음 -해당 구구단 출력

<!--code1-->
<!--.\WebContent\gugudan.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gugudan</title>
</head>
<body>
	<form action = "Gugudan" method = "post">	
		단: <input type = "number" name = "" min = "1" max = "9">
		<input type = "submit" value = "제출">	
	</form>
</body>
</html>
//
//code2
//.\src\Gugudan.java
//
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;
//
class Const {
    static final int DAN_MAX = 9;
    static final int DAN_MIN = 1;
    static final String H1 = "<h1>";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    static void reset() {
        print.setLength(0);
    }
    static <T> void append(T t) {
        print.append(t);
    }
    static String getStringAndReset() {
        String str = print.toString();
        reset();
        return str;
    }
    static <T> void printAndReset(T t) {
        System.out.println(print);
        reset();
    }
}
@WebServlet("/Gugudan")
public class Gugudan extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Gugudan() {
        super();
    }
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Print.printAndReset("doPost() ..");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		String numberStr = request.getParameter("단");
		int number = Integer.parseInt(numberStr);
		writer.println("<html>");
		writer.println("<head>");
		writer.println("<body>");
        for (int i = Const.DAN_MIN; i <= Const.DAN_MAX; i++) {
            Print.append(Const.H1);
            Print.append(number);
            Print.append(" x ");
            Print.append(i);
            Print.append(" = ");
            Print.append(number * i);
            Print.append(Const.H1);
            writer.println(Print.getStringAndReset());
        };
		writer.println("</body>");
		writer.println("</head>");
		writer.println("</html>");
		doGet(request, response);
	}
}



3) /circle - input 박스에 반지름 입력 -해당 넓이 출력

<!--.\WebContent\circle.html-->
<!--code1-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>circle</title>
</head>
<body>
	<form action = "Circle" method = "post">	
		반지름: <input type = "text" name = "반지름" size = "10">
		<input type = "submit" value = "제출">	
	</form>
</body>
</html>
//
//.\src\Circle
//code2
//
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
class Const {
    static final double PI = Math.PI;
    static final String H1 = "<h1>";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    static void reset() {
        print.setLength(0);
    }
    static <T> void append(T t) {
        print.append(t);
    }
    static String getStringAndReset() {
        String str = print.toString();
        reset();
        return str;
    }
    static <T> void printAndReset(T t) {
        System.out.println(print);
        reset();
    }
}
@WebServlet("/Circle")
public class Circle extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Circle() {
        super();
    }
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Print.printAndReset("doPost() ..");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		String radiusStr = request.getParameter("반지름");
		double radius = Double.parseDouble(radiusStr);
        double area = Const.PI * Math.pow(radius, 2);
        Print.append("<html><head><body>");
        Print.append(Const.H1);
        Print.append("원의 넓이는 ");
        Print.append(area);
        Print.append(" 입니다.");
        Print.append(Const.H1);
        Print.append("</body></head></html>");
        writer.println(Print.getStringAndReset());
		doGet(request, response);
	}
}



4) /rectangle - input 박스두에 가로세로 입력- 넓이 출력 (※ 각각 reset 버튼도 달아 줄것)

<!--.\WebContent\rectangle.html-->
<!--code1-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>rectangle</title>
</head>
<body>
	<form action = "Rectangle" method = "post">	
		가로: <input type = "text" name = "가로" size = "10">
		<br>
		세로: <input type = "text" name = "세로" size = "10">
		<br>
		<input type = "submit" value = "제출">
		<input type = "reset" value = "리셋">
	</form>
</body>
</html>
//
//.\src\Circle
//code2
//
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
final class Const {
    static final String H1 = "<h1>";
}
final class Print {
    private static StringBuilder print = new StringBuilder();
    static void reset() {
        print.setLength(0);
    }
    static <T> void append(T t) {
        print.append(t);
    }
    static String getStringAndReset() {
        String str = print.toString();
        reset();
        return str;
    }
    static <T> void printAndReset(T t) {
        System.out.println(print);
        reset();
    }
}
@WebServlet("/Rectangle")
public final class Rectangle extends HttpServlet {
    public Rectangle() {
        super();
    }
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Print.printAndReset("doPost() ..");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		String widthStr = request.getParameter("가로");
		String heightStr = request.getParameter("세로");
		double width = Double.parseDouble(widthStr);
		double height = Double.parseDouble(heightStr);
        double area = width * height;
        Print.append("<html><head><body>");
        Print.append(Const.H1);
        Print.append("사각형의 넓이는 ");
        Print.append(area);
        Print.append(" 입니다.");
        Print.append(Const.H1);
        Print.append("</body></head></html>");
        writer.println(Print.getStringAndReset());
		doGet(request, response);
	}
}



2.get 방식과 post 방식의 차이는?

  • get: input data is on the link.
  • post: input data is hidden.



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

  • The path on the link of the file.



4. 아래의 객체에 대하여 설명하시오.

(HttpServletRequest request, HttpServletResponse response)
  • request: read
  • response: write



5. 아래의 submit 에 대하여 설명하시오.

<button type="submit" value="전송">
  • The button has String "전송" to show.
  • The button is connected to next link with elements of input.
profile
Brave but clumsy

0개의 댓글