본문 바로가기

WEB_Programming/JSTL

12. JSTL 예외 처리

1. c:out 에서 예외 생성하기
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Throw an Exception</title>
  </head>

  <body>10 divided by 0 is
  <c:out value="${10/0}" />

  <br />
  </body>
</html>


2. c:catch 예외잡기
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
  <head>
    <title>The c:catch action</title>
  </head>
  <body>
    <h1>So you want to make a call from your cell phone?!</h1>
    <h2>Checking the signal strength...</h2>

    <c:catch var="signalException">
      <%
        int i= (int) (Math.random() * 10);

        if (i < 5 )
          throw new NullPointerException(); %>
    </c:catch>


    <c:choose>
      <c:when test="${signalException != null}">
       
        Exception!!!
      </c:when>
      <c:otherwise>
        No Exception!!!
      </c:otherwise>
    </c:choose>
  </body>
</html>


3. 예외 잡기와 예외 메시지 출력
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Catch an Exception</title>
  </head>

  <body>
    <c:catch var="e">
    <c:set var="x" value="10" scope="page" />

    <c:set var="y" value="five" scope="page" />

    x divided by y is
    <c:out value="${x/y}" />

    <br />
    </c:catch>

    <br />
    <c:if test="${e!=null}">The caught exception is:
    <c:out value="${e}" />

    <br />
    </c:if>

    <c:if test="${e==null}">No exception was thrown
    <br />
    </c:if>
  </body>
</html>


4. 예외 잡기
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Catch an Exception?</title>
  </head>

  <body>
    <c:catch var="e">
    <c:set var="x" value="10" scope="page" />

    <c:set var="y" value="five" scope="page" />

    10 divided by 0 is
    <c:out value="${10/0}" />

    <br />
    </c:catch>

    <c:if test="${e!=null}">The caught exception is:
    <c:out value="${e}" />

    <br />
    </c:if>

    <c:if test="${e==null}">No exception was thrown
    <br />
    </c:if>
  </body>
</html>


5. Catch 에러
5.1 index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<h1>Peter's Junk-Mail Service</h1>

<c:if test="${param.submitted}">
  <c:if test="${empty param.name}" var="noName" />
  <c:if test="${empty param.email}" var="noEmail" />
  <c:if test="${empty param.age}" var="noAge" />

  <c:catch var="ageError">
    <fmt:parseNumber var="parsedAge" value="${param.age}" />
    <c:if test="${parsedAge < 13}" var="youngAge" />
  </c:catch>
  <c:if test="${not empty ageError}" var="badAge" />

  <c:if
   test="${not (noName or noEmail or noAge or badAge or youngAge)}">
    <c:set value="${param.name}" var="name" scope="request"/>
    <c:set value="${param.email}" var="email" scope="request"/>
    <c:set value="${param.age}" var="age" scope="request"/>
    <jsp:forward page="spamFormHandler.jsp" />
  </c:if>
</c:if>

<form method="post">
  <P>
  Thanks for signing up for our junk-mail service.
  Once you submit your information on the form below,
  you'll begin to receive all the "spam" you ever wanted.
  </p>

  <input type="hidden" name="submitted" value="true" />

  <P>
  Enter your name:
  <input type="text" name="name"
    value="<c:out value="${param.name}"/>" />
  <br />
  <c:if test="${noName}">
   <small><font color="red">
     Note: you must enter a name
   </font></small>
  </c:if>
  </p>

  <P>
  Enter your email address:
  <input type="text" name="email"
    value="<c:out value="${param.email}"/>" />
  <br />
  <c:if test="${noEmail}">
   <small><font color="red">
     Note: you must enter an email address
   </font></small>
  </c:if>
  </p>

  <P>
  Enter your age:
  <input type="text" name="age" size="3"
    value="<c:out value="${param.age}"/>" />
  <br />
  <c:choose>
    <c:when test="${noAge}">
     <small><font color="red">
       Note: you must enter your age
     </font></small>
    </c:when>
    <c:when test="${badAge}">
     <small><font color="red">
       Note: I couldn't decipher the age you typed in
     </font></small>
    </c:when>
    <c:when test="${youngAge}">
     <small><font color="red">
       Note: You're too young to receive pornographic
       junk mail.  Please grow older and try again.
     </font></small>
    </c:when>
  </c:choose>
  </p>

  <input type="submit" value="Sign up" />

</form>


5.2 spamFormHandler.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<h4>
 <font face="arial">
  Excellent!  The information is now correct.  (We could save it in a
  database or process it further.)
 </font>
</h4>

'WEB_Programming > JSTL' 카테고리의 다른 글

14. JSTL Input 폼 처리  (0) 2008.06.20
13. JSTL CheckBox 폼 처리  (0) 2008.06.20
11. JSTL Cookie 처리  (0) 2008.06.20
10. JSTL의 Scope  (0) 2008.06.20
9. JSTL에서 JavaBeans 사용  (0) 2008.06.20