In this tutorial you will learn about the JSTL core Miscellaneous
JSTL Core Tag catch Example
In this tutorial you will learn about the JSTL core Miscellaneous <c:catch> tag.
JSTL provides the facility for error handling. In JSTL this can be accomplished using the <c:catch> tag by specifying a variable for the exception. To specify a variable <c:catch> tag has an attribute named var.
Attributes of <c:catch>
- var : This attribute specifies the name of a variable that holds the java.lang.Throwable exception (if any), occurs in the body.
Example :
Here I am giving a simple example that will demonstrate you how to use the <c:catch> tag in jsp. At first I have created a jsp page named JstlCoreTagCatchExample.jsp into which I am trying to import a page that doesn't exists into the context it will throws an exception of page not found. On this page I have also checked that is exc != null using the if condition if this condition returns true then the page will be redirected to a new page errorPage.jsp.
JstlCoreTagCatchExample.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix= "c" %> <!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=ISO-8859-1"> <title>JSTL Example</title> </head> <body> <h3>JSTL Core Tag catch Example</h3> <c:catch var="exc"> <c:import url="b.jsp"/> </c:catch> <c:if test = "${exc != null}"> <c:redirect url="errorPage.jsp"></c:redirect> </c:if> </body> </html>
errorPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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=ISO-8859-1"> <title>JSP Error Page</title> </head> <body> <h3>404 Page Not Found Error</h3> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
After adding of these jar files you may execute your program in the following ways :
- Select JstlCoreTagCatchExample.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your JstlCoreTagCatchExample.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
When the execution process will be completed successfully an output will be displayed on the browser as :
[ 0 ] Comments