Use of tag of Core JSTL tags

In this example we have used Core JSTL tag that is used to handle exception generated during run time.

Use of tag of Core JSTL tags

Use of <c:catch> tag of Core JSTL tags

     

In this example we have used Core JSTL tag <c:catch> that is used to handle exception generated during run time. Here we will create a simple calculator to divide two numbers, if user enters values instead a digit, it shows a error message as a response. Tag <c:catch> is member of Core tag library of JSTL so before use Core JSTL tags we must include following line of code :-

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



catchException.jsp

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<html>
<head>
<title>Example of tag catch of Core JSTL tags</title>
</head>
<body bgcolor="#A9F5F2">
<form method="POST">
<table>
<tr>
<td><input type="text" name="num" size="10" /></td>
<td>Divide By</td>
<td><input type="text" name="devider" size="10" /> <input
type="submit" name="calculate" value="calculate" /></td>
</tr>
</table>
</form>
<c:if test="${pageContext.request.method=='POST'}">
<c:catch var="catchException">
<c:set var="num" value="${param.num}" />
<c:set var="devider" value="${param.devider}" />
<b>Response : </b>
<c:out value="${num/devider}" />
</c:catch>
<c:if test="${catchException!=null}">
<table border="1" width="50%">
<tr>
<td><font color="red" size="4">Found Exception :</font> <c:out
value=" ${catchException}" /></td>
</tr>
<tr>
<td><font color="red" size="4">Exception Message :</font> <c:out
value="${catchException.message}" /></td>
</tr>
</table>
</c:if>
</c:if>
</body>
</html>

Steps to run this example :

1:  Download the zip file of code and unzip this file, you will get a folder named  'catch_exception_jstlCoreTag'.
2:  Paste this folder in 'Apache Tomcat 6.0.16-->webapps' or generally in directory 'C:\apache-tomcat-6.0.16\webapps'.
3:  Start tomcat server by click on startup.bat file in 'C:\apache-tomcat-6.0.16\bin'.
4: Open browser and type url 'http://localhost:8080/catch_exception_jstlCoreTag/catchException.jsp' or click on this link.

When program will run in browser this will show the following output.....

when user will fill all the fields and click on calculate button, next response will be like as below.......

If user enter values other than digit this will show generated exception as response message. For example user enters a digit in first text box and a string in other text box so the response will be a error message.....

Download Source Code