IT/JSP

[쉽게 배우는 JSP 웹프로그래밍 개정2판] 6장 설명, 연습문제

곰탱이들 2024. 6. 4.

6장 연습문제 소스코드

6장 연습문제

 

1. 폼을 구성하는 태그의 종류로 옳지 않은 것은 무엇인가?

  • 4번, textarea: 한 줄을 입력할 수 있는 태그이다.

 

2. form 태그의 속성으로 옳지 않은 것은 무엇인가?

  • 3번, name: 폼을 식별하기 위한 이름으로 중복으로 설정할 수 있다.

 

3. input 태그의 type 속성 값으로 옳지 않은 것은 무엇인가?

  • 4번, checkbox: 체크박스로 열거된 것 중 하나만 선택할 때 사용한다.

 

4. input 태그의 type 속성 값 중에서 보이지 않게 숨겨서 값을 전송할 때 사용하는 값은 무엇인가?

  • 4번, hidden

 

5. 다음 중 단일 요청 파라미터의 값을 받는 메소드는 무엇인가?

  • 1번, request_getParameter()

 

6. 다음은 폼 페이지에서 회원 가입 양식에 연락처를 입력하는 프로그램이다. 이 프로그램의 밑줄에 들어갈 올바른 것은 무엇인가?

  • 2번, select

 

7. 다음은 폼 페이지에서 여러 줄의 텍스트를 입력할 수 있도록 작성한 것이다. 프로그램의 밑줄에 들어갈 올바른 것 은 무엇인가?

  • 1번, textarea

소스코드

8.

<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <h2>Form Example</h2>
    <form action="form01_process.jsp" method="post">
        Name: <input type="text" name="name"><br>
        Address: <input type="text" name="address"><br>
        Email: <input type="text" name="email"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Form Processing</title>
</head>
<body>
    <h2>Form Processing</h2>
    <%-- Receiving parameters using request.getParameter() --%>
    <% 
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String email = request.getParameter("email");

        StringBuffer sb = new StringBuffer();
        sb.append("이름: ").append(name).append("<br>");
        sb.append("주소: ").append(address).append("<br>");
        sb.append("이메일: ").append(email).append("<br>");

        out.println(sb.toString());
    %>
</body>
</html>

 

9.

<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <h2>Form Example</h2>
    <form action="form02_process.jsp" method="post">
        이름: <input type="text" name="name"><br>
        주소: <input type="text" name="address"><br>
        이메일: <input type="text" name="email"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Form Processing</title>
</head>
<body>
    <h2>Form Processing</h2>
    <% 
        import java.util.Enumeration;

        Enumeration<String> parameterNames = request.getParameterNames();
        StringBuffer sb = new StringBuffer();

        while (parameterNames.hasMoreElements()) {
            String paramName = parameterNames.nextElement();
            String paramValue = request.getParameter(paramName);
            sb.append(paramName).append(": ").append(paramValue).append("<br>");
        }

        out.println(sb.toString());
    %>
</body>
</html>

 

10.

<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <h2>Form Example</h2>
    <form action="form03_process.jsp" method="post">
        <input type="checkbox" name="fruit" value="orange"> 오렌지<br>
        <input type="checkbox" name="fruit" value="apple"> 사과<br>
        <input type="checkbox" name="fruit" value="banana"> 바나나<br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Form Processing</title>
</head>
<body>
    <h2>Form Processing</h2>
    <% 
        String[] fruits = request.getParameterValues("fruit");

        if (fruits != null && fruits.length > 0) {
            out.println("Selected fruits:<br>");
            for (String fruit : fruits) {
                out.println(fruit + "<br>");
            }
        } else {
            out.println("No fruits selected.");
        }
    %>
</body>
</html>

댓글