0% found this document useful (0 votes)
34 views4 pages

JSTL

The document contains code snippets demonstrating the use of various JSTL core and function tags to output and manipulate text values. Some key actions shown include: - Outputting text using <c:out> - Importing content from an external URL using <c:import> - Setting and removing variables in different scopes using <c:set> and <c:remove> - Conditionally displaying content using <c:if>, <c:choose>, <c:when> and <c:otherwise> - Iterating over values using <c:forEach> and <c:forTokens> - Generating URLs and redirecting using <c:url> and <c:redirect>

Uploaded by

Vivek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

JSTL

The document contains code snippets demonstrating the use of various JSTL core and function tags to output and manipulate text values. Some key actions shown include: - Outputting text using <c:out> - Importing content from an external URL using <c:import> - Setting and removing variables in different scopes using <c:set> and <c:remove> - Conditionally displaying content using <c:if>, <c:choose>, <c:when> and <c:otherwise> - Iterating over values using <c:forEach> and <c:forTokens> - Generating URLs and redirecting using <c:url> and <c:redirect>

Uploaded by

Vivek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//JSTL/Core

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


<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:out value="${'Welcome to Alliance'}"/>
<c:import var="data" url="http://www.google.com"/>
<c:out value="${data}"/>
<c:set var="Income" scope="session" value="${4000*4}"/>
<c:out value="${Income}"/>
<c:set var="income" scope="session" value="${4000*4}"/>
<p>Before Remove Value is: <c:out value="${income}"/></p>
<c:remove var="income"/>
<p>After Remove Value is: <c:out value="${income}"/></p>
<c:catch var ="catchtheException">
<% int x = 2/0;%>
</c:catch>

<c:if test = "${catchtheException != null}">


<p>The type of exception is : ${catchtheException} <br />
There is an exception: ${catchtheException.message}</p>
</c:if>

<c:set var="income" scope="session" value="${4000*4}"/>


<c:if test="${income > 8000}">
<p>My income is: <c:out value="${income}"/><p>
</c:if>

<c:set var="income" scope="session" value="${4000*4}"/>


<p>Your income is : <c:out value="${income}"/></p>
<c:choose>
<c:when test="${income <= 1000}">
Income is not good.
</c:when>
<c:when test="${income > 10000}">
Income is very good.
</c:when>
<c:otherwise>
Income is undetermined...
</c:otherwise>
</c:choose>

<c:set value="10" var="num"></c:set>


<c:choose>
<c:when test="${num%2==0}">
<c:out value="${num} is even number"></c:out>
</c:when>
<c:otherwise>
<c:out value="${num} is odd number"></c:out>
</c:otherwise>
</c:choose>

<c:forEach var="j" begin="1" end="10">


Item <c:out value="${j}"/><p>
</c:forEach>

<c:forTokens items="Rahul-Nakul-Rajesh" delims="-" var="name">


<c:out value="${name}"/><p>
</c:forTokens>

<c:url value="/index1.jsp" var="completeURL"/>


<c:param name="trackingId" value="786"/>
<c:param name="user" value="Nakul"/>
</c:url>
${completeURL}

<c:set var="url" value="0" scope="request"/>


<c:if test="${url<1}">
<c:redirect url="http://javatpoint.com"/>
</c:if>
<c:if test="${url>1}">
<c:redirect url="http://facebook.com"/>
</c:if>
</body>
</html>
//JSTL/Functions
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="String" value="Welcome to Alliance"/>

<c:if test="${fn:contains(String, 'Alliance')}">


<p>Found Alliance string<p>
</c:if>

<c:if test="${fn:contains(String, 'ALLIANCE')}">


<p>Found Alliance string<p>
</c:if>

<c:if test="${fn:containsIgnoreCase(String, ‘Alliance')}">


<p>Found Alliance string<p>
</c:if>

<c:if test="${fn:containsIgnoreCase(String, 'ALLIANCE')}">


<p>Found ALLIANCE string<p>
</c:if>

<c:set var="String" value="Welcome to JSP programming"/>

<c:if test="${fn:endsWith(String, 'programming')}">


<p>String ends with programming<p>
</c:if>

<c:if test="${fn:endsWith(String, 'JSP')}">


<p>String ends with JSP<p>
</c:if>

<c:set var="string1" value="It is first String."/>


<c:set var="string2" value="It is <xyz>second String.</xyz>"/>

<p>With escapeXml() Function:</p>


<p>string-1 : ${fn:escapeXml(string1)}</p>
<p>string-2 : ${fn:escapeXml(string2)}</p>

<p>Without escapeXml() Function:</p>


<p>string-1 : ${string1}</p>
<p>string-2 : ${string2}</p>

<c:set var="string1" value="It is first String."/>


<c:set var="string2" value="It is <xyz>second String.</xyz>"/>

<p>Index-1 : ${fn:indexOf(string1, "first")}</p>


<p>Index-2 : ${fn:indexOf(string2, "second")}</p>

<c:set var="str1" value="Welcome to JSP programming "/>


<p>String-1 Length is : ${fn:length(str1)}</p>

<c:set var="str2" value="${fn:trim(str1)}" />


<p>String-2 Length is : ${fn:length(str2)}</p>
<p>Final value of string is : ${str2}</p>

<c:set var="msg" value="The Example of JSTL fn:startsWith() Function"/>


The string starts with "The": ${fn:startsWith(msg, 'The')}
<br>The string starts with "Example": ${fn:startsWith(msg, 'Example')}

<c:set var="str1" value="Welcome-to-JSP-Programming."/>


<c:set var="str2" value="${fn:split(str1, '-')}" />
<c:set var="str3" value="${fn:join(str2, ' ')}" />

<p>String-3 : ${str3}</p>
<c:set var="str4" value="${fn:split(str3, ' ')}" />
<c:set var="str5" value="${fn:join(str4, '-')}" />

<p>String-5 : ${str5}</p>

<c:set var="site" value="Sun Java"/>


<c:set var="author" value="James S. Gosling"/>
${fn:toUpperCase(site)} developed by ${fn:toUpperCase(author)}.

<c:set var="string" value="This is the first string."/>


${fn:substring(string, 5, 17)}

<c:set var="string" value="James Gosling"/>


${fn:substringAfter(string, "James")}

<c:set var="str1" value="This is first string"/>


<c:set var="str2" value="Hello"/>
Length of the String-1 is: ${fn:length(str1)}<br>
Length of the String-2 is: ${fn:length(str2)}

<c:set var="author" value="Ramesh Kumar"/>


<c:set var="string" value="pqr xyz abc PQR"/>
${fn:replace(author, "Ramesh", "Suresh")}
${fn:replace(string, "pqr", "hello")}

</body> </html>

You might also like