Share on Google+Share on Google+

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 <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="" %>


<%@ taglib uri="" prefix="c"%>
<title>Example of tag catch of Core JSTL tags</title>
<body bgcolor="#A9F5F2">
<form method="POST">
<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>
<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:if test="${catchException!=null}">
<table border="1" width="50%">
<td><font color="red" size="4">Found Exception :</font> <c:out
value=" ${catchException}" /></td>
<td><font color="red" size="4">Exception Message :</font> <c:out
value="${catchException.message}" /></td>

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



Posted on: August 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Use of tag of Core JSTL tags  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image