Home » Enterprise Java » jstl » Conditional content with JSTL in JSP page

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Conditional content with JSTL in JSP page

This is an example of how to create conditional content in a JSP page using JSTL. JavaServer Pages Standard Tag Library (JSTL) encapsulates as simple tags the core functionality common to many Web applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. Creating conditional content in a JSP page using JSTL implies that you should:

  • Create a jsp page that contains the <%code fragment%> scriptlet. It can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
  • Include JSTL Core library in your JSP page, using the <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> syntax.
  • Use the <c:if> tag to evaluate an expression. It displays its body content only if the expression evaluates to true. The test attribute holds the condition to evaluate.
  • Use the <c:choose> tag, that works like a Java switch statement in that it lets you choose between a number of alternatives. It uses <c:when> tag, with an attribute named test that evaluates a condition. It also uses <c:otherwise> to perform the default clause.

Let’s take a look at the code snippet that follows:

ConditionalContentJSTL.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/tld/c-rt.tld" prefix="c-rt" %>

<html>

<head>
	<title>Java Code Geeks Snippets - Conditional Content with JSTL in JSP Page</title>
</head>

<body>

	<%-- if condition --%>
	<c-rt:if test='${param.myparam1 == "myvalue1"}'>
	    This is printed if parameter "myparam1" equals "myvalue1" </br>
	</c-rt:if>
	
	<%-- if/else condition --%>
	<c-rt:choose>
	    <c-rt:when test='${param.myparam2 == "myvalue2"}'>
	
  This is printed if parameter "myparam2" equals "myvalue2" </br>
	    </c-rt:when>
	    <c-rt:otherwise>
	
 	This is printed if parameter "myparam2" DOES NOT equal "myvalue2" </br>
	    </c-rt:otherwise>
	</c-rt:choose>
	
	<%-- multiple conditions --%>
	<c-rt:choose>
	    <c-rt:when test='${param.myparam3 == "0"}'>
	
  This is printed if parameter "myparam1" equals 0 </br>
	    </c-rt:when>
	    <c-rt:when test='${param.myparam3 == "1"}'>
	
  This is printed if parameter "myparam1" equals 1 </br>
	    </c-rt:when>
	    <c-rt:otherwise>
	
 This is printed for any other value of the parameter </br>
	    </c-rt:otherwise>
	</c-rt:choose>

</body>

URL:

http://localhost:8080/jcgsnippets/ConditionalContentJSTL.jsp?myparam1=myvalue1&myparam2=other&myparam3=1

Output:

This is printed if parameter "myparam1" equals "myvalue1"
This is printed if parameter "myparam2" DOES NOT equal "myvalue2"
This is printed if parameter "myparam1" equals 1

 
This was an example of how to create conditional content in a JSP page using JSTL.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of