본문 바로가기

JSP 웹 프로그래밍/쇼핑몰 페이지 만들기

[웹 쇼핑몰] 예외 처리 페이지 만들기

 

오류 페이지 작성하기

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!-- 
text/html, text/xml, text/plain
charset : 문자 세트 설정 
 -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>Welcome</title>
</head>
<body>
	<%@include file="menu.jsp" %>
	
	<!-- 표현문  -->
	<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 herf="/products.jsp" class="btn btn-secondary">상품목록&raquo;</a></p>
	</div>
	<!-- 2) welcome.jsp 끝 -->
	<%@include file="footer.jsp" %>
</body>
</html>

상품 상세 보기 페이지 수정하기

<%@page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page import="kr.or.ddit.vo.ProductVO"%>
<%@ page import="java.util.ArrayList"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page errorPage="exceptionNoProductId.jsp" %> -추가함 
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<title>상품목록</title>
<script type="text/javascript">
$(function(){
	//셀렉터(선택자)
	$('#selLanguage').on('change',function(){
		let str = $(this).val();
		
		console.log(str)
		
		location.href="/product.jsp?productId=${param.productId}&language="+str;
	});
	
	
});

</script>
</head>
<body>
	<!-- ?productId=P1234&language=en -->
	<c:set var="language" value="${param.language}"/>
	<c:if test="${language==null}">
		<!-- 한국말로 기본 세팅 -->
		<c:set var="language" value="ko" />
	</c:if>
   <fmt:setLocale value="${language}"/>
   <!-- bundle패키지의 message.properties -->
   <fmt:bundle basename="bundle.message">
    <%@include file="menu.jsp" %>
   <!--   상품 목록시작       -->
   <div class="jumbotron">
      <div class="container">
         <h1 class="display-3"><fmt:message key="product_description"/></h1>
      </div>
   </div>
   	<div class="container">
   		<div class="row" style="jstify-content: space=between; display:flex;">
   			<div>
   				<select id="selLanguage">
   					<option value="ko" 
   						<c:if test="${param.language=='ko'}">selected</c:if>
   					>Korea</option>
   					<option value="en" 
   						<c:if test="${param.language=='en'}">selected</c:if>
   					>English</option>
   				</select>
   			</div>
	        <div class="text-right">
		         <a href="/product.jsp?productId=${param.productId}&language=ko">Korean</a>
		         <a href="/product.jsp?productId=${param.productId}&language=en">English</a>
	      	</div>
   		</div>
      <%
         // /product.jsp?productId=P1234 ->get방식으로 불러옴
         String productId = request.getParameter("productId");//P1234
         
      
         ProductRepository productDAO = ProductRepository.getInstance();
         ProductVO productVO = productDAO.getProductById(productId);
         out.print("productVO : " + productVO);
         //productVO가 null인 경우 오류발생 , 추가함 
         productVO.toString();
      %>
      <!-- java의 productVO 객체 -> JSTL productVO 변수 -->
      <c:set var="productVO" value="<%=productVO%>" />
      <div class="row">
         <div class="col-md-6" style="width:50%;"><!--  6 은 style="width:50%;"와 같다. -->
            <img alt="${productVO.pname}" title="${productVO.pname}"
                   src="/images/${productVO.filename}"
                    style="width:50%;"/>
            <h3>${productVO.pname}</h3>
            <p>${productVO.description}</p>
            <p>
               <b><fmt:message key="productId" /> : </b>
               <span class="badge badge-danger" 
                  style="font-color:red;">${productVO.productId}</span>
            </p>
            <p>
               <b><fmt:message key="manufacturer" /> : ${productVO.manufacturer}</b>
            </p>
            <p>
               <b><fmt:message key="category" /> : ${productVO.category}</b>
            </p>
            <p>
               <b><fmt:message key="unitsInStock" /> : ${productVO.unitsInStock}</b>
            </p>
            <h4>${productVO.unitPrice} <fmt:message key="won" /></h4>
            <p>
               <a href="/products.jsp" class="btn btn-secondary"><fmt:message key="product_list" />&raquo;</a>
            </p>
         </div>
      </div>
      <hr/>
   </div>
   <!--   상품 목록 끝        -->
   <%@include file="footer.jsp" %>
   </fmt:bundle>
</body>
</html>

페이지가 없을 때 추가하기 

 

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!-- 
text/html, text/xml, text/plain
charset : 문자 세트 설정 
 -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>Welcome</title>
</head>
<body>
	<%@include file="menu.jsp" %>
	<!-- 2) welcome.jsp 시작 -->
	
	<!-- 표현문  -->
	<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">상품목록&raquo;</a></p>
	</div>
	<!-- 2) welcome.jsp 끝 -->
	<%@include file="footer.jsp" %>
</body>
</html>

 

web.xml 파일에 추가 작성하기

 

 <error-page>
     <!-- 오류 코드 설정 
	     오류 코드 : 웹 서버가 제공하는 기본 오류 페이지에 나타나는 404, 500과 같이
	     사용자의 요청이 올바르지 않을 때 출력되는 코드. 응답 상태 코드.
	     
	     JSP페이지에서 발생하는 오류가 web.xml 파일에 설정된 오류 코드와 일치하는
	     경우 오류 코드와 오류 페이지를 보여줌
     -->
     	<error-code>404</error-code>
     	<!-- 오류처리 페이지 설정 -->
     	<location>/exceptionNoPage.jsp</location>
     </error-page>
     
     <error-page>