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

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

by 그적 2020. 11. 16.

1. 예외처리란 무엇인가?

: 예외 처리는 프로그램이 처리되는 동안 특정한 문제가 발생했을 때 처리를 중단하고 다른 처리를 하는 것으로 오류 처리라고도 한다.

 

 

2. page 디렉티브 태그를 이용한 예외 처리 기법에 사용되는 속성에 대해 설명하시오. 

: <%page errorPage=" ... " %>와 같이 errorPage 속성으로 오류 페이지를 호출하는 방식과 <%page isErrorPage="true" %> 와 같은 isErrorPage 속성으로 오류 페이지를 제작할 수 있다.

 

 

3. web.xml 파일을 이용한 예외 처리 기법에 대해 설명하시오.

두가지 종류가 있으며, 오류 코드에 따라 처리하는 방법과 예외 유형으로 오류 페이지를 호출할 수 있는 방법이 존재한다.

 <error-page>
      <error-code> 오류코드 </error-code>
      <location> 오류 페이지의 URI </location>
</error-page>
<error-page>
     <exception-type>예외 유형</exception-type>
     <location>오류 페이지의 URI</location>
</error-page>

 

 

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

// errorPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page errorPage="isErrorPage.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Exception</title>
</head>
<body>

      <%
         int x = 1;
         if (x == 1) {
            throw new RuntimeException("");
         }
      %>

</body>
</html>

// isErrorPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Exception</title>
</head>
<body>
<% response.setStatus(200); %>

<h3>오류 발생</h3>
<table border="1">
	<tr>
		<td width="100px">Error :</td>
		<td> <%= request.getAttribute("javax.servlet.error.exception") %> : 오류발생!! </td>
		<!-- <td width="400px"> <%= exception.getClass().getName() %> : 오류발생!! </td> -->
		
	</tr>
	<tr>
		<td>URI :</td>
		<td> <%= request.getAttribute("javax.servlet.error.request_uri") %> </td>
		<!-- <td><%= request.getRequestURI() %> </td> --> 
		
	</tr>
	<tr>
		<td>Status Code :</td>
		<td> <%= request.getAttribute("javax.servlet.error.status_code") %></td>
	</tr>
</table>

</body>
</html>

 

 

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

// exception.jsp

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

<form action="exception_process.jsp">
	<p> 아이디 : <input type="text" name="id"/>
	<p> 비밀번호 : <input type="text" name="passwd"/>
	<p> <input type="submit" value="전송"/> 
</form>

</body>
</html>

// exception_process.jsp

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

<%
String id = request.getParameter("id");
String pw = request.getParameter("passwd");

if(id.equals("") || pw.equals("")){
    throw new ServletException("");
}else{
%>
	
	<p> 아이디 : <%=id %>
	<p> 비밀번호 : <%=pw %>
	
<%
}
%>

</body>
</html>

// exception_error.jsp

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

<p> 오류 발생 : 요청 파라미터 값이 없습니다.
<%@include file="exception.jsp" %>

</body>
</html>

// web.xml 파일에 아래 코드 추가

<error-page>
     <exception-type>javax.servlet.ServletException</exception-type>
     <location>/ch11/exception_error.jsp</location>
</error-page>

 

 

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

// tryCatch.jsp

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

<%
try{
	int a = 100/0;
}catch(Exception e){
%>
	<p> 오류 발생 : <%= e.getLocalizedMessage() %>
<%	
}
%>

</body>
</html>

 

 

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

(이전 10장 연습문제에서 수정된 부분만 업로드 하겠습니다.)

// exceptionNoBookId.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" 
	href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="utf-8">
<title>Book Market</title>
</head>

<body>
<%@ include file="menu.jsp" %>

<%!
String main = "해당 도서가 존재하지 않습니다.";
%>
<div class="jumbotron">
	<div class="container">
		<h1 class = "display-3"><%=main %></h1>
	</div>
</div>

<main role="main">
<div class="contaimer">
	<div class="text-center">
		<p><%=request.getRequestURL() %>?<%=request.getQueryString() %>
		<p> <a href="products.jsp" class="btn btn-secondary"> 상품 목록 &raquo;</a>
		<%--<h3><%@ include file="date.jsp" %></h3>--%>
	</div>
</div>
<%@ include file="footer.jsp" %>
</main>

</body>
</html>

// exceptionNoPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" 
	href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="utf-8">
<title>Book Market</title>
</head>

<body>
<%@ include file="menu.jsp" %>

<%!
String main = "요청하신 페이지가 존재하지 않습니다.";
%>
<div class="jumbotron">
	<div class="container">
		<h1 class = "display-3"><%=main %></h1>
	</div>
</div>

<main role="main">
<div class="contaimer">
	<div class="text-center">
		<p><%=request.getRequestURL() %>?<%=request.getQueryString() %>
		<p> <a href="products.jsp" class="btn btn-secondary"> 상품 목록 &raquo;</a>
		<%--<h3><%@ include file="date.jsp" %></h3>--%>
	</div>
</div>
<%@ include file="footer.jsp" %>
</main>

</body>
</html>

// products.jsp 파일에 아래 코드 추가

<%@ page errorPage="exceptionNoPage.jsp" %>

// product.jsp 파일에 아래 코드 추가

<%@ page errorPage="exceptionNoBookId.jsp" %>

// web.xml 파일에 아래 코드 추가

<error-page>
     <error-code>404</error-code>
     <location>/exceptionNoPage.jsp</location>
</error-page>

댓글