본문 바로가기
문제풀이/쉽게 배우는 JSP 웹프로그래밍

[쉽게 배우는 JSP 웹프로그래밍] 6장 연습문제

by 그적 2020. 10. 8.

1. form 태그에 사용하는 속성에 대해 간단히 설명하시오.

 

 form태그의 모든 속성은 필수가 아니라 선택적으로 사용한다. 가장 기본적인 두 속성은 처리할 웹페이지의 URL을 설정하는 action 속성과 HTTP 전송 방식을 설정하는 method 속성이다. method 속성의 기본 디폴트 값은 GET 방식이므로 생략이 가능하다. 그밖에 폼 이름은 name 속성, 응답을 실행할 프레임은 target 속성, 콘텐츠 MIME 유형은 enctype 속성, 문자 인코딩은 accept-charset 속성으로 설정할 수 있다.

 

 

2. form 태그 내에 중첩하여 사용하는 태그를 나열하고 설명하시오.

 

 input 태그, select 태그, textarea 태그가 있다.

input 태그는 사용자가 텍스트 입력이나 선택 등을 다양하게 할 수 있도록 공간을 만드는 태그로, 종료 없이 단독으로 사용 가능하다. select 태그는 여러 개의 항목을 나타낼 수 있으며, 시작과 종료 태그가 존재한다. select 태그 내에 option 태그를 사용하여 여러 항목들을 삽입한다. textarea 태그는 텍스트를 입력할 수 있는 태그이다.

 

 

3. 폼 페이지에서 전송된 데이터를 전달받는 내장 객체와 관련된 메서드는 무엇인가?

 

request.getParameter() 메서드이다. 

 

 

4. form 태그를 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

    ** 4번문제에서 StringBuffer 객체를 사용하라는 이유를 모르겠다. 일단 나는 아래 코드와 같이 작성했다.

// form01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Processing</title>
</head>
<body>

<form name="enter" action="form01_process.jsp">
	<p> 이름 : <input type="text" name="name" >
	<p> 주소 : <input type="text" name="address">
	<p> 이메일 : <input type="text" name="email">
	<p> <input type="submit" value="전송"> 
</form>

</body>
</html>

// form01_process.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Processing</title>
</head>
<body>

	<p> 아이디 : <% StringBuffer buf1 = new StringBuffer(request.getParameter("name")); out.print(buf1); %>
	<p> 주소 : <% StringBuffer buf2 = new StringBuffer(request.getParameter("address")); out.print(buf2); %>
	<p> 이메일 : <% StringBuffer buf3 = new StringBuffer(request.getParameter("email")); out.print(buf3); %>

</body>
</html>

 

 

5. form 태그를 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

 

// form02.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Processing</title>
</head>
<body>

<form name="enter2" action="form02_process.jsp">
	<p> 이름 : <input type="text" name="name">
	<p> 주소 : <input type="text" name="address">
	<p> 이메일 : <input type="text" name="email">
	<p><input type="submit" value="전송">

</form>

</body>
</html>

// form02_process.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*, java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Processing</title>
</head>
<body>

<%
Enumeration paraNames = request.getParameterNames();
while(paraNames.hasMoreElements()){
	StringBuffer text = new StringBuffer((String)paraNames.nextElement());
	out.println(text + " : ");
	
	String value = request.getParameter(text.toString());
	out.println(value+"<br>");
}
%>

</body>
</html>

 

 

6. form 태그를 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

// form03.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Implicit Objects</title>
</head>
<body>

<form name="choice" action="form03_process.jsp">
	<p>
	오렌지 <input type="checkbox" name="check" value="Orange" >
	사과 <input type="checkbox" name="check" value="Apple">
	바나나 <input type="checkbox" name="check" value="Banana">
	<input type="submit" value="전송">
	</p>
</form>

</body>
</html>

// form03_process.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*, java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<p> 선택한 과일 
<%
String[] e = request.getParameterValues("check");
for(int i=0; i<e.length; i++)
	out.print(e[i] + " ");
