Search code examples
jspjsp-tags

How to Pass foreach loop variable to URL decoder in JSP?


I am getting a hashmap from server named as list and I'm iterating through it. I want to encode they key value of list and assign it href of <a>. So how can I achieve this?

 <c:forEach items="${list}" var="report" begin="0">
    <tr><td><a href=<%= URLEncoder.encode("what should i put here to encode report.key") %>>${report.key}</a></td>
    <td>${report.value}</td></tr>
</c:forEach>

Solution

  • One can do this in 2 ways.

    <c:forEach items="${list}" var="report" begin="0">
    <tr><td><a href=<%= URLEncoder.encode(pageContext.getAttribute("report")) %> >${report.key}</a></td>
    <td>${report.value}</td></tr>
    

    OR

     <c:forEach items="${list}" var="report">
        <c:url value="name" var="report"/>
        <tr>
           <td><a href="${name}">${report.key}</a></td>
           <td>${report.value}</td>
        </tr>
     </c:forEach>