0

I have inserted several strings into my ArrayList. I want to plan an anchor for each string in the the Arraylist so that when I click on each string, it will send the string to other page by using session.setAttribute. However, my code only recognize the last string of the arrayList.

for(int i = 0; i < arrList.size(); i++) {
%>
    <a href="somepage.jsp">arrList.get(i)</a>
<% } %>

So assume that my arrList contains some strings: toyota, honda, bmw ... If I click on toyota, it will display toyota on somepage.jsp.

user228229
  • 661
  • 1
  • 8
  • 10

1 Answers1

0

You can do that easily using jstl . see How to avoid Java code in JSP files?

iterate the values inside a html table and an anchor tag to it like this ,

<table>
    <thead>Col1</thead>
    <thead>Edit</thead>
    <c:forEach var="temp" items="${arrList}">
        <tr>
            <td>
                <c:out value="${temp}"></c:out>
            </td>
            <td>
                <a href="PageNameHere?id=${temp}">Send current value</a>
            </td>
        </tr>
    </c:forEach>
</table>

Also dont forget to add this <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> import to your jsp to use jstl

Hope this helps!!

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56