%>

</body>
</html>

 

 

7. 다음 조건에 맞게 도서 웹 쇼핑몰을 위한 웹 애플리케이션을 만들고 실행 결과를 확인하시오.

  (5장에서 작성한 소스들에서 수정이 된 코드들만 올림. >> 5장 연습문제 : jihyeong-ji99hy99.tistory.com/98)

 

// addProduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" 
	href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<title>상품 등록</title>

</head>
<body>

<%@ include file="menu.jsp" %>
<div class="jumbotron">
	<div class="contrainer">
		<h1 class="display-3">상품 등록</h1>
	</div>
</div>
<div>
	<form name="newProduct" action="processAddProduct.jsp" class="form-horizontal" method="post">
		<div class="form-group row">
			<label class="col-sm-2">도서코드</label>
			<div class="col-sm-3">
				<input type="text" name="productId" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">도서명</label>
			<div class="col-sm-3">
				<input type="text" name="name" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">가격</label>
			<div class="col-sm-3">
				<input type="text" name="unitPrice" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">저자</label>
			<div class="col-sm-3">
				<input type="text" name="author" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">출판사</label>
			<div class="col-sm-3">
				<input type="text" name="publisher" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">출판일</label>
			<div class="col-sm-3">
				<input type="text" name="releaseDate" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">총 페이지 수</label>
			<div class="col-sm-3">
				<input type="text" name="totalPages" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">상세 정보</label>
			<div class="col-sm-5">
				<textarea name="description" cols="50" rows="2" class="form-control"></textarea>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">분류</label>
			<div class="col-sm-3">
				<input type="text" name="category" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">재고 수</label>
			<div class="col-sm-3">
				<input type="text" name="unitsInstock" class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">상태</label>
			<div class="col-sm-5">
				<input type="radio" name="condition" value="New " >
				신규 제품
				<input type="radio" name="condition" value="Old ">
				중고 제품
				<input type="radio" name="condition" value="Refurbished">
				재생 제품
			</div>
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
					<input type="submit" class="btn btn-primary" value="등록">
				</div>
			</div>
		</div>
		
	</form>
</div>

</body>
</html>

// processAddProduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="dto.Product" %>
<%@page import="dao.ProductRepository" %>

<%
request.setCharacterEncoding("UTF-8");

String productId = request.getParameter("productId");
String name = request.getParameter("name");
String unitPrice = request.getParameter("unitPrice");
String author = request.getParameter("author");
String description = request.getParameter("description");
String publisher = request.getParameter("publisher");
String category = request.getParameter("category");
String unitsInStock = request.getParameter("unitsInStock");
//long stock = Long.parseLong(unitsInStock);
String totalPages = request.getParameter("totalPages");
//long total_Pages = Long.parseLong(totalPages);
String releaseDate = request.getParameter("releaseDate");
String condition = request.getParameter("condition");

Integer price;
if(unitPrice == null)
	price = 0;
else
	price = Integer.valueOf(unitPrice);

long stock;
if(unitsInStock == null)
	stock = 0;
else
	stock = Long.valueOf(unitsInStock);

long total_Pages;
if(totalPages == null)
	total_Pages = 0;
else
	total_Pages = Long.valueOf(totalPages);

ProductRepository dao = ProductRepository.getInstance();

Product newProduct = new Product();
newProduct.setProductId(productId);
newProduct.setPname(name);
newProduct.setUnitPrice(price);
newProduct.setAuthor(author);
newProduct.setDescription(description);
newProduct.setPublisher(publisher);
newProduct.setCategory(category);
newProduct.setUnitsInStock(stock);
newProduct.setTotalPages(total_Pages);
newProduct.setReleaseDate(releaseDate);
newProduct.setCondition(condition);

dao.addProduct(newProduct);

response.sendRedirect("products.jsp");

%>

// products.jsp - 변경된 부분

// product.jsp - 변경된 부분

// src 폴더의 dao 패키지 - 추가된 부분

 

댓글