Problem solving/쉽게 배우는 JSP 웹 프로그래밍

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

공부좀하시졍 2021. 1. 3. 18:14

4. page 디렉티브 태그를 이용한 예외 처리 기법을 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

errorPage.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page errorPage="isErrorPage.jsp" %>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<%
		int x = 1;
		if(x == 1) {
			throw new RuntimeException("오류 발생!!!");
		}
	%>
</body>
</html>

isErrorPage.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<h3>오류 발생</h3>
	<table border="1">
		<tr>
			<th>Error:</th>
			<td><%=exception.getClass().getName() %>: <%=exception.getMessage() %></td>
		</tr>
		<tr>
			<th>URI:</th>
			<td><%=request.getRequestURI() %></td>
		</tr>
		<tr>
			<th>Status code:</th>
			<td><%=response.getStatus() %></td>
		</tr>
	</table>
</body>
</html>

5. web.xml 파일을 이용한 예외 처리 기법으로 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

exception_error.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Exception</title>
</head>
<body>
	오류 발생 : 요청 파라미터 값이 없습니다!
	<%@ include file="exception.jsp" %>
</body>
</html>

exception.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<form action="exception_process.jsp" method="post">
		<p> 아이디 : <input type="text" name="id">
		<p> 비밀번호 : <input type="text" name="password">
		<p> <input type="submit" value="전송">
	</form>
</body>
</html>

 

exception_process.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page errorPage="exception_error.jsp" %>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<%
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		
		if (id.equals("") || password.equals("")){
			throw new ServletException();
		}
		else {
			out.println("id : " + id + " password : " + password);
		}
	%>
</body>
</html>

web.xml

	<erroe-page>
		<exception-code>500</exception-code>
		<location>/ch11/exception_error.jsp</location>
	</erroe-page>

6. try-cat-finally 이용한 예외 처리 기법으로 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

tryCatch.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<%
		try {
			int a = 12;
			int b = 0;
			int c = a / b;
			out.println("a / b = " + c);
		} catch (ArithmeticException e) {
			out.println("오류 발생 : " + e.getMessage());
		}
		
	%>
</body>
</html>

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

exceptionNoBookId.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
<title>상품ID 오류</title>
</head>
<body>
	<jsp:include page="menu.jsp" />
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">해당 도서가 존재하지 않습니다.</h2>
		</div>
	</div>
	<div class="container">
		<p><%=request.getRequestURL() %>?<%=request.getQueryString() %>
		<p> <a href="products.jsp" class="btn btn-secondary"> 상품 목록 &raquo;</a>
	</div>
</body>
</html>

exceptionNoPage.jsp

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
<title>페이지 오류</title>
</head>
<body>
	<jsp:include page="menu.jsp" />
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">요청하신 페이지를 찾을 수 없습니다!</h2>
		</div>
	</div>
	<div class="container">
		<p><%=request.getRequestURL() %></p>
		<p> <a href="products.jsp" class="btn btn-secondary"> 도서 목록 &raquo;</a>
	</div>
</body>
</html>