JSTL Core Tag c:param Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
In this post, you will learn how to use the <c:param> tag in the JSTL core tags library with code examples.
The <c:param> tag works in conjunction with <c:url> tag. We use <c:param> tag to provide request parameters and associated values to a <c:url> tag. The <c:param> tag is nested in <c:import>, <c:url> and <c:redirect> tag to supply request parameters.
JSTL <c:param> Syntax:
<c:param name="<string>" value="<string>"/>Attributes:
Name | Required | Type | Description |
name | True | java.lang.String | Name of the request query string parameter. |
value | False | java.lang.String | Associated value of the parameter. |
JSTL <c:param> Example:
The below code example sends two parameters to the URL using the <c:param> tag.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><c:param> Demo</title>
</head>
<body>
<h1><c:param> Demo</h1>
<c:url var="myURL" value="/tag-types/core/param.jsp">
<c:param name="param1" value="value1"/>
<c:param name="param2" value="value2"/>
</c:url>
<a href="/<c:out value="${myURL}"/>">
Click to send two test parametets and values using <c:param></a>
<br/>
<br/>
<c:if test="${not empty param.param1 && not empty param.param2}">
Param1
<c:out value="${param.param1}"/><br/>
Param2
<c:out value="${param.param2}"/>
</c:if>
</body>
</html>The below example creating an URL with one request parameter and associated value to get the details of the item. <c:import> tag using the URL to display the item details.
<c:url value="/getItemDetails.jsp" var="itemDetailsURL">
<c:param name="itemId" value="I1980X"/>
</c:url>
<c:import url="${ itemDetailsURL }"/>
Eventually, the URL will be evaluated like shown below:
"/getItemDetails.jsp?itemId=I1980X"
Output:
In the above screen capture, clicking on the link sends two parameters with values. Please note the URL change after the submission. Recommended Usage of JSTL <c:param> tag:
Other JSTL Core Tags:
if | catch | choose | forEach | forTokens | import | out | redirect | remove | set | url
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments