20 - JSP Standard Tag Library (JSTL): Page 4 of 7

20.3.5 <c:remove>

The <c:remove> tag is used to remove the variable from given scope or from all the scopes. The syntax and attributes are as follows:

<c:set var=”variable_name” scope=”page | application | request | sesion”/>

· var- the variable which is to be removed.

· scope- scope is an optional attribute and it is the scope from which variable to be removed. If scope is not defined means variable to be removed from all scopes.

Lets create cremove.jsp to demonstrate the <c:remove> tag like below

<html>
  <head>
    <title> c:remove tag example </title>
  </head>
  <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:setvar="pageVariable" value="Variable in Page Scope" />
    Page Scoped Variable :: <c:out value="${pageVariable}"/>
    <br/>
    <c:setvar="sessionVariable" value="Variable in Session Scope"  scope="session"/>
    Session Scoped Variable :: <c:out value="${sessionVariable}"/>
    <br/>
    <c:removevar="pageVariable" scope="page"/>
    Page Scoped Variable After c:remove :: <c:out value="${pageVariable}"/>
    <br/>
    <c:removevar="sessionVariable"/>
    Session Scoped Variable After c:remove :: <c:out value="${sessionVariable}"/>
    <br/>
  </body>
</html>

Access cremove.jsp by hitting http://localhost:8080/jsp-tutorial/cremove.jsp

20.3.6 <c:if>

The <c:if> tag is used to implement conditions and is equivalent to if conditions in java. If the condition evaluates to true then the block gets executed.

Syntax of this tag is

     <c:if test="${condition}"> . . . </c:if>c:if>

Optionally there are two other attributes var and scope can be used. In this case , result of condition gets stored in a scope with variable name.

     <c:if test="${condition}" var=”variable_name” scope=”page | request | application| session”> application| session”>

Lets create cif.jsp to demonstrate the <c:if> tag like below.

<html>
  <head>
    <title> c:if tag example </title>
  </head>
  <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:setvar="bonus" value="2000" />
    <c:if test="${bonus > 1000}">
    Bonus is greater than $ 1000!!!
    </c:if>
    <br/>
    <c:if test="${bonus < 1000}"var="evaluatedValue" scope="request"/>
    Is Bonus less than $1000 ::   <c:out value="${evaluatedValue}"></c:out>
  </body>
</html>

Access cif.jsp by hitting http://localhost:8080/jsp-tutorial/cif.jsp

20.3.7 <c:catch>

The <c:catch> tag is used to catch the exception and wrap it into an object. In other words , we can say error handling. We should write the statement which can throw exceptions under <c:catch> block so the errors will be handled gracefully

The syntax and attributes are as follows:

     <c:catch var=”variable_name”> ... </c:catch>ch>

Where var is the variable in which exception object (instance of java.lang.Throwable )will be stored if occurs. If there is no exception in the block of statements in <c:catch> then the variable will have null value. so we must check for a null before using the variable.

Lets create ccatch.jsp to demonstrate the <c:catch> tag like below. In this example we are intentionally throwing null pointer exception.

<html>
  <head>
    <title> c:catch tag example </title>
  </head>
    <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:catchvar="exceptionObject">
    <%
      String str=null;
      str.toLowerCase();
    %>
    </c:catch>
    <c:if test = "${exceptionObject != null}">
    <p>The exception is : ${exceptionObject} </p>
    </c:if>
  </body>
</html>

Access ccatch.jsp by hitting http://localhost:8080/jsp-tutorial/ccatch.jsp

 

Like us on Facebook