프로그래밍/JSP Servlet

JSTL/Servlet – JSTL <c:catch> 커스텀 액션 사용 하기

가카리 2016. 1. 30. 22:49
반응형

 

JSTL/Servlet – JSTL <c:catch> 커스텀 액션 사용하기

 

<c:catch> 커스텀 액션은 자바에서 try-catch구문과 같습니다.

 

    <c:catch var="익셉션 변수">

        에러가 발생할 수 있는 부분

        <c:if test="${e != null}">

            에러메시지 : ${e.message} <!—에러 메시지를 출력하는 코드 -->

        </c:if>

    </c:catch>

 

 

Divide.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

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

<%

    String str1 = request.getParameter("NUM1");

    String str2 = request.getParameter("NUM2");

    int num1 = Integer.parseInt(str1);

    int num2 = Integer.parseInt(str2);

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    <c:catch var="e"><!-- 일부러 divide by zero 에러를 발생 시킴 -->

        <% int result = num1 / num2; %>

        결과는? <%=result %>

    </c:catch>

    <c:if test="${e != null }" ><!-- 실제 출력되는지 확인 -->

        에러 메시지 : ${e.message }

    </c:if>

</body>

</html>

 

출력 화면

반드시 주소창에 NUM1=3&NUM2=0을 추가시켜줘야 합니다 아니면 에러 납니다.

반응형