Java Tutorial/JSTL/Exception

Материал из Java эксперт
Версия от 15:24, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Catch Error

index.jsp



<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.ru/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>





Catch Exception

<%@ taglib uri="http://java.sun.ru/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>





Catch Exception and Print Out Exception Message

<%@ taglib uri="http://java.sun.ru/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>





C:Catch Exception

<%@ taglib uri="http://java.sun.ru/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>





Generate Exception in c:out

<%@ taglib uri="http://java.sun.ru/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>