JSTL Core Tag set Example

JSTL Core Tag set Example


Posted in : Java Posted on : April 1, 2012 at 6:42 PM Comments : [ 0 ]

In this tutorial you will learn about how to use the JSTL core variable support tag set in JSP.

JSTL Core Tag set Example

In this tutorial you will learn about how to use the JSTL core variable support tag set in JSP.

The set tag of JSTL core is an easy way to use rather than the use of setProperty action because it sets the value or property of an EL variable. Use of <jsp:setProperty> action is very limited because of the lacks of functionality such as a key value in Map can not be set. <c:set> tag of JSTL have been removed all the lackness of <jsp:setProperty> and allows you to create/set variables/attributes. Following are the purpose of the use of set tag :

  • Using set tag, properties of bean can be set.
  • Using set tag, values of Map can be set.
  • Using set tag, on a page variable can be created by specifying the scope i.e request, session or application scope.

Following attributes may be used in the <c:set> tag :

  • value : This attribute is used to store the information. In JSTL for <c:set> tag values can be given in body of tag instead of an attribute line ( In such case when the value is long ).
  • target : This attribute is used when a property of bean or value of map is required to set. target points to the real object not the name of an object.
  • property : This attribute is used to specifies the property. In case of the specified target attribute is a map then the property attribute will be the key and the value attribute will be the value for that key.
  • var : This attribute is used to define the name of variable that is required to create or to store the values. If the variable defined by var is not existed then it is created by the <c:set> jstl tag only if the value is not null.
  • scope : This attribute is an optional attribute that may have the page, request, session, or application scope. The default scope is the page scope.

Example :

Here I am giving below a simple example which will demonstrate how can you use the set tag of core JSTL. In the example I simply set the value using "value" attribute into a variable using "var" attribute. As an output it will simply display the text written under the HTML tags and the values set into the variables.

JstlCoreTagSetExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h2>JSTL Core Tag set Example.</h2>
<c:set var="str" value="Dev"/>
<c:set var="str1" value="Manuals" />
<h3>Welcome To 
<c:out value= "${str}" />
<c:out value= "${str1}" /> 
</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 JstlCoreTagSetExample.jsp page of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlCoreTagSetExample.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 -> 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 your eclipse browser as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics