Exception Handling using JSTL tag….

Here in this post we will learn how to handle Exception in jsp page using JSTL tags

Here is the sample code for this app…

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<strong>Exception Handling Example</strong><br>
<a href="<c:url value='Check.jsp' />" >Go to Exception Page</a><br>
<br>
<%-- The above line is similar to response.encodeURL("url") method of servlet --%>
</body>
</html>

when we click on Go to Exception Page it will send u too Check.jsp page contain the following code:
check.jsp

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

<html>
<body>
<strong>Exception Handling Example</strong>
<br>
<c:catch var=”myexception” ><%—This is similar to try { }catch{} of normal exception handling –-%>
<strong>Inside the Catch Block : >></strong>
<br>
<br>
<% int x = 10/0; %>
</c:catch>
<%– the <c:chosoe> , c:when and c:otherwise is equivalant to if {}else {} statement !! thanx –%>

<c:choose>
<c:when test=”${myexception != null}” >
There was an Exception : ${myexception.message}<br><br>
</c:when>
<c:otherwise >
No Problem thanks!!
</c:otherwise >
</c:choose>
</body>
</html>

——————————————————–
at int x=10/0; there is devide by zero exception which is catch by the above given block

the running example can be found here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s