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>&lt;c:param&gt; Demo</title>
    </head>
    <body>
        <h1>&lt;c:param&gt; 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 &lt;c:param&gt;</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:

param-demo

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:



Useful to send parameters and associated values to a resource specified in the <c:url> tag.

 

Other JSTL Core Tags:

if |  catch  |  choose   |  forEach  |  forTokens   |  import   |  out   |  redirect  |  remove   |  set  |  url


About the Author:

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.



Add comment