JAVA/JSP

[JSP] 구현7 - 예외 처리

아잠만_ 2024. 7. 12. 17:33

구현

구현2 - 상세보기 구현

구현3 - 상품 추가

구현4 - 이미지 추가

구현5 - 유효성검사 / 다국어

구현6 - 시큐리티

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 
  	선언적 시큐리티 처리 : web.xml 파일에 보안 구성을 작성
  	(역할-role, 사용자-username, URL 경로)을 작성
  	 - 역할/사용자 : tomcat-users.xml에 등록된 역할/사용자
  web.xml : 웹 애플리케이션 배포 설명자 파일(톰캣 설정)
   -->
   <!-- 시큐리티 역할(role) 설정 시작 -->
   <security-role>
   	<role-name>tomcat</role-name>
   </security-role>
   <security-role>
   	<role-name>role1</role-name>
   </security-role>
   <security-role>
   	<role-name>admin</role-name>
   </security-role>
   <!-- 시큐리티 역할(role) 설정 끝 -->
   
   <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<!-- 1번째 security01-->
   <!-- 시큐리티 제약 사항(constraint)을 설정 시작 -->
   <security-constraint>
   	<web-resource-collection>
   		<!--  웹 자원의 이름 설정(생략 가능) -->
   		<web-resource-name>JSPBOOK</web-resource-name>
   		<!-- 접근 제한 요청 URL -->
   		<url-pattern>/ch10/security01.jsp</url-pattern>
   		<!-- HTTP 메서드(get, post) -->
   		<http-method>GET</http-method>
   	</web-resource-collection>
   	
   	<!-- 권한이 부여된 사용자만이 웹 자원에 접근할 수 있도록 설정 -->
   	<auth-constraint>
   		<description>설명</description>
   		<role-name>role1</role-name>
   	</auth-constraint>
   </security-constraint>
   <!-- 시큐리티 제약 사항(constraint)을 설정 끝 -->
   
<!-- 2번째 security02-->
   <!-- 시큐리티 제약 사항(constraint)을 설정 시작 -->
   <security-constraint>
   	<web-resource-collection>
   	
   		<!--  웹 자원의 이름 설정(생략 가능) -->
   		<web-resource-name>JSPBOOK</web-resource-name>
   		
   		<!-- 접근 제한 요청 URL -->
   		<url-pattern>/ch10/security02.jsp</url-pattern>
   		
   		<!-- HTTP 메서드(get, post) -->
   		<http-method>GET</http-method>
   
   	</web-resource-collection>
   	
   	<!-- 권한이 부여된 사용자만이 웹 자원에 접근할 수 있도록 설정 -->
   	<auth-constraint>
   		<description>설명</description>
   		<role-name>tomcat</role-name>
   	</auth-constraint>
   </security-constraint>
   <!-- 시큐리티 제약 사항(constraint)을 설정 끝 -->
   
<!-- 3번째 addProducts-->
   <!-- 시큐리티 제약 사항(constraint)을 설정 시작 -->
   <security-constraint>
   	<web-resource-collection>
   	
   		<!--  웹 자원의 이름 설정(생략 가능) -->
   		<web-resource-name>JSPBOOK</web-resource-name>
   		
   		<!-- 접근 제한 요청 URL -->
   		<url-pattern>/addProduct.jsp</url-pattern>
   		
   		<!-- HTTP 메서드(get, post) -->
   		<http-method>GET</http-method>
   
   	</web-resource-collection>
   	
   	<!-- 권한이 부여된 사용자만이 웹 자원에 접근할 수 있도록 설정 -->
   	<auth-constraint>
   		<description>관리자만 가능</description>
   		<role-name>admin</role-name>
   	</auth-constraint>
   </security-constraint>
   <!-- 시큐리티 제약 사항(constraint)을 설정 끝 -->
   <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
   
   <!-- 인증(authentication) 처리를 위한 로그인 및 로그인 실패 페이지 설정 시작 -->
   <login-config>
   	<!-- 웹 자원에 대한 인증 처리 방식 BASIC(간단,일반) / DIGEST(암호화) 
   							/ FORM(사용자 폼 페이지) /CLIENT-CERT(공인 키 인증) -->
   	<auth-method>FORM</auth-method>
   	<!-- 인증(로그인) 처리를 위한 로그인 및 오류 페이지를 설정 -->
   	<form-login-config>
   		<!-- 인증(로그인) 처리를 위한 로그인 페이지 설정 -->
   		<form-login-page>/login.jsp</form-login-page>
   		<!-- 인증(로그인) 실패 시 오류 페이지를 설정 -->
   		<form-error-page>/login_failed.jsp</form-error-page>
   	</form-login-config>
   </login-config>
   <!-- 시큐리티 인증 설정 끝 -->
   
   <!-- 오류가 나면 오류 타입에 맞춰 오류 처리 jsp로 매핑 -->
   <!-- 404 : page not found. URL에 해당되는 jsp가 없음 -->
  <!-- 
  [주요 오류 코드 종류]
  200 : 요청이 정상적으로 처리됨
  307 : 임시로 페이지가 리다이렉트 됨
  400 : 클라이언트의 요청이 잘못된 구문으로 구성됨
  401 : 접근이 허용되지 않음
  404 : 지정된 URL을 처리하기 위한 자원이 존재하지 않음
  405 : 요청된 메소드가 허용되지 않음
  500 : 서버 내부 오류(JSP에서 예외 발생)
  503 : 서버 과부하나 보수 중인 경우. 서버가 일시적으로 서비시를 제공할 수 없음
   -->
   <!-- 오류 코드 설정 
     오류 코드 : 웹 서버가 제공하는 기본 오류 페이지에 나타나는 404, 500과 같이
     사용자의 요청이 올바르지 않을 때 출력되는 코드. 응답 상태 코드.
     
     JSP페이지에서 발생하는 오류가 web.xml 파일에 설정된 오류 코드와 일치하는
     경우 오류 코드와 오류 페이지를 보여줌
     -->
     <error-page>
     	<error-code>404</error-code>
     	<!-- 오류 페이지 설정 -->
     	<location>/exceptionNoPage.jsp</location>
     </error-page>
     
     <error-page>
     	<error-code>403</error-code>
     	<!-- 오류 페이지 설정 (접근권한 없음) -->
     	<location>/error/error403.jsp</location>
     </error-page>
     
     <error-page>
     	<error-code>500</error-code>
     	<!-- 오류 페이지 설정 (개발자의 오류, 잘못된 코드를 실행) 
     	
     	우선순위
     	jsp페이지의 page디렉티브의 errorPage > web.xml의 error-code
     	-->
     	<location>/error/error500.jsp</location>
     </error-page>
     
     <!-- 
     	[2] 예외 유형에 따른 오류 페이지 호출 방법은 JSP 페이지가 발생시키는 오류가 web.xml 파일에
   		설정된 예외 유형과 일치하는 경우 예외 유형과 오류 페이지를 보여줌
   		
   		우선순위
     	jsp페이지의 page디렉티브의 errorPage > web.xml의 error-code
      -->
      <error-page>
      <!-- 자바 예외 유형의 정규화된 클래스 이름을 설정 -->
      	<exception-type>java.lang.NullPointerException</exception-type>
      	<location>/error/errorNullPointer.jsp</location>
      </error-page>
</web-app>

exceptionNoProductId.jsp

<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.List"%>
<%@page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>상품 목록</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
	<c:set var="lang" value="${param.language}"/>
	<c:if test="${lang==null}">
		<c:set var="lang" value="ko"/>
	</c:if>
	<fmt:setLocale value="${lang}"/> <!-- 영어 en -->
	<fmt:bundle basename="bundle.message">
	<!-- header -->
	<!-- 
	디렉티브 태그 	속성 	    속성의 값		 -->
	<%@ include file="menu.jsp" %>
		<%
			String pid = request.getParameter("pid");
			ProductRepository dao = ProductRepository.getInstance();
			ProductVO vo = dao.getProductById(pid);
		%>
	<!-- 상품 목록 시작 -->
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">해당 상품이 존재하지 않습니다</h2>
		</div>
	</div>
	<div class="container">
		<!-- getRequestURI() : http://localhost/product.jsp -->
		<!-- getQueryString() : productId=P1239 -->
		<p><%=request.getRequestURI() %>?<%=request.getQueryString() %></p>
		<p><a href="/products.jsp" class="btn btn-secondary" role="button"><fmt:message key="product_list" /> &raquo;</a></p>
	</div>
	<!-- 상품 목록 끝 -->
	
	<!-- footer -->
	<%@ include file="footer.jsp" %>	
	</fmt:bundle>
</body>
</html>

exceptionNoPage.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
	<!-- header -->
	<%@ include file="menu.jsp" %>
	
	<%! // 선언문
		// 전역변수
		String greeting = "Welcome to My Page";
		String tagline = "I want to sleep";
	%>
	<!-- 표현문 -->
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">요청하신 페이지를 찾을 수 없습니다</h2>
		</div>
	</div>
	<div class="container">
		<p><%=request.getRequestURI() %></p>
		<p><a href="/products.jsp" class="btn btn-secondary" role="button">상품목록 &raquo;</a></p>
	</div>
	<!-- footer -->
	<%@ include file="footer.jsp" %>
</body>
</html>