JSTL - How to create a variable using c:set and c:out

Home > Search > How-to
  by

Creating variables in JSTL is incredibly easy. First, add the following line to the top of your JSP page.

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

 

Inside of the <body> tags, use c:set to create a variable. In this example, a variable called name is created with a value of Jeremy. This is a key:value pair.

<c:set var="name" value="Jeremy"/>

 

Use c:out to print a variable. This markup should produce the value of the variable called name, which will print Jeremy in this example.

<c:out value="${name}">Default Value</c:out>

 

In this example, when running the app, "Jeremy" is displayed. If c:set was not used, then "Default Value" would be displayed.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